| Commit | Line | Data |
|---|---|---|
| e50464be | 1 | <?php |
| cd266b9d MM |
2 | // $Id$ |
| 3 | ||
| 56306fe2 TD |
4 | /** |
| 5 | * Implementation of hook_help(). | |
| 6 | */ | |
| 7 | function privatemsg_help($section) { | |
| b6e89a11 | 8 | switch ($section) { |
| 7cbe55d7 K |
9 | case 'admin/help#privatemsg': |
| 10 | $output = '<p>'. t('The private messaging module allows users to send messages to each other without having to share email addresses. An inbox link will appear in the navigation menu. The "write to author" links are included in posts, allowing users to write a private message instead of commenting openly. Allowing users to communicate directly is an important part of building the strength of the community.') .'</p>'; | |
| 11 | $output .= '<p>'. t('Users can also select whether to receive email notices of new messages by editing their user profile. The contacts list contains only users that you have previously messaged. To contact users not in your list, you need to know their local user name. Administrators can set messaging options such as frequency of emails, message status display, and number of messages to display per page. They can also configure \'Write to Author\' options.') .'</p>'; | |
| 12 | $output .= t('<p>You can</p> | |
| 13 | <ul> | |
| 14 | <li>administer privatemsg at <a href="%admin-settings-privatemsg">administer >> settings >> private message</a>.</li> | |
| 15 | <li>view your private messages at <a href="%privatemsg">view inbox</a>.</li> | |
| 16 | ', array('%admin-settings-privatemsg' => url('admin/settings/privatemsg'), '%privatemsg' => url('privatemsg'))) .'</ul>'; | |
| 17 | $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%privatemsg">Privatemsg page</a>.', array('%privatemsg' => 'http://www.drupal.org/handbook/modules/privatemsg/')) .'</p>'; | |
| 18 | return $output; | |
| 56306fe2 TD |
19 | case 'admin/modules#description': |
| 20 | return t('Allows private messages between users.'); | |
| b6e89a11 | 21 | } |
| cd266b9d MM |
22 | } |
| 23 | ||
| 56306fe2 TD |
24 | /** |
| 25 | * Implementation of hook_link(). | |
| 26 | */ | |
| 496acb83 AH |
27 | function privatemsg_link($type, $node = 0, $main = 0) { |
| 28 | global $user; | |
| 29 | static $access = array(); | |
| 30 | if (user_access('access private messages') && ($type == 'node' || $type == 'comment') && variable_get("privatemsg_link_$type", 0) && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { | |
| 31 | if (!isset($access[$node->uid])) { | |
| 32 | $author = user_load(array('uid' => $node->uid)); | |
| edd074cd | 33 | $access[$node->uid] = user_access('access private messages', $author) && $author->uid && (isset($author->privatemsg_allow) ? $author->privatemsg_allow : 1); |
| 496acb83 AH |
34 | } |
| 35 | if ($access[$node->uid]) { | |
| 36 | return array(l(t('write to author'), "privatemsg/msgto/$node->uid")); | |
| 6e7cb68f | 37 | } |
| e50464be | 38 | } |
| e50464be MM |
39 | } |
| 40 | ||
| 56306fe2 TD |
41 | /** |
| 42 | * Implementation of hook_menu(). | |
| 43 | */ | |
| c15e380b | 44 | function privatemsg_menu($may_cache) { |
| 531c0806 | 45 | $items = array(); |
| 5964c151 | 46 | global $user; |
| c15e380b | 47 | |
| 3e0c9a83 ND |
48 | if ($may_cache) { |
| 49 | $items[] = array('path' => 'admin/settings/privatemsg', | |
| 50 | 'title' => 'privatemsg', | |
| 51 | 'callback' => 'privatemsg_configure'); | |
| 52 | } | |
| 53 | else { | |
| 0427268c | 54 | $new = (int)_privatemsg_get_new_messages(); |
| c15e380b | 55 | $items[] = array('path' => 'privatemsg', |
| 0427268c | 56 | 'title' => t('my inbox') . ($new ? ' ('. $new .')' : ''), |
| c15e380b | 57 | 'callback' => 'privatemsg_page', |
| fa3921d2 | 58 | 'access' => user_access('access private messages'), |
| edd074cd | 59 | 'type' => $user->uid && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) ? MENU_DYNAMIC_ITEM : MENU_CALLBACK); |
| 0427268c AH |
60 | |
| 61 | if ($new && (isset($user->privatemsg_setmessage_notify) ? $user->privatemsg_setmessage_notify : 1)) { | |
| 37462af4 AH |
62 | $m = drupal_set_message(); |
| 63 | if (empty($m)) { | |
| 64 | drupal_set_message(strtr(format_plural($new, 'You have one new <a href="%url">private message</a>', 'You have %count new <a href="%url">private messages</a>'), array('%url' => url('privatemsg')))); | |
| 65 | } | |
| 0427268c | 66 | } |
| c15e380b | 67 | } |
| 531c0806 JW |
68 | return $items; |
| 69 | } | |
| 70 | ||
| 56306fe2 TD |
71 | /** |
| 72 | * Implementation of hook_user(). | |
| 73 | */ | |
| 3e0c9a83 | 74 | function privatemsg_user($type, &$edit, &$user, $category = NULL) { |
| 97b48ceb TD |
75 | switch ($type) { |
| 76 | case 'view': | |
| 8ec8d6e3 | 77 | if (user_access('access private messages') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { |
| 496acb83 AH |
78 | return array(t('Private messages') => array(array( |
| 79 | 'value' => l(t('send private message'), 'privatemsg/msgto/'. $user->uid), | |
| 80 | 'class' => 'send-message'))); | |
| 97b48ceb TD |
81 | } |
| 82 | else if ($GLOBALS['user']->uid) { | |
| 83 | return; | |
| 84 | } | |
| 5964c151 | 85 | else if (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) { |
| 97b48ceb | 86 | if (variable_get('user_register', 1)) { |
| 496acb83 AH |
87 | return array(t('Private messages') => array(array( |
| 88 | 'value' => t('<a href="%login">login</a> or <a href="%register">register</a> to send private messages to this user', array('%login' => url('user/login'), '%register' => url('user/register'))), | |
| 89 | 'class' => 'need-login'))); | |
| 97b48ceb TD |
90 | } |
| 91 | else { | |
| 496acb83 AH |
92 | return array(t('Private messages') => array(array( |
| 93 | 'value' => t('<a href="%login">login</a> to send private messages to this user', array('%login' => url('user/login'))), | |
| 94 | 'class' => 'need-login'))); | |
| 97b48ceb TD |
95 | } |
| 96 | } | |
| 3e0c9a83 ND |
97 | break; |
| 98 | ||
| 97b48ceb TD |
99 | case 'form': |
| 100 | if (user_access('access private messages') && $category == 'account') { | |
| 3e0c9a83 ND |
101 | $form = array(); |
| 102 | $form['privatemsg_settings'] = array( | |
| 103 | '#type' => 'fieldset', | |
| 104 | '#title' => t('Private message settings'), | |
| 105 | '#weight' => 4, | |
| 106 | '#collapsible' => TRUE | |
| 107 | ); | |
| 108 | $form['privatemsg_settings']['privatemsg_allow'] = array( | |
| 109 | '#type' => 'checkbox', | |
| 110 | '#title' => t('Allow private messages'), | |
| 111 | '#default_value' => isset($edit['privatemsg_allow']) ? $edit['privatemsg_allow'] : 1, | |
| 112 | '#description' => t('Check this box to allow users to send you private messages.') | |
| 113 | ); | |
| 0427268c AH |
114 | $form['privatemsg_settings']['privatemsg_setmessage_notify'] = array( |
| 115 | '#type' => 'checkbox', | |
| 116 | '#title' => t('Aggressive notification of new messages'), | |
| 117 | '#default_value' => isset($edit['privatemsg_setmessage_notify']) ? $edit['privatemsg_setmessage_notify'] : 1, | |
| 118 | '#description' => t('Show status message on every page until new messages are read.') | |
| 119 | ); | |
| 3e0c9a83 ND |
120 | $form['privatemsg_settings']['privatemsg_mailalert'] = array( |
| 121 | '#type' => 'checkbox', | |
| 122 | '#title' => t('Receive daily e-mail for unread messages'), | |
| 496acb83 | 123 | '#default_value' => isset($edit['privatemsg_mailalert']) ? $edit['privatemsg_mailalert'] : 0, |
| 3e0c9a83 ND |
124 | '#description' => t('Check this box to receive e-mail notification for unread messages. Only one e-mail will be sent per day.') |
| 125 | ); | |
| 126 | ||
| 127 | return $form; | |
| 97b48ceb | 128 | } |
| 3e0c9a83 | 129 | break; |
| 7eeb3778 | 130 | } |
| 09d9ad4e | 131 | } |
| e50464be | 132 | |
| 3e0c9a83 ND |
133 | function privatemsg_configure() { |
| 134 | $form = array(); | |
| 135 | ||
| 136 | $form['privatemsg_max_rate'] = array( | |
| 137 | '#type' => 'select', | |
| 138 | '#title' => t('Private messaging max rate'), | |
| 139 | '#default_value' => variable_get('privatemsg_max_rate', 15), | |
| 140 | '#options' => drupal_map_assoc(array(5, 10, 15, 20, 30, 60), 'format_interval'), | |
| 141 | '#description' => t('Max submit rate for private messaging. To prevent abuse.') | |
| 142 | ); | |
| 143 | $form['privatemsg_sent_status'] = array( | |
| 144 | '#type' => 'select', | |
| 145 | '#title' => t('Sent message status'), | |
| 146 | '#default_value' => variable_get('privatemsg_sent_status', 1), | |
| 147 | '#options' => array(t('Disabled'), t('Enabled')), | |
| 148 | '#description' => t('If enabled users can see whether a message has been read or not.') | |
| 149 | ); | |
| 150 | $form['privatemsg_per_page'] = array( | |
| 151 | '#type' => 'select', | |
| 152 | '#title' => t('Messages per page'), | |
| 153 | '#default_value' => variable_get('privatemsg_per_page', 10), | |
| 154 | '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100)), | |
| 155 | '#description' => t('The maximum number of messages displayed per page; links to browse messages automatically appear.') | |
| 156 | ); | |
| 157 | ||
| 158 | $form['links'] = array( | |
| 159 | '#type' => 'fieldset', | |
| 160 | '#title' => t('"Write to author" links') | |
| 161 | ); | |
| 162 | $form['links']['privatemsg_link_node'] = array( | |
| 163 | '#type' => 'checkbox', | |
| 164 | '#title' => t('Display link with posts'), | |
| 165 | '#return_value' => 1, | |
| 166 | '#default_value' => variable_get('privatemsg_link_node', 0), | |
| 167 | '#description' => t('Provide a link to send private messages to users with posts they start.') | |
| 168 | ); | |
| 169 | $form['links']['privatemsg_link_comment'] = array( | |
| 170 | '#type' => 'checkbox', | |
| 171 | '#title' => t('Display link with comments'), | |
| 172 | '#return_value' => 1, | |
| 173 | '#default_value' => variable_get('privatemsg_link_comment', 0), | |
| 174 | '#description' => t('Provide a link to send private messages to users with their comments.') | |
| 175 | ); | |
| 176 | ||
| 177 | return system_settings_form('privatemsg_settings', $form); | |
| e50464be MM |
178 | } |
| 179 | ||
| 56306fe2 TD |
180 | /** |
| 181 | * Implementation of hook_perm(). | |
| 182 | */ | |
| 939d3820 | 183 | function privatemsg_perm() { |
| 01b35b0d | 184 | return array('access private messages'); |
| 939d3820 | 185 | } |
| e50464be | 186 | |
| 56306fe2 TD |
187 | /** |
| 188 | * Implementation of hook_cron(). | |
| 189 | */ | |
| 939d3820 MM |
190 | function privatemsg_cron() { |
| 191 | // perform these actions just once per day | |
| 01b35b0d | 192 | if (variable_get('privatemsg_last_cron', 0) < (time() - 3600*24)) { |
| 939d3820 MM |
193 | _privatemsg_prune(); |
| 194 | _privatemsg_mailalert(); | |
| 01b35b0d | 195 | variable_set('privatemsg_last_cron', time()); |
| e50464be MM |
196 | } |
| 197 | } | |
| 198 | ||
| 939d3820 MM |
199 | function _privatemsg_prune() { |
| 200 | // move deleted message older than 1 month to archive table, and optimize table | |
| 6c04b224 | 201 | $result = db_query("SELECT * FROM {privatemsg} WHERE author_del = 1 AND recipient_del = 1 AND timestamp < '%d'", (time() - 3600*24*30)); |
| 939d3820 | 202 | while ($message = db_fetch_object($result)) { |
| 5d2d0a74 | 203 | db_query("INSERT INTO {privatemsg_archive} (id, author, recipient, subject, message, timestamp, hostname, format, folder) VALUES ('%d', '%d', '%d', '%s', '%s', '%d', '%s', '%d', '%d')", $message->id, $message->author, $message->recipient, $message->subject, $message->message, $message->timestamp, $message->hostname, $message->format, $message->folder); |
| 6c04b224 | 204 | db_query("DELETE FROM {privatemsg} WHERE id = '%d'", $message->id); |
| 939d3820 | 205 | } |
| b0288e00 | 206 | |
| 939d3820 | 207 | // this is MySQL-specific |
| c03518b1 MW |
208 | if ($GLOBALS['db_type'] == 'mysql') { |
| 209 | db_query("OPTIMIZE TABLE {privatemsg}"); | |
| 210 | } | |
| 939d3820 | 211 | } |
| e50464be | 212 | |
| 939d3820 | 213 | function _privatemsg_mailalert() { |
| 0419133e TD |
214 | global $locale; |
| 215 | $initial_locale = $locale; | |
| 216 | if (function_exists('locale')) { | |
| 217 | $languages = locale_supported_languages(); | |
| 218 | $languages = $languages['name']; | |
| 219 | } | |
| 220 | ||
| 97b48ceb | 221 | $from = variable_get('site_mail', ini_get('sendmail_from')); |
| 5d2d0a74 | 222 | $result = db_query('SELECT COUNT(*) AS c, recipient FROM {privatemsg} WHERE newmsg = 1 AND recipient_del = 0 GROUP BY recipient'); |
| b0288e00 | 223 | |
| 939d3820 | 224 | while ($alert = db_fetch_object($result)) { |
| 0419133e TD |
225 | $user = user_load(array('uid' => $alert->recipient)); |
| 226 | ||
| f8dcbcbb | 227 | if ((isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1) && (isset($user->privatemsg_mailalert) ? $user->privatemsg_mailalert : 0) && $user->status != 0) { |
| 0419133e TD |
228 | // use each user's individual locale |
| 229 | if (function_exists('locale') && $languages[$user->language]) { | |
| 230 | $locale = $user->language; | |
| 231 | } | |
| 232 | ||
| 97b48ceb TD |
233 | $subject = t('New private messages at %site.', array('%site' => variable_get('site_name', 'drupal'))); |
| 234 | $message = t('Hi %name, | |
| 235 | This is an automatic reminder from the site %site. You have %new unread private messages. | |
| 236 | ||
| 237 | To read your messages, follow this link: | |
| 238 | %link1 | |
| 239 | ||
| 93569aeb | 240 | If you don\'t want to receive these emails again, change your preferences here: |
| 85769d19 | 241 | %link2', array('%name' => $user->name, '%site' => variable_get('site_name', 'drupal'), '%new' => $alert->c, '%link1' => url('user/login', 'destination=privatemsg', NULL, 1), '%link2' => url('user/'. $user->uid .'/edit', NULL, NULL, 1))); |
| 0419133e | 242 | |
| 97b48ceb | 243 | user_mail($user->mail, $subject, $message, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"); |
| 0419133e TD |
244 | |
| 245 | // revert to previous (site default) locale | |
| 246 | $locale = $initial_locale; | |
| 939d3820 MM |
247 | } |
| 248 | } | |
| e50464be MM |
249 | } |
| 250 | ||
| 939d3820 | 251 | function privatemsg_page() { |
| b5e4a50c | 252 | global $user; |
| 6680897e | 253 | |
| afe33eb4 | 254 | $breadcrumb = NULL; |
| 6680897e MM |
255 | $op = $_POST["op"]; |
| 256 | $edit = $_POST["edit"]; | |
| 257 | $recipient = $_POST["recipient"]; | |
| 3e0c9a83 ND |
258 | if (isset($edit['messages'])) { |
| 259 | $msg = array_keys($edit['messages']); | |
| 260 | } | |
| 261 | else { | |
| 262 | $msg = array(); | |
| 263 | } | |
| b0288e00 | 264 | |
| 6680897e | 265 | if (empty($op)) { |
| 939d3820 MM |
266 | $op = arg(1); |
| 267 | } | |
| 268 | $arg = arg(2); | |
| e50464be | 269 | |
| edd074cd AH |
270 | if (!$user->uid && !in_array($op, array('form', 'msgto', 'send', t('Send private message')))) { |
| 271 | // If guest is given access to privatemsg, only allow sending messages | |
| 272 | return drupal_access_denied(); | |
| 273 | } | |
| 274 | ||
| c59d21ed | 275 | switch ($op) { |
| 07239b15 | 276 | case 'list': |
| b0288e00 | 277 | $output = _privatemsg_list($arg); |
| 01b35b0d | 278 | $title = t('Private messages'); |
| b0288e00 | 279 | break; |
| 07239b15 | 280 | case 'view': |
| b0288e00 TD |
281 | $output = _privatemsg_view($arg); |
| 282 | $title = t("Read message"); | |
| b73ef2e8 | 283 | $breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg')); |
| b0288e00 | 284 | break; |
| 07239b15 | 285 | case t('Write a new message'): |
| b0288e00 | 286 | $arg = ""; |
| 07239b15 TD |
287 | case 'form': |
| 288 | case 'reply': | |
| b0288e00 TD |
289 | $output = _privatemsg_form($arg); |
| 290 | $title = t("Write a new message"); | |
| b73ef2e8 | 291 | $breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg')); |
| b0288e00 | 292 | break; |
| 07239b15 | 293 | case 'msgto': |
| 496acb83 | 294 | $msg = (object)$msg; |
| e2a1310b | 295 | $msg->recipient = db_result(db_query("SELECT name FROM {users} WHERE uid = '%d'", $arg)); |
| b0288e00 TD |
296 | $output = _privatemsg_form($msg); |
| 297 | $title = t("Write a new message"); | |
| b73ef2e8 | 298 | $breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg')); |
| b0288e00 | 299 | break; |
| 07239b15 TD |
300 | case 'send': |
| 301 | case t('Send private message'): | |
| b0288e00 TD |
302 | if (!$edit["recipient"]) { |
| 303 | $edit["recipient"] = $recipient; | |
| 304 | } | |
| b73ef2e8 | 305 | $breadcrumb = array(l(t('Home'), ''), l(t('private messages'), 'privatemsg')); |
| b0288e00 TD |
306 | $output = _privatemsg_edit($edit); |
| 307 | break; | |
| 07239b15 | 308 | case t('Move to folder'): |
| b0288e00 TD |
309 | if ($edit["folder"] == 0 || db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '%d'", $edit["folder"], $user->uid))) { |
| 310 | // this folder belongs to him | |
| 939d3820 | 311 | if ($msg) { |
| b0288e00 TD |
312 | foreach ($msg as $mid) { |
| 313 | _privatemsg_move($mid, $edit["folder"]); | |
| c03518b1 | 314 | } |
| 496acb83 | 315 | drupal_goto('privatemsg/list/'. $edit['folder']); |
| b0288e00 | 316 | break; |
| 939d3820 | 317 | } |
| b0288e00 | 318 | } |
| e50464be | 319 | |
| afe33eb4 | 320 | $output = _privatemsg_list(); |
| b0288e00 | 321 | break; |
| 07239b15 | 322 | case t('Delete messages'): |
| b0288e00 TD |
323 | if ($msg) { |
| 324 | foreach ($msg as $id) { | |
| 325 | _privatemsg_delete($id); | |
| 939d3820 | 326 | } |
| b0288e00 | 327 | } |
| afe33eb4 | 328 | $output = _privatemsg_list(); |
| b0288e00 | 329 | break; |
| e50464be | 330 | |
| 07239b15 | 331 | case 'delete': |
| b0288e00 | 332 | _privatemsg_delete($arg); |
| afe33eb4 | 333 | $output = _privatemsg_list(); |
| b0288e00 TD |
334 | break; |
| 335 | ||
| 07239b15 TD |
336 | case t('New folder'): |
| 337 | case t('Add folder'): | |
| b0288e00 TD |
338 | if ($edit["name"]) { |
| 339 | // check for uniqueness | |
| 340 | if (!db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE name = '%s' AND uid = '%d'", $edit["name"], $user->uid))) { | |
| 341 | db_query("INSERT INTO {privatemsg_folder} (uid, name) VALUES ('%d', '%s')", $user->uid, $edit["name"]); | |
| 342 | } | |
| afe33eb4 | 343 | $output = _privatemsg_list(); |
| b0288e00 TD |
344 | } |
| 345 | else { | |
| 346 | $title = t('Create new folder'); | |
| afe33eb4 | 347 | $breadcrumb = array(l(t('Home'), ''), l('Private messages', 'privatemsg')); |
| b0288e00 TD |
348 | $output = _privatemsg_new_folder($edit); |
| 349 | } | |
| 350 | break; | |
| 351 | ||
| 07239b15 | 352 | case t('Delete folder'): |
| b0288e00 TD |
353 | // check ownership |
| 354 | if (db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '%d'", $edit["current_folder"], $user->uid))) { | |
| 355 | db_query("DELETE FROM {privatemsg_folder} WHERE fid = '%d'", $edit["current_folder"]); | |
| 356 | db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = '%d'", $edit["current_folder"]); | |
| 357 | } | |
| 358 | ||
| afe33eb4 | 359 | $output = _privatemsg_list(); |
| b0288e00 | 360 | break; |
| 07239b15 | 361 | case t('Empty folder'): |
| b0288e00 TD |
362 | $fid = $edit["current_folder"]; |
| 363 | ||
| 364 | if ($fid == 1) { | |
| 365 | db_query("UPDATE {privatemsg} SET author_del = 1 WHERE author = '%d'", $user->uid); | |
| 366 | } | |
| 367 | else if ($fid == 0 || db_result(db_query("SELECT fid FROM {privatemsg_folder} WHERE fid = '%d' AND uid = '%d'", $fid, $user->uid))) { | |
| 368 | // check ownership | |
| 369 | db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE folder = '%d' AND recipient = '%d'", $edit["current_folder"], $user->uid); | |
| 370 | } | |
| 371 | ||
| afe33eb4 | 372 | $output = _privatemsg_list(); |
| b0288e00 TD |
373 | break; |
| 374 | default; | |
| afe33eb4 | 375 | $output = _privatemsg_list(); |
| 01b35b0d | 376 | $title = t('Private messages'); |
| b0288e00 | 377 | break; |
| 939d3820 | 378 | } |
| 88a3d95c TD |
379 | drupal_set_title($title); |
| 380 | drupal_set_breadcrumb($breadcrumb); | |
| f56af8a1 | 381 | return $output; |
| 939d3820 MM |
382 | } |
| 383 | ||
| afe33eb4 | 384 | function _privatemsg_list($current_folder = 0) { |
| 6c04b224 | 385 | global $user; |
| b0288e00 | 386 | |
| 939d3820 | 387 | if ($current_folder != 1) { |
| 5d2d0a74 | 388 | $result = pager_query("SELECT id, subject, p.timestamp, u.uid, u.name, newmsg FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND p.recipient = %d AND folder = '%s' AND p.recipient_del = 0 ORDER BY p.timestamp DESC", variable_get("privatemsg_per_page", 10), 0, NULL, $user->uid, $current_folder); |
| b0288e00 | 389 | |
| 939d3820 | 390 | if ($current_folder > 0) { |
| c59d21ed | 391 | $folder_name = db_result(db_query("SELECT name FROM {privatemsg_folder} WHERE fid = %d AND uid = %d", $current_folder, $user->uid)); |
| 939d3820 MM |
392 | } |
| 393 | else { | |
| 394 | $folder_name = t("Inbox"); | |
| e50464be | 395 | } |
| 939d3820 MM |
396 | } |
| 397 | else { | |
| 398 | // sent messages | |
| 5d2d0a74 | 399 | $result = pager_query("SELECT id, subject, p.timestamp, u.uid, u.name, newmsg FROM {privatemsg} p, {users} u WHERE p.recipient = u.uid AND p.author = %d AND p.author_del = 0 ORDER BY p.timestamp DESC", variable_get("privatemsg_per_page", 10), 0, NULL, $user->uid); |
| b0288e00 | 400 | |
| 939d3820 | 401 | $folder_name = t("Sent messages"); |
| e50464be | 402 | } |
| b0288e00 | 403 | |
| 939d3820 MM |
404 | $messages = array(); |
| 405 | while ($message = db_fetch_object($result)) { | |
| 406 | $messages[] = $message; | |
| 407 | } | |
| b0288e00 | 408 | |
| 939d3820 | 409 | $folders[] = array(0, t("Inbox")); |
| c59d21ed | 410 | $result = db_query("SELECT fid, name FROM {privatemsg_folder} WHERE uid = %d", $user->uid); |
| 939d3820 MM |
411 | while ($folder = db_fetch_object($result)) { |
| 412 | $folders[] = array($folder->fid, $folder->name); | |
| e50464be | 413 | } |
| 939d3820 | 414 | $folders[] = array(1, t("Sent messages")); |
| b0288e00 | 415 | |
| afe33eb4 | 416 | return theme("privatemsg_list", $current_folder, $messages, $folders); |
| 939d3820 | 417 | } |
| e50464be | 418 | |
| 939d3820 MM |
419 | function _privatemsg_format_folder($current, $fid, $name) { |
| 420 | if ($current == $fid) { | |
| 5d2d0a74 | 421 | return "<strong>$name</strong>"; |
| 939d3820 MM |
422 | } |
| 423 | else { | |
| 424 | return l($name, "privatemsg/list/$fid"); | |
| 425 | } | |
| e50464be MM |
426 | } |
| 427 | ||
| afe33eb4 | 428 | function _privatemsg_form($message = 0) { |
| b5e4a50c | 429 | global $user; |
| 3e0c9a83 | 430 | $form = array(); |
| 939d3820 MM |
431 | |
| 432 | if ($message) { | |
| 3e0c9a83 | 433 | // This is a reply to another message |
| 939d3820 | 434 | if (!is_object($message)) { |
| 62aa5483 | 435 | $message = db_fetch_object(db_query("SELECT subject, message, u.name AS recipient FROM {privatemsg} p, {users} u WHERE u.uid = p.author AND id = '%d' AND recipient = '%d'", $message, $user->uid)); |
| e50464be | 436 | |
| 6c04b224 TD |
437 | if (!stristr($message->subject, t("Re:"))) { |
| 438 | $message->subject = t("Re:").' '.$message->subject; | |
| 939d3820 | 439 | } |
| b0288e00 TD |
440 | |
| 441 | // quoting | |
| c59d21ed | 442 | $message->message = "\n".str_replace("\n", "\n> ", "\n".$message->message); |
| 939d3820 | 443 | } |
| 09d9ad4e | 444 | } |
| b0288e00 | 445 | |
| 3e0c9a83 | 446 | drupal_add_js(drupal_get_path('module', 'privatemsg') .'/privatemsg.js'); |
| 5d2d0a74 | 447 | |
| 3e0c9a83 ND |
448 | $form['recipient'] = array( |
| 449 | '#prefix' => '<div class="container-inline">', | |
| 450 | '#type' => 'textfield', | |
| 451 | '#title' => t("To"), | |
| 452 | '#default_value' => $message->recipient, | |
| 453 | '#size' => 50, | |
| 454 | '#maxlength' => 64 | |
| 455 | ); | |
| 5d2d0a74 | 456 | $result = db_query("SELECT DISTINCT(name) AS name FROM {privatemsg} p, {users} u WHERE p.author = u.uid AND recipient = '%d' AND p.timestamp > (UNIX_TIMESTAMP(NOW()) - (3600 * 24 * 30)) ORDER BY name", $user->uid); |
| 3e0c9a83 | 457 | $contacts = array(t('Contacts')); |
| 5d2d0a74 | 458 | while ($name = db_fetch_object($result)) { |
| 3e0c9a83 ND |
459 | $contacts[] = check_plain($name->name); |
| 460 | } | |
| 1431d10a AH |
461 | if (module_exist('buddylist')) { |
| 462 | $result = db_query("SELECT u.name FROM {buddylist} b, {users} u WHERE b.buddy = u.uid AND b.uid = '%d'", $user->uid); | |
| 463 | while ($name = db_fetch_object($result)) { | |
| 464 | $buddyname = check_plain($name->name); | |
| 465 | if (!in_array($buddyname, $contacts)) { | |
| 466 | $contacts[] = $buddyname; | |
| 467 | } | |
| 468 | } | |
| 469 | sort($contacts); | |
| 470 | } | |
| 3e0c9a83 ND |
471 | $form['quick'] = array( |
| 472 | '#type' => 'select', | |
| 473 | '#options' => drupal_map_assoc($contacts), | |
| 474 | '#attributes' => array('style' => 'display: none;'), // hidden unless JS is working | |
| 475 | '#suffix' => '</div>' | |
| 476 | ); | |
| 477 | ||
| 478 | $form['subject'] = array( | |
| 479 | '#type' => 'textfield', | |
| 480 | '#title' => t("Subject"), | |
| 481 | '#default_value' => $message->subject, | |
| 482 | '#size' => 50, | |
| 483 | '#maxlength' => 64 | |
| 484 | ); | |
| f352752e | 485 | $form['privatemsgbody'] = array( |
| 3e0c9a83 ND |
486 | '#type' => 'textarea', |
| 487 | '#title' => t("Message"), | |
| 488 | '#default_value' => $message->message, | |
| 489 | '#cols' => 80, | |
| 490 | '#rows' => 5 | |
| 491 | ); | |
| 492 | $form[] = filter_form($message->format); | |
| 493 | $form['op'] = array( | |
| 494 | '#type' => 'submit', | |
| 495 | '#value' => t("Send private message"), | |
| 496 | '#name' => 'op' | |
| 497 | ); | |
| 498 | ||
| 499 | return drupal_get_form('privatemsg_message_form', $form); | |
| e50464be MM |
500 | } |
| 501 | ||
| 939d3820 | 502 | function _privatemsg_edit($edit) { |
| 6680897e | 503 | global $user; |
| b0288e00 | 504 | |
| 5d2d0a74 TD |
505 | if ($edit['recipient'] == '') { |
| 506 | form_set_error('recipient', t('The <em>Recipient</em> field is required.')); | |
| 496acb83 | 507 | return _privatemsg_form((object)$edit); |
| b0288e00 | 508 | } |
| c03518b1 | 509 | else { |
| 5d2d0a74 TD |
510 | $recipient = user_load(array('name' => $edit['recipient'])); |
| 511 | } | |
| 512 | ||
| 513 | if (!$recipient->uid) { | |
| 514 | form_set_error('recipient', t('The <em>Recipient</em> does not exist.')); | |
| 496acb83 | 515 | return _privatemsg_form((object)$edit); |
| 5d2d0a74 | 516 | } |
| 3e0c9a83 ND |
517 | else if (!(isset($recipient->privatemsg_allow) ? $recipient->privatemsg_allow : 1)) { |
| 518 | form_set_error('recipient', t('%name does not accept private messages.', array('%name' => $recipient->name))); | |
| 496acb83 | 519 | return _privatemsg_form((object)$edit); |
| 5d2d0a74 | 520 | } |
| 496acb83 | 521 | else if (trim($edit['subject']) == '') { |
| 5d2d0a74 | 522 | form_set_error('subject', t('The <em>Subject</em> field is required.')); |
| 496acb83 | 523 | return _privatemsg_form((object)$edit); |
| 5d2d0a74 | 524 | } |
| f352752e AH |
525 | if (trim($edit['privatemsgbody']) == '') { |
| 526 | form_set_error('privatemsgbody', t('The <em>Message</em> field is required.')); | |
| 496acb83 | 527 | return _privatemsg_form((object)$edit); |
| 5d2d0a74 TD |
528 | } |
| 529 | else if (array_key_exists('format', $edit) && !filter_access($edit['format'])) { | |
| 530 | form_set_error('format', t('The supplied input format is invalid.')); | |
| 496acb83 | 531 | return _privatemsg_form((object)$edit); |
| 5d2d0a74 TD |
532 | } |
| 533 | else { | |
| f352752e | 534 | $result = db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d')", $user->uid, $recipient->uid, $edit['subject'], $edit['privatemsgbody'], time(), 1, getenv("REMOTE_ADDR"), $edit['format']); |
| 5d2d0a74 | 535 | drupal_set_message(t('Message sent.')); |
| edd074cd | 536 | drupal_goto($user->uid ? 'privatemsg' : ''); |
| e50464be MM |
537 | } |
| 538 | } | |
| 539 | ||
| 939d3820 | 540 | function _privatemsg_view($message_id) { |
| b5e4a50c | 541 | global $user; |
| b0288e00 | 542 | |
| 5d2d0a74 | 543 | $result = db_query("SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.format FROM {privatemsg} p, {users} u WHERE (recipient = '%d' OR author = '%d') AND author = u.uid AND id = '%d'", $user->uid, $user->uid, $message_id); |
| b0288e00 | 544 | |
| e50464be | 545 | $message = db_fetch_object($result); |
| 5d2d0a74 TD |
546 | if (($message->newmsg) && ($user->uid == $message->recipient)) { |
| 547 | $result = db_query("UPDATE {privatemsg} SET newmsg = 0 WHERE id = %d", $message_id); | |
| 939d3820 | 548 | } |
| b0288e00 | 549 | |
| c03518b1 | 550 | return theme("privatemsg_view", $message); |
| e50464be MM |
551 | } |
| 552 | ||
| 939d3820 | 553 | function _privatemsg_delete($id) { |
| e50464be | 554 | global $user; |
| 939d3820 | 555 | |
| c59d21ed | 556 | $result = db_query("SELECT author, recipient FROM {privatemsg} WHERE (recipient = %d OR author = %d) AND id = %d", $user->uid, $user->uid, $id); |
| 0fa4736b | 557 | |
| 939d3820 MM |
558 | if ($message = db_fetch_object($result)) { |
| 559 | if ($message->author == $user->uid) { | |
| 6c04b224 | 560 | db_query("UPDATE {privatemsg} SET author_del = 1 WHERE id = '%d'", $id); |
| 939d3820 | 561 | } |
| d9b4b7ae | 562 | if ($message->recipient == $user->uid) { |
| 6c04b224 | 563 | db_query("UPDATE {privatemsg} SET recipient_del = 1 WHERE id = '%d'", $id); |
| e50464be | 564 | } |
| b0288e00 | 565 | |
| 939d3820 | 566 | return true; |
| b0288e00 TD |
567 | } |
| 568 | else { | |
| 939d3820 | 569 | return false; |
| e50464be MM |
570 | } |
| 571 | } | |
| 572 | ||
| 01b35b0d TD |
573 | function _privatemsg_get_new_messages($uid = 0) { |
| 574 | global $user; | |
| 496acb83 | 575 | static $cache = array(); |
| 01b35b0d TD |
576 | if ($uid == 0) { |
| 577 | $uid = $user->uid; | |
| 578 | } | |
| 496acb83 AH |
579 | if (!isset($cache[$uid])) { |
| 580 | $cache[$uid] = db_result(db_query("SELECT COUNT(*) FROM {privatemsg} WHERE recipient = '%d' AND newmsg = 1 AND recipient_del = 0", $uid)); | |
| 581 | } | |
| 582 | return $cache[$uid]; | |
| e50464be MM |
583 | } |
| 584 | ||
| 939d3820 | 585 | function _privatemsg_new_folder($edit) { |
| 3e0c9a83 ND |
586 | $form = array(); |
| 587 | ||
| 588 | $form['name'] = array( | |
| 589 | '#type' => 'textfield', | |
| 590 | '#title' => t('Name'), | |
| 591 | '#size' => 50, | |
| 592 | '#maxlength' => 64 | |
| 593 | ); | |
| 594 | $form['op'] = array( | |
| 595 | '#type' => 'submit', | |
| 596 | '#value' => t('Add folder'), | |
| 597 | '#name' => 'op' | |
| 598 | ); | |
| 599 | ||
| 600 | return drupal_get_form('privatemsg_new_folder', $form); | |
| e50464be MM |
601 | } |
| 602 | ||
| 939d3820 | 603 | function _privatemsg_move($mid, $fid) { |
| e50464be | 604 | global $user; |
| 6c04b224 | 605 | db_query("UPDATE {privatemsg} SET folder = '%d' WHERE id = '%d' AND recipient = '%d'", $fid, $mid, $user->uid); |
| e50464be MM |
606 | } |
| 607 | ||
| 6c04b224 TD |
608 | /** |
| 609 | @addtogroup theme_system | |
| 610 | ||
| 611 | Privatemsg module specific theme functions | |
| 612 | @{ | |
| 613 | **/ | |
| 614 | ||
| 615 | /** | |
| 616 | Returns content to view a private message | |
| 617 | ||
| 618 | @param message | |
| 619 | **/ | |
| 620 | function theme_privatemsg_view($message) { | |
| 621 | global $user; | |
| 622 | ||
| 623 | if ($message) { | |
| 2ca200d0 K |
624 | $body = ' |
| 625 | <p><strong>'.t('From').':</strong> '. theme('username', $message) .'<br /> | |
| 626 | <strong>'.t('To').':</strong> '. theme('username', user_load(array('uid' => $message->recipient))) .'<br /> | |
| 627 | <strong>'.t('Subject').':</strong> '.check_plain($message->subject) .'<br /> | |
| 628 | <strong>'.t('Date').':</strong> '. format_date($message->timestamp) .'</p> | |
| 629 | <div class="privatemsgbody"> | |
| 630 | '. check_markup($message->message, $message->format, FALSE). | |
| 631 | '</div>'; | |
| f7fe5a12 | 632 | $links = array(); |
| 6c04b224 | 633 | if ($message->recipient == $user->uid) { |
| 44301cd9 | 634 | $author = user_load(array('uid' => $message->uid)); |
| edd074cd | 635 | if ($author->uid && (isset($author->privatemsg_allow) ? $author->privatemsg_allow : 1)) { |
| 44301cd9 AH |
636 | $links[] = l(t('Reply to this message'), "privatemsg/reply/$message->id"); |
| 637 | } | |
| 638 | else { | |
| 639 | $links[] = t('Sender does not accept replies'); | |
| 640 | } | |
| 6c04b224 TD |
641 | } |
| 642 | if (($message->recipient == $user->uid) || (variable_get("privatemsg_sent_status", 1))) { | |
| f7fe5a12 | 643 | $links[] = l(t('Delete this message'), "privatemsg/delete/$message->id", array('onClick' => "return confirm('".t('Are you sure to delete this message?')."')")); |
| 6c04b224 | 644 | } |
| 0fa4736b | 645 | |
| f7fe5a12 | 646 | $links[] = l(t('List messages'), 'privatemsg'); |
| 97b48ceb | 647 | $body .= '<div class="links">'. theme('links', $links) .'</div>'; |
| c59d21ed TD |
648 | } |
| 649 | else { | |
| f7fe5a12 TD |
650 | drupal_set_message(t('Error: you can\'t read this message'), 'error'); |
| 651 | $body = ''; | |
| 6c04b224 TD |
652 | } |
| 653 | ||
| 654 | return $body; | |
| 655 | } | |
| 656 | ||
| 657 | /** | |
| 658 | Returns content to view a list of private messages | |
| 659 | ||
| 660 | @param current_folder | |
| 6c04b224 TD |
661 | @param messages |
| 662 | @param folders | |
| 663 | **/ | |
| afe33eb4 | 664 | function theme_privatemsg_list($current_folder, $messages, $folders) { |
| 3e0c9a83 ND |
665 | $form = array(); |
| 666 | ||
| afe33eb4 TD |
667 | $extra_folders = array(); |
| 668 | foreach ($folders as $folder) { | |
| 669 | $folder_list[] = _privatemsg_format_folder($current_folder, $folder[0], $folder[1]); | |
| 670 | if ($folder[0] != 1 && $folder[0] != $current_folder) { | |
| 671 | $extra_folders[$folder[0]] = $folder[1]; | |
| 672 | } | |
| 673 | } | |
| 0fa4736b | 674 | $out = theme('links', $folder_list); |
| afe33eb4 | 675 | |
| 3e0c9a83 ND |
676 | $form['messages'] = array( |
| 677 | '#theme' => 'privatemsg_message_table', | |
| 678 | '#tree' => TRUE | |
| 679 | ); | |
| 680 | $form['messages']['current_folder'] = array( | |
| 681 | '#type' => 'value', | |
| 682 | '#value' => $current_folder | |
| 683 | ); | |
| 6c04b224 | 684 | foreach ($messages as $message) { |
| 6c04b224 | 685 | if ($current_folder != 1) { |
| 5d2d0a74 | 686 | $new = $message->newmsg; |
| 6c04b224 TD |
687 | } |
| 688 | else { | |
| 689 | if (variable_get("privatemsg_sent_status", 1)) { | |
| 5d2d0a74 | 690 | $new = $message->newmsg; |
| 6c04b224 TD |
691 | } |
| 692 | else { | |
| 693 | $new = 0; | |
| 694 | } | |
| 695 | } | |
| 0fa4736b | 696 | |
| 3e0c9a83 ND |
697 | $form['messages'][$message->id] = array(); |
| 698 | $form['messages'][$message->id]['selected'] = array( | |
| 699 | '#type' => 'checkbox', | |
| 700 | ); | |
| 701 | $form['messages'][$message->id]['date'] = array( | |
| 702 | '#type' => 'value', | |
| 703 | '#value' => format_date($message->timestamp, 'small') | |
| 704 | ); | |
| 705 | $form['messages'][$message->id]['user'] = array( | |
| 706 | '#type' => 'value', | |
| 707 | '#value' => theme('username', $message) | |
| 708 | ); | |
| 709 | $form['messages'][$message->id]['subject'] = array( | |
| 710 | '#type' => 'value', | |
| 711 | '#value' => l($message->subject, 'privatemsg/view/'. $message->id) . ($new ? (' '. theme('mark')) : '') | |
| 712 | ); | |
| 713 | } | |
| 714 | $form[] = array('#type' => 'markup', '#value' => theme('pager', array(), variable_get('privatemsg_per_page', 10))); | |
| 0fa4736b | 715 | |
| afe33eb4 | 716 | if (count($messages) > 0) { |
| 3e0c9a83 ND |
717 | $form['delete_messages'] = array( |
| 718 | '#type' => 'submit', | |
| 719 | '#value' => t('Delete messages'), | |
| 720 | '#name' => 'op', | |
| 721 | '#attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete these messages?') ."')") | |
| 722 | ); | |
| afe33eb4 | 723 | } |
| 6c04b224 | 724 | // folder management |
| afe33eb4 | 725 | if ((count($extra_folders) > 0) && ($current_folder != 1) && (count($messages) > 0)) { |
| 3e0c9a83 ND |
726 | $form['folder'] = array( |
| 727 | '#prefix' => '<div class="container-inline">', | |
| 728 | '#type' => 'select', | |
| 729 | '#options' => $extra_folders | |
| 730 | ); | |
| 731 | $form['move_messages'] = array( | |
| 732 | '#type' => 'submit', | |
| 733 | '#value' => t('Move to folder'), | |
| 734 | '#name' => 'op', | |
| 735 | '#suffix' => '</div>' | |
| 736 | ); | |
| 6c04b224 | 737 | } |
| b0288e00 | 738 | |
| 3e0c9a83 ND |
739 | $form[] = array('#type' => 'markup', '#value' => '<br />'); |
| 740 | ||
| 741 | $form['new_message'] = array( | |
| 742 | '#type' => 'submit', | |
| 743 | '#value' => t('Write a new message'), | |
| 744 | '#name' => 'op' | |
| 745 | ); | |
| 746 | ||
| 747 | $form[] = array('#type' => 'markup', '#value' => '<br />'); | |
| 748 | ||
| 6c04b224 TD |
749 | if ($current_folder > 1) { |
| 750 | // you can't delete Inbox | |
| 3e0c9a83 ND |
751 | $form['delete_folder'] = array( |
| 752 | '#type' => 'submit', | |
| 753 | '#value' => t('Delete folder'), | |
| 754 | '#name' => 'op', | |
| 755 | '#attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete this folder and all its messages?') ."')") | |
| 756 | ); | |
| 6c04b224 | 757 | } |
| afe33eb4 | 758 | if (count($messages) > 0) { |
| 3e0c9a83 ND |
759 | $form['empty_folder'] = array( |
| 760 | '#type' => 'submit', | |
| 761 | '#value' => t('Empty folder'), | |
| 762 | '#name' => 'op', | |
| 763 | '#attributes' => array('onclick' => "return confirm('". t('Are you sure you want to delete every message in this folder?') ."')") | |
| 764 | ); | |
| 765 | } | |
| 766 | $form['current_folder'] = array( | |
| 767 | '#type' => 'hidden', | |
| 768 | '#value' => $current_folder | |
| 769 | ); | |
| 770 | $form['new_folder'] = array( | |
| 771 | '#type' => 'submit', | |
| 772 | '#value' => t('New folder'), | |
| 773 | '#name' => 'op' | |
| 774 | ); | |
| 775 | ||
| 776 | return $out . drupal_get_form('privatemsg_list_form', $form); | |
| 777 | } | |
| 778 | ||
| 779 | function theme_privatemsg_message_table($form) { | |
| 780 | $rows = array(); | |
| 781 | ||
| 782 | foreach (element_children($form) as $key) { | |
| 783 | if ($key != 'current_folder') { | |
| 784 | $row = array(); | |
| 785 | $row[] = form_render($form[$key]['selected']); | |
| 786 | $row[] = $form[$key]['date']['#value']; | |
| 787 | $row[] = $form[$key]['user']['#value']; | |
| 788 | $row[] = $form[$key]['subject']['#value']; | |
| 789 | $rows[] = $row; | |
| 790 | } | |
| afe33eb4 | 791 | } |
| b0288e00 | 792 | |
| 3e0c9a83 ND |
793 | if (count($rows) == 0) { |
| 794 | $rows[] = array(array('data' => t('No messages.'), 'colspan' => 4)); | |
| 795 | } | |
| 796 | ||
| 797 | $header = array( | |
| 798 | NULL, | |
| 799 | t('Date'), | |
| 800 | ($form['current_folder']['#value'] == 1 ? t('To') : t('From')), | |
| 801 | t('Subject'), | |
| 802 | ); | |
| 803 | ||
| 804 | return theme('table', $header, $rows); | |
| 6c04b224 TD |
805 | } |
| 806 | ||
| 807 | /** @} End of addtogroup theme_system **/ | |
| 808 | ||
| 7eeb3778 | 809 | ?> |