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 * Set up our tables (event and rsvp)
55 * @return boolean hook value; true means continue processing, false means stop.
57 function onCheckSchema()
59 $schema = Schema::get();
61 $schema->ensureTable('happening', Happening::schemaDef());
62 $schema->ensureTable('rsvp', RSVP::schemaDef());
70 * @param URLMapper $m path-to-action mapper
72 * @return boolean hook value; true means continue processing, false means stop.
74 public function onRouterInitialized(URLMapper $m)
76 $m->connect('main/event/new',
77 array('action' => 'newevent'));
78 $m->connect('main/event/rsvp',
79 array('action' => 'newrsvp'));
80 $m->connect('main/event/rsvp/cancel',
81 array('action' => 'cancelrsvp'));
82 $m->connect('event/:id',
83 array('action' => 'showevent'),
84 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
85 $m->connect('rsvp/:id',
86 array('action' => 'showrsvp'),
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('main/event/updatetimes',
89 array('action' => 'timelist'));
93 function onPluginVersion(array &$versions)
95 $versions[] = array('name' => 'Event',
96 'version' => GNUSOCIAL_VERSION,
97 'author' => 'Evan Prodromou',
98 'homepage' => 'http://status.net/wiki/Plugin:Event',
100 // TRANS: Plugin description.
101 _m('Event invitations and RSVPs.'));
105 function appTitle() {
106 // TRANS: Title for event application.
107 return _m('TITLE','Event');
115 return array(Happening::OBJECT_TYPE,
122 * Given a parsed ActivityStreams activity, save it into a notice
123 * and other data structures.
125 * @param Activity $activity
126 * @param Profile $actor
127 * @param array $options=array()
129 * @return Notice the resulting notice
131 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
133 if (count($activity->objects) != 1) {
134 // TRANS: Exception thrown when there are too many activity objects.
135 throw new Exception(_m('Too many activity objects.'));
138 $happeningObj = $activity->objects[0];
140 if ($happeningObj->type != Happening::OBJECT_TYPE) {
141 // TRANS: Exception thrown when event plugin comes across a non-event type object.
142 throw new Exception(_m('Wrong type for object.'));
147 switch ($activity->verb) {
148 case ActivityVerb::POST:
149 // FIXME: get startTime, endTime, location and URL
150 $notice = Happening::saveNew($actor,
153 $happeningObj->title,
155 $happeningObj->summary,
162 $happening = Happening::getKV('uri', $happeningObj->id);
163 if (empty($happening)) {
164 // FIXME: save the event
165 // TRANS: Exception thrown when trying to RSVP for an unknown event.
166 throw new Exception(_m('RSVP for unknown event.'));
168 $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
171 // TRANS: Exception thrown when event plugin comes across a undefined verb.
172 throw new Exception(_m('Unknown verb for events.'));
179 * Turn a Notice into an activity object
181 * @param Notice $notice
183 * @return ActivityObject
185 function activityObjectFromNotice(Notice $notice)
189 switch ($notice->object_type) {
190 case Happening::OBJECT_TYPE:
191 $happening = Happening::fromNotice($notice);
196 $rsvp = RSVP::fromNotice($notice);
197 $happening = $rsvp->getEvent();
201 if (empty($happening)) {
202 // TRANS: Exception thrown when event plugin comes across a unknown object type.
203 throw new Exception(_m('Unknown object type.'));
206 $notice = $happening->getNotice();
208 if (empty($notice)) {
209 // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
210 throw new Exception(_m('Unknown event notice.'));
213 $obj = new ActivityObject();
215 $obj->id = $happening->uri;
216 $obj->type = Happening::OBJECT_TYPE;
217 $obj->title = $happening->title;
218 $obj->summary = $happening->description;
219 $obj->link = $notice->getUrl();
221 // XXX: how to get this stuff into JSON?!
223 $obj->extra[] = array('dtstart',
224 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
225 common_date_iso8601($happening->start_time));
227 $obj->extra[] = array('dtend',
228 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
229 common_date_iso8601($happening->end_time));
231 // FIXME: add location
234 // XXX: probably need other stuff here
240 * Change the verb on RSVP notices
242 * @param Notice $notice
244 * @return ActivityObject
246 protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
247 switch ($stored->object_type) {
251 $act->verb = $stored->object_type;
260 * @param HTMLOutputter $out
263 function entryForm($out)
265 return new EventForm($out);
269 * When a notice is deleted, clean up related tables.
271 * @param Notice $notice
273 function deleteRelated(Notice $notice)
275 switch ($notice->object_type) {
276 case Happening::OBJECT_TYPE:
277 common_debug("Deleting event from notice...");
278 $happening = Happening::fromNotice($notice);
279 $happening->delete();
284 common_debug("Deleting rsvp from notice...");
285 $rsvp = RSVP::fromNotice($notice);
286 common_debug("to delete: $rsvp->id");
290 common_debug("Not deleting related, wtf...");
294 function onEndShowScripts(Action $action)
296 $action->script($this->path('js/event.js'));
299 function onEndShowStyles(Action $action)
301 $action->cssLink($this->path('css/event.css'));
305 function onStartAddNoticeReply($nli, $parent, $child)
307 // Filter out any poll responses
308 if (($parent->object_type == Happening::OBJECT_TYPE) &&
309 in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
315 protected function showNoticeItemNotice(NoticeListItem $nli)
321 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
323 switch ($stored->object_type) {
324 case Happening::OBJECT_TYPE:
325 $this->showEvent($stored, $out, $scoped);
330 $this->showRSVP($stored, $out, $scoped);
335 protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
337 $profile = $stored->getProfile();
338 $event = Happening::fromNotice($stored);
340 if (!$event instanceof Happening) {
341 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
342 $out->element('p', null, _m('Deleted.'));
346 $out->elementStart('div', 'h-event');
348 $out->elementStart('h3', 'p-summary p-name');
351 $out->element('a', array('href' => $event->getUrl()), $event->title);
352 } catch (InvalidUrlException $e) {
353 $out->text($event->title);
356 $out->elementEnd('h3');
358 $now = new DateTime();
359 $startDate = new DateTime($event->start_time);
360 $endDate = new DateTime($event->end_time);
361 $userTz = new DateTimeZone(common_timezone());
363 // Localize the time for the observer
364 $now->setTimeZone($userTz);
365 $startDate->setTimezone($userTz);
366 $endDate->setTimezone($userTz);
368 $thisYear = $now->format('Y');
369 $startYear = $startDate->format('Y');
370 $endYear = $endDate->format('Y');
372 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
374 if ($startYear != $thisYear || $endYear != $thisYear) {
375 $dateFmt .= 'Y,'; // append year if we need to think about years
378 $startDateStr = $startDate->format($dateFmt);
379 $endDateStr = $endDate->format($dateFmt);
383 $startTimeStr = $startDate->format($timeFmt);
384 $endTimeStr = $endDate->format("{$timeFmt} (T)");
386 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
388 // TRANS: Field label for event description.
389 $out->element('strong', null, _m('Time:'));
391 $out->element('time', array('class' => 'dt-start',
392 'datetime' => common_date_iso8601($event->start_time)),
393 $startDateStr . ' ' . $startTimeStr);
395 $out->element('time', array('class' => 'dt-end',
396 'datetime' => common_date_iso8601($event->end_time)),
397 $startDateStr != $endDateStr
398 ? "$endDateStr $endTimeStr"
401 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
403 if (!empty($event->location)) {
404 $out->elementStart('div', 'event-location');
405 // TRANS: Field label for event description.
406 $out->element('strong', null, _m('Location:'));
407 $out->element('span', 'p-location', $event->location);
408 $out->elementEnd('div');
411 if (!empty($event->description)) {
412 $out->elementStart('div', 'event-description');
413 // TRANS: Field label for event description.
414 $out->element('strong', null, _m('Description:'));
415 $out->element('div', 'p-description', $event->description);
416 $out->elementEnd('div');
419 $rsvps = $event->getRSVPs();
421 $out->elementStart('div', 'event-rsvps');
423 // TRANS: Field label for event description.
424 $out->element('strong', null, _m('Attending:'));
425 $out->elementStart('ul', 'attending-list');
427 foreach ($rsvps as $verb => $responses) {
428 $out->elementStart('li', 'rsvp-list');
431 $out->text(_('Yes:'));
434 $out->text(_('No:'));
437 $out->text(_('Maybe:'));
441 foreach ($responses as $response) {
442 $ids[] = $response->profile_id;
444 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
445 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
448 $out->elementEnd('li');
451 $out->elementEnd('ul');
452 $out->elementEnd('div');
454 if ($scoped instanceof Profile) {
455 $rsvp = $event->getRSVP($scoped);
458 $form = new RSVPForm($event, $out);
460 $form = new CancelRSVPForm($rsvp, $out);
465 $out->elementEnd('div');
468 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
470 $rsvp = RSVP::fromNotice($stored);
473 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
474 $out->element('p', null, _m('Deleted.'));
478 $out->elementStart('div', 'rsvp');
479 $out->raw($rsvp->asHTML());
480 $out->elementEnd('div');