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

newevent.php 7.1 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 66 67 68 69 70
     * @return string Action title
     */

    function title()
    {
        return _('New event');
    }

    /**
     * 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 84 85 86 87 88 89
        if (empty($this->user)) {
            throw new ClientException(_("Must be logged in to post a event."),
                                      403);
        }

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

92
        $this->title       = $this->trimmed('title');
93 94 95 96 97

        if (empty($this->title)) {
            throw new ClientException(_('Title required.'));
        }

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        $startDate = $this->trimmed('startdate');

        if (empty($startDate)) {
            throw new ClientException(_('Start date required.'));
        }

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

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

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

        if (empty($endDate)) {
            throw new ClientException(_('End date required.'));
        }

        $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 138 139 140 141 142 143 144 145 146
        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) {
            throw new Exception(sprintf(_('Could not parse date "%s"'),
                                        $start));
        }


        if ($this->endTime == 0) {
            throw new Exception(sprintf(_('Could not parse date "%s"'),
                                        $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 182 183 184
        try {
            if (empty($this->title)) {
                throw new ClientException(_('Event must have a title.'));
            }

185
            if (empty($this->startTime)) {
Evan Prodromou's avatar
Evan Prodromou committed
186
                throw new ClientException(_('Event must have a start time.'));
187 188
            }

189
            if (empty($this->endTime)) {
Evan Prodromou's avatar
Evan Prodromou committed
190 191
                throw new ClientException(_('Event must have an end time.'));
            }
192

Evan Prodromou's avatar
Evan Prodromou committed
193 194 195
            $profile = $this->user->getProfile();

            $saved = Happening::saveNew($profile,
196 197
                                        $this->startTime,
                                        $this->endTime,
Evan Prodromou's avatar
Evan Prodromou committed
198 199
                                        $this->title,
                                        $this->location,
Evan Prodromou's avatar
Evan Prodromou committed
200 201
                                        $this->description,
                                        $this->url);
202

Evan Prodromou's avatar
Evan Prodromou committed
203 204 205 206
            $event = Happening::fromNotice($saved);

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

207 208 209 210
        } catch (ClientException $ce) {
            $this->error = $ce->getMessage();
            $this->showPage();
            return;
Evan Prodromou's avatar
Evan Prodromou committed
211
        }
212

Evan Prodromou's avatar
Evan Prodromou committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        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.
            $this->element('title', null, _('Event saved'));
            $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
228 229 230
    }

    /**
231
     * Show the event form
Evan Prodromou's avatar
Evan Prodromou committed
232 233 234
     *
     * @return void
     */
235

Evan Prodromou's avatar
Evan Prodromou committed
236 237
    function showContent()
    {
238 239
        if (!empty($this->error)) {
            $this->element('p', 'error', $this->error);
Evan Prodromou's avatar
Evan Prodromou committed
240
        }
241 242 243 244 245 246

        $form = new EventForm($this);

        $form->show();

        return;
Evan Prodromou's avatar
Evan Prodromou committed
247 248 249 250 251
    }

    /**
     * Return true if read only.
     *
252
     * MAY override
Evan Prodromou's avatar
Evan Prodromou committed
253
     *
254
     * @param array $args other arguments
Evan Prodromou's avatar
Evan Prodromou committed
255 256 257
     *
     * @return boolean is read only action?
     */
258

Evan Prodromou's avatar
Evan Prodromou committed
259 260
    function isReadOnly($args)
    {
261 262 263 264 265 266
        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
            return true;
        } else {
            return false;
        }
Evan Prodromou's avatar
Evan Prodromou committed
267
    }
Evan Prodromou's avatar
Evan Prodromou committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283


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