]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/newevent.php
Re-do the logic for the event micro-app's dropdowns (way trickier than it seems at...
[quix0rs-gnu-social.git] / plugins / Event / newevent.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Add a new event
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  * Add a new event
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 NeweventAction extends Action
48 {
49     protected $user        = null;
50     protected $error       = null;
51     protected $complete    = null;
52     protected $title       = null;
53     protected $location    = null;
54     protected $description = null;
55     protected $startTime   = null;
56     protected $endTime     = null;
57
58     /**
59      * Returns the title of the action
60      *
61      * @return string Action title
62      */
63     function title()
64     {
65         // TRANS: Title for new event form.
66         return _m('TITLE','New event');
67     }
68
69     /**
70      * For initializing members of the class.
71      *
72      * @param array $argarray misc. arguments
73      *
74      * @return boolean true
75      */
76     function prepare($argarray)
77     {
78         parent::prepare($argarray);
79
80         $this->user = common_current_user();
81
82         if (empty($this->user)) {
83             // TRANS: Client exception thrown when trying to post an event while not logged in.
84             throw new ClientException(_m('Must be logged in to post a event.'),
85                                       403);
86         }
87
88         if ($this->isPost()) {
89             $this->checkSessionToken();
90         }
91
92         try {
93
94             $this->title = $this->trimmed('title');
95
96             if (empty($this->title)) {
97                 // TRANS: Client exception thrown when trying to post an event without providing a title.
98                 throw new ClientException(_m('Title required.'));
99             }
100
101             $this->location    = $this->trimmed('location');
102             $this->url         = $this->trimmed('url');
103             $this->description = $this->trimmed('description');
104             $tz                = $this->trimmed('tz');
105
106             $startDate = $this->trimmed('startdate');
107
108             if (empty($startDate)) {
109                 // TRANS: Client exception thrown when trying to post an event without providing a start date.
110                 throw new ClientException(_m('Start date required.'));
111             }
112
113             $startTime = $this->trimmed('event-starttime');
114
115             if (empty($startTime)) {
116                 $startTime = '00:00';
117             }
118
119             $endDate   = $this->trimmed('enddate');
120
121             if (empty($endDate)) {
122                 // TRANS: Client exception thrown when trying to post an event without providing an end date.
123                 throw new ClientException(_m('End date required.'));
124             }
125
126             $endTime   = $this->trimmed('event-endtime');
127
128             if (empty($endTime)) {
129                 $endTime = '00:00';
130             }
131
132             $start = $startDate . ' ' . $startTime . ' ' . $tz;
133
134             common_debug("Event start: '$start'");
135
136             $end = $endDate . ' ' . $endTime . ' ' . $tz;
137
138             common_debug("Event start: '$end'");
139
140             $this->startTime = strtotime($start);
141             $this->endTime   = strtotime($end);
142
143             if ($this->startTime == 0) {
144                 // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
145                 // TRANS: %s is the data that could not be processed.
146                 throw new ClientException(sprintf(_m('Could not parse date "%s".'),
147                                             $start));
148             }
149
150             if ($this->endTime == 0) {
151                 // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
152                 // TRANS: %s is the data that could not be processed.
153                 throw new ClientException(sprintf(_m('Could not parse date "%s".'),
154                                             $end));
155             }
156         } catch (ClientException $ce) {
157             if ($this->boolean('ajax')) {
158                 $this->outputAjaxError($ce->getMessage());
159                 return false;
160             } else {
161                 $this->error = $ce->getMessage();
162                 $this->showPage();
163                 return false;
164             }
165         }
166
167         return true;
168     }
169
170     /**
171      * Handler method
172      *
173      * @param array $argarray is ignored since it's now passed in in prepare()
174      *
175      * @return void
176      */
177     function handle($argarray=null)
178     {
179         parent::handle($argarray);
180
181         if ($this->isPost()) {
182             $this->newEvent();
183         } else {
184             $this->showPage();
185         }
186
187         return;
188     }
189
190     /**
191      * Add a new event
192      *
193      * @return void
194      */
195     function newEvent()
196     {
197         try {
198             if (empty($this->title)) {
199                 // TRANS: Client exception thrown when trying to post an event without providing a title.
200                 throw new ClientException(_m('Event must have a title.'));
201             }
202
203             if (empty($this->startTime)) {
204                 // TRANS: Client exception thrown when trying to post an event without providing a start time.
205                 throw new ClientException(_m('Event must have a start time.'));
206             }
207
208             if (empty($this->endTime)) {
209                 // TRANS: Client exception thrown when trying to post an event without providing an end time.
210                 throw new ClientException(_m('Event must have an end time.'));
211             }
212
213             $options = array();
214
215             // Does the heavy-lifting for getting "To:" information
216
217             ToSelector::fillOptions($this, $options);
218
219             $profile = $this->user->getProfile();
220
221             $saved = Happening::saveNew($profile,
222                                         $this->startTime,
223                                         $this->endTime,
224                                         $this->title,
225                                         $this->location,
226                                         $this->description,
227                                         $this->url,
228                                         $options);
229
230             $event = Happening::fromNotice($saved);
231
232             RSVP::saveNew($profile, $event, RSVP::POSITIVE);
233
234         } catch (ClientException $ce) {
235             if ($this->boolean('ajax')) {
236                 $this->outputAjaxError($ce->getMessage());
237                 return;
238             } else {
239                 $this->error = $ce->getMessage();
240                 $this->showPage();
241                 return;
242             }
243         }
244
245         if ($this->boolean('ajax')) {
246             header('Content-Type: text/xml;charset=utf-8');
247             $this->xw->startDocument('1.0', 'UTF-8');
248             $this->elementStart('html');
249             $this->elementStart('head');
250             // TRANS: Page title after sending a notice.
251             $this->element('title', null, _m('Event saved'));
252             $this->elementEnd('head');
253             $this->elementStart('body');
254             $this->showNotice($saved);
255             $this->elementEnd('body');
256             $this->elementEnd('html');
257         } else {
258             common_redirect($saved->bestUrl(), 303);
259         }
260     }
261
262     // @todo factor this out into a base class
263     function outputAjaxError($msg)
264     {
265         header('Content-Type: text/xml;charset=utf-8');
266         $this->xw->startDocument('1.0', 'UTF-8');
267         $this->elementStart('html');
268         $this->elementStart('head');
269         // TRANS: Page title after an AJAX error occurs
270         $this->element('title', null, _('Ajax Error'));
271         $this->elementEnd('head');
272         $this->elementStart('body');
273         $this->element('p', array('id' => 'error'), $msg);
274         $this->elementEnd('body');
275         $this->elementEnd('html');
276         return;
277     }
278
279     /**
280      * Show the event form
281      *
282      * @return void
283      */
284     function showContent()
285     {
286         if (!empty($this->error)) {
287             $this->element('p', 'error', $this->error);
288         }
289
290         $form = new EventForm($this);
291
292         $form->show();
293
294         return;
295     }
296
297     /**
298      * Return true if read only.
299      *
300      * MAY override
301      *
302      * @param array $args other arguments
303      *
304      * @return boolean is read only action?
305      */
306     function isReadOnly($args)
307     {
308         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
309             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
310             return true;
311         } else {
312             return false;
313         }
314     }
315
316
317     /**
318      * Output a notice
319      *
320      * Used to generate the notice code for Ajax results.
321      *
322      * @param Notice $notice Notice that was saved
323      *
324      * @return void
325      */
326     function showNotice($notice)
327     {
328         $nli = new NoticeListItem($notice, $this);
329         $nli->show();
330     }
331 }