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

newevent.php 7.33 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 7
 * Add a new event
 * 
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
 */
if (!defined('STATUSNET')) {
31 32
    // 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
33 34 35 36
    exit(1);
}

/**
37
 * Add a new event
Evan Prodromou's avatar
Evan Prodromou committed
38
 *
39 40 41 42 43 44
 * @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
45
 */
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 65
     * @return string Action title
     */

    function title()
    {
66
        return _m('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 77

    function prepare($argarray)
Evan Prodromou's avatar
Evan Prodromou committed
78
    {
79
        parent::prepare($argarray);
Evan Prodromou's avatar
Evan Prodromou committed
80 81 82

        $this->user = common_current_user();

83
        if (empty($this->user)) {
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
        $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');
Evan Prodromou's avatar
Evan Prodromou committed
99
        $this->url         = $this->trimmed('url');
100 101
        $this->description = $this->trimmed('description');

102 103 104
        $startDate = $this->trimmed('startdate');

        if (empty($startDate)) {
105
            throw new ClientException(_m('Start date required.'));
106 107 108 109 110 111 112 113 114 115 116
        }

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

        if (empty($startTime)) {
            $startTime = '00:00';
        }

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

        if (empty($endDate)) {
117
            throw new ClientException(_m('End date required.'));
118 119 120
        }

        $endTime   = $this->trimmed('endtime');
Evan Prodromou's avatar
Evan Prodromou committed
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        if (empty($endTime)) {
            $endTime = '00:00';
        }

        $start = $startDate . ' ' . $startTime;

        common_debug("Event start: '$start'");

        $end = $endDate . ' ' . $endTime;

        common_debug("Event start: '$end'");

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

        if ($this->startTime == 0) {
138
            throw new Exception(sprintf(_m('Could not parse date "%s".'),
139 140 141 142 143
                                        $start));
        }


        if ($this->endTime == 0) {
144
            throw new Exception(sprintf(_m('Could not parse date "%s".'),
145 146
                                        $end));
        }
Evan Prodromou's avatar
Evan Prodromou committed
147

Evan Prodromou's avatar
Evan Prodromou committed
148 149 150 151
        return true;
    }

    /**
152
     * Handler method
Evan Prodromou's avatar
Evan Prodromou committed
153
     *
154
     * @param array $argarray is ignored since it's now passed in in prepare()
Evan Prodromou's avatar
Evan Prodromou committed
155 156 157
     *
     * @return void
     */
158 159

    function handle($argarray=null)
Evan Prodromou's avatar
Evan Prodromou committed
160
    {
161 162 163 164 165 166 167
        parent::handle($argarray);

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

169
        return;
Evan Prodromou's avatar
Evan Prodromou committed
170 171 172
    }

    /**
173
     * Add a new event
Evan Prodromou's avatar
Evan Prodromou committed
174
     *
175
     * @return void
Evan Prodromou's avatar
Evan Prodromou committed
176
     */
177 178

    function newEvent()
Evan Prodromou's avatar
Evan Prodromou committed
179
    {
180 181
        try {
            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.'));
Evan Prodromou's avatar
Evan Prodromou committed
191
            }
192

193 194 195 196 197 198
            $options = array();

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

            ToSelector::fillOptions($this, $options);
            
Evan Prodromou's avatar
Evan Prodromou committed
199 200 201
            $profile = $this->user->getProfile();

            $saved = Happening::saveNew($profile,
202 203
                                        $this->startTime,
                                        $this->endTime,
Evan Prodromou's avatar
Evan Prodromou committed
204 205
                                        $this->title,
                                        $this->location,
Evan Prodromou's avatar
Evan Prodromou committed
206
                                        $this->description,
207 208
                                        $this->url,
                                        $options);
209

Evan Prodromou's avatar
Evan Prodromou committed
210 211 212 213
            $event = Happening::fromNotice($saved);

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

214 215 216 217
        } catch (ClientException $ce) {
            $this->error = $ce->getMessage();
            $this->showPage();
            return;
Evan Prodromou's avatar
Evan Prodromou committed
218
        }
219

Evan Prodromou's avatar
Evan Prodromou committed
220 221 222 223 224 225
        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.
226
            $this->element('title', null, _m('Event saved'));
Evan Prodromou's avatar
Evan Prodromou committed
227 228 229 230 231 232 233 234
            $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
235 236 237
    }

    /**
238
     * Show the event form
Evan Prodromou's avatar
Evan Prodromou committed
239 240 241
     *
     * @return void
     */
242

Evan Prodromou's avatar
Evan Prodromou committed
243 244
    function showContent()
    {
245 246
        if (!empty($this->error)) {
            $this->element('p', 'error', $this->error);
Evan Prodromou's avatar
Evan Prodromou committed
247
        }
248 249 250 251 252 253

        $form = new EventForm($this);

        $form->show();

        return;
Evan Prodromou's avatar
Evan Prodromou committed
254 255 256 257 258
    }

    /**
     * Return true if read only.
     *
259
     * MAY override
Evan Prodromou's avatar
Evan Prodromou committed
260
     *
261
     * @param array $args other arguments
Evan Prodromou's avatar
Evan Prodromou committed
262 263 264
     *
     * @return boolean is read only action?
     */
265

Evan Prodromou's avatar
Evan Prodromou committed
266 267
    function isReadOnly($args)
    {
268 269 270 271 272 273
        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
            return true;
        } else {
            return false;
        }
Evan Prodromou's avatar
Evan Prodromou committed
274
    }
Evan Prodromou's avatar
Evan Prodromou committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290


    /**
     * 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
291
}