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');
105 $startDate = $this->trimmed('startdate');
107 if (empty($startDate)) {
108 // TRANS: Client exception thrown when trying to post an event without providing a start date.
109 throw new ClientException(_m('Start date required.'));
112 $startTime = $this->trimmed('event-starttime');
114 if (empty($startTime)) {
115 $startTime = '00:00';
118 $endDate = $this->trimmed('enddate');
120 if (empty($endDate)) {
121 // TRANS: Client exception thrown when trying to post an event without providing an end date.
122 throw new ClientException(_m('End date required.'));
125 $endTime = $this->trimmed('event-endtime');
127 if (empty($endTime)) {
131 $start = $startDate . ' ' . $startTime;
133 common_debug("Event start: '$start'");
135 $end = $endDate . ' ' . $endTime;
137 common_debug("Event start: '$end'");
139 $this->startTime = strtotime($start);
140 $this->endTime = strtotime($end);
142 if ($this->startTime == 0) {
143 // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
144 // TRANS: %s is the data that could not be processed.
145 throw new ClientException(sprintf(_m('Could not parse date "%s".'),
149 if ($this->endTime == 0) {
150 // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
151 // TRANS: %s is the data that could not be processed.
152 throw new ClientException(sprintf(_m('Could not parse date "%s".'),
155 } catch (ClientException $ce) {
156 if ($this->boolean('ajax')) {
157 $this->outputAjaxError($ce->getMessage());
160 $this->error = $ce->getMessage();
172 * @param array $argarray is ignored since it's now passed in in prepare()
176 function handle($argarray=null)
178 parent::handle($argarray);
180 if ($this->isPost()) {
197 if (empty($this->title)) {
198 // TRANS: Client exception thrown when trying to post an event without providing a title.
199 throw new ClientException(_m('Event must have a title.'));
202 if (empty($this->startTime)) {
203 // TRANS: Client exception thrown when trying to post an event without providing a start time.
204 throw new ClientException(_m('Event must have a start time.'));
207 if (empty($this->endTime)) {
208 // TRANS: Client exception thrown when trying to post an event without providing an end time.
209 throw new ClientException(_m('Event must have an end time.'));
214 // Does the heavy-lifting for getting "To:" information
216 ToSelector::fillOptions($this, $options);
218 $profile = $this->user->getProfile();
220 $saved = Happening::saveNew($profile,
229 $event = Happening::fromNotice($saved);
231 RSVP::saveNew($profile, $event, RSVP::POSITIVE);
233 } catch (ClientException $ce) {
234 if ($this->boolean('ajax')) {
235 $this->outputAjaxError($ce->getMessage());
238 $this->error = $ce->getMessage();
244 if ($this->boolean('ajax')) {
245 header('Content-Type: text/xml;charset=utf-8');
246 $this->xw->startDocument('1.0', 'UTF-8');
247 $this->elementStart('html');
248 $this->elementStart('head');
249 // TRANS: Page title after sending a notice.
250 $this->element('title', null, _m('Event saved'));
251 $this->elementEnd('head');
252 $this->elementStart('body');
253 $this->showNotice($saved);
254 $this->elementEnd('body');
255 $this->elementEnd('html');
257 common_redirect($saved->bestUrl(), 303);
261 // @todo factor this out into a base class
262 function outputAjaxError($msg)
264 header('Content-Type: text/xml;charset=utf-8');
265 $this->xw->startDocument('1.0', 'UTF-8');
266 $this->elementStart('html');
267 $this->elementStart('head');
268 // TRANS: Page title after an AJAX error occurs
269 $this->element('title', null, _('Ajax Error'));
270 $this->elementEnd('head');
271 $this->elementStart('body');
272 $this->element('p', array('id' => 'error'), $msg);
273 $this->elementEnd('body');
274 $this->elementEnd('html');
279 * Show the event form
283 function showContent()
285 if (!empty($this->error)) {
286 $this->element('p', 'error', $this->error);
289 $form = new EventForm($this);
297 * Return true if read only.
301 * @param array $args other arguments
303 * @return boolean is read only action?
305 function isReadOnly($args)
307 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
308 $_SERVER['REQUEST_METHOD'] == 'HEAD') {
319 * Used to generate the notice code for Ajax results.
321 * @param Notice $notice Notice that was saved
325 function showNotice($notice)
327 $nli = new NoticeListItem($notice, $this);