Example #1
0
/**
 * Callback that fires on add_to_calendar event
 *
 * @param string $event  "events_api"
 * @param string $type   "add_to_calendar"
 * @param array  $params Event params
 * @return boolean
 */
function add_to_calendar($event, $type, $params)
{
    $event = $params['event'];
    $calendar = $params['calendar'];
    if (!$event instanceof Event || !$calendar instanceof $calendar) {
        return true;
    }
    $user = $calendar->getContainerEntity();
    if (!$user instanceof ElggUser) {
        return true;
    }
    $ia = elgg_set_ignore_access(false);
    if (!has_access_to_entity($event, $user)) {
        // the user can't see it, lets not notify them
        elgg_set_ignore_access($ia);
        return true;
    }
    elgg_set_ignore_access($ia);
    // notify the user
    $notify_self = false;
    // support for notify self
    if (is_callable('notify_self_should_notify')) {
        $notify_self = notify_self_should_notify($user);
    }
    if (elgg_get_logged_in_user_guid() == $user->guid && !$notify_self) {
        return true;
    }
    $methods = get_calendar_notification_methods($user, 'addtocal');
    if (!$methods) {
        return true;
    }
    $owner = $event->getOwnerEntity();
    $owner_link = elgg_view('output/url', array('text' => $owner->name, 'href' => $owner->getURL()));
    $in_group = '';
    $in_group_link = '';
    $container = $event->getContainerEntity();
    $container_link = elgg_view('output/url', array('text' => $container->name, 'href' => $container->getURL()));
    if ($container instanceof \ElggGroup) {
        $in_group = elgg_echo('events:notify:subject:ingroup', array($container->name));
        $in_group_link = elgg_echo('events:notify:subject:ingroup', array($container_link));
    }
    $event_link = elgg_view('output/url', array('text' => $event->title, 'href' => $event->getURL()));
    $subject = elgg_echo('event:notify:addtocal:subject', array($event->title, $in_group, $owner->name));
    $timezone = Util::getClientTimezone($user);
    $message = elgg_echo('event:notify:addtocal:message', array($owner_link, $event_link, $in_group_link, elgg_view('output/events_ui/date_range', array('start' => $event->getStartTimestamp(), 'end' => $event->getEndTimestamp(), 'timezone' => $timezone)), $event->location, $event->description));
    $params = array('event' => $event, 'entity' => $event, 'calendar' => $calendar, 'user' => $user);
    $subject = elgg_trigger_plugin_hook('events_ui', 'subject:addtocal', $params, $subject);
    $message = elgg_trigger_plugin_hook('events_ui', 'message:addtocal', $params, $message);
    $params = array();
    if ($event->canComment($user->guid)) {
        $params = array('entity' => $event);
    }
    notify_user($user->guid, $event->container_guid, $subject, $message, $params, $methods);
}
Example #2
0
/**
 * Send reminder notifications to users based on their notification settings
 * @todo if there are a *lot* of recipients we should somehow break this off into parallel threads
 * 
 * @param Event $event Event
 * @return void
 */
function send_event_reminder($event, $remindertime = null)
{
    $force_send = true;
    if ($remindertime === null) {
        $remindertime = time();
        $force_send = false;
        // default cron send
    }
    $dbprefix = elgg_get_config('dbprefix');
    $options = array('type' => 'object', 'subtype' => 'calendar', 'relationship' => Calendar::EVENT_CALENDAR_RELATIONSHIP, 'relationship_guid' => $event->guid, 'joins' => array("JOIN {$dbprefix}users_entity ue ON e.container_guid = ue.guid"), 'limit' => false);
    $calendars = new ElggBatch('elgg_get_entities_from_relationship', $options);
    $starttimestamp = $event->getNextOccurrence($remindertime);
    $endtimestamp = $starttimestamp + $event->delta;
    // prevent sending if it was in the past, unless this is a forced reminder
    if (!$force_send && $starttimestamp < strtotime('-10 minutes')) {
        return true;
    }
    $owner = $event->getOwnerEntity();
    $owner_link = elgg_view('output/url', array('text' => $owner->name, 'href' => $owner->getURL()));
    $in_group = '';
    $in_group_link = '';
    $container = $event->getContainerEntity();
    $container_link = elgg_view('output/url', array('text' => $container->name, 'href' => $container->getURL()));
    if ($container instanceof \ElggGroup) {
        $in_group = elgg_echo('events:notify:subject:ingroup', array($container->name));
        $in_group_link = elgg_echo('events:notify:subject:ingroup', array($container_link));
    }
    $event_link = elgg_view('output/url', array('text' => $event->title, 'href' => $event->getURL()));
    $notified = array();
    // users could have multiple calendars
    foreach ($calendars as $calendar) {
        $user = $calendar->getContainerEntity();
        if (in_array($user->guid, $notified)) {
            continue;
        }
        $ia = elgg_set_ignore_access(false);
        if (!has_access_to_entity($event, $user)) {
            error_log($user->username . ' does not have access to ' . $event->guid);
            // the user can't see it, lets not notify them
            $notified[] = $user->guid;
            elgg_set_ignore_access($ia);
            continue;
        }
        elgg_set_ignore_access($ia);
        $notify_self = false;
        // support for notify self
        if (is_callable('notify_self_should_notify')) {
            $notify_self = notify_self_should_notify($user);
        }
        if (elgg_get_logged_in_user_guid() == $user->guid && !$notify_self) {
            $notified[] = $user->guid;
            continue;
        }
        $methods = get_calendar_notification_methods($user, 'eventreminder');
        if (!$methods) {
            $notified[] = $user->guid;
            continue;
        }
        $timezone = Util::getClientTimezone($user);
        $dt = new DateTime(null, new DateTimeZone($timezone));
        $dt->modify("{$event->start_date} {$event->start_time}");
        $original_subject = elgg_echo('event:notify:eventreminder:subject', array($event->title, $in_group, $dt->format('D, F j g:ia T')));
        $original_message = elgg_echo('event:notify:eventreminder:message', array($event_link, $in_group_link, elgg_view('output/events_ui/date_range', array('start' => $starttimestamp, 'end' => $endtimestamp, 'timezone' => $timezone)), $event->location, $event->description));
        $params = array('event' => $event, 'entity' => $event, 'calendar' => $calendar, 'user' => $user, 'starttime' => $starttimestamp, 'endtime' => $endtimestamp);
        $subject = elgg_trigger_plugin_hook('events_ui', 'subject:eventreminder', $params, $original_subject);
        $message = elgg_trigger_plugin_hook('events_ui', 'message:eventreminder', $params, $original_message);
        notify_user($user->guid, $event->container_guid, $subject, $message, array(), $methods);
        $notified[] = $user->guid;
    }
}