<?php /** * Display a timezone input */ namespace Events\UI; use Events\API\Util; elgg_load_js('events/timezone'); $name = elgg_extract('name', $vars, 'timezone'); $value = elgg_extract('value', $vars, Util::getClientTimezone()); $this_timezone = Util::getTimezoneInfo($value); $this_country_code = $this_timezone->country_code; $timezones = Util::getTimezonesByCountry(); $country_options = array(); $timezone_options = array(); foreach ($timezones as $country_code => $country_timezones) { $country_options[$country_code] = elgg_echo("timezone:country:{$country_code}"); if ($country_code == $this_country_code) { foreach ($country_timezones as $country_timezone) { $timezone_options[$country_timezone->id] = $country_timezone->label; } } } asort($country_options); ?> <div class="elgg-input-timezone clearfix"> <div class="elgg-col elgg-col-1of3"> <?php echo elgg_view('input/dropdown', array('data-timezone-country' => $this_country_code, 'value' => $this_country_code, 'options_values' => $country_options)); ?>
*/ namespace Events\UI; use DateTime; use DateTimeZone; use Events\API\Util; $start = (int) elgg_extract('start', $vars); $end = (int) elgg_extract('end', $vars); $timezone = elgg_extract('timezone', $vars, Util::UTC); if (!$start || !$end) { return; } if (!$vars['user']) { $vars['user'] = elgg_get_logged_in_user_entity(); } $client_tz = Util::getClientTimezone($vars['user']); $dt_start = new DateTime("@{$start}", new DateTimeZone($timezone)); $start_at_org_tz = $dt_start->format('D, F j, Y H:ia T'); $dt_start->setTimezone(new DateTimeZone($client_tz)); $dt_end = new DateTime("@{$end}", new DateTimeZone($timezone)); $end_at_org_tz = $dt_end->format('D, F j, Y H:ia T'); $dt_end->setTimezone(new DateTimeZone($client_tz)); $output = ''; if ($dt_start->format('Y-m-d') == $dt_end->format('Y-m-d')) { if (date('Y') == $dt_start->format('Y')) { $output .= $dt_start->format('D, F j'); } else { $output .= $dt_start->format('D, F j, Y'); } $output .= ', ' . $dt_start->format('g:ia') . ' - ' . $dt_end->format('g:ia'); } else {
/** * 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); }
/** * Checks if two timestamps fall on the same week day of the month (e.g. 3rd Monday) * * @param int $ts1 First timestamp * @param int $ts2 Second timestamp * @return bool */ public static function isOnSameWeekDayOfMonth($ts1 = 0, $ts2 = 0, $tz = null) { if (!Util::isValidTimezone($tz)) { $tz = Util::getClientTimezone(); } if (!self::isOnSameDayOfWeek($ts1, $ts2, $tz)) { return false; } return self::getWeekDayNthInMonth($ts1, $tz) == self::getWeekDayNthInMonth($ts2, $tz); }
/** * 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; } }
<?php namespace Events\UI; use Events\API\Util; $tz = Util::getClientTimezone(); echo "elgg.config.timezone='{$tz}';";
</div> <?php if (elgg_get_plugin_setting('timezone_picker', 'events_ui')) { ?> <div class="events-ui-row"> <label><?php echo elgg_echo('events:edit:label:timezone'); ?> </label> <?php echo elgg_view('input/timezone', array('name' => 'timezone', 'value' => $entity->timezone ?: Util::getClientTimezone())); ?> </div> <?php } else { elgg_echo('input/hidden', array('name' => 'timezone', 'value' => Util::getClientTimezone())); } ?> <div class="events-ui-row"> <ul class="elgg-menu elgg-menu-hz"> <?php // <li> // <label> // // echo elgg_view('input/checkbox', array( // 'name' => 'all_day', // 'value' => 1, // 'checked' => $entity->all_day ? true : false // )); // echo elgg_echo('events_ui:allday'); //
/** * Filter default timezones to only include those specified in plugin settings * * @param string $hook "timezones" * @param string $type "events_api" * @param array $return Current list of timezones * @param array $params Additional params * @return array Filtered list */ function filter_timezones($hook, $type, $return, $params) { $setting = elgg_get_plugin_setting('custom_timezones', 'events_ui'); $custom = $setting ? unserialize($setting) : false; if (!empty($custom)) { $default = Util::getClientTimezone(); foreach ($return as $key => $value) { if (!in_array($key, $custom) && $key !== $default) { unset($return[$key]); } } } return $return; }
/** * Returns all event occurrences (one-time and recurring) in a given time range * To prevent memory leaks, the return is a sorted array formatted as: * <code> * array( * 0 => array( * 'id' => $guid, * 'start' => $start_time, * 'end' => $end_time, * 'title' => $title, * 'description' => $description, * 'url' => $url, * ... * ), * ); * </code> * * @param int $starttime Range start timestamp * @param int $endtime Range end timestamp * @param bool $export Export EventInstance objects to array * @param string $consumer Consumer name (passed to the export hook, so plugins can decide on exportable values) * @return EventsInstance[]|array */ public function getAllEventInstances($starttime = null, $endtime = null, $export = true, $consumer = '', $tz = null) { $instances = array(); if (!Util::isValidTimezone($tz)) { if (elgg_is_logged_in()) { // if logged in use the timezone settings of the current user $tz = Util::getClientTimezone(); } else { // use timezone of calendar owner $tz = Util::getClientTimezone($this->getOwnerEntity()); } } $events = $this->getAllEvents($starttime, $endtime); foreach ($events as $event) { /* @var $event Event */ if (!$event instanceof Event) { continue; } $start_times = $event->getStartTimes($starttime, $endtime, $tz); foreach ($start_times as $start_time) { $instance = new EventInstance($event, $start_time); $instance->setCalendar($this); $instances[] = $instance; } } usort($instances, array($this, 'compareInstancesByStartTime')); if ($export) { foreach ($instances as $key => $instance) { $instances[$key] = $instance->export($consumer); } } return $instances; }
<?php /** * Allows users to select their preferred timezone */ namespace Events\UI; use Events\API\Util; $user = elgg_get_page_owner_entity(); if ($user) { $title = elgg_echo('user:set:timezone'); $content = '<label>' . elgg_echo('user:timezone:label') . '</label>'; $content .= elgg_view("input/timezone", array('name' => 'timezone', 'value' => Util::getClientTimezone($user))); echo elgg_view_module('info', $title, $content); }
forward('', '403'); } } $entity = get_entity($guid); if (!$entity instanceof Calendar) { forward('', '404'); } $owner = $entity->getOwnerEntity(); elgg_set_page_owner_guid($owner->guid); //elgg_push_breadcrumb(elgg_echo('events:calendar'), "calendar/all"); elgg_push_breadcrumb($owner->name, "calendar/owner/{$owner->username}"); elgg_push_breadcrumb($entity->getDisplayName()); $start = (int) get_input('start', time()); $start = (int) Util::getMonthStart($start); $end = (int) Util::getMonthEnd($start); $timezone = Util::getClientTimezone(); $start_local = $start - Util::getOffset($start, Util::UTC, $timezone); $end_local = $end - Util::getOffset($end, Util::UTC, $timezone); $events = $entity->getAllEventInstances($start_local, $end_local); if (!$is_logged_in) { logout(); } elgg_register_menu_item('title', array('name' => 'calendar_view', 'href' => elgg_http_add_url_query_elements("calendar/view/{$entity->guid}", array('start' => $start, 'end' => $end)), 'text' => elgg_echo('events:view:calendar'), 'title' => elgg_echo('events:view:calendar:switch'), 'link_class' => 'elgg-button elgg-button-action')); elgg_register_menu_item('title', array('name' => 'ical_view', 'href' => $entity->getIcalURL("calendar/ical/{$entity->guid}/calendar{$entity->guid}.ics", array()), 'text' => elgg_echo('events:view:ical'), 'title' => elgg_echo('events:view:ical'), 'link_class' => 'elgg-button elgg-button-action js-events-ui-ical-modal-trigger')); $dt = new DateTime(null, new DateTimeZone(Util::UTC)); $prev_start = $dt->setTimestamp($start)->modify('-1 month')->getTimestamp(); $next_start = $dt->setTimestamp($start)->modify('+1 month')->getTimestamp(); elgg_register_menu_item('title', array('name' => 'prev_month', 'text' => "« " . $dt->setTimestamp($prev_start)->format('F'), 'href' => elgg_http_add_url_query_elements("calendar/feed/{$entity->guid}", array('start' => $prev_start)), 'link_class' => 'elgg-button elgg-button-action mlm', 'priority' => 100)); elgg_register_menu_item('title', array('name' => 'next_month', 'text' => $dt->setTimestamp($next_start)->format('F') . " »", 'href' => elgg_http_add_url_query_elements("calendar/feed/{$entity->guid}", array('start' => $next_start)), 'link_class' => 'elgg-button elgg-button-action', 'priority' => 101)); $title = elgg_echo('events:feed:month', array($dt->setTimestamp($start)->format('F'))); $content = elgg_view('events_ui/feed', array('events' => $events));
public function getRecurringDescription($viewer = null) { if (!$this->isRecurring()) { return elgg_echo('events_ui:repeat:once'); } if ($viewer === null) { $viewer = elgg_get_logged_in_user_entity(); } $c_timezone = Util::getClientTimezone($viewer); $timezone = $this->timezone ? $this->timezone : Util::UTC; $dt_start = new DateTime("@{$this->start_timestamp}", new DateTimeZone($timezone)); $dt_start->setTimezone(new DateTimeZone($c_timezone)); $description = elgg_echo('events_ui:repeat:' . $this->repeat_frequency); switch ($this->repeat_frequency) { case 'monthly': if ($this->repeat_monthly_by == 'day_of_month') { $description .= elgg_echo('repeat_ui:repeat_monthly_by:day_of_month:date', array($dt_start->format('jS'))); } else { $day = $dt_start->format('j'); $weeknum = ceil($day / 7); $weekday = elgg_echo('events:wd:' . $dt_start->format('D')); $suffix = str_replace($weeknum, '', date('jS', mktime(12, 0, 0, 04, $weeknum, 2015))); $description .= elgg_echo('repeat_ui:repeat_monthly_by:day_of_month:weekday', array($weeknum . $suffix, $weekday)); } break; case 'weekly': $weekdays = array_map(function ($val) { return elgg_echo('events:wd:' . $val); }, (array) $this->repeat_weekly_days); $description .= elgg_echo('repeat_ui:repeat:weekly:weekday', array(implode(', ', $weekdays))); break; } return $description; }