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' => 'newrsvp'));
82 $m->connect('main/event/rsvp/cancel',
83 array('action' => 'cancelrsvp'));
84 $m->connect('event/:id',
85 array('action' => 'showevent'),
86 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
87 $m->connect('rsvp/:id',
88 array('action' => 'showrsvp'),
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('main/event/updatetimes',
91 array('action' => 'timelist'));
93 $m->connect(':nickname/events',
94 array('action' => 'events'),
95 array('nickname' => Nickname::DISPLAY_FMT));
99 function onPluginVersion(array &$versions)
101 $versions[] = array('name' => 'Event',
102 'version' => GNUSOCIAL_VERSION,
103 'author' => 'Evan Prodromou',
104 'homepage' => 'http://status.net/wiki/Plugin:Event',
106 // TRANS: Plugin description.
107 _m('Event invitations and RSVPs.'));
111 function appTitle() {
112 // TRANS: Title for event application.
113 return _m('TITLE','Event');
121 return array(Happening::OBJECT_TYPE);
125 return array(ActivityVerb::POST,
131 public function newFormAction() {
132 // such as 'newbookmark' or 'newevent' route
133 return 'new'.$this->tag();
136 function onStartShowEntryForms(&$tabs)
138 $tabs[$this->tag()] = array('title' => $this->appTitle(),
139 'href' => common_local_url($this->newFormAction()),
144 function onStartMakeEntryForm($tag, $out, &$form)
146 if ($tag == $this->tag()) {
147 $form = $this->entryForm($out);
154 protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
159 protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
164 protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
166 throw new ServerException('Event does not handle doActionPost yet', 501);
169 protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
171 return new RSVPForm(Happening::fromStored($target), $action);
174 protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
177 case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
178 return Happening::saveActivityObject($act, $stored);
181 case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
182 return RSVP::saveActivityObject($act, $stored);
188 function activityObjectFromNotice(Notice $stored)
193 case ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST)) &&
194 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
195 $happening = Happening::fromStored($stored);
197 // FIXME: Why are these object_type??
198 case ActivityUtils::compareTypes($stored->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
199 $rsvp = RSVP::fromNotice($stored);
200 $happening = $rsvp->getEvent();
203 // TRANS: Exception thrown when event plugin comes across a unknown object type.
204 throw new Exception(_m('Unknown object type.'));
207 $obj = new ActivityObject();
209 $obj->id = $happening->getUri();
210 $obj->type = Happening::OBJECT_TYPE;
211 $obj->title = $happening->title;
212 $obj->summary = $happening->description;
213 $obj->link = $happening->getStored()->getUrl();
215 $obj->extra[] = array('dtstart',
216 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
217 common_date_iso8601($happening->start_time));
218 $obj->extra[] = array('dtend',
219 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
220 common_date_iso8601($happening->end_time));
221 $obj->extra[] = array('location', false, $happening->location);
222 $obj->extra[] = array('url', false, $happening->url);
228 * Change the verb on RSVP notices
230 * @param Notice $notice
232 * @return ActivityObject
234 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
236 // FIXME: Why are these object_type??
237 case ActivityUtils::compareTypes($stored->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
238 $act->verb = $stored->object_type;
247 * @param HTMLOutputter $out
250 function entryForm($out)
252 return new EventForm($out);
256 * When a notice is deleted, clean up related tables.
258 * @param Notice $notice
260 function deleteRelated(Notice $notice)
262 switch ($notice->object_type) {
263 case Happening::OBJECT_TYPE:
264 common_log(LOG_DEBUG, "Deleting event from notice...");
266 $happening = Happening::fromStored($notice);
267 $happening->delete();
268 } catch (NoResultException $e) {
275 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
276 $rsvp = RSVP::fromNotice($notice);
277 common_log(LOG_DEBUG, "to delete: $rsvp->id");
281 common_log(LOG_DEBUG, "Not deleting related, wtf...");
285 function onEndShowScripts($action)
287 $action->script($this->path('js/event.js'));
290 function onEndShowStyles($action)
292 $action->cssLink($this->path('css/event.css'));
296 function onStartAddNoticeReply($nli, $parent, $child)
298 if (($parent->object_type == Happening::OBJECT_TYPE) &&
299 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
305 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
308 case ActivityUtils::compareTypes($stored->verb, array(ActivityVerb::POST)) &&
309 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
310 $this->showEvent($stored, $out, $scoped);
312 case ActivityUtils::compareVerbs($stored->verb, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
313 $this->showRSVP($stored, $out, $scoped);
316 throw new ServerException('This is not an Event notice');
321 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
323 common_debug('shownotice'.$stored->getID());
324 $profile = $stored->getProfile();
325 $event = Happening::fromStored($stored);
327 $out->elementStart('div', 'h-event');
329 $out->elementStart('h3', 'p-summary p-name');
332 $out->element('a', array('href' => $event->getUrl()), $event->title);
333 } catch (InvalidUrlException $e) {
334 $out->text($event->title);
337 $out->elementEnd('h3');
339 $now = new DateTime();
340 $startDate = new DateTime($event->start_time);
341 $endDate = new DateTime($event->end_time);
342 $userTz = new DateTimeZone(common_timezone());
344 // Localize the time for the observer
345 $now->setTimeZone($userTz);
346 $startDate->setTimezone($userTz);
347 $endDate->setTimezone($userTz);
349 $thisYear = $now->format('Y');
350 $startYear = $startDate->format('Y');
351 $endYear = $endDate->format('Y');
353 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
355 if ($startYear != $thisYear || $endYear != $thisYear) {
356 $dateFmt .= 'Y,'; // append year if we need to think about years
359 $startDateStr = $startDate->format($dateFmt);
360 $endDateStr = $endDate->format($dateFmt);
364 $startTimeStr = $startDate->format($timeFmt);
365 $endTimeStr = $endDate->format("{$timeFmt} (T)");
367 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
369 // TRANS: Field label for event description.
370 $out->element('strong', null, _m('Time:'));
372 $out->element('time', array('class' => 'dt-start',
373 'datetime' => common_date_iso8601($event->start_time)),
374 $startDateStr . ' ' . $startTimeStr);
376 $out->element('time', array('class' => 'dt-end',
377 'datetime' => common_date_iso8601($event->end_time)),
378 $startDateStr != $endDateStr
379 ? "$endDateStr $endTimeStr"
382 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
384 if (!empty($event->location)) {
385 $out->elementStart('div', 'event-location');
386 // TRANS: Field label for event description.
387 $out->element('strong', null, _m('Location:'));
388 $out->element('span', 'p-location', $event->location);
389 $out->elementEnd('div');
392 if (!empty($event->description)) {
393 $out->elementStart('div', 'event-description');
394 // TRANS: Field label for event description.
395 $out->element('strong', null, _m('Description:'));
396 $out->element('div', 'p-description', $event->description);
397 $out->elementEnd('div');
400 $rsvps = $event->getRSVPs();
402 $out->elementStart('div', 'event-rsvps');
404 // TRANS: Field label for event description.
405 $out->element('strong', null, _m('Attending:'));
406 $out->elementStart('ul', 'attending-list');
408 foreach ($rsvps as $verb => $responses) {
409 $out->elementStart('li', 'rsvp-list');
412 $out->text(_('Yes:'));
415 $out->text(_('No:'));
418 $out->text(_('Maybe:'));
422 foreach ($responses as $response) {
423 $ids[] = $response->profile_id;
425 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
426 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
429 $out->elementEnd('li');
432 $out->elementEnd('ul');
433 $out->elementEnd('div');
435 if ($scoped instanceof Profile) {
436 $rsvp = $event->getRSVP($scoped);
439 $form = new RSVPForm($out, array('event'=>$event));
441 $form = new CancelRSVPForm($out, array('rsvp'=>$rsvp));
446 $out->elementEnd('div');
449 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
451 $rsvp = RSVP::fromNotice($stored);
454 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
455 $out->element('p', null, _m('Deleted.'));
459 $out->elementStart('div', 'rsvp');
460 $out->raw($rsvp->asHTML());
461 $out->elementEnd('div');
464 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
466 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
467 // TRANS: Menu item in sample plugin.
469 // TRANS: Menu item title in sample plugin.
470 _m('A list of your events'), false, 'nav_timeline_events');