]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/newevent.php
Localisation updates from http://translatewiki.net.
[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         $this->title       = $this->trimmed('title');
93
94         if (empty($this->title)) {
95             // TRANS: Client exception thrown when trying to post an event without providing a title.
96             throw new ClientException(_m('Title required.'));
97         }
98
99         $this->location    = $this->trimmed('location');
100         $this->url         = $this->trimmed('url');
101         $this->description = $this->trimmed('description');
102
103         $startDate = $this->trimmed('startdate');
104
105         if (empty($startDate)) {
106             // TRANS: Client exception thrown when trying to post an event without providing a start date.
107             throw new ClientException(_m('Start date required.'));
108         }
109
110         $startTime = $this->trimmed('starttime');
111
112         if (empty($startTime)) {
113             $startTime = '00:00';
114         }
115
116         $endDate   = $this->trimmed('enddate');
117
118         if (empty($endDate)) {
119             // TRANS: Client exception thrown when trying to post an event without providing an end date.
120             throw new ClientException(_m('End date required.'));
121         }
122
123         $endTime   = $this->trimmed('endtime');
124
125         if (empty($endTime)) {
126             $endTime = '00:00';
127         }
128
129         $start = $startDate . ' ' . $startTime;
130
131         common_debug("Event start: '$start'");
132
133         $end = $endDate . ' ' . $endTime;
134
135         common_debug("Event start: '$end'");
136
137         $this->startTime = strtotime($start);
138         $this->endTime   = strtotime($end);
139
140         if ($this->startTime == 0) {
141             // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
142             // TRANS: %s is the data that could not be processed.
143             throw new Exception(sprintf(_m('Could not parse date "%s".'),
144                                         $start));
145         }
146
147
148         if ($this->endTime == 0) {
149             // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
150             // TRANS: %s is the data that could not be processed.
151             throw new Exception(sprintf(_m('Could not parse date "%s".'),
152                                         $end));
153         }
154
155         return true;
156     }
157
158     /**
159      * Handler method
160      *
161      * @param array $argarray is ignored since it's now passed in in prepare()
162      *
163      * @return void
164      */
165     function handle($argarray=null)
166     {
167         parent::handle($argarray);
168
169         if ($this->isPost()) {
170             $this->newEvent();
171         } else {
172             $this->showPage();
173         }
174
175         return;
176     }
177
178     /**
179      * Add a new event
180      *
181      * @return void
182      */
183     function newEvent()
184     {
185         try {
186             if (empty($this->title)) {
187                 // TRANS: Client exception thrown when trying to post an event without providing a title.
188                 throw new ClientException(_m('Event must have a title.'));
189             }
190
191             if (empty($this->startTime)) {
192                 // TRANS: Client exception thrown when trying to post an event without providing a start time.
193                 throw new ClientException(_m('Event must have a start time.'));
194             }
195
196             if (empty($this->endTime)) {
197                 // TRANS: Client exception thrown when trying to post an event without providing an end time.
198                 throw new ClientException(_m('Event must have an end time.'));
199             }
200
201             $options = array();
202
203             // Does the heavy-lifting for getting "To:" information
204
205             ToSelector::fillOptions($this, $options);
206
207             $profile = $this->user->getProfile();
208
209             $saved = Happening::saveNew($profile,
210                                         $this->startTime,
211                                         $this->endTime,
212                                         $this->title,
213                                         $this->location,
214                                         $this->description,
215                                         $this->url,
216                                         $options);
217
218             $event = Happening::fromNotice($saved);
219
220             RSVP::saveNew($profile, $event, RSVP::POSITIVE);
221
222         } catch (ClientException $ce) {
223             $this->error = $ce->getMessage();
224             $this->showPage();
225             return;
226         }
227
228         if ($this->boolean('ajax')) {
229             header('Content-Type: text/xml;charset=utf-8');
230             $this->xw->startDocument('1.0', 'UTF-8');
231             $this->elementStart('html');
232             $this->elementStart('head');
233             // TRANS: Page title after sending a notice.
234             $this->element('title', null, _m('Event saved'));
235             $this->elementEnd('head');
236             $this->elementStart('body');
237             $this->showNotice($saved);
238             $this->elementEnd('body');
239             $this->elementEnd('html');
240         } else {
241             common_redirect($saved->bestUrl(), 303);
242         }
243     }
244
245     /**
246      * Show the event form
247      *
248      * @return void
249      */
250     function showContent()
251     {
252         if (!empty($this->error)) {
253             $this->element('p', 'error', $this->error);
254         }
255
256         $form = new EventForm($this);
257
258         $form->show();
259
260         return;
261     }
262
263     /**
264      * Return true if read only.
265      *
266      * MAY override
267      *
268      * @param array $args other arguments
269      *
270      * @return boolean is read only action?
271      */
272     function isReadOnly($args)
273     {
274         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
275             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
276             return true;
277         } else {
278             return false;
279         }
280     }
281
282
283     /**
284      * Output a notice
285      *
286      * Used to generate the notice code for Ajax results.
287      *
288      * @param Notice $notice Notice that was saved
289      *
290      * @return void
291      */
292     function showNotice($notice)
293     {
294         $nli = new NoticeListItem($notice, $this);
295         $nli->show();
296     }
297 }