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

newevent.php 5.94 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 55 56
    protected $user        = null;
    protected $error       = null;
    protected $complete    = null;
    protected $title       = null;
    protected $location    = null;
    protected $description = null;
    protected $start_time  = null;
    protected $end_time    = 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 93 94 95
        $this->title       = $this->trimmed('title');
        $this->location    = $this->trimmed('location');
        $this->description = $this->trimmed('description');

Evan Prodromou's avatar
Evan Prodromou committed
96 97 98 99 100 101 102 103
        $start_date = $this->trimmed('start_date');
        $start_time = $this->trimmed('start_time');
        $end_date   = $this->trimmed('end_date');
        $end_time   = $this->trimmed('end_time');

        $this->start_time = strtotime($start_date . ' ' . $start_time);
        $this->end_time   = strtotime($end_date . ' ' . $end_time);

Evan Prodromou's avatar
Evan Prodromou committed
104 105 106 107
        return true;
    }

    /**
108
     * Handler method
Evan Prodromou's avatar
Evan Prodromou committed
109
     *
110
     * @param array $argarray is ignored since it's now passed in in prepare()
Evan Prodromou's avatar
Evan Prodromou committed
111 112 113
     *
     * @return void
     */
114 115

    function handle($argarray=null)
Evan Prodromou's avatar
Evan Prodromou committed
116
    {
117 118 119 120 121 122 123
        parent::handle($argarray);

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

125
        return;
Evan Prodromou's avatar
Evan Prodromou committed
126 127 128
    }

    /**
129
     * Add a new event
Evan Prodromou's avatar
Evan Prodromou committed
130
     *
131
     * @return void
Evan Prodromou's avatar
Evan Prodromou committed
132
     */
133 134

    function newEvent()
Evan Prodromou's avatar
Evan Prodromou committed
135
    {
136 137 138 139 140
        try {
            if (empty($this->title)) {
                throw new ClientException(_('Event must have a title.'));
            }

Evan Prodromou's avatar
Evan Prodromou committed
141 142
            if (empty($this->start_time)) {
                throw new ClientException(_('Event must have a start time.'));
143 144
            }

Evan Prodromou's avatar
Evan Prodromou committed
145 146 147
            if (empty($this->end_time)) {
                throw new ClientException(_('Event must have an end time.'));
            }
148

Evan Prodromou's avatar
Evan Prodromou committed
149 150 151 152 153 154
            $saved = Happening::saveNew($this->user->getProfile(),
                                        $this->start_time,
                                        $this->end_time,
                                        $this->title,
                                        $this->location,
                                        $this->description);
155 156 157 158 159

        } catch (ClientException $ce) {
            $this->error = $ce->getMessage();
            $this->showPage();
            return;
Evan Prodromou's avatar
Evan Prodromou committed
160
        }
161

Evan Prodromou's avatar
Evan Prodromou committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        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
177 178 179
    }

    /**
180
     * Show the event form
Evan Prodromou's avatar
Evan Prodromou committed
181 182 183
     *
     * @return void
     */
184

Evan Prodromou's avatar
Evan Prodromou committed
185 186
    function showContent()
    {
187 188
        if (!empty($this->error)) {
            $this->element('p', 'error', $this->error);
Evan Prodromou's avatar
Evan Prodromou committed
189
        }
190 191 192 193 194 195

        $form = new EventForm($this);

        $form->show();

        return;
Evan Prodromou's avatar
Evan Prodromou committed
196 197 198 199 200
    }

    /**
     * Return true if read only.
     *
201
     * MAY override
Evan Prodromou's avatar
Evan Prodromou committed
202
     *
203
     * @param array $args other arguments
Evan Prodromou's avatar
Evan Prodromou committed
204 205 206
     *
     * @return boolean is read only action?
     */
207

Evan Prodromou's avatar
Evan Prodromou committed
208 209
    function isReadOnly($args)
    {
210 211 212 213 214 215
        if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
            $_SERVER['REQUEST_METHOD'] == 'HEAD') {
            return true;
        } else {
            return false;
        }
Evan Prodromou's avatar
Evan Prodromou committed
216
    }
Evan Prodromou's avatar
Evan Prodromou committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232


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