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