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('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class EventPlugin extends MicroAppPlugin
50 var $oldSaveNew = true;
53 * Set up our tables (event and rsvp)
58 * @return boolean hook value; true means continue processing, false means stop.
60 function onCheckSchema()
62 $schema = Schema::get();
64 $schema->ensureTable('happening', Happening::schemaDef());
65 $schema->ensureTable('rsvp', RSVP::schemaDef());
73 * @param URLMapper $m path-to-action mapper
75 * @return boolean hook value; true means continue processing, false means stop.
77 public function onRouterInitialized(URLMapper $m)
79 $m->connect('main/event/new',
80 array('action' => 'newevent'));
81 $m->connect('main/event/rsvp',
82 array('action' => 'newrsvp'));
83 $m->connect('main/event/rsvp/cancel',
84 array('action' => 'cancelrsvp'));
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'));
96 function onPluginVersion(array &$versions)
98 $versions[] = array('name' => 'Event',
99 'version' => GNUSOCIAL_VERSION,
100 'author' => 'Evan Prodromou',
101 'homepage' => 'http://status.net/wiki/Plugin:Event',
103 // TRANS: Plugin description.
104 _m('Event invitations and RSVPs.'));
108 function appTitle() {
109 // TRANS: Title for event application.
110 return _m('TITLE','Event');
118 return array(Happening::OBJECT_TYPE,
125 * Given a parsed ActivityStreams activity, save it into a notice
126 * and other data structures.
128 * @param Activity $activity
129 * @param Profile $actor
130 * @param array $options=array()
132 * @return Notice the resulting notice
134 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
136 if (count($activity->objects) != 1) {
137 // TRANS: Exception thrown when there are too many activity objects.
138 throw new Exception(_m('Too many activity objects.'));
141 $happeningObj = $activity->objects[0];
143 if ($happeningObj->type != Happening::OBJECT_TYPE) {
144 // TRANS: Exception thrown when event plugin comes across a non-event type object.
145 throw new Exception(_m('Wrong type for object.'));
148 $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
149 if($dtstart->length == 0) {
150 // TRANS: Exception thrown when has no start date
151 throw new Exception(_m('No start date for event.'));
154 $dtend = $happeningObj->element->getElementsByTagName('dtend');
155 if($dtend->length == 0) {
156 // TRANS: Exception thrown when has no end date
157 throw new Exception(_m('No end date for event.'));
160 // location is optional
162 $location_object = $happeningObj->element->getElementsByTagName('location');
163 if($location_object->length > 0) {
164 $location = $location_object->item(0)->nodeValue;
169 $url_object = $happeningObj->element->getElementsByTagName('url');
170 if($url_object->length > 0) {
171 $url = $url_object->item(0)->nodeValue;
176 switch ($activity->verb) {
177 case ActivityVerb::POST:
178 // FIXME: get startTime, endTime, location and URL
179 $notice = Happening::saveNew($actor,
180 $dtstart->item(0)->nodeValue,
181 $dtend->item(0)->nodeValue,
182 $happeningObj->title,
184 $happeningObj->summary,
191 $happening = Happening::getKV('uri', $happeningObj->id);
192 if (empty($happening)) {
193 // FIXME: save the event
194 // TRANS: Exception thrown when trying to RSVP for an unknown event.
195 throw new Exception(_m('RSVP for unknown event.'));
197 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
200 // TRANS: Exception thrown when event plugin comes across a undefined verb.
201 throw new Exception(_m('Unknown verb for events.'));
208 * Turn a Notice into an activity object
210 * @param Notice $notice
212 * @return ActivityObject
214 function activityObjectFromNotice(Notice $notice)
218 switch ($notice->object_type) {
219 case Happening::OBJECT_TYPE:
220 $happening = Happening::fromNotice($notice);
225 $rsvp = RSVP::fromNotice($notice);
226 $happening = $rsvp->getEvent();
230 if (empty($happening)) {
231 // TRANS: Exception thrown when event plugin comes across a unknown object type.
232 throw new Exception(_m('Unknown object type.'));
235 $notice = $happening->getNotice();
237 if (empty($notice)) {
238 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
239 throw new Exception(_m('Unknown event notice.'));
242 $obj = new ActivityObject();
244 $obj->id = $happening->uri;
245 $obj->type = Happening::OBJECT_TYPE;
246 $obj->title = $happening->title;
247 $obj->summary = $happening->description;
248 $obj->link = $notice->getUrl();
250 // XXX: how to get this stuff into JSON?!
252 $obj->extra[] = array('dtstart',
253 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
254 common_date_iso8601($happening->start_time));
256 $obj->extra[] = array('dtend',
257 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
258 common_date_iso8601($happening->end_time));
260 $obj->extra[] = array('location', false, $happening->location);
261 $obj->extra[] = array('url', false, $happening->url);
263 // XXX: probably need other stuff here
269 * Change the verb on RSVP notices
271 * @param Notice $notice
273 * @return ActivityObject
275 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
276 switch ($stored->object_type) {
280 $act->verb = $stored->object_type;
289 * @param HTMLOutputter $out
292 function entryForm($out)
294 return new EventForm($out);
298 * When a notice is deleted, clean up related tables.
300 * @param Notice $notice
302 function deleteRelated(Notice $notice)
304 switch ($notice->object_type) {
305 case Happening::OBJECT_TYPE:
306 common_log(LOG_DEBUG, "Deleting event from notice...");
307 $happening = Happening::fromNotice($notice);
308 $happening->delete();
313 common_log(LOG_DEBUG, "Deleting rsvp from notice...");
314 $rsvp = RSVP::fromNotice($notice);
315 common_log(LOG_DEBUG, "to delete: $rsvp->id");
319 common_log(LOG_DEBUG, "Not deleting related, wtf...");
323 function onEndShowScripts($action)
325 $action->script($this->path('js/event.js'));
328 function onEndShowStyles($action)
330 $action->cssLink($this->path('css/event.css'));
334 function onStartAddNoticeReply($nli, $parent, $child)
336 // Filter out any poll responses
337 if (($parent->object_type == Happening::OBJECT_TYPE) &&
338 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
344 protected function showNoticeItemNotice(NoticeListItem $nli)
350 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
352 switch ($stored->object_type) {
353 case Happening::OBJECT_TYPE:
354 $this->showEvent($stored, $out, $scoped);
359 $this->showRSVP($stored, $out, $scoped);
364 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
366 $profile = $stored->getProfile();
367 $event = Happening::fromNotice($stored);
369 if (!$event instanceof Happening) {
370 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
371 $out->element('p', null, _m('Deleted.'));
375 $out->elementStart('div', 'h-event');
377 $out->elementStart('h3', 'p-summary p-name');
380 $out->element('a', array('href' => $event->getUrl()), $event->title);
381 } catch (InvalidUrlException $e) {
382 $out->text($event->title);
385 $out->elementEnd('h3');
387 $now = new DateTime();
388 $startDate = new DateTime($event->start_time);
389 $endDate = new DateTime($event->end_time);
390 $userTz = new DateTimeZone(common_timezone());
392 // Localize the time for the observer
393 $now->setTimeZone($userTz);
394 $startDate->setTimezone($userTz);
395 $endDate->setTimezone($userTz);
397 $thisYear = $now->format('Y');
398 $startYear = $startDate->format('Y');
399 $endYear = $endDate->format('Y');
401 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
403 if ($startYear != $thisYear || $endYear != $thisYear) {
404 $dateFmt .= 'Y,'; // append year if we need to think about years
407 $startDateStr = $startDate->format($dateFmt);
408 $endDateStr = $endDate->format($dateFmt);
412 $startTimeStr = $startDate->format($timeFmt);
413 $endTimeStr = $endDate->format("{$timeFmt} (T)");
415 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
417 // TRANS: Field label for event description.
418 $out->element('strong', null, _m('Time:'));
420 $out->element('time', array('class' => 'dt-start',
421 'datetime' => common_date_iso8601($event->start_time)),
422 $startDateStr . ' ' . $startTimeStr);
424 $out->element('time', array('class' => 'dt-end',
425 'datetime' => common_date_iso8601($event->end_time)),
426 $startDateStr != $endDateStr
427 ? "$endDateStr $endTimeStr"
430 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
432 if (!empty($event->location)) {
433 $out->elementStart('div', 'event-location');
434 // TRANS: Field label for event description.
435 $out->element('strong', null, _m('Location:'));
436 $out->element('span', 'p-location', $event->location);
437 $out->elementEnd('div');
440 if (!empty($event->description)) {
441 $out->elementStart('div', 'event-description');
442 // TRANS: Field label for event description.
443 $out->element('strong', null, _m('Description:'));
444 $out->element('div', 'p-description', $event->description);
445 $out->elementEnd('div');
448 $rsvps = $event->getRSVPs();
450 $out->elementStart('div', 'event-rsvps');
452 // TRANS: Field label for event description.
453 $out->element('strong', null, _m('Attending:'));
454 $out->elementStart('ul', 'attending-list');
456 foreach ($rsvps as $verb => $responses) {
457 $out->elementStart('li', 'rsvp-list');
460 $out->text(_('Yes:'));
463 $out->text(_('No:'));
466 $out->text(_('Maybe:'));
470 foreach ($responses as $response) {
471 $ids[] = $response->profile_id;
473 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
474 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
477 $out->elementEnd('li');
480 $out->elementEnd('ul');
481 $out->elementEnd('div');
483 if ($scoped instanceof Profile) {
484 $rsvp = $event->getRSVP($scoped);
487 $form = new RSVPForm($event, $out);
489 $form = new CancelRSVPForm($rsvp, $out);
494 $out->elementEnd('div');
497 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
499 $rsvp = RSVP::fromNotice($stored);
502 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
503 $out->element('p', null, _m('Deleted.'));
507 $out->elementStart('div', 'rsvp');
508 $out->raw($rsvp->asHTML());
509 $out->elementEnd('div');