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

noticeform.php 10.5 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Form for posting a notice
 *
 * PHP version 5
 *
 * LICENCE: 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/>.
 *
 * @category  Form
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
26
 * @copyright 2009 StatusNet, Inc.
27
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28
 * @link      http://status.net/
29 30
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
32 33 34
    exit(1);
}

35
require_once INSTALLDIR.'/lib/form.php';
36 37 38 39 40 41 42

/**
 * Form for posting a notice
 *
 * Frequently-used form for posting a notice
 *
 * @category Form
43
 * @package  StatusNet
44 45
 * @author   Evan Prodromou <evan@status.net>
 * @author   Sarven Capadisli <csarven@status.net>
46
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47
 * @link     http://status.net/
48 49 50 51 52 53 54 55
 *
 * @see      HTMLOutputter
 */
class NoticeForm extends Form
{
    /**
     * Current action, used for returning to this page.
     */
56
    var $actionName = null;
57 58 59 60 61 62

    /**
     * Pre-filled content of the form
     */
    var $content = null;

63 64 65 66 67
    /**
     * The current user
     */
    var $user = null;

68 69 70 71 72
    /**
     * The notice being replied to
     */
    var $inreplyto = null;

73 74 75 76 77 78 79 80 81
    /**
     * Pre-filled location content of the form
     */

    var $lat;
    var $lon;
    var $location_id;
    var $location_ns;

82 83 84 85 86 87 88 89 90
    /** select this group from the drop-down by default. */
    var $to_group;

    /** select this user from the drop-down by default. */
    var $to_user;

    /** Pre-click the private checkbox. */
    var $private;

91 92 93
    /**
     * Constructor
     *
94 95 96 97 98 99 100 101 102
     * @param Action $action  Action we're being embedded into
     * @param array  $options Array of optional parameters
     *                        'user' a user instead of current
     *                        'content' notice content
     *                        'inreplyto' ID of notice to reply to
     *                        'lat' Latitude
     *                        'lon' Longitude
     *                        'location_id' ID of location
     *                        'location_ns' Namespace of location
103
     */
104
    function __construct($action, $options=null)
105
    {
106 107 108 109
        // XXX: ??? Is this to keep notice forms distinct?
        // Do we have to worry about sub-second race conditions?
        // XXX: Needs to be above the parent::__construct() call...?

110 111
        $this->id_suffix = time();

112 113 114 115 116 117 118
        parent::__construct($action);

        if (is_null($options)) {
            $options = array();
        }

        $this->actionName  = $action->trimmed('action');
119

120
        $prefill = array('content', 'inreplyto', 'lat', 
121 122
                         'lon', 'location_id', 'location_ns',
                         'to_group', 'to_profile', 'private');
123 124 125 126 127 128

        foreach ($prefill as $fieldName) {
            if (array_key_exists($fieldName, $options)) {
                $this->$fieldName = $options[$fieldName];
            }
        }
129

130 131 132 133 134 135 136 137 138 139
        // Prefill the profile if we're replying

        if (empty($this->to_profile) &&
            !empty($this->inreplyto)) {
            $notice = Notice::staticGet('id', $this->inreplyto);
            if (!empty($notice)) {
                $this->to_profile = $notice->getProfile();
            }
        }

140 141
        if (array_key_exists('user', $options)) {
            $this->user = $options['user'];
142 143 144
        } else {
            $this->user = common_current_user();
        }
145

146 147
        $this->profile = $this->user->getProfile();

148 149 150
        if (common_config('attachments', 'uploads')) {
            $this->enctype = 'multipart/form-data';
        }
151 152 153 154 155
    }

    /**
     * ID of the form
     *
156
     * @return string ID of the form
157 158 159 160
     */

    function id()
    {
161
        return 'form_notice_' . $this->id_suffix;
162 163
    }

164 165 166 167 168 169 170 171
   /**
     * Class of the form
     *
     * @return string class of the form
     */

    function formClass()
    {
172
        return 'form_notice ajax-notice';
173 174
    }

175 176 177 178 179 180 181 182 183 184 185
    /**
     * Action of the form
     *
     * @return string URL of the action
     */

    function action()
    {
        return common_local_url('newnotice');
    }

186 187 188 189 190 191 192
    /**
     * Legend of the Form
     *
     * @return void
     */
    function formLegend()
    {
193
        // TRANS: Form legend for notice form.
194 195 196
        $this->out->element('legend', null, _('Send a notice'));
    }

197 198 199 200 201 202 203
    /**
     * Data elements
     *
     * @return void
     */
    function formData()
    {
204
        if (Event::handle('StartShowNoticeFormData', array($this))) {
205 206
            $this->out->element('label', array('for' => 'notice_data-text',
                                               'id' => 'notice_data-text-label'),
207
                                // TRANS: Title for notice label. %s is the user's nickname.
208 209
                                sprintf(_('What\'s up, %s?'), $this->user->nickname));
            // XXX: vary by defined max size
210
            $this->out->element('textarea', array('class' => 'notice_data-text',
211 212 213 214 215 216 217 218
                                                  'cols' => 35,
                                                  'rows' => 4,
                                                  'name' => 'status_textarea'),
                                ($this->content) ? $this->content : '');

            $contentLimit = Notice::maxContent();

            if ($contentLimit > 0) {
219
                $this->out->element('span',
220
                                    array('class' => 'count'),
221 222 223 224
                                    $contentLimit);
            }

            if (common_config('attachments', 'uploads')) {
225
                $this->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
226
                $this->out->elementStart('label', array('class' => 'notice_data-attach'));
227
                // TRANS: Input label in notice form for adding an attachment.
228 229
                $this->out->text(_('Attach'));
                $this->out->element('input', array('class' => 'notice_data-attach',
230
                                                   'type' => 'file',
231
                                                   'name' => 'attach',
232 233
                                                   // TRANS: Title for input field to attach a file to a notice.
                                                   'title' => _('Attach a file.')));
234
                $this->out->elementEnd('label');
235
            }
236 237
            if (!empty($this->actionName)) {
                $this->out->hidden('notice_return-to', $this->actionName, 'returnto');
238 239
            }
            $this->out->hidden('notice_in-reply-to', $this->inreplyto, 'inreplyto');
240 241 242 243 244 245 246 247

            $this->out->elementStart('div');
            $toWidget = new ToSelector($this->out,
                                       $this->user,
                                       (!empty($this->to_group) ? $this->to_group : $this->to_user));

            $toWidget->show();
            $this->out->elementEnd('div');
248

249
            if ($this->user->shareLocation()) {
250 251 252 253
                $this->out->hidden('notice_data-lat', empty($this->lat) ? (empty($this->profile->lat) ? null : $this->profile->lat) : $this->lat, 'lat');
                $this->out->hidden('notice_data-lon', empty($this->lon) ? (empty($this->profile->lon) ? null : $this->profile->lon) : $this->lon, 'lon');
                $this->out->hidden('notice_data-location_id', empty($this->location_id) ? (empty($this->profile->location_id) ? null : $this->profile->location_id) : $this->location_id, 'location_id');
                $this->out->hidden('notice_data-location_ns', empty($this->location_ns) ? (empty($this->profile->location_ns) ? null : $this->profile->location_ns) : $this->location_ns, 'location_ns');
254

255
                $this->out->elementStart('div', array('class' => 'notice_data-geo_wrap',
256
                                                      'data-api' => common_local_url('geocode')));
257 258 259

                // @fixme checkbox method allows no way to change the id without changing the name
                //$this->out->checkbox('notice_data-geo', _('Share my location'), true);
260
                $this->out->elementStart('label', 'notice_data-geo');
261 262 263 264 265 266 267 268
                $this->out->element('input', array(
                    'name' => 'notice_data-geo',
                    'type' => 'checkbox',
                    'class' => 'checkbox',
                    'id' => $this->id() . '-notice_data-geo',
                    'checked' => true, // ?
                ));
                $this->out->text(' ');
269 270 271 272
                // TRANS: Field label to add location to a notice.
                $this->out->text(_('Share my location'));
                $this->out->elementEnd('label');
                               
273
                $this->out->elementEnd('div');
274 275 276 277
                // TRANS: Text to not share location for a notice in notice form.
                $share_disable_text = _('Do not share my location');
                // TRANS: Timeout error text for location retrieval in notice form.
                $error_timeout_text = _('Sorry, retrieving your geo location is taking longer than expected, please try again later');
278
                $this->out->inlineScript(' var NoticeDataGeo_text = {'.
279 280
                    'ShareDisable: ' .json_encode($share_disable_text).','.
                    'ErrorTimeout: ' .json_encode($error_timeout_text).
281
                    '}');
282 283
            }

Sarven Capadisli's avatar
Sarven Capadisli committed
284
            Event::handle('EndShowNoticeFormData', array($this));
285
        }
286 287 288 289 290 291 292 293 294 295
    }

    /**
     * Action elements
     *
     * @return void
     */

    function formActions()
    {
296 297
        $this->out->element('input', array('id' => 'notice_action-submit',
                                           'class' => 'submit',
298 299
                                           'name' => 'status_submit',
                                           'type' => 'submit',
300 301
                                           // TRANS: Button text for sending notice.
                                           'value' => _m('BUTTON', 'Send')));
302
    }
303
}