]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/EventPlugin.php
a1dffea3b96b6befbe0207c08447ffafba66560b
[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      * Map URLs to actions
69      *
70      * @param Net_URL_Mapper $m path-to-action mapper
71      *
72      * @return boolean hook value; true means continue processing, false means stop.
73      */
74     function onRouterInitialized($m)
75     {
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'));
90         return true;
91     }
92
93     function onPluginVersion(array &$versions)
94     {
95         $versions[] = array('name' => 'Event',
96                             'version' => GNUSOCIAL_VERSION,
97                             'author' => 'Evan Prodromou',
98                             'homepage' => 'http://status.net/wiki/Plugin:Event',
99                             'description' =>
100                             // TRANS: Plugin description.
101                             _m('Event invitations and RSVPs.'));
102         return true;
103     }
104
105     function appTitle() {
106         // TRANS: Title for event application.
107         return _m('TITLE','Event');
108     }
109
110     function tag() {
111         return 'event';
112     }
113
114     function types() {
115         return array(Happening::OBJECT_TYPE,
116                      RSVP::POSITIVE,
117                      RSVP::NEGATIVE,
118                      RSVP::POSSIBLE);
119     }
120
121     /**
122      * Given a parsed ActivityStreams activity, save it into a notice
123      * and other data structures.
124      *
125      * @param Activity $activity
126      * @param Profile $actor
127      * @param array $options=array()
128      *
129      * @return Notice the resulting notice
130      */
131     function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
132     {
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.'));
136         }
137
138         $happeningObj = $activity->objects[0];
139
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.'));
143         }
144
145         $notice = null;
146
147         switch ($activity->verb) {
148         case ActivityVerb::POST:
149                 // FIXME: get startTime, endTime, location and URL
150             $notice = Happening::saveNew($actor,
151                                          $start_time,
152                                          $end_time,
153                                          $happeningObj->title,
154                                          null,
155                                          $happeningObj->summary,
156                                          null,
157                                          $options);
158             break;
159         case RSVP::POSITIVE:
160         case RSVP::NEGATIVE:
161         case RSVP::POSSIBLE:
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.'));
167             }
168             $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
169             break;
170         default:
171             // TRANS: Exception thrown when event plugin comes across a undefined verb.
172             throw new Exception(_m('Unknown verb for events.'));
173         }
174
175         return $notice;
176     }
177
178     /**
179      * Turn a Notice into an activity object
180      *
181      * @param Notice $notice
182      *
183      * @return ActivityObject
184      */
185     function activityObjectFromNotice(Notice $notice)
186     {
187         $happening = null;
188
189         switch ($notice->object_type) {
190         case Happening::OBJECT_TYPE:
191             $happening = Happening::fromNotice($notice);
192             break;
193         case RSVP::POSITIVE:
194         case RSVP::NEGATIVE:
195         case RSVP::POSSIBLE:
196             $rsvp  = RSVP::fromNotice($notice);
197             $happening = $rsvp->getEvent();
198             break;
199         }
200
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.'));
204         }
205
206         $notice = $happening->getNotice();
207
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.'));
211         }
212
213         $obj = new ActivityObject();
214
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();
220
221         // XXX: how to get this stuff into JSON?!
222
223         $obj->extra[] = array('dtstart',
224                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
225                               common_date_iso8601($happening->start_time));
226
227         $obj->extra[] = array('dtend',
228                               array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
229                               common_date_iso8601($happening->end_time));
230
231                 // FIXME: add location
232                 // FIXME: add URL
233                 
234         // XXX: probably need other stuff here
235
236         return $obj;
237     }
238
239     /**
240      * Change the verb on RSVP notices
241      *
242      * @param Notice $notice
243      *
244      * @return ActivityObject
245      */
246     protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
247         switch ($stored->object_type) {
248         case RSVP::POSITIVE:
249         case RSVP::NEGATIVE:
250         case RSVP::POSSIBLE:
251             $act->verb = $stored->object_type;
252             break;
253         }
254         return true;
255     }
256
257     function adaptNoticeListItem($nli)
258     {
259         $notice = $nli->notice;
260
261         switch ($notice->object_type) {
262         case Happening::OBJECT_TYPE:
263             return new EventListItem($nli);
264             break;
265         case RSVP::POSITIVE:
266         case RSVP::NEGATIVE:
267         case RSVP::POSSIBLE:
268             return new RSVPListItem($nli);
269             break;
270         }
271         return null;
272     }
273
274
275     /**
276      * Form for our app
277      *
278      * @param HTMLOutputter $out
279      * @return Widget
280      */
281     function entryForm($out)
282     {
283         return new EventForm($out);
284     }
285
286     /**
287      * When a notice is deleted, clean up related tables.
288      *
289      * @param Notice $notice
290      */
291     function deleteRelated(Notice $notice)
292     {
293         switch ($notice->object_type) {
294         case Happening::OBJECT_TYPE:
295             common_log(LOG_DEBUG, "Deleting event from notice...");
296             $happening = Happening::fromNotice($notice);
297             $happening->delete();
298             break;
299         case RSVP::POSITIVE:
300         case RSVP::NEGATIVE:
301         case RSVP::POSSIBLE:
302             common_log(LOG_DEBUG, "Deleting rsvp from notice...");
303             $rsvp = RSVP::fromNotice($notice);
304             common_log(LOG_DEBUG, "to delete: $rsvp->id");
305             $rsvp->delete();
306             break;
307         default:
308             common_log(LOG_DEBUG, "Not deleting related, wtf...");
309         }
310     }
311
312     function onEndShowScripts($action)
313     {
314         $action->script($this->path('js/event.js'));
315     }
316
317     function onEndShowStyles($action)
318     {
319         $action->cssLink($this->path('css/event.css'));
320         return true;
321     }
322
323     function onStartAddNoticeReply($nli, $parent, $child)
324     {
325         // Filter out any poll responses
326         if (($parent->object_type == Happening::OBJECT_TYPE) &&
327             in_array($child->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE))) {
328             return false;
329         }
330         return true;
331     }
332 }