3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
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/
47 class NeweventAction extends Action
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;
59 * Returns the title of the action
61 * @return string Action title
65 // TRANS: Title for new event form.
66 return _m('TITLE','New event');
70 * For initializing members of the class.
72 * @param array $argarray misc. arguments
74 * @return boolean true
76 function prepare($argarray)
78 parent::prepare($argarray);
80 $this->user = common_current_user();
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.'),
88 if ($this->isPost()) {
89 $this->checkSessionToken();
94 $this->title = $this->trimmed('title');
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.'));
101 $this->location = $this->trimmed('location');
102 $this->url = $this->trimmed('url');
103 $this->description = $this->trimmed('description');
104 $tz = $this->trimmed('tz');
106 $startDate = $this->trimmed('startdate');
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.'));
113 $startTime = $this->trimmed('event-starttime');
115 if (empty($startTime)) {
116 $startTime = '00:00';
119 $endDate = $this->trimmed('enddate');
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.'));
126 $endTime = $this->trimmed('event-endtime');
128 if (empty($endTime)) {
132 $start = $startDate . ' ' . $startTime . ' ' . $tz;
134 common_debug("Event start: '$start'");
136 $end = $endDate . ' ' . $endTime . ' ' . $tz;
138 common_debug("Event start: '$end'");
140 $this->startTime = strtotime($start);
141 $this->endTime = strtotime($end);
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".'),
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".'),
156 } catch (ClientException $ce) {
157 if ($this->boolean('ajax')) {
158 $this->outputAjaxError($ce->getMessage());
161 $this->error = $ce->getMessage();
173 * @param array $argarray is ignored since it's now passed in in prepare()
177 function handle($argarray=null)
179 parent::handle($argarray);
181 if ($this->isPost()) {
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.'));
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.'));
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.'));
215 // Does the heavy-lifting for getting "To:" information
217 ToSelector::fillOptions($this, $options);
219 $profile = $this->user->getProfile();
221 $saved = Happening::saveNew($profile,
230 $event = Happening::fromNotice($saved);
232 RSVP::saveNew($profile, $event, RSVP::POSITIVE);
234 } catch (ClientException $ce) {
235 if ($this->boolean('ajax')) {
236 $this->outputAjaxError($ce->getMessage());
239 $this->error = $ce->getMessage();
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');
258 common_redirect($saved->bestUrl(), 303);
262 // @todo factor this out into a base class
263 function outputAjaxError($msg)
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');
280 * Show the event form
284 function showContent()
286 if (!empty($this->error)) {
287 $this->element('p', 'error', $this->error);
290 $form = new EventForm($this);
298 * Return true if read only.
302 * @param array $args other arguments
304 * @return boolean is read only action?
306 function isReadOnly($args)
308 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
309 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
320 * Used to generate the notice code for Ajax results.
322 * @param Notice $notice Notice that was saved
326 function showNotice($notice)
328 $nli = new NoticeListItem($notice, $this);