| 1 |
<?php
|
| 2 |
|
| 3 |
define('LIGHTCRM_DEFAULT_MAIL', 'Hello %username,
|
| 4 |
|
| 5 |
This is an automatically email. Please do not reply to the origin of this email.
|
| 6 |
|
| 7 |
A reply has been posted for you on %site. Please go to %uri to reply.
|
| 8 |
|
| 9 |
Subject: %subject
|
| 10 |
|
| 11 |
Comment: %comment');
|
| 12 |
|
| 13 |
/**
|
| 14 |
* hook_access implementation
|
| 15 |
*/
|
| 16 |
function lightcrm_access($op, $node) {
|
| 17 |
global $user;
|
| 18 |
if ( $op == 'view') {
|
| 19 |
$access = ( user_access('access own lightcrm') && ( $user->uid == $node->uid ) ) ||
|
| 20 |
user_access('access all lightcrm') ;
|
| 21 |
}
|
| 22 |
|
| 23 |
return $access;
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* hook_help implementation
|
| 28 |
*/
|
| 29 |
function lightcrm_help($section) {
|
| 30 |
|
| 31 |
switch ($section) {
|
| 32 |
case 'admin/modules#description':
|
| 33 |
return t('By using the comment module, it provides a user-website communication.');
|
| 34 |
case 'node/add#lightcrm':
|
| 35 |
return t('User contact CRM node.');
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* hook_menu implementation
|
| 41 |
*/
|
| 42 |
function lightcrm_menu($may_cache) {
|
| 43 |
global $user;
|
| 44 |
$items = array();
|
| 45 |
if ($may_cache) {
|
| 46 |
} else {
|
| 47 |
if (arg(0) == 'user' && is_numeric(arg(1))) {
|
| 48 |
$access = ( user_access('access own lightcrm') && ( $user->uid == arg(1) ) ) ||
|
| 49 |
user_access('access all lightcrm') ;
|
| 50 |
$items[] = array(
|
| 51 |
'path' => 'user/'. arg(1) .'/lightcrm',
|
| 52 |
'title' => t('LightCrm'),
|
| 53 |
'callback' => '_lightcrm_user_crm',
|
| 54 |
'access' => $access,
|
| 55 |
'type' => MENU_IS_LOCAL_TASK,
|
| 56 |
'weight' => 5,
|
| 57 |
);
|
| 58 |
}
|
| 59 |
}
|
| 60 |
return $items;
|
| 61 |
}
|
| 62 |
|
| 63 |
function _lightcrm_node_create($uid) {
|
| 64 |
$node = node_load(array('type' => 'lightcrm', 'uid' => $uid));
|
| 65 |
$user = user_load(array('uid'=>$uid));
|
| 66 |
|
| 67 |
if ( !$node ) {
|
| 68 |
$node = new StdClass();
|
| 69 |
$node->type = 'lightcrm';
|
| 70 |
$node->uid = $user->uid;
|
| 71 |
$node->title = $user->name;
|
| 72 |
$node->comment = 2;
|
| 73 |
$node->status = 1;
|
| 74 |
|
| 75 |
node_save($node);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
function _lightcrm_user_crm() {
|
| 80 |
_lightcrm_node_create(arg(1));
|
| 81 |
|
| 82 |
$node = node_load(array('type' => 'lightcrm', 'uid' => arg(1)));
|
| 83 |
|
| 84 |
//$node = node_load(4);
|
| 85 |
$output = node_show($node, 0);
|
| 86 |
return $output;
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Implementation of hook_form().
|
| 91 |
*/
|
| 92 |
function lightcrm_form(&$node, &$param) {
|
| 93 |
|
| 94 |
$form['title'] = array(
|
| 95 |
'#type' => 'textfield',
|
| 96 |
'#title' => t('Title'),
|
| 97 |
'#default_value' => $node->title,
|
| 98 |
'#size' => 60,
|
| 99 |
'#maxlength' => 128,
|
| 100 |
'#required' => TRUE,
|
| 101 |
);
|
| 102 |
|
| 103 |
/*
|
| 104 |
$form['tagnode'] = array(
|
| 105 |
'#type' => 'tagnode_element',
|
| 106 |
'#tagnode-type' => 'university',
|
| 107 |
//'#default_value' => $node->title,
|
| 108 |
//'#size' => 60,
|
| 109 |
//'#maxlength' => 128,
|
| 110 |
'#required' => TRUE,
|
| 111 |
);
|
| 112 |
|
| 113 |
*/
|
| 114 |
|
| 115 |
|
| 116 |
return $form;
|
| 117 |
}
|
| 118 |
|
| 119 |
function lightcrm_node_info() {
|
| 120 |
return array('lightcrm' => array('name' => t('lightcrm'), 'base' => 'lightcrm'));
|
| 121 |
}
|
| 122 |
|
| 123 |
function _lightcrm_comment_add($uid, $subject, $body) {
|
| 124 |
global $user;
|
| 125 |
$_SESSION['_lightcrm_comment_add'] = TRUE;
|
| 126 |
|
| 127 |
_lightcrm_node_create($uid);
|
| 128 |
|
| 129 |
//$usr = user_load(array('uid'=>$uid));
|
| 130 |
$node = node_load(array('type' => 'lightcrm', 'uid' => $uid));
|
| 131 |
|
| 132 |
$comment = array();
|
| 133 |
$comment['pid'] = 0;
|
| 134 |
$comment['nid'] = $node->nid;
|
| 135 |
$comment['uid'] = $user->uid;
|
| 136 |
$comment['subject'] = $subject;
|
| 137 |
$comment['comment'] = $body;
|
| 138 |
|
| 139 |
comment_save($comment);
|
| 140 |
}
|
| 141 |
|
| 142 |
/**
|
| 143 |
* Implementation of hook_comment().
|
| 144 |
*/
|
| 145 |
function lightcrm_comment($comment, $op) {
|
| 146 |
global $user;
|
| 147 |
global $base_url;
|
| 148 |
|
| 149 |
if ($op == 'insert' || $op == 'update') {
|
| 150 |
|
| 151 |
$nid = $comment['nid'];
|
| 152 |
$node = node_load($nid);
|
| 153 |
|
| 154 |
// send mail
|
| 155 |
if ( variable_get('lightcrm_notify', 1) ) {
|
| 156 |
$body = variable_get('lightcrm_mail', LIGHTCRM_DEFAULT_MAIL);
|
| 157 |
|
| 158 |
$variables = array(
|
| 159 |
'%subject' => $comment['subject'],
|
| 160 |
'%comment' => $comment['comment'],
|
| 161 |
"%site" => variable_get("site_name", "drupal"),
|
| 162 |
"%uri" => url('user/'.$node->uid .'/lightcrm', NULL, NULL, TRUE),
|
| 163 |
);
|
| 164 |
$from = variable_get("site_mail", ini_get("sendmail_from"));
|
| 165 |
|
| 166 |
$subject = variable_get('lightcrm_mailsubject', 'A new comment has been posted.');
|
| 167 |
|
| 168 |
if ( $node->uid == $user->uid ) {
|
| 169 |
// site admin
|
| 170 |
$variables['%username'] = 'administrator';
|
| 171 |
$mail = variable_get("site_mail", ini_get("sendmail_from"));
|
| 172 |
|
| 173 |
|
| 174 |
$roles = variable_get('lightcrm_mailroles', array());
|
| 175 |
foreach ($roles as $role_k => $role_v) {
|
| 176 |
if ( $role_v > 0 ) {
|
| 177 |
if ( $role_where ) {
|
| 178 |
$role_where .= ' OR ';
|
| 179 |
}
|
| 180 |
if ( $role_v == DRUPAL_AUTHENTICATED_RID ) {
|
| 181 |
$role_where = '1';
|
| 182 |
} else {
|
| 183 |
$role_where = 'ur.rid = '. $role_v;
|
| 184 |
}
|
| 185 |
}
|
| 186 |
}
|
| 187 |
|
| 188 |
$result = db_query('SELECT u.mail, u.name FROM {users} u LEFT JOIN {users_roles} ur on u.uid = ur.uid WHERE ' . $role_where );
|
| 189 |
while ($row = db_fetch_object($result)) {
|
| 190 |
$variables['%username'] = $row->name;
|
| 191 |
$user_body = strtr($body, $variables);
|
| 192 |
user_mail($row->mail, $subject, $user_body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
|
| 193 |
}
|
| 194 |
|
| 195 |
} else {
|
| 196 |
$usr = user_load(array('uid' => $node->uid));
|
| 197 |
$mail = $usr->mail;
|
| 198 |
$variables['%username'] = $usr->name;
|
| 199 |
$user_body = strtr($body, $variables);
|
| 200 |
user_mail($mail, $subject, $user_body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
|
| 201 |
}
|
| 202 |
|
| 203 |
if ( $_SESSION['_lightcrm_comment_add'] ) {
|
| 204 |
unset($_SESSION['_lightcrm_comment_add']);
|
| 205 |
} else {
|
| 206 |
if ( node_get_base($node->type) == 'lightcrm' ) {
|
| 207 |
drupal_goto('user/' . $node->uid . '/lightcrm');
|
| 208 |
} // if
|
| 209 |
} // else
|
| 210 |
|
| 211 |
}
|
| 212 |
|
| 213 |
} // if
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* hook_perm implementation
|
| 218 |
*/
|
| 219 |
function lightcrm_perm() {
|
| 220 |
return array(
|
| 221 |
'access own lightcrm',
|
| 222 |
'access all lightcrm',
|
| 223 |
);
|
| 224 |
}
|
| 225 |
|
| 226 |
/**
|
| 227 |
* hook_view implementation
|
| 228 |
*/
|
| 229 |
function lightcrm_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 230 |
$user = user_load(array('uid' => $node->uid));
|
| 231 |
|
| 232 |
drupal_set_title($user->name);
|
| 233 |
|
| 234 |
if ( arg(0) == 'node' ) {
|
| 235 |
drupal_goto('user/'. $node->uid .'/lightcrm');
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
/**
|
| 240 |
* Implementation of hook_settings().
|
| 241 |
*/
|
| 242 |
function lightcrm_settings() {
|
| 243 |
//$output = implode("\n", flexinode_invoke_all('settings'));
|
| 244 |
$form['lightcrm_notify'] = array(
|
| 245 |
'#type' => 'checkbox',
|
| 246 |
'#title' => t('User notify'),
|
| 247 |
'#description' => t('Mail the other party when a comment a post by the user or by the administrators.'),
|
| 248 |
'#default_value' => variable_get('lightcrm_notify', 1),
|
| 249 |
);
|
| 250 |
|
| 251 |
$form['lightcrm_mail'] = array(
|
| 252 |
'#type' => 'textarea',
|
| 253 |
'#title' => t('Mail body'),
|
| 254 |
'#description' => t('The mail that is going to be sent to the user. Placeholder variables: %username, %comment, %subject, %site, %uri.'),
|
| 255 |
'#default_value' => variable_get('lightcrm_mail', LIGHTCRM_DEFAULT_MAIL),
|
| 256 |
);
|
| 257 |
|
| 258 |
$form['lightcrm_mailsubject'] = array(
|
| 259 |
'#type' => 'textfield',
|
| 260 |
'#title' => t('Mail subject'),
|
| 261 |
'#description' => t('The subject of the mail that is going to be sent to the user. Placeholder variables: %username, %comment, %subject, %site, %uri.'),
|
| 262 |
'#default_value' => variable_get('lightcrm_mailsubject', 'A new comment has been posted.'),
|
| 263 |
);
|
| 264 |
|
| 265 |
$roles = user_roles(1);
|
| 266 |
$form['lightcrm_mailroles'] = array(
|
| 267 |
'#type' => 'checkboxes',
|
| 268 |
'#title' => t('Mail roles'),
|
| 269 |
'#description' => t('Roles that receive email notifications'),
|
| 270 |
'#options' => $roles,
|
| 271 |
'#default_value' => variable_get('lightcrm_mailroles', array()),
|
| 272 |
);
|
| 273 |
|
| 274 |
|
| 275 |
return $form;
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
* Implementation of hook_block().
|
| 280 |
*
|
| 281 |
* This hook both declares to Drupal what blocks are provided by the module, and
|
| 282 |
* generates the contents of the blocks themselves.
|
| 283 |
*/
|
| 284 |
function lightcrm_block($op = 'list', $delta = 0) {
|
| 285 |
// The $op parameter determines what piece of information is being requested.
|
| 286 |
if ($op == 'list') {
|
| 287 |
// If $op is "list", we just need to return a list of block descriptions. This
|
| 288 |
// is used to provide a list of possible blocks to the administrator.
|
| 289 |
$blocks[0]['info'] = t('lightcrm: New comments');
|
| 290 |
return $blocks;
|
| 291 |
}
|
| 292 |
else if ($op == 'view') {
|
| 293 |
// If $op is "view", then we need to generate the block for display purposes.
|
| 294 |
// The $delta parameter tells us which block is being requested.
|
| 295 |
switch ($delta) {
|
| 296 |
case 0:
|
| 297 |
global $user;
|
| 298 |
$node = node_load(array('type' => 'lightcrm', 'uid' => $user->uid));
|
| 299 |
$num = comment_num_new($node->nid);
|
| 300 |
// The subject is displayed at the top of the block. Note that it should
|
| 301 |
// be passed through t() for translation.
|
| 302 |
$block['subject'] = t('%new new comment%s posted', array('%new' => $num, '%s' => $num > 1 ? 's' : ''));
|
| 303 |
// The content of the block is typically generated by calling a custom
|
| 304 |
// function.
|
| 305 |
$block['content'] = lightcrm_block_newcomments();
|
| 306 |
break;
|
| 307 |
|
| 308 |
|
| 309 |
}
|
| 310 |
return $block;
|
| 311 |
}
|
| 312 |
}
|
| 313 |
|
| 314 |
function lightcrm_block_newcomments() {
|
| 315 |
global $user;
|
| 316 |
|
| 317 |
$node = node_load(array('type' => 'lightcrm', 'uid' => $user->uid));
|
| 318 |
//$result = comment_num_new($node->nid);
|
| 319 |
//print_r($result);
|
| 320 |
if ($user->uid) {
|
| 321 |
// Retrieve the timestamp at which the current user last viewed the
|
| 322 |
// specified node.
|
| 323 |
if (!$timestamp) {
|
| 324 |
$timestamp = node_last_viewed($node->nid);
|
| 325 |
}
|
| 326 |
$timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
|
| 327 |
|
| 328 |
// Use the timestamp to retrieve the number of new comments.
|
| 329 |
$result = db_query('SELECT c.cid, c.subject, c.comment, c.uid FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d and c.uid != %d', $node->nid, $timestamp, COMMENT_PUBLISHED, $user->uid);
|
| 330 |
|
| 331 |
while ( $row = db_fetch_object($result) ) {
|
| 332 |
//$items[] = l($row->cid.' <b>('.comment_num_new($row->nid).')</b>', 'node/'.$row->nid, array(), NULL, NULL, FALSE, TRUE);
|
| 333 |
$items[] = l($row->subject, 'user/'.$user->uid.'/lightcrm');
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
| 337 |
if ( ! empty($items ) ) {
|
| 338 |
$output = theme_item_list($items);
|
| 339 |
}
|
| 340 |
|
| 341 |
return $output;
|
| 342 |
}
|
| 343 |
|
| 344 |
|
| 345 |
?>
|