]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
date picker for event form
[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('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Event plugin
39  *
40  * @category  Sample
41  * @package   StatusNet
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/
46  */
47 class EventPlugin extends MicroappPlugin
48 {
49     /**
50      * Set up our tables (event and rsvp)
51      *
52      * @see Schema
53      * @see ColumnDef
54      *
55      * @return boolean hook value; true means continue processing, false means stop.
56      */
57     function onCheckSchema()
58     {
59         $schema = Schema::get();
60
61         $schema->ensureTable('happening', Happening::schemaDef());
62         $schema->ensureTable('rsvp', RSVP::schemaDef());
63
64         return true;
65     }
66
67     /**
68      * Load related modules when needed
69      *
70      * @param string $cls Name of the class to be loaded
71      *
72      * @return boolean hook value; true means continue processing, false means stop.
73      */
74     function onAutoload($cls)
75     {
76         $dir = dirname(__FILE__);
77
78         switch ($cls)
79         {
80         case 'NeweventAction':
81         case 'NewrsvpAction':
82         case 'CancelrsvpAction':
83         case 'ShoweventAction':
84         case 'ShowrsvpAction':
85             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
86             return false;
87         case 'EventForm':
88         case 'RSVPForm':
89         case 'CancelRSVPForm':
90             include_once $dir . '/'.strtolower($cls).'.php';
91             break;
92         case 'Happening':
93         case 'RSVP':
94             include_once $dir . '/'.$cls.'.php';
95             return false;
96         default:
97             return true;
98         }
99     }
100
101     /**
102      * Map URLs to actions
103      *
104      * @param Net_URL_Mapper $m path-to-action mapper
105      *
106      * @return boolean hook value; true means continue processing, false means stop.
107      */
108
109     function onRouterInitialized($m)
110     {
111         $m->connect('main/event/new',
112                     array('action' => 'newevent'));
113         $m->connect('main/event/rsvp',
114                     array('action' => 'newrsvp'));
115         $m->connect('main/event/rsvp/cancel',
116                     array('action' => 'cancelrsvp'));
117         $m->connect('event/:id',
118                     array('action' => 'showevent'),
119                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
120         $m->connect('rsvp/:id',
121                     array('action' => 'showrsvp'),
122                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
123         return true;
124     }
125
126     function onPluginVersion(&$versions)
127     {
128         $versions[] = array('name' => 'Event',
129                             'version' => STATUSNET_VERSION,
130                             'author' => 'Evan Prodromou',
131                             'homepage' => 'http://status.net/wiki/Plugin:Event',
132                             'description' =>
133                             _m('Event invitations and RSVPs.'));
134         return true;
135     }
136
137     function appTitle() {
138         return _m('Event');
139     }
140
141     function tag() {
142         return 'event';
143     }
144
145     function types() {
146         return array(Happening::OBJECT_TYPE,
147                      RSVP::POSITIVE,
148                      RSVP::NEGATIVE,
149                      RSVP::POSSIBLE);
150     }
151
152     /**
153      * Given a parsed ActivityStreams activity, save it into a notice
154      * and other data structures.
155      *
156      * @param Activity $activity
157      * @param Profile $actor
158      * @param array $options=array()
159      *
160      * @return Notice the resulting notice
161      */
162     function saveNoticeFromActivity($activity, $actor, $options=array())
163     {
164         if (count($activity->objects) != 1) {
165             throw new Exception('Too many activity objects.');
166         }
167
168         $happeningObj = $activity->objects[0];
169
170         if ($happeningObj->type != Happening::OBJECT_TYPE) {
171             throw new Exception('Wrong type for object.');
172         }
173
174         $notice = null;
175
176         switch ($activity->verb) {
177         case ActivityVerb::POST:
178             $notice = Happening::saveNew($actor, 
179                                      $start_time, 
180                                      $end_time,
181                                      $happeningObj->title,
182                                      null,
183                                      $happeningObj->summary,
184                                      $options);
185             break;
186         case RSVP::POSITIVE:
187         case RSVP::NEGATIVE:
188         case RSVP::POSSIBLE:
189             $happening = Happening::staticGet('uri', $happeningObj->id);
190             if (empty($happening)) {
191                 // FIXME: save the event
192                 throw new Exception("RSVP for unknown event.");
193             }
194             $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
195             break;
196         default:
197             throw new Exception("Unknown verb for events");
198         }
199
200         return $notice;
201     }
202
203     /**
204      * Turn a Notice into an activity object
205      *
206      * @param Notice $notice
207      *
208      * @return ActivityObject
209      */
210
211     function activityObjectFromNotice($notice)
212     {
213         $happening = null;
214
215         switch ($notice->object_type) {
216         case Happening::OBJECT_TYPE:
217             $happening = Happening::fromNotice($notice);
218             break;
219         case RSVP::POSITIVE:
220         case RSVP::NEGATIVE:
221         case RSVP::POSSIBLE:
222             $rsvp  = RSVP::fromNotice($notice);
223             $happening = $rsvp->getEvent();
224             break;
225         }
226
227         if (empty($happening)) {
228             throw new Exception("Unknown object type.");
229         }
230
231         $notice = $happening->getNotice();
232
233         if (empty($notice)) {
234             throw new Exception("Unknown event notice.");
235         }
236
237         $obj = new ActivityObject();
238
239         $obj->id      = $happening->uri;
240         $obj->type    = Happening::OBJECT_TYPE;
241         $obj->title   = $happening->title;
242         $obj->summary = $happening->description;
243         $obj->link    = $notice->bestUrl();
244
245         // XXX: how to get this stuff into JSON?!
246
247         $obj->extra[] = array('dtstart',
248                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
249                               common_date_iso8601($happening->start_date));
250
251         $obj->extra[] = array('dtend',
252                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
253                               common_date_iso8601($happening->end_date));
254
255         // XXX: probably need other stuff here
256
257         return $obj;
258     }
259
260     /**
261      * Change the verb on RSVP notices
262      *
263      * @param Notice $notice
264      *
265      * @return ActivityObject
266      */
267
268     function onEndNoticeAsActivity($notice, &$act) {
269         switch ($notice->object_type) {
270         case RSVP::POSITIVE:
271         case RSVP::NEGATIVE:
272         case RSVP::POSSIBLE:
273             $act->verb = $notice->object_type;
274             break;
275         }
276         return true;
277     }
278
279     /**
280      * Custom HTML output for our notices
281      *
282      * @param Notice $notice
283      * @param HTMLOutputter $out
284      */
285
286     function showNotice($notice, $out)
287     {
288         switch ($notice->object_type) {
289         case Happening::OBJECT_TYPE:
290             $this->showEventNotice($notice, $out);
291             break;
292         case RSVP::POSITIVE:
293         case RSVP::NEGATIVE:
294         case RSVP::POSSIBLE:
295             $this->showRSVPNotice($notice, $out);
296             break;
297         }
298
299         // @fixme we have to start the name/avatar and open this div
300         $out->elementStart('div', array('class' => 'event-info entry-content')); // EVENT-INFO.ENTRY-CONTENT IN
301
302         $profile = $notice->getProfile();
303         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
304
305         $out->element('img',
306                       array('src' => ($avatar) ?
307                             $avatar->displayUrl() :
308                             Avatar::defaultImage(AVATAR_MINI_SIZE),
309                             'class' => 'avatar photo bookmark-avatar',
310                             'width' => AVATAR_MINI_SIZE,
311                             'height' => AVATAR_MINI_SIZE,
312                             'alt' => $profile->getBestName()));
313
314         $out->raw('&#160;'); // avoid &nbsp; for AJAX XML compatibility
315
316         $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
317         $out->element('a',
318                       array('class' => 'url',
319                             'href' => $profile->profileurl,
320                             'title' => $profile->getBestName()),
321                       $profile->nickname);
322         $out->elementEnd('span');
323     }
324
325     function showRSVPNotice($notice, $out)
326     {
327         $out->raw($notice->rendered);
328         return;
329     }
330
331     function showEventNotice($notice, $out)
332     {
333         $profile = $notice->getProfile();
334         $event   = Happening::fromNotice($notice);
335
336         assert(!empty($event));
337         assert(!empty($profile));
338
339         $out->elementStart('div', 'vevent'); // VEVENT IN
340
341         $out->elementStart('h3');  // VEVENT/H3 IN
342
343         if (!empty($event->url)) {
344             $out->element('a',
345                           array('href' => $event->url,
346                                 'class' => 'event-title entry-title summary'),
347                           $event->title);
348         } else {
349             $out->text($event->title);
350         }
351
352         $out->elementEnd('h3'); // VEVENT/H3 OUT
353
354         // FIXME: better dates
355
356         $out->elementStart('div', 'event-times'); // VEVENT/EVENT-TIMES IN
357         $out->element('abbr', array('class' => 'dtstart',
358                                     'title' => common_date_iso8601($event->start_time)),
359                       common_exact_date($event->start_time));
360         $out->text(' - ');
361         $out->element('span', array('class' => 'dtend',
362                                     'title' => common_date_iso8601($event->end_time)),
363                       common_exact_date($event->end_time));
364         $out->elementEnd('div'); // VEVENT/EVENT-TIMES OUT
365
366         if (!empty($event->description)) {
367             $out->element('div', 'description', $event->description);
368         }
369
370         if (!empty($event->location)) {
371             $out->element('div', 'location', $event->location);
372         }
373
374         $rsvps = $event->getRSVPs();
375
376         $out->element('div', 'event-rsvps',
377                       sprintf(_('Yes: %d No: %d Maybe: %d'),
378                               count($rsvps[RSVP::POSITIVE]),
379                               count($rsvps[RSVP::NEGATIVE]),
380                               count($rsvps[RSVP::POSSIBLE])));
381
382         $user = common_current_user();
383
384         if (!empty($user)) {
385             $rsvp = $event->getRSVP($user->getProfile());
386             common_log(LOG_DEBUG, "RSVP is: " . ($rsvp ? $rsvp->id : 'none'));
387
388             if (empty($rsvp)) {
389                 $form = new RSVPForm($event, $out);
390             } else {
391                 $form = new CancelRSVPForm($rsvp, $out);
392             }
393
394             $form->show();
395         }
396
397         $out->elementEnd('div'); // vevent out
398     }
399
400     /**
401      * Form for our app
402      *
403      * @param HTMLOutputter $out
404      * @return Widget
405      */
406
407     function entryForm($out)
408     {
409         return new EventForm($out);
410     }
411
412     /**
413      * When a notice is deleted, clean up related tables.
414      *
415      * @param Notice $notice
416      */
417
418     function deleteRelated($notice)
419     {
420         switch ($notice->object_type) {
421         case Happening::OBJECT_TYPE:
422             common_log(LOG_DEBUG, "Deleting event from notice...");
423             $happening = Happening::fromNotice($notice);
424             $happening->delete();
425             break;
426         case RSVP::POSITIVE:
427         case RSVP::NEGATIVE:
428         case RSVP::POSSIBLE:
429             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
430             $rsvp = RSVP::fromNotice($notice);
431             common_log(LOG_DEBUG, "to delete: $rsvp->id");
432             $rsvp->delete();
433             break;
434         default:
435             common_log(LOG_DEBUG, "Not deleting related, wtf...");
436         }
437     }
438
439     function onEndShowScripts($action)
440     {
441         $action->inlineScript('$(document).ready(function() { $("#startdate").datepicker(); $("#enddate").datepicker(); });');
442     }
443 }