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