We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

newevent.php 9.95 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2
<?php
/**
3 4
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2011, StatusNet, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
5
 *
6
 * Add a new event
7
 *
Evan Prodromou's avatar
Evan Prodromou committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * PHP version 5
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28
 *
 * @category  Event
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2011 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
29
 */
30

Evan Prodromou's avatar
Evan Prodromou committed
31
if (!defined('STATUSNET')) {
32 33
    // This check helps protect against security problems;
    // your code file can't be executed directly from the web.
Evan Prodromou's avatar
Evan Prodromou committed
34 35 36 37
    exit(1);
}

/**
38
 * Add a new event
Evan Prodromou's avatar
Evan Prodromou committed
39
 *
40 41 42 43 44 45
 * @category  Event
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @copyright 2011 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
46
 */
47
class NeweventAction extends Action
Evan Prodromou's avatar
Evan Prodromou committed
48
{
49 50 51 52 53 54
    protected $user        = null;
    protected $error       = null;
    protected $complete    = null;
    protected $title       = null;
    protected $location    = null;
    protected $description = null;
55 56
    protected $startTime   = null;
    protected $endTime     = null;
Evan Prodromou's avatar
Evan Prodromou committed
57 58

    /**
59
     * Returns the title of the action
Evan Prodromou's avatar
Evan Prodromou committed
60
     *
61 62 63 64
     * @return string Action title
     */
    function title()
    {
65 66
        // TRANS: Title for new event form.
        return _m('TITLE','New event');
67 68 69 70
    }

    /**
     * For initializing members of the class.
Evan Prodromou's avatar
Evan Prodromou committed
71
     *
72
     * @param array $argarray misc. arguments
Evan Prodromou's avatar
Evan Prodromou committed
73
     *
74
     * @return boolean true
Evan Prodromou's avatar
Evan Prodromou committed
75
     */
76
    function prepare($argarray)
Evan Prodromou's avatar
Evan Prodromou committed
77
    {
78
        parent::prepare($argarray);
Evan Prodromou's avatar
Evan Prodromou committed
79 80 81

        $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.'),
85 86 87 88 89
                                      403);
        }

        if ($this->isPost()) {
            $this->checkSessionToken();
Evan Prodromou's avatar
Evan Prodromou committed
90 91
        }

92
        try {
93

94
            $this->title = $this->trimmed('title');
95

96 97 98 99
            if (empty($this->title)) {
                // TRANS: Client exception thrown when trying to post an event without providing a title.
                throw new ClientException(_m('Title required.'));
            }
100

101 102 103
            $this->location    = $this->trimmed('location');
            $this->url         = $this->trimmed('url');
            $this->description = $this->trimmed('description');
104
            $tz                = $this->trimmed('tz');
105

106
            $startDate = $this->trimmed('startdate');
107

108 109 110 111
            if (empty($startDate)) {
                // TRANS: Client exception thrown when trying to post an event without providing a start date.
                throw new ClientException(_m('Start date required.'));
            }
112

113
            $startTime = $this->trimmed('event-starttime');
114

115 116 117
            if (empty($startTime)) {
                $startTime = '00:00';
            }
118

119
            $endDate   = $this->trimmed('enddate');
120

121 122 123 124
            if (empty($endDate)) {
                // TRANS: Client exception thrown when trying to post an event without providing an end date.
                throw new ClientException(_m('End date required.'));
            }
Evan Prodromou's avatar
Evan Prodromou committed
125

126
            $endTime   = $this->trimmed('event-endtime');
127

128 129 130
            if (empty($endTime)) {
                $endTime = '00:00';
            }
131

132
            $start = $startDate . ' ' . $startTime . ' ' . $tz;
Zach Copley's avatar
Zach Copley committed
133
            $end   = $endDate . ' ' . $endTime . ' ' . $tz;
134

135 136
            $this->startTime = strtotime($start);
            $this->endTime   = strtotime($end);
137

138 139 140 141 142 143
            if ($this->startTime == 0) {
                // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
                // TRANS: %s is the data that could not be processed.
                throw new ClientException(sprintf(_m('Could not parse date "%s".'),
                                            $start));
            }
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            if ($this->endTime == 0) {
                // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
                // TRANS: %s is the data that could not be processed.
                throw new ClientException(sprintf(_m('Could not parse date "%s".'),
                                            $end));
            }
        } catch (ClientException $ce) {
            if ($this->boolean('ajax')) {
                $this->outputAjaxError($ce->getMessage());
                return false;
            } else {
                $this->error = $ce->getMessage();
                $this->showPage();
                return false;
            }
160
        }
Evan Prodromou's avatar
Evan Prodromou committed
161

Evan Prodromou's avatar
Evan Prodromou committed
162 163 164 165
        return true;
    }

    /**
166
     * Handler method
Evan Prodromou's avatar
Evan Prodromou committed
167
     *
168
     * @param array $argarray is ignored since it's now passed in in prepare()
Evan Prodromou's avatar
Evan Prodromou committed
169 170 171
     *
     * @return void
     */
172
    function handle($argarray=null)
Evan Prodromou's avatar
Evan Prodromou committed
173
    {
174 175 176 177 178 179 180
        parent::handle($argarray);

        if ($this->isPost()) {
            $this->newEvent();
        } else {
            $this->showPage();
        }
Evan Prodromou's avatar
Evan Prodromou committed
181

182
        return;
Evan Prodromou's avatar
Evan Prodromou committed
183 184 185
    }

    /**
186
     * Add a new event
Evan Prodromou's avatar
Evan Prodromou committed
187
     *
188
     * @return void
Evan Prodromou's avatar
Evan Prodromou committed
189
     */
190
    function newEvent()
Evan Prodromou's avatar
Evan Prodromou committed
191
    {
192
        try {
Zach Copley's avatar
Zach Copley committed
193

194
            if (empty($this->title)) {
195
                // TRANS: Client exception thrown when trying to post an event without providing a title.
196
                throw new ClientException(_m('Event must have a title.'));
197 198
            }

199
            if (empty($this->startTime)) {
200
                // TRANS: Client exception thrown when trying to post an event without providing a start time.
201
                throw new ClientException(_m('Event must have a start time.'));
202 203
            }

204
            if (empty($this->endTime)) {
205
                // TRANS: Client exception thrown when trying to post an event without providing an end time.
206
                throw new ClientException(_m('Event must have an end time.'));
Evan Prodromou's avatar
Evan Prodromou committed
207
            }
208

209
            if (!empty($this->url) && Validate::uri($this->url) === false) {
Zach Copley's avatar
Zach Copley committed
210 211 212 213
                // TRANS: Client exception thrown when trying to post an event with an invalid URL.
                throw new ClientException(_m('URL must be valid.'));
            }

214 215 216 217 218
            $options = array();

            // Does the heavy-lifting for getting "To:" information

            ToSelector::fillOptions($this, $options);
219

Evan Prodromou's avatar
Evan Prodromou committed
220 221 222
            $profile = $this->user->getProfile();

            $saved = Happening::saveNew($profile,
223 224
                                        $this->startTime,
                                        $this->endTime,
Evan Prodromou's avatar
Evan Prodromou committed
225 226
                                        $this->title,
                                        $this->location,
Evan Prodromou's avatar
Evan Prodromou committed
227
                                        $this->description,
228 229
                                        $this->url,
                                        $options);
230

Evan Prodromou's avatar
Evan Prodromou committed
231 232 233 234
            $event = Happening::fromNotice($saved);

            RSVP::saveNew($profile, $event, RSVP::POSITIVE);

235
        } catch (ClientException $ce) {
236 237
            if ($this->boolean('ajax')) {
                $this->outputAjaxError($ce->getMessage());
238
                return;
239 240 241 242 243
            } else {
                $this->error = $ce->getMessage();
                $this->showPage();
                return;
            }
Evan Prodromou's avatar
Evan Prodromou committed
244
        }
245

Evan Prodromou's avatar
Evan Prodromou committed
246 247 248 249 250 251
        if ($this->boolean('ajax')) {
            header('Content-Type: text/xml;charset=utf-8');
            $this->xw->startDocument('1.0', 'UTF-8');
            $this->elementStart('html');
            $this->elementStart('head');
            // TRANS: Page title after sending a notice.
252
            $this->element('title', null, _m('Event saved'));
Evan Prodromou's avatar
Evan Prodromou committed
253 254 255 256 257 258 259 260
            $this->elementEnd('head');
            $this->elementStart('body');
            $this->showNotice($saved);
            $this->elementEnd('body');
            $this->elementEnd('html');
        } else {
            common_redirect($saved->bestUrl(), 303);
        }
Evan Prodromou's avatar
Evan Prodromou committed
261 262
    }

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    // @todo factor this out into a base class
    function outputAjaxError($msg)
    {
        header('Content-Type: text/xml;charset=utf-8');
        $this->xw->startDocument('1.0', 'UTF-8');
        $this->elementStart('html');
        $this->elementStart('head');
        // TRANS: Page title after an AJAX error occurs
        $this->element('title', null, _('Ajax Error'));
        $this->elementEnd('head');
        $this->elementStart('body');
        $this->element('p', array('id' => 'error'), $msg);
        $this->elementEnd('body');
        $this->elementEnd('html');
        return;
    }

Evan Prodromou's avatar
Evan Prodromou committed
280
    /**
281
     * Show the event form
Evan Prodromou's avatar
Evan Prodromou committed
282 283 284 285 286
     *
     * @return void
     */
    function showContent()
    {
287 288
        if (!empty($this->error)) {
            $this->element('p', 'error', $this->error);
Evan Prodromou's avatar
Evan Prodromou committed
289
        }
290 291 292 293 294 295

        $form = new EventForm($this);

        $form->show();

        return;
Evan Prodromou's avatar
Evan Prodromou committed
296 297 298 299 300
    }

    /**
     * Return true if read only.
     *
301
     * MAY override
Evan Prodromou's avatar
Evan Prodromou committed
302
     *
303
     * @param array $args other arguments
Evan Prodromou's avatar
Evan Prodromou committed
304 305 306 307 308
     *
     * @return boolean is read only action?
     */
    function isReadOnly($args)
    {
309 310 311 312 313 314
        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
            return true;
        } else {
            return false;
        }
Evan Prodromou's avatar
Evan Prodromou committed
315
    }
Evan Prodromou's avatar
Evan Prodromou committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331


    /**
     * Output a notice
     *
     * Used to generate the notice code for Ajax results.
     *
     * @param Notice $notice Notice that was saved
     *
     * @return void
     */
    function showNotice($notice)
    {
        $nli = new NoticeListItem($notice, $this);
        $nli->show();
    }
Evan Prodromou's avatar
Evan Prodromou committed
332
}