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
46 * Set up our tables (event and rsvp)
51 * @return boolean hook value; true means continue processing, false means stop.
53 function onCheckSchema()
55 $schema = Schema::get();
57 $schema->ensureTable('happening', Happening::schemaDef());
58 $schema->ensureTable('rsvp', RSVP::schemaDef());
63 public function onBeforePluginCheckSchema()
65 RSVP::beforeSchemaUpdate();
72 * @param URLMapper $m path-to-action mapper
74 * @return boolean hook value; true means continue processing, false means stop.
76 public function onRouterInitialized(URLMapper $m)
78 $m->connect('main/event/new',
79 array('action' => 'newevent'));
80 $m->connect('main/event/rsvp',
81 array('action' => 'rsvp'));
82 $m->connect('main/event/rsvp/:rsvp', // this will probably change to include event notice id
83 array('action' => 'rsvp'),
84 array('rsvp' => '[a-z]+'));
85 $m->connect('event/:id',
86 array('action' => 'showevent'),
87 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
88 $m->connect('rsvp/:id',
89 array('action' => 'showrsvp'),
90 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
91 $m->connect('main/event/updatetimes',
92 array('action' => 'timelist'));
94 $m->connect(':nickname/events',
95 array('action' => 'events'),
96 array('nickname' => Nickname::DISPLAY_FMT));
100 function onPluginVersion(array &$versions)
102 $versions[] = array('name' => 'Event',
103 'version' => GNUSOCIAL_VERSION,
104 'author' => 'Evan Prodromou',
105 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Event',
107 // TRANS: Plugin description.
108 _m('Event invitations and RSVPs.'));
112 function appTitle() {
113 // TRANS: Title for event application.
114 return _m('TITLE','Event');
122 return array(Happening::OBJECT_TYPE);
126 return array(ActivityVerb::POST,
132 function isMyNotice(Notice $notice) {
133 if (!empty($notice->object_type)) {
134 return parent::isMyNotice($notice);
136 return $this->isMyVerb($notice->verb);
139 public function newFormAction() {
140 // such as 'newbookmark' or 'newevent' route
141 return 'new'.$this->tag();
144 function onStartShowEntryForms(&$tabs)
146 $tabs[$this->tag()] = array('title' => $this->appTitle(),
147 'href' => common_local_url($this->newFormAction()),
152 function onStartMakeEntryForm($tag, $out, &$form)
154 if ($tag == $this->tag()) {
155 $form = $this->entryForm($out);
162 protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
167 protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
172 protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
174 throw new ServerException('Event does not handle doActionPost yet', 501);
177 protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
179 return new RSVPForm(Happening::fromStored($target), $action);
182 protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
185 case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
186 return Happening::saveActivityObject($act, $stored);
189 case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
190 return RSVP::saveActivityObject($act, $stored);
196 function activityObjectFromNotice(Notice $stored)
201 case $stored->isVerb([ActivityVerb::POST]) && $stored->isObjectType([Happening::OBJECT_TYPE]):
202 $obj = Happening::fromStored($stored)->asActivityObject();
204 // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
205 case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
206 case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
207 $obj = RSVP::fromStored($stored)->asActivityObject();
210 // TRANS: Exception thrown when event plugin comes across a unknown object type.
211 throw new Exception(_m('Unknown object type.'));
220 * @param HTMLOutputter $out
223 function entryForm($out)
225 return new EventForm($out);
228 function deleteRelated(Notice $stored)
231 case $stored->isObjectType([Happening::OBJECT_TYPE]):
232 common_log(LOG_DEBUG, "Deleting event from notice...");
234 $happening = Happening::fromStored($stored);
235 $happening->delete();
236 } catch (NoResultException $e) {
240 // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
241 case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
242 case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
243 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
245 $rsvp = RSVP::fromStored($stored);
247 } catch (NoResultException $e) {
254 function onEndShowScripts($action)
256 $action->script($this->path('js/event.js'));
259 function onEndShowStyles($action)
261 $action->cssLink($this->path('css/event.css'));
265 function onStartAddNoticeReply($nli, $parent, $child)
267 if (($parent->object_type == Happening::OBJECT_TYPE) &&
268 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
274 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
277 case ActivityUtils::compareTypes($stored->verb, array(ActivityVerb::POST)) &&
278 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
279 $this->showEvent($stored, $out, $scoped);
281 case ActivityUtils::compareVerbs($stored->verb, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
282 $this->showRSVP($stored, $out, $scoped);
285 throw new ServerException('This is not an Event notice');
290 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
292 $profile = $stored->getProfile();
293 $event = Happening::fromStored($stored);
295 $out->elementStart('div', 'h-event');
297 $out->elementStart('h3', 'p-summary p-name');
300 $out->element('a', array('href' => $event->getUrl()), $event->title);
301 } catch (InvalidUrlException $e) {
302 $out->text($event->title);
305 $out->elementEnd('h3');
307 $now = new DateTime();
308 $startDate = new DateTime($event->start_time);
309 $endDate = new DateTime($event->end_time);
310 $userTz = new DateTimeZone(common_timezone());
312 // Localize the time for the observer
313 $now->setTimeZone($userTz);
314 $startDate->setTimezone($userTz);
315 $endDate->setTimezone($userTz);
317 $thisYear = $now->format('Y');
318 $startYear = $startDate->format('Y');
319 $endYear = $endDate->format('Y');
321 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
323 if ($startYear != $thisYear || $endYear != $thisYear) {
324 $dateFmt .= 'Y,'; // append year if we need to think about years
327 $startDateStr = $startDate->format($dateFmt);
328 $endDateStr = $endDate->format($dateFmt);
332 $startTimeStr = $startDate->format($timeFmt);
333 $endTimeStr = $endDate->format("{$timeFmt} (T)");
335 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
337 // TRANS: Field label for event description.
338 $out->element('strong', null, _m('Time:'));
340 $out->element('time', array('class' => 'dt-start',
341 'datetime' => common_date_iso8601($event->start_time)),
342 $startDateStr . ' ' . $startTimeStr);
344 $out->element('time', array('class' => 'dt-end',
345 'datetime' => common_date_iso8601($event->end_time)),
346 $startDateStr != $endDateStr
347 ? "$endDateStr $endTimeStr"
350 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
352 if (!empty($event->location)) {
353 $out->elementStart('div', 'event-location');
354 // TRANS: Field label for event description.
355 $out->element('strong', null, _m('Location:'));
356 $out->element('span', 'p-location', $event->location);
357 $out->elementEnd('div');
360 if (!empty($event->description)) {
361 $out->elementStart('div', 'event-description');
362 // TRANS: Field label for event description.
363 $out->element('strong', null, _m('Description:'));
364 $out->element('div', 'p-description', $event->description);
365 $out->elementEnd('div');
368 $rsvps = $event->getRSVPs();
370 $out->elementStart('div', 'event-rsvps');
372 // TRANS: Field label for event description.
373 $out->element('strong', null, _m('Attending:'));
374 $out->elementStart('ul', 'attending-list');
376 foreach ($rsvps as $verb => $responses) {
377 $out->elementStart('li', 'rsvp-list');
380 $out->text(_('Yes:'));
383 $out->text(_('No:'));
386 $out->text(_('Maybe:'));
390 foreach ($responses as $response) {
391 $ids[] = $response->profile_id;
393 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
394 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
397 $out->elementEnd('li');
400 $out->elementEnd('ul');
401 $out->elementEnd('div');
403 if ($scoped instanceof Profile) {
404 $form = new RSVPForm($out, array('event'=>$event, 'scoped'=>$scoped));
407 $out->elementEnd('div');
410 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
413 $rsvp = RSVP::fromStored($stored);
414 } catch (NoResultException $e) {
415 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
416 $out->element('p', null, _m('Deleted.'));
420 $out->elementStart('div', 'rsvp');
421 $out->raw($rsvp->asHTML());
422 $out->elementEnd('div');
425 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
427 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
428 // TRANS: Menu item in sample plugin.
430 // TRANS: Menu item title in sample plugin.
431 _m('A list of your events'), false, 'nav_timeline_events');