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' => 'http://status.net/wiki/Plugin: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 common_debug('shownotice'.$stored->getID());
293 $profile = $stored->getProfile();
294 $event = Happening::fromStored($stored);
296 $out->elementStart('div', 'h-event');
298 $out->elementStart('h3', 'p-summary p-name');
301 $out->element('a', array('href' => $event->getUrl()), $event->title);
302 } catch (InvalidUrlException $e) {
303 $out->text($event->title);
306 $out->elementEnd('h3');
308 $now = new DateTime();
309 $startDate = new DateTime($event->start_time);
310 $endDate = new DateTime($event->end_time);
311 $userTz = new DateTimeZone(common_timezone());
313 // Localize the time for the observer
314 $now->setTimeZone($userTz);
315 $startDate->setTimezone($userTz);
316 $endDate->setTimezone($userTz);
318 $thisYear = $now->format('Y');
319 $startYear = $startDate->format('Y');
320 $endYear = $endDate->format('Y');
322 $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
324 if ($startYear != $thisYear || $endYear != $thisYear) {
325 $dateFmt .= 'Y,'; // append year if we need to think about years
328 $startDateStr = $startDate->format($dateFmt);
329 $endDateStr = $endDate->format($dateFmt);
333 $startTimeStr = $startDate->format($timeFmt);
334 $endTimeStr = $endDate->format("{$timeFmt} (T)");
336 $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
338 // TRANS: Field label for event description.
339 $out->element('strong', null, _m('Time:'));
341 $out->element('time', array('class' => 'dt-start',
342 'datetime' => common_date_iso8601($event->start_time)),
343 $startDateStr . ' ' . $startTimeStr);
345 $out->element('time', array('class' => 'dt-end',
346 'datetime' => common_date_iso8601($event->end_time)),
347 $startDateStr != $endDateStr
348 ? "$endDateStr $endTimeStr"
351 $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
353 if (!empty($event->location)) {
354 $out->elementStart('div', 'event-location');
355 // TRANS: Field label for event description.
356 $out->element('strong', null, _m('Location:'));
357 $out->element('span', 'p-location', $event->location);
358 $out->elementEnd('div');
361 if (!empty($event->description)) {
362 $out->elementStart('div', 'event-description');
363 // TRANS: Field label for event description.
364 $out->element('strong', null, _m('Description:'));
365 $out->element('div', 'p-description', $event->description);
366 $out->elementEnd('div');
369 $rsvps = $event->getRSVPs();
371 $out->elementStart('div', 'event-rsvps');
373 // TRANS: Field label for event description.
374 $out->element('strong', null, _m('Attending:'));
375 $out->elementStart('ul', 'attending-list');
377 foreach ($rsvps as $verb => $responses) {
378 $out->elementStart('li', 'rsvp-list');
381 $out->text(_('Yes:'));
384 $out->text(_('No:'));
387 $out->text(_('Maybe:'));
391 foreach ($responses as $response) {
392 $ids[] = $response->profile_id;
394 $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
395 $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
398 $out->elementEnd('li');
401 $out->elementEnd('ul');
402 $out->elementEnd('div');
404 if ($scoped instanceof Profile) {
405 $form = new RSVPForm($out, array('event'=>$event, 'scoped'=>$scoped));
408 $out->elementEnd('div');
411 protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
414 $rsvp = RSVP::fromStored($stored);
415 } catch (NoResultException $e) {
416 // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
417 $out->element('p', null, _m('Deleted.'));
421 $out->elementStart('div', 'rsvp');
422 $out->raw($rsvp->asHTML());
423 $out->elementEnd('div');
426 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
428 $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
429 // TRANS: Menu item in sample plugin.
431 // TRANS: Menu item title in sample plugin.
432 _m('A list of your events'), false, 'nav_timeline_events');