]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
[TRANSLATION] Update license and copyright notice in translation files
[quix0rs-gnu-social.git] / plugins / Event / EventPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Microapp plugin for event invitations and RSVPs
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Event
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Event plugin
35  *
36  * @category  Event
37  * @package   StatusNet
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/
42  */
43 class EventPlugin extends ActivityVerbHandlerPlugin
44 {
45     const PLUGIN_VERSION = '2.0.0';
46
47     /**
48      * Set up our tables (event and rsvp)
49      *
50      * @see Schema
51      * @see ColumnDef
52      *
53      * @return boolean hook value; true means continue processing, false means stop.
54      */
55     function onCheckSchema()
56     {
57         $schema = Schema::get();
58
59         $schema->ensureTable('happening', Happening::schemaDef());
60         $schema->ensureTable('rsvp', RSVP::schemaDef());
61
62         return true;
63     }
64
65     public function onBeforePluginCheckSchema()
66     {
67         RSVP::beforeSchemaUpdate();
68         return true;
69     }
70
71     /**
72      * Map URLs to actions
73      *
74      * @param URLMapper $m path-to-action mapper
75      *
76      * @return boolean hook value; true means continue processing, false means stop.
77      */
78     public function onRouterInitialized(URLMapper $m)
79     {
80         $m->connect('main/event/new',
81                     array('action' => 'newevent'));
82         $m->connect('main/event/rsvp',
83                     array('action' => 'rsvp'));
84         $m->connect('main/event/rsvp/:rsvp',    // this will probably change to include event notice id
85                     array('action' => 'rsvp'),
86                     array('rsvp'   => '[a-z]+'));
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'));
95
96         $m->connect(':nickname/events',
97                     array('action' => 'events'),
98                     array('nickname' => Nickname::DISPLAY_FMT));
99         return true;
100     }
101
102     function onPluginVersion(array &$versions)
103     {
104         $versions[] = array('name' => 'Event',
105                             'version' => self::PLUGIN_VERSION,
106                             'author' => 'Evan Prodromou',
107                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Event',
108                             'description' =>
109                             // TRANS: Plugin description.
110                             _m('Event invitations and RSVPs.'));
111         return true;
112     }
113
114     function appTitle() {
115         // TRANS: Title for event application.
116         return _m('TITLE','Event');
117     }
118
119     function tag() {
120         return 'event';
121     }
122
123     function types() {
124         return array(Happening::OBJECT_TYPE);
125     }
126
127     function verbs() {
128         return array(ActivityVerb::POST,
129                      RSVP::POSITIVE,
130                      RSVP::NEGATIVE,
131                      RSVP::POSSIBLE);
132     }
133
134     function isMyNotice(Notice $notice) {
135         if (!empty($notice->object_type)) {
136             return parent::isMyNotice($notice);
137         }
138         return $this->isMyVerb($notice->verb);
139     }
140
141     public function newFormAction() {
142         // such as 'newbookmark' or 'newevent' route
143         return 'new'.$this->tag();
144     }
145
146     function onStartShowEntryForms(&$tabs)
147     {
148         $tabs[$this->tag()] = array('title' => $this->appTitle(),
149                                     'href'  => common_local_url($this->newFormAction()),
150                                    );
151         return true;
152     }
153
154     function onStartMakeEntryForm($tag, $out, &$form)
155     {
156         if ($tag == $this->tag()) {
157             $form = $this->entryForm($out);
158             return false;
159         }
160
161         return true;
162     }
163
164     protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
165     {
166         return $verb;
167     }
168
169     protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
170     {
171         return true;
172     }
173
174     protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
175     {
176         throw new ServerException('Event does not handle doActionPost yet', 501);
177     }
178
179     protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
180     {
181         return new RSVPForm(Happening::fromStored($target), $action);
182     }
183
184     protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
185     {
186         switch (true) {
187         case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
188             return Happening::saveActivityObject($act, $stored);
189             break;
190
191         case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
192             return RSVP::saveActivityObject($act, $stored);
193             break;
194         }
195         return null;
196     }
197
198     function activityObjectFromNotice(Notice $stored)
199     {
200         $happening = null;
201
202         switch (true) {
203         case $stored->isVerb([ActivityVerb::POST]) && $stored->isObjectType([Happening::OBJECT_TYPE]):
204             $obj = Happening::fromStored($stored)->asActivityObject();
205             break;
206         // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
207         case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
208         case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
209             $obj = RSVP::fromStored($stored)->asActivityObject();
210             break;
211         default:
212             // TRANS: Exception thrown when event plugin comes across a unknown object type.
213             throw new Exception(_m('Unknown object type.'));
214         }
215
216         return $obj;
217     }
218
219     /**
220      * Form for our app
221      *
222      * @param HTMLOutputter $out
223      * @return Widget
224      */
225     function entryForm($out)
226     {
227         return new EventForm($out);
228     }
229
230     function deleteRelated(Notice $stored)
231     {
232         switch (true) {
233         case $stored->isObjectType([Happening::OBJECT_TYPE]):
234             common_log(LOG_DEBUG, "Deleting event from notice...");
235             try {
236                 $happening = Happening::fromStored($stored);
237                 $happening->delete();
238             } catch (NoResultException $e) {
239                 // already gone
240             }
241             break;
242         // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
243         case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
244         case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
245             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
246             try {
247                 $rsvp = RSVP::fromStored($stored);
248                 $rsvp->delete();
249             } catch (NoResultException $e) {
250                 // already gone
251             }
252             break;
253         }
254     }
255
256     function onEndShowScripts($action)
257     {
258         $action->script($this->path('js/event.js'));
259     }
260
261     function onEndShowStyles($action)
262     {
263         $action->cssLink($this->path('css/event.css'));
264         return true;
265     }
266
267     function onStartAddNoticeReply($nli, $parent, $child)
268     {
269         if (($parent->object_type == Happening::OBJECT_TYPE) &&
270             in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
271             return false;
272         }
273         return true;
274     }
275
276     protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
277     {
278         switch (true) {
279         case ActivityUtils::compareTypes($stored->verb, array(ActivityVerb::POST)) &&
280                 ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)):
281             $this->showEvent($stored, $out, $scoped);
282             break;
283         case ActivityUtils::compareVerbs($stored->verb, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
284             $this->showRSVP($stored, $out, $scoped);
285             break;
286         default:
287             throw new ServerException('This is not an Event notice');
288         }
289         return true;
290     }
291
292     protected function showEvent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
293     {
294         $profile = $stored->getProfile();
295         $event   = Happening::fromStored($stored);
296
297         $out->elementStart('div', 'h-event');
298
299         $out->elementStart('h3', 'p-summary p-name');
300
301         try {
302             $out->element('a', array('href' => $event->getUrl()), $event->title);
303         } catch (InvalidUrlException $e) {
304             $out->text($event->title);
305         }
306
307         $out->elementEnd('h3');
308
309         $now       = new DateTime();
310         $startDate = new DateTime($event->start_time);
311         $endDate   = new DateTime($event->end_time);
312         $userTz    = new DateTimeZone(common_timezone());
313
314         // Localize the time for the observer
315         $now->setTimeZone($userTz);
316         $startDate->setTimezone($userTz);
317         $endDate->setTimezone($userTz);
318
319         $thisYear  = $now->format('Y');
320         $startYear = $startDate->format('Y');
321         $endYear   = $endDate->format('Y');
322
323         $dateFmt = 'D, F j, '; // e.g.: Mon, Aug 31
324
325         if ($startYear != $thisYear || $endYear != $thisYear) {
326             $dateFmt .= 'Y,'; // append year if we need to think about years
327         }
328
329         $startDateStr = $startDate->format($dateFmt);
330         $endDateStr = $endDate->format($dateFmt);
331
332         $timeFmt = 'g:ia';
333
334         $startTimeStr = $startDate->format($timeFmt);
335         $endTimeStr = $endDate->format("{$timeFmt} (T)");
336
337         $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
338
339         // TRANS: Field label for event description.
340         $out->element('strong', null, _m('Time:'));
341
342         $out->element('time', array('class' => 'dt-start',
343                                     'datetime' => common_date_iso8601($event->start_time)),
344                       $startDateStr . ' ' . $startTimeStr);
345         $out->text(' – ');
346         $out->element('time', array('class' => 'dt-end',
347                                     'datetime' => common_date_iso8601($event->end_time)),
348                       $startDateStr != $endDateStr
349                                     ? "$endDateStr $endTimeStr"
350                                     :  $endTimeStr);
351
352         $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
353
354         if (!empty($event->location)) {
355             $out->elementStart('div', 'event-location');
356             // TRANS: Field label for event description.
357             $out->element('strong', null, _m('Location:'));
358             $out->element('span', 'p-location', $event->location);
359             $out->elementEnd('div');
360         }
361
362         if (!empty($event->description)) {
363             $out->elementStart('div', 'event-description');
364             // TRANS: Field label for event description.
365             $out->element('strong', null, _m('Description:'));
366             $out->element('div', 'p-description', $event->description);
367             $out->elementEnd('div');
368         }
369
370         $rsvps = $event->getRSVPs();
371
372         $out->elementStart('div', 'event-rsvps');
373
374         // TRANS: Field label for event description.
375         $out->element('strong', null, _m('Attending:'));
376         $out->elementStart('ul', 'attending-list');
377
378         foreach ($rsvps as $verb => $responses) {
379             $out->elementStart('li', 'rsvp-list');
380             switch ($verb) {
381             case RSVP::POSITIVE:
382                 $out->text(_('Yes:'));
383                 break;
384             case RSVP::NEGATIVE:
385                 $out->text(_('No:'));
386                 break;
387             case RSVP::POSSIBLE:
388                 $out->text(_('Maybe:'));
389                 break;
390             }
391             $ids = array();
392             foreach ($responses as $response) {
393                 $ids[] = $response->profile_id;
394             }
395             $ids = array_slice($ids, 0, ProfileMiniList::MAX_PROFILES + 1);
396             $minilist = new ProfileMiniList(Profile::multiGet('id', $ids), $out);
397             $minilist->show();
398
399             $out->elementEnd('li');
400         }
401
402         $out->elementEnd('ul');
403         $out->elementEnd('div');
404
405         if ($scoped instanceof Profile) {
406             $form = new RSVPForm($out, array('event'=>$event, 'scoped'=>$scoped));
407             $form->show();
408         }
409         $out->elementEnd('div');
410     }
411
412     protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
413     {
414         try {
415             $rsvp = RSVP::fromStored($stored)->asHTML();
416         } catch (NoResultException $e) {
417             // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
418             $out->element('p', null, _m('Deleted.'));
419             return;
420         }
421
422         $out->elementStart('div', 'rsvp');
423         $out->raw($rsvp);
424         $out->elementEnd('div');
425     }
426
427     function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
428     {
429         $menu->menuItem(common_local_url('events', array('nickname' => $target->getNickname())),
430                           // TRANS: Menu item in sample plugin.
431                           _m('Happenings'),
432                           // TRANS: Menu item title in sample plugin.
433                           _m('A list of your events'), false, 'nav_timeline_events');
434         return true;
435     }
436 }