Beispiel #1
0
function live_notifications()
{
    // Register a page handler, so we can have nice URLs
    elgg_register_page_handler('live_notifications', 'live_notifications_page_handler');
    elgg_register_event_handler('pagesetup', 'system', 'live_notifications_notifier');
    elgg_register_event_handler('pagesetup', 'system', 'live_notifications_plugin_pagesetup');
    // Extend system CSS with our own styles
    elgg_extend_view('css/elgg', 'live_notifications/css');
    elgg_extend_view('page/elements/topbar', 'live_notifications/popup');
    elgg_extend_view('js/elgg', 'live_notifications/js/live_notifications.init.js');
    elgg_register_entity_type('object', 'notification');
    //disable message notifications site,
    //Comment this line if you want to receive also default site notifications
    unregister_notification_handler("site");
    //Cron to remove old notifications two week ago
    elgg_register_plugin_hook_handler('cron', 'daily', 'live_notifications_cron');
    //Extend add_to_river function for catch a event and create notification
    elgg_register_plugin_hook_handler('creating', 'river', 'catch_add_to_river_event');
    elgg_register_plugin_hook_handler("action", "likes/add", "likes_notification_action");
    //Actions
    $actions_base = elgg_get_plugins_path() . 'live_notifications/actions';
    elgg_register_action('live_notifications/refresh_count', "{$actions_base}/refresh_count.php");
    elgg_register_action('live_notifications/read_all', "{$actions_base}/read_all.php");
    elgg_register_action('live_notifications/delete', "{$actions_base}/delete.php");
    elgg_register_action('live_notifications/delete_all', "{$actions_base}/delete_all.php");
}
/**
 * Sent out the notifications for the provided annotation_id
 *
 * @param int    $id    the id of the annotation
 * @param string $event the type of event
 *
 * @return void
 */
function advanced_notifications_annotation_notification($id, $event)
{
    global $NOTIFICATION_HANDLERS;
    // get the annotation
    $annotation = elgg_get_annotation_from_id($id);
    if (!empty($annotation)) {
        // are notifications on this annotation allowed
        if (advanced_notifications_is_registered_notification_annotation($annotation)) {
            // get the entity the annotation was made on
            $entity = $annotation->getEntity();
            // get the owner of the annotation
            $owner = $annotation->getOwnerEntity();
            if (!empty($entity) && !empty($owner)) {
                // make sure the entity isn't a PRIVATE entity, this shouldn't happed as the commandline shouldn't be called
                if ($entity->access_id != ACCESS_PRIVATE) {
                    // is the entity a registered entity type/subtype, this shouldn't happen see above
                    $default_subject = advanced_notifications_is_registered_notification_entity($entity, true);
                    if (!empty($default_subject)) {
                        // prepare the message to sent
                        $default_message = $default_subject . ": " . $entity->getURL();
                        // check if we need to disable site notifications
                        if (elgg_get_plugin_setting("replace_site_notifications", "advanced_notifications") == "yes") {
                            unregister_notification_handler("site");
                        }
                        if (!empty($NOTIFICATION_HANDLERS) && is_array($NOTIFICATION_HANDLERS)) {
                            // this could take a long time, especialy with large groups
                            set_time_limit(0);
                            // prepare options to get the interested users
                            $options = array("type" => "user", "site_guids" => ELGG_ENTITIES_ANY_VALUE, "limit" => false, "joins" => array("JOIN " . elgg_get_config("dbprefix") . "users_entity ue ON e.guid = ue.guid"), "wheres" => array("(ue.banned = 'no')", "(e.guid <> " . $owner->getGUID() . ")"), "relationship_guid" => $entity->getContainerGUID(), "inverse_relationship" => true, "callback" => "advanced_notifications_row_to_guid");
                            foreach ($NOTIFICATION_HANDLERS as $method => $dummy) {
                                // get the interested users for the entity
                                $options["relationship"] = "notify" . $method;
                                // allow the interested user options to be ajusted
                                $params = array("annotation" => $annotation, "entity" => $entity, "options" => $options, "method" => $method);
                                $options = elgg_trigger_plugin_hook("interested_users:options", "notify:" . $method, $params, $options);
                                if (!empty($options)) {
                                    // we got through the hook, so get the users
                                    $user_guids = elgg_get_entities_from_relationship($options);
                                    if (!empty($user_guids)) {
                                        // process each user
                                        foreach ($user_guids as $user_guid) {
                                            // fetch the user entity to process
                                            $user = get_user($user_guid);
                                            if (!empty($user)) {
                                                // check if the user has access to the entity
                                                if (has_access_to_entity($entity, $user)) {
                                                    // trigger a hook to make a custom message
                                                    $message = elgg_trigger_plugin_hook("notify:annotation:message", $annotation->getSubtype(), array("annotation" => $annotation, "to_entity" => $user, "method" => $method), $default_message);
                                                    // check if the hook made a correct message
                                                    if (empty($message) && $message !== false) {
                                                        // the hook did it incorrect, so reset the message
                                                        $message = $default_message;
                                                    }
                                                    // this is new, trigger a hook to make a custom subject
                                                    $subject = elgg_trigger_plugin_hook("notify:annotation:subject", $annotation->getSubtype(), array("annotation" => $annotation, "to_entity" => $user, "method" => $method), $default_subject);
                                                    // check if the hook made a correct subject
                                                    if (empty($subject)) {
                                                        // the hook did it incorrect, so reset the subject
                                                        $subject = $default_subject;
                                                    }
                                                    // if the hook returnd false, don't sent a notification
                                                    if ($message !== false) {
                                                        notify_user($user->getGUID(), $entity->getContainerGUID(), $subject, $message, null, $method);
                                                    }
                                                }
                                            }
                                            // cleanup some of the caches
                                            _elgg_invalidate_query_cache();
                                            _elgg_invalidate_cache_for_entity($user_guid);
                                            unset($user);
                                        }
                                    }
                                    // some small cleanup
                                    unset($user_guids);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}