]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/classes/Happening.php
Removing unnecessary debug messages etc.
[quix0rs-gnu-social.git] / plugins / Event / classes / Happening.php
1 <?php
2 /**
3  * Data class for happenings
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('GNUSOCIAL')) { exit(1); }
31
32 /**
33  * Data class for happenings
34  *
35  * There's already an Event class in lib/event.php, so we couldn't
36  * call this an Event without causing a hole in space-time.
37  *
38  * "Happening" seemed good enough.
39  *
40  * @category Event
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
44  * @link     http://status.net/
45  *
46  * @see      Managed_DataObject
47  */
48 class Happening extends Managed_DataObject
49 {
50     const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
51
52     public $__table = 'happening'; // table name
53     public $id;                    // varchar(36) UUID
54     public $uri;                   // varchar(191)   not 255 because utf8mb4 takes more space
55     public $profile_id;            // int
56     public $start_time;            // datetime
57     public $end_time;              // datetime
58     public $title;                 // varchar(191)   not 255 because utf8mb4 takes more space
59     public $location;              // varchar(191)   not 255 because utf8mb4 takes more space
60     public $url;                   // varchar(191)   not 255 because utf8mb4 takes more space
61     public $description;           // text
62     public $created;               // datetime
63
64     /**
65      * The One True Thingy that must be defined and declared.
66      */
67     public static function schemaDef()
68     {
69         return array(
70             'description' => 'A real-world happening',
71             'fields' => array(
72                 'id' => array('type' => 'char',
73                               'length' => 36,
74                               'not null' => true,
75                               'description' => 'UUID'),
76                 'uri' => array('type' => 'varchar',
77                                'length' => 191,
78                                'not null' => true),
79                 'profile_id' => array('type' => 'int', 'not null' => true),
80                 'start_time' => array('type' => 'datetime', 'not null' => true),
81                 'end_time' => array('type' => 'datetime', 'not null' => true),
82                 'title' => array('type' => 'varchar',
83                                  'length' => 191,
84                                  'not null' => true),
85                 'location' => array('type' => 'varchar',
86                                     'length' => 191),
87                 'url' => array('type' => 'varchar',
88                                'length' => 191),
89                 'description' => array('type' => 'text'),
90                 'created' => array('type' => 'datetime',
91                                    'not null' => true),
92             ),
93             'primary key' => array('id'),
94             'unique keys' => array(
95                 'happening_uri_key' => array('uri'),
96             ),
97             'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id')),
98                                     'happening_uri__key' => array('notice', array('uri' => 'uri'))),
99             'indexes' => array('happening_created_idx' => array('created'),
100                                'happening_start_end_idx' => array('start_time', 'end_time')),
101         );
102     }
103
104     public static function saveActivityObject(Activity $act, Notice $stored)
105     {
106         if (count($act->objects) !== 1) {
107             // TRANS: Exception thrown when there are too many activity objects.
108             throw new Exception(_m('Too many activity objects.'));
109         }
110         $actobj = $act->objects[0];
111         if (!ActivityUtils::compareTypes($actobj->type, [Happening::OBJECT_TYPE])) {
112             // TRANS: Exception thrown when event plugin comes across a non-event type object.
113             throw new Exception(_m('Wrong type for object.'));
114         }
115
116         try {
117             $other = Happening::getByKeys(['uri' => $actobj->id]);
118             throw AlreadyFulfilledException('Happening already exists.');
119         } catch (NoResultException $e) {
120             // alright, let's save this
121         }
122
123         $dtstart = null;
124         $dtend = null;
125         $location = null;
126         $url = null;
127
128         foreach ($actobj->extra as $extra) {
129             switch ($extra[0]) {
130             case 'dtstart':
131                 $dtstart = $extra[2];
132             case 'dtend':
133                 $dtend = $extra[2];
134                 break;
135             case 'location':
136                 // location is optional
137                 $location = $extra[2];
138                 break;
139             case 'url':
140                 // url is optional
141                 $url = $extra[2];
142             }
143         }
144         if(empty($dtstart)) {
145             // TRANS: Exception thrown when has no start date
146             throw new Exception(_m('No start date for event.'));
147         }
148         if(empty($dtend)) {
149             // TRANS: Exception thrown when has no end date
150             throw new Exception(_m('No end date for event.'));
151         }
152
153         // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
154         $start_time = new DateTime($dtstart);
155         $start_time->setTimezone(new DateTimeZone('UTC'));
156         $start_time = $start_time->format('Y-m-d H:i:s');
157         $end_time = new DateTime($dtend);
158         $end_time->setTimezone(new DateTimeZone('UTC'));
159         $end_time = $end_time->format('Y-m-d H:i:s');
160
161         $ev = new Happening();
162
163         $ev->id          = UUID::gen();
164         $ev->uri         = $actobj->id;
165         $ev->profile_id  = $stored->getProfile()->getID();
166         $ev->start_time  = $start_time;
167         $ev->end_time    = $end_time;
168         $ev->title       = $actobj->title;
169         $ev->location    = $location;
170         $ev->description = $stored->getContent();
171         $ev->url         = $url;
172         $ev->created     = $stored->getCreated();
173
174         $ev->insert();
175         return $ev;
176     }
177
178     public function insert()
179     {
180         $result = parent::insert();
181         if ($result === false) {
182             common_log_db_error($this, 'INSERT', __FILE__);
183             throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
184         }
185         return $result;
186     }
187
188     /**
189      * Returns the profile's canonical url, not necessarily a uri/unique id
190      *
191      * @return string $url
192      */
193     public function getUrl()
194     {
195         if (empty($this->url) ||
196                 !filter_var($this->url, FILTER_VALIDATE_URL)) {
197             throw new InvalidUrlException($this->url);
198         }
199         return $this->url;
200     }
201
202     public function getUri()
203     {
204         return $this->uri;
205     }
206
207     public function getStored()
208     {
209         return Notice::getByKeys(array('uri'=>$this->getUri()));
210     }
211
212     static function fromStored(Notice $stored)
213     {
214         if (!ActivityUtils::compareTypes($stored->getObjectType(), [self::OBJECT_TYPE])) {
215             throw new ServerException('Notice is not of type '.self::OBJECT_TYPE);
216         }
217         return self::getByKeys(array('uri'=>$stored->getUri()));
218     }
219
220     function getRSVPs()
221     {
222         return RSVP::forEvent($this);
223     }
224
225     function getRSVP($profile)
226     {
227         return RSVP::pkeyGet(array('profile_id' => $profile->getID(),
228                                    'event_uri' => $this->getUri()));
229     }
230
231     static public function getObjectType()
232     {
233         return self::OBJECT_TYPE;
234     }
235
236     public function asActivityObject()
237     {
238         $actobj = new ActivityObject();
239         $actobj->id = $this->getUri();
240         $actobj->type = self::getObjectType();
241         $actobj->title = $this->title;
242         $actobj->summary = $this->description;
243         $actobj->extra[] = array('dtstart',
244                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
245                                 common_date_iso8601($this->start_time));
246         $actobj->extra[] = array('dtend',
247                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
248                                 common_date_iso8601($this->end_time));
249         $actobj->extra[] = array('location',
250                                 array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
251                                 $this->location);
252         try {
253             $actobj->extra[] = array('url',
254                                     array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
255                                     $this->getUrl());
256         } catch (InvalidUrlException $e) {
257             // oh well, no URL for you!
258         }
259
260         /* We don't use these ourselves, but we add them to be nice RSS/XML citizens */
261         $actobj->extra[] = array('startdate',
262                                 array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
263                                 common_date_iso8601($this->start_time));
264         $actobj->extra[] = array('enddate',
265                                 array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
266                                 common_date_iso8601($this->end_time));
267         $actobj->extra[] = array('location',
268                                 array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
269                                 $this->location);
270
271         return $actobj;
272     }
273 }