]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
7676a25ac01a2a29d0abfe33d7e6318eb918a39e
[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  Event
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         case 'TimelistAction':
86             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
87             return false;
88         case 'EventListItem':
89         case 'RSVPListItem':
90         case 'EventForm':
91         case 'RSVPForm':
92         case 'CancelRSVPForm':
93         case 'EventTimeList':
94             include_once $dir . '/'.strtolower($cls).'.php';
95             break;
96         case 'Happening':
97         case 'RSVP':
98             include_once $dir . '/'.$cls.'.php';
99             return false;
100         default:
101             return true;
102         }
103     }
104
105     /**
106      * Map URLs to actions
107      *
108      * @param Net_URL_Mapper $m path-to-action mapper
109      *
110      * @return boolean hook value; true means continue processing, false means stop.
111      */
112     function onRouterInitialized($m)
113     {
114         $m->connect('main/event/new',
115                     array('action' => 'newevent'));
116         $m->connect('main/event/rsvp',
117                     array('action' => 'newrsvp'));
118         $m->connect('main/event/rsvp/cancel',
119                     array('action' => 'cancelrsvp'));
120         $m->connect('event/:id',
121                     array('action' => 'showevent'),
122                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
123         $m->connect('rsvp/:id',
124                     array('action' => 'showrsvp'),
125                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
126         $m->connect('main/event/updatetimes',
127                     array('action' => 'timelist'));
128         return true;
129     }
130
131     function onPluginVersion(&$versions)
132     {
133         $versions[] = array('name' => 'Event',
134                             'version' => STATUSNET_VERSION,
135                             'author' => 'Evan Prodromou',
136                             'homepage' => 'http://status.net/wiki/Plugin:Event',
137                             'description' =>
138                             // TRANS: Plugin description.
139                             _m('Event invitations and RSVPs.'));
140         return true;
141     }
142
143     function appTitle() {
144         // TRANS: Title for event application.
145         return _m('TITLE','Event');
146     }
147
148     function tag() {
149         return 'event';
150     }
151
152     function types() {
153         return array(Happening::OBJECT_TYPE,
154                      RSVP::POSITIVE,
155                      RSVP::NEGATIVE,
156                      RSVP::POSSIBLE);
157     }
158
159     /**
160      * Given a parsed ActivityStreams activity, save it into a notice
161      * and other data structures.
162      *
163      * @param Activity $activity
164      * @param Profile $actor
165      * @param array $options=array()
166      *
167      * @return Notice the resulting notice
168      */
169     function saveNoticeFromActivity($activity, $actor, $options=array())
170     {
171         if (count($activity->objects) != 1) {
172             // TRANS: Exception thrown when there are too many activity objects.
173             throw new Exception(_m('Too many activity objects.'));
174         }
175
176         $happeningObj = $activity->objects[0];
177
178         if ($happeningObj->type != Happening::OBJECT_TYPE) {
179             // TRANS: Exception thrown when event plugin comes across a non-event type object.
180             throw new Exception(_m('Wrong type for object.'));
181         }
182
183         $notice = null;
184
185         switch ($activity->verb) {
186         case ActivityVerb::POST:
187                 // FIXME: get startTime, endTime, location and URL
188             $notice = Happening::saveNew($actor,
189                                          $start_time,
190                                          $end_time,
191                                          $happeningObj->title,
192                                          null,
193                                          $happeningObj->summary,
194                                          null,
195                                          $options);
196             break;
197         case RSVP::POSITIVE:
198         case RSVP::NEGATIVE:
199         case RSVP::POSSIBLE:
200             $happening = Happening::getKV('uri', $happeningObj->id);
201             if (empty($happening)) {
202                 // FIXME: save the event
203                 // TRANS: Exception thrown when trying to RSVP for an unknown event.
204                 throw new Exception(_m('RSVP for unknown event.'));
205             }
206             $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
207             break;
208         default:
209             // TRANS: Exception thrown when event plugin comes across a undefined verb.
210             throw new Exception(_m('Unknown verb for events.'));
211         }
212
213         return $notice;
214     }
215
216     /**
217      * Turn a Notice into an activity object
218      *
219      * @param Notice $notice
220      *
221      * @return ActivityObject
222      */
223     function activityObjectFromNotice($notice)
224     {
225         $happening = null;
226
227         switch ($notice->object_type) {
228         case Happening::OBJECT_TYPE:
229             $happening = Happening::fromNotice($notice);
230             break;
231         case RSVP::POSITIVE:
232         case RSVP::NEGATIVE:
233         case RSVP::POSSIBLE:
234             $rsvp  = RSVP::fromNotice($notice);
235             $happening = $rsvp->getEvent();
236             break;
237         }
238
239         if (empty($happening)) {
240             // TRANS: Exception thrown when event plugin comes across a unknown object type.
241             throw new Exception(_m('Unknown object type.'));
242         }
243
244         $notice = $happening->getNotice();
245
246         if (empty($notice)) {
247             // TRANS: Exception thrown when referring to a notice that is not an event an in event context.
248             throw new Exception(_m('Unknown event notice.'));
249         }
250
251         $obj = new ActivityObject();
252
253         $obj->id      = $happening->uri;
254         $obj->type    = Happening::OBJECT_TYPE;
255         $obj->title   = $happening->title;
256         $obj->summary = $happening->description;
257         $obj->link    = $notice->bestUrl();
258
259         // XXX: how to get this stuff into JSON?!
260
261         $obj->extra[] = array('dtstart',
262                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
263                               common_date_iso8601($happening->start_time));
264
265         $obj->extra[] = array('dtend',
266                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
267                               common_date_iso8601($happening->end_time));
268
269                 // FIXME: add location
270                 // FIXME: add URL
271                 
272         // XXX: probably need other stuff here
273
274         return $obj;
275     }
276
277     /**
278      * Change the verb on RSVP notices
279      *
280      * @param Notice $notice
281      *
282      * @return ActivityObject
283      */
284     function onEndNoticeAsActivity($notice, &$act) {
285         switch ($notice->object_type) {
286         case RSVP::POSITIVE:
287         case RSVP::NEGATIVE:
288         case RSVP::POSSIBLE:
289             $act->verb = $notice->object_type;
290             break;
291         }
292         return true;
293     }
294
295     function adaptNoticeListItem($nli)
296     {
297         $notice = $nli->notice;
298
299         switch ($notice->object_type) {
300         case Happening::OBJECT_TYPE:
301             return new EventListItem($nli);
302             break;
303         case RSVP::POSITIVE:
304         case RSVP::NEGATIVE:
305         case RSVP::POSSIBLE:
306             return new RSVPListItem($nli);
307             break;
308         }
309         return null;
310     }
311
312
313     /**
314      * Form for our app
315      *
316      * @param HTMLOutputter $out
317      * @return Widget
318      */
319     function entryForm($out)
320     {
321         return new EventForm($out);
322     }
323
324     /**
325      * When a notice is deleted, clean up related tables.
326      *
327      * @param Notice $notice
328      */
329     function deleteRelated($notice)
330     {
331         switch ($notice->object_type) {
332         case Happening::OBJECT_TYPE:
333             common_log(LOG_DEBUG, "Deleting event from notice...");
334             $happening = Happening::fromNotice($notice);
335             $happening->delete();
336             break;
337         case RSVP::POSITIVE:
338         case RSVP::NEGATIVE:
339         case RSVP::POSSIBLE:
340             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
341             $rsvp = RSVP::fromNotice($notice);
342             common_log(LOG_DEBUG, "to delete: $rsvp->id");
343             $rsvp->delete();
344             break;
345         default:
346             common_log(LOG_DEBUG, "Not deleting related, wtf...");
347         }
348     }
349
350     function onEndShowScripts($action)
351     {
352         $action->script($this->path('event.js'));
353     }
354
355     function onEndShowStyles($action)
356     {
357         $action->cssLink($this->path('event.css'));
358         return true;
359     }
360
361     function onStartAddNoticeReply($nli, $parent, $child)
362     {
363         // Filter out any poll responses
364         if (($parent->object_type == Happening::OBJECT_TYPE) &&
365             in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
366             return false;
367         }
368         return true;
369     }
370 }