3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Microapp plugin for event invitations and RSVPs
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('GNUSOCIAL')) { exit(1); }
38 * @author Evan Prodromou <evan@status.net>
39 * @copyright 2011 StatusNet, Inc.
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41 * @link http://status.net/
43 class EventPlugin extends ActivityVerbHandlerPlugin
45 const PLUGIN_VERSION = '2.0.0';
48 * Set up our tables (event and rsvp)
53 * @return boolean hook value; true means continue processing, false means stop.
55 function onCheckSchema()
57 $schema = Schema::get();
59 $schema->ensureTable('happening', Happening::schemaDef());
60 $schema->ensureTable('rsvp', RSVP::schemaDef());
65 public function onBeforePluginCheckSchema()
67 RSVP::beforeSchemaUpdate();
74 * @param URLMapper $m path-to-action mapper
76 * @return boolean hook value; true means continue processing, false means stop.
78 public function onRouterInitialized(URLMapper $m)
80 $m->connect('main/event/new',
81 array('action' => 'newevent'));
82 $m->connect('main/event/rsvp',
83 array('action' => 'rsvp'));
84 $m->connect('main/event/rsvp/:rsvp', // this will probably change to include event notice id
85 array('action' => 'rsvp'),
86 array('rsvp' => '[a-z]+'));
87 $m->connect('event/:id',
88 array('action' => 'showevent'),
89 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
90 $m->connect('rsvp/:id',
91 array('action' => 'showrsvp'),
92 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
93 $m->connect('main/event/updatetimes',
94 array('action' => 'timelist'));
96 $m->connect(':nickname/events',
97 array('action' => 'events'),
98 array('nickname' => Nickname::DISPLAY_FMT));
102 function onPluginVersion(array &$versions)
104 $versions[] = array('name' => 'Event',
105 'version' => self::PLUGIN_VERSION,
106 'author' => 'Evan Prodromou',
107 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Event',
109 // TRANS: Plugin description.
110 _m('Event invitations and RSVPs.'));
114 function appTitle() {
115 // TRANS: Title for event application.
116 return _m('TITLE','Event');
124 return array(Happening::OBJECT_TYPE);
128 return array(ActivityVerb::POST,
134 function isMyNotice(Notice $notice) {
135 if (!empty($notice->object_type)) {
136 return parent::isMyNotice($notice);
138 return $this->isMyVerb($notice->verb);
141 public function newFormAction() {
142 // such as 'newbookmark' or 'newevent' route
143 return 'new'.$this->tag();
146 function onStartShowEntryForms(&$tabs)
148 $tabs[$this->tag()] = array('title' => $this->appTitle(),
149 'href' => common_local_url($this->newFormAction()),
154 function onStartMakeEntryForm($tag, $out, &$form)
156 if ($tag == $this->tag()) {
157 $form = $this->entryForm($out);
164 protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
169 protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
174 protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
176 throw new ServerException('Event does not handle doActionPost yet', 501);
179 protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
181 return new RSVPForm(Happening::fromStored($target), $action);
184 protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
187 case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
188 return Happening::saveActivityObject($act, $stored);
191 case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
192 return RSVP::saveActivityObject($act, $stored);
198 function activityObjectFromNotice(Notice $stored)
203 case $stored->isVerb([ActivityVerb::POST]) && $stored->isObjectType([Happening::OBJECT_TYPE]):
204 $obj = Happening::fromStored($stored)->asActivityObject();
206 // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
207 case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
208 case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
209 $obj = RSVP::fromStored($stored)->asActivityObject();
212 // TRANS: Exception thrown when event plugin comes across a unknown object type.
213 throw new Exception(_m('Unknown object type.'));
222 * @param HTMLOutputter $out
225 function entryForm($out)
227 return new EventForm($out);
230 function deleteRelated(Notice $stored)
233 case $stored->isObjectType([Happening::OBJECT_TYPE]):
234 common_log(LOG_DEBUG, "Deleting event from notice...");
236 $happening = Happening::fromStored($stored);
237 $happening->delete();
238 } catch (NoResultException $e) {
242 // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
243 case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
244 case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
245 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
247 $rsvp = RSVP::fromStored($stored);
249 } catch (NoResultException $e) {
256 function onEndShowScripts($action)
258 $action->script($this->path('js/event.js'));
261 function onEndShowStyles($action)
263 $action->cssLink($this->path('css/event.css'));
267 function onStartAddNoticeReply($nli, $parent, $child)
269 if (($parent->object_type == Happening::OBJECT_TYPE) &&
270 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
276 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
279 case ActivityUtils::compareTypes($stored->verb, array(ActivityVerb::POST)) &&
280 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
281 $this->showEvent($stored, $out, $scoped);
283 case ActivityUtils::compareVerbs($stored->verb, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
284 $this->showRSVP($stored, $out, $scoped);
287 throw new ServerException('This is not an Event notice');
292 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
294 $profile = $stored->getProfile();
295 $event = Happening::fromStored($stored);
297 $out->elementStart('div', 'h-event');
299 $out->elementStart('h3', 'p-summary p-name');
302 $out->element('a', array('href' => $event->getUrl()), $event->title);
303 } catch (InvalidUrlException $e) {
304 $out->text($event->title);
307 $out->elementEnd('h3');
309 $now = new DateTime();
310 $startDate = new DateTime($event->start_time);
311 $endDate = new DateTime($event->end_time);
312 $userTz = new DateTimeZone(common_timezone());
314 // Localize the time for the observer
315 $now->setTimeZone($userTz);
316 $startDate->setTimezone($userTz);
317 $endDate->setTimezone($userTz);
319 $thisYear = $now->format('Y');
320 $startYear = $startDate->format('Y');
321 $endYear = $endDate->format('Y');
323 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
325 if ($startYear != $thisYear || $endYear != $thisYear) {
326 $dateFmt .= 'Y,'; // append year if we need to think about years
329 $startDateStr = $startDate->format($dateFmt);
330 $endDateStr = $endDate->format($dateFmt);
334 $startTimeStr = $startDate->format($timeFmt);
335 $endTimeStr = $endDate->format("{$timeFmt} (T)");
337 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
339 // TRANS: Field label for event description.
340 $out->element('strong', null, _m('Time:'));
342 $out->element('time', array('class' => 'dt-start',
343 'datetime' => common_date_iso8601($event->start_time)),
344 $startDateStr . ' ' . $startTimeStr);
346 $out->element('time', array('class' => 'dt-end',
347 'datetime' => common_date_iso8601($event->end_time)),
348 $startDateStr != $endDateStr
349 ? "$endDateStr $endTimeStr"
352 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
354 if (!empty($event->location)) {
355 $out->elementStart('div', 'event-location');
356 // TRANS: Field label for event description.
357 $out->element('strong', null, _m('Location:'));
358 $out->element('span', 'p-location', $event->location);
359 $out->elementEnd('div');
362 if (!empty($event->description)) {
363 $out->elementStart('div', 'event-description');
364 // TRANS: Field label for event description.
365 $out->element('strong', null, _m('Description:'));
366 $out->element('div', 'p-description', $event->description);
367 $out->elementEnd('div');
370 $rsvps = $event->getRSVPs();
372 $out->elementStart('div', 'event-rsvps');
374 // TRANS: Field label for event description.
375 $out->element('strong', null, _m('Attending:'));
376 $out->elementStart('ul', 'attending-list');
378 foreach ($rsvps as $verb => $responses) {
379 $out->elementStart('li', 'rsvp-list');
382 $out->text(_('Yes:'));
385 $out->text(_('No:'));
388 $out->text(_('Maybe:'));
392 foreach ($responses as $response) {
393 $ids[] = $response->profile_id;
395 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
396 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
399 $out->elementEnd('li');
402 $out->elementEnd('ul');
403 $out->elementEnd('div');
405 if ($scoped instanceof Profile) {
406 $form = new RSVPForm($out, array('event'=>$event, 'scoped'=>$scoped));
409 $out->elementEnd('div');
412 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
415 $rsvp = RSVP::fromStored($stored)->asHTML();
416 } catch (NoResultException $e) {
417 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
418 $out->element('p', null, _m('Deleted.'));
422 $out->elementStart('div', 'rsvp');
424 $out->elementEnd('div');
427 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
429 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
430 // TRANS: Menu item in sample plugin.
432 // TRANS: Menu item title in sample plugin.
433 _m('A list of your events'), false, 'nav_timeline_events');