diff options
Diffstat (limited to 'mod/messages/start.php')
| -rw-r--r-- | mod/messages/start.php | 174 |
1 files changed, 117 insertions, 57 deletions
diff --git a/mod/messages/start.php b/mod/messages/start.php index fd8b7712d..6d0e82744 100644 --- a/mod/messages/start.php +++ b/mod/messages/start.php @@ -28,25 +28,11 @@ function messages_init() { 'text' => elgg_echo('messages:sentmessages'), 'href' => "messages/sent/" . elgg_get_logged_in_user_entity()->username, 'context' => 'messages', - )); - - $class = "elgg-icon messages-icon"; - $text = "<span class='$class'></span>"; - - // get unread messages - $num_messages = (int)messages_count_unread(); - if ($num_messages != 0) { - $text .= "<span class=\"messages-new\">$num_messages</span>"; - } - - elgg_register_menu_item('topbar', array( - 'name' => 'messages', - 'href' => 'messages/inbox/' . elgg_get_logged_in_user_entity()->username, - 'text' => $text, - 'priority' => 600, - )); + )); } + elgg_register_event_handler('pagesetup', 'system', 'messages_notifier'); + // Extend system CSS with our own styles, which are defined in the messages/css view elgg_extend_view('css/elgg', 'messages/css'); elgg_extend_view('js/elgg', 'messages/js'); @@ -65,6 +51,9 @@ function messages_init() { elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'messages_notification_msg'); register_notification_object('object', 'messages', elgg_echo('messages:new')); + // delete messages sent by a user when user is deleted + elgg_register_event_handler('delete', 'user', 'messages_purge'); + // ecml elgg_register_plugin_hook_handler('get_views', 'ecml', 'messages_ecml_views_hook'); @@ -88,23 +77,30 @@ function messages_init() { */ function messages_page_handler($page) { + $current_user = elgg_get_logged_in_user_entity(); + if (!$current_user) { + register_error(elgg_echo('noaccess')); + $_SESSION['last_forward_from'] = current_page_url(); + forward(''); + } + elgg_load_library('elgg:messages'); - elgg_push_breadcrumb(elgg_echo('messages'), 'messages/inbox/' . elgg_get_logged_in_user_entity()->username); + elgg_push_breadcrumb(elgg_echo('messages'), 'messages/inbox/' . $current_user->username); if (!isset($page[0])) { $page[0] = 'inbox'; } - // supporting the old inbox url /messages/<username> - $user = get_user_by_username($page[0]); - if ($user) { + // Support the old inbox url /messages/<username>, but only if it matches the logged in user. + // Otherwise having a username like "read" on the system could confuse this function. + if ($current_user->username === $page[0]) { $page[1] = $page[0]; $page[0] = 'inbox'; } if (!isset($page[1])) { - $page[1] = elgg_get_logged_in_user_entity()->username; + $page[1] = $current_user->username; } $base_dir = elgg_get_plugins_path() . 'messages/pages/messages'; @@ -129,11 +125,36 @@ function messages_page_handler($page) { default: return false; } - return true; } /** + * Display notification of new messages in topbar + */ +function messages_notifier() { + if (elgg_is_logged_in()) { + $class = "elgg-icon elgg-icon-mail"; + $text = "<span class='$class'></span>"; + $tooltip = elgg_echo("messages"); + + // get unread messages + $num_messages = (int)messages_count_unread(); + if ($num_messages != 0) { + $text .= "<span class=\"messages-new\">$num_messages</span>"; + $tooltip .= " (" . elgg_echo("messages:unreadcount", array($num_messages)) . ")"; + } + + elgg_register_menu_item('topbar', array( + 'name' => 'messages', + 'href' => 'messages/inbox/' . elgg_get_logged_in_user_entity()->username, + 'text' => $text, + 'priority' => 600, + 'title' => $tooltip, + )); + } +} + +/** * Override the canEditMetadata function to return true for messages * */ @@ -201,18 +222,20 @@ function messages_can_edit_container($hook_name, $entity_type, $return_value, $p * * @param string $subject The subject line of the message * @param string $body The body of the mesage - * @param int $send_to The GUID of the user to send to - * @param int $from Optionally, the GUID of the user to send from - * @param int $reply The GUID of the message to reply from (default: none) - * @param true|false $notify Send a notification (default: true) - * @param true|false $add_to_sent If true (default), will add a message to the sender's 'sent' tray + * @param int $recipient_guid The GUID of the user to send to + * @param int $sender_guid Optionally, the GUID of the user to send from + * @param int $original_msg_guid The GUID of the message to reply from (default: none) + * @param bool $notify Send a notification (default: true) + * @param bool $add_to_sent If true (default), will add a message to the sender's 'sent' tray * @return bool */ -function messages_send($subject, $body, $send_to, $from = 0, $reply = 0, $notify = true, $add_to_sent = true) { +function messages_send($subject, $body, $recipient_guid, $sender_guid = 0, $original_msg_guid = 0, $notify = true, $add_to_sent = true) { + // @todo remove globals global $messagesendflag; $messagesendflag = 1; + // @todo remove globals global $messages_pm; if ($notify) { $messages_pm = 1; @@ -220,33 +243,40 @@ function messages_send($subject, $body, $send_to, $from = 0, $reply = 0, $notify $messages_pm = 0; } - // If $from == 0, set to current user - if ($from == 0) { - $from = (int) elgg_get_logged_in_user_guid(); + // If $sender_guid == 0, set to current user + if ($sender_guid == 0) { + $sender_guid = (int) elgg_get_logged_in_user_guid(); } // Initialise 2 new ElggObject $message_to = new ElggObject(); $message_sent = new ElggObject(); + $message_to->subtype = "messages"; $message_sent->subtype = "messages"; - $message_to->owner_guid = $send_to; - $message_to->container_guid = $send_to; - $message_sent->owner_guid = $from; - $message_sent->container_guid = $from; + + $message_to->owner_guid = $recipient_guid; + $message_to->container_guid = $recipient_guid; + $message_sent->owner_guid = $sender_guid; + $message_sent->container_guid = $sender_guid; + $message_to->access_id = ACCESS_PUBLIC; $message_sent->access_id = ACCESS_PUBLIC; + $message_to->title = $subject; $message_to->description = $body; + $message_sent->title = $subject; $message_sent->description = $body; - $message_to->toId = $send_to; // the user receiving the message - $message_to->fromId = $from; // the user receiving the message + + $message_to->toId = $recipient_guid; // the user receiving the message + $message_to->fromId = $sender_guid; // the user receiving the message $message_to->readYet = 0; // this is a toggle between 0 / 1 (1 = read) $message_to->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag $message_to->hiddenTo = 0; // this is used when a user deletes a message in their inbox - $message_sent->toId = $send_to; // the user receiving the message - $message_sent->fromId = $from; // the user receiving the message + + $message_sent->toId = $recipient_guid; // the user receiving the message + $message_sent->fromId = $sender_guid; // the user receiving the message $message_sent->readYet = 0; // this is a toggle between 0 / 1 (1 = read) $message_sent->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag $message_sent->hiddenTo = 0; // this is used when a user deletes a message in their inbox @@ -259,7 +289,7 @@ function messages_send($subject, $body, $send_to, $from = 0, $reply = 0, $notify // Save the copy of the message that goes to the sender if ($add_to_sent) { - $success2 = $message_sent->save(); + $message_sent->save(); } $message_to->access_id = ACCESS_PRIVATE; @@ -272,22 +302,25 @@ function messages_send($subject, $body, $send_to, $from = 0, $reply = 0, $notify // if the new message is a reply then create a relationship link between the new message // and the message it is in reply to - if ($reply && $success){ - $create_relationship = add_entity_relationship($message_sent->guid, "reply", $reply); + if ($original_msg_guid && $success) { + add_entity_relationship($message_sent->guid, "reply", $original_msg_guid); } $message_contents = strip_tags($body); - if ($send_to != elgg_get_logged_in_user_entity() && $notify) { + if (($recipient_guid != elgg_get_logged_in_user_guid()) && $notify) { + $recipient = get_user($recipient_guid); + $sender = get_user($sender_guid); + $subject = elgg_echo('messages:email:subject'); $body = elgg_echo('messages:email:body', array( - elgg_get_logged_in_user_entity()->name, + $sender->name, $message_contents, - elgg_get_site_url() . "messages/inbox/" . $user->username, - elgg_get_logged_in_user_entity()->name, - elgg_get_site_url() . "messages/compose?send_to=" . elgg_get_logged_in_user_guid() + elgg_get_site_url() . "messages/inbox/" . $recipient->username, + $sender->name, + elgg_get_site_url() . "messages/compose?send_to=" . $sender_guid )); - notify_user($send_to, elgg_get_logged_in_user_guid(), $subject, $body); + notify_user($recipient_guid, $sender_guid, $subject, $body); } $messagesendflag = 0; @@ -345,16 +378,10 @@ function messages_count_unread() { "msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'", ), 'owner_guid' => $user_guid, - 'limit' => 0 + 'count' => true, ); - $num_messages = elgg_get_entities_from_metadata($options); - - if (is_array($num_messages)) { - return sizeof($num_messages); - } - - return 0; + return elgg_get_entities_from_metadata($options); } /** @@ -401,6 +428,39 @@ function messages_user_hover_menu($hook, $type, $return, $params) { return $return; } +/** + * Delete messages from a user who is being deleted + * + * @param string $event Event name + * @param string $type Event type + * @param ElggUser $user User being deleted + */ +function messages_purge($event, $type, $user) { + + if (!$user->getGUID()) { + return; + } + + // make sure we delete them all + $entity_disable_override = access_get_show_hidden_status(); + access_show_hidden_entities(true); + $ia = elgg_set_ignore_access(true); + + $options = array( + 'type' => 'object', + 'subtype' => 'messages', + 'metadata_name' => 'fromId', + 'metadata_value' => $user->getGUID(), + 'limit' => 0, + ); + $batch = new ElggBatch('elgg_get_entities_from_metadata', $options); + foreach ($batch as $e) { + $e->delete(); + } + + elgg_set_ignore_access($ia); + access_show_hidden_entities($entity_disable_override); +} /** * Register messages with ECML. |
