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 MicroAppPlugin
46 var $oldSaveNew = true;
49 * Set up our tables (event and rsvp)
54 * @return boolean hook value; true means continue processing, false means stop.
56 function onCheckSchema()
58 $schema = Schema::get();
60 $schema->ensureTable('happening', Happening::schemaDef());
61 $schema->ensureTable('rsvp', RSVP::schemaDef());
66 public function onBeforePluginCheckSchema()
68 RSVP::beforeSchemaUpdate();
75 * @param URLMapper $m path-to-action mapper
77 * @return boolean hook value; true means continue processing, false means stop.
79 public function onRouterInitialized(URLMapper $m)
81 $m->connect('main/event/new',
82 array('action' => 'newevent'));
83 $m->connect('main/event/rsvp',
84 array('action' => 'newrsvp'));
85 $m->connect('main/event/rsvp/cancel',
86 array('action' => 'cancelrsvp'));
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' => GNUSOCIAL_VERSION,
106 'author' => 'Evan Prodromou',
107 'homepage' => 'http://status.net/wiki/Plugin: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 protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
136 if (count($act->objects) !== 1) {
137 // TRANS: Exception thrown when there are too many activity objects.
138 throw new Exception(_m('Too many activity objects.'));
140 $actobj = $activity->objects[0];
142 switch ($activity->verb) {
143 case ActivityVerb::POST:
144 $actobj = $activity->objects[0];
145 if (!ActivityUtils::compareTypes($actobj->type, array(Happening::OBJECT_TYPE))) {
146 // TRANS: Exception thrown when event plugin comes across a non-event type object.
147 throw new Exception(_m('Wrong type for object.'));
149 return Happening::saveActivityObject($actobj, $stored->getProfile());
154 $happening = Happening::getKV('uri', $actobj->id);
155 if (empty($happening)) {
156 // FIXME: save the event
157 // TRANS: Exception thrown when trying to RSVP for an unknown event.
158 throw new Exception(_m('RSVP for unknown event.'));
160 $object = RSVP::saveNewFromNotice($stored, $happening, $activity->verb);
161 // Our data model expects this
162 $stored->object_type = $activity->verb;
166 common_log(LOG_ERR, 'Unknown verb for events.');
172 * Turn a Notice into an activity object
174 * @param Notice $notice
176 * @return ActivityObject
178 function activityObjectFromNotice(Notice $notice)
182 switch ($notice->object_type) {
183 case Happening::OBJECT_TYPE:
184 $happening = Happening::fromNotice($notice);
189 $rsvp = RSVP::fromNotice($notice);
190 $happening = $rsvp->getEvent();
194 if (empty($happening)) {
195 // TRANS: Exception thrown when event plugin comes across a unknown object type.
196 throw new Exception(_m('Unknown object type.'));
199 $notice = $happening->getNotice();
201 if (empty($notice)) {
202 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
203 throw new Exception(_m('Unknown event notice.'));
206 $obj = new ActivityObject();
208 $obj->id = $happening->uri;
209 $obj->type = Happening::OBJECT_TYPE;
210 $obj->title = $happening->title;
211 $obj->summary = $happening->description;
212 $obj->link = $notice->getUrl();
214 // XXX: how to get this stuff into JSON?!
216 $obj->extra[] = array('dtstart',
217 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
218 common_date_iso8601($happening->start_time));
220 $obj->extra[] = array('dtend',
221 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
222 common_date_iso8601($happening->end_time));
224 $obj->extra[] = array('location', false, $happening->location);
225 $obj->extra[] = array('url', false, $happening->url);
227 // XXX: probably need other stuff here
233 * Change the verb on RSVP notices
235 * @param Notice $notice
237 * @return ActivityObject
239 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
240 switch ($stored->object_type) {
244 $act->verb = $stored->object_type;
253 * @param HTMLOutputter $out
256 function entryForm($out)
258 return new EventForm($out);
262 * When a notice is deleted, clean up related tables.
264 * @param Notice $notice
266 function deleteRelated(Notice $notice)
268 switch ($notice->object_type) {
269 case Happening::OBJECT_TYPE:
270 common_log(LOG_DEBUG, "Deleting event from notice...");
271 $happening = Happening::fromNotice($notice);
272 $happening->delete();
277 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
278 $rsvp = RSVP::fromNotice($notice);
279 common_log(LOG_DEBUG, "to delete: $rsvp->id");
283 common_log(LOG_DEBUG, "Not deleting related, wtf...");
287 function onEndShowScripts($action)
289 $action->script($this->path('js/event.js'));
292 function onEndShowStyles($action)
294 $action->cssLink($this->path('css/event.css'));
298 function onStartAddNoticeReply($nli, $parent, $child)
300 // Filter out any poll responses
301 if (($parent->object_type == Happening::OBJECT_TYPE) &&
302 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
308 protected function showNoticeItemNotice(NoticeListItem $nli)
314 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
316 switch ($stored->object_type) {
317 case Happening::OBJECT_TYPE:
318 $this->showEvent($stored, $out, $scoped);
323 $this->showRSVP($stored, $out, $scoped);
328 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
330 $profile = $stored->getProfile();
331 $event = Happening::fromNotice($stored);
333 if (!$event instanceof Happening) {
334 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
335 $out->element('p', null, _m('Deleted.'));
339 $out->elementStart('div', 'h-event');
341 $out->elementStart('h3', 'p-summary p-name');
344 $out->element('a', array('href' => $event->getUrl()), $event->title);
345 } catch (InvalidUrlException $e) {
346 $out->text($event->title);
349 $out->elementEnd('h3');
351 $now = new DateTime();
352 $startDate = new DateTime($event->start_time);
353 $endDate = new DateTime($event->end_time);
354 $userTz = new DateTimeZone(common_timezone());
356 // Localize the time for the observer
357 $now->setTimeZone($userTz);
358 $startDate->setTimezone($userTz);
359 $endDate->setTimezone($userTz);
361 $thisYear = $now->format('Y');
362 $startYear = $startDate->format('Y');
363 $endYear = $endDate->format('Y');
365 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
367 if ($startYear != $thisYear || $endYear != $thisYear) {
368 $dateFmt .= 'Y,'; // append year if we need to think about years
371 $startDateStr = $startDate->format($dateFmt);
372 $endDateStr = $endDate->format($dateFmt);
376 $startTimeStr = $startDate->format($timeFmt);
377 $endTimeStr = $endDate->format("{$timeFmt} (T)");
379 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
381 // TRANS: Field label for event description.
382 $out->element('strong', null, _m('Time:'));
384 $out->element('time', array('class' => 'dt-start',
385 'datetime' => common_date_iso8601($event->start_time)),
386 $startDateStr . ' ' . $startTimeStr);
388 $out->element('time', array('class' => 'dt-end',
389 'datetime' => common_date_iso8601($event->end_time)),
390 $startDateStr != $endDateStr
391 ? "$endDateStr $endTimeStr"
394 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
396 if (!empty($event->location)) {
397 $out->elementStart('div', 'event-location');
398 // TRANS: Field label for event description.
399 $out->element('strong', null, _m('Location:'));
400 $out->element('span', 'p-location', $event->location);
401 $out->elementEnd('div');
404 if (!empty($event->description)) {
405 $out->elementStart('div', 'event-description');
406 // TRANS: Field label for event description.
407 $out->element('strong', null, _m('Description:'));
408 $out->element('div', 'p-description', $event->description);
409 $out->elementEnd('div');
412 $rsvps = $event->getRSVPs();
414 $out->elementStart('div', 'event-rsvps');
416 // TRANS: Field label for event description.
417 $out->element('strong', null, _m('Attending:'));
418 $out->elementStart('ul', 'attending-list');
420 foreach ($rsvps as $verb => $responses) {
421 $out->elementStart('li', 'rsvp-list');
424 $out->text(_('Yes:'));
427 $out->text(_('No:'));
430 $out->text(_('Maybe:'));
434 foreach ($responses as $response) {
435 $ids[] = $response->profile_id;
437 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
438 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
441 $out->elementEnd('li');
444 $out->elementEnd('ul');
445 $out->elementEnd('div');
447 if ($scoped instanceof Profile) {
448 $rsvp = $event->getRSVP($scoped);
451 $form = new RSVPForm($event, $out);
453 $form = new CancelRSVPForm($rsvp, $out);
458 $out->elementEnd('div');
461 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
463 $rsvp = RSVP::fromNotice($stored);
466 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
467 $out->element('p', null, _m('Deleted.'));
471 $out->elementStart('div', 'rsvp');
472 $out->raw($rsvp->asHTML());
473 $out->elementEnd('div');
476 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
478 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
479 // TRANS: Menu item in sample plugin.
481 // TRANS: Menu item title in sample plugin.
482 _m('A list of your events'), false, 'nav_timeline_events');