/**
 * Handles the sending of notifications when replied on group forum topic
 *
 * @param string         $event      the name of the event
 * @param string         $type       the type of the event
 * @param ElggAnnotation $annotation the supplied ElggAnnotation
 *
 * @return void
 */
function advanced_notifications_create_annotation_event_handler($event, $type, $annotation)
{
    if (!empty($annotation) && $annotation instanceof ElggAnnotation) {
        // is this an annotation on which notifications should be sent
        if (advanced_notifications_is_registered_notification_annotation($annotation)) {
            // check if the entity isn't PRIVATE
            $entity = $annotation->getEntity();
            if (!empty($entity) && $entity->access_id != ACCESS_PRIVATE) {
                // is the entity a registered notification entity
                if (advanced_notifications_is_registered_notification_entity($entity)) {
                    // prepare to call the commandline
                    $commandline_options = array("event" => $event, "type" => $type, "id" => $annotation->id);
                    // in older versions of elgg do this differently
                    if (version_compare(get_version(true), "1.8.6", "<")) {
                        $entity = $annotation->getEntity();
                        unset($commandline_options["id"]);
                        $commandline_options["type"] = $entity->getType();
                        $commandline_options["guid"] = $entity->getGUID();
                        $commandline_options["input"] = "group_topic_post|" . base64_encode($annotation->value);
                    }
                    advanced_notifications_start_commandline($commandline_options);
                }
            }
        }
    }
}
/**
 * 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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}