noticeform.php 11.1 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('GNUSOCIAL')) { exit(1); }
32 33 34 35 36 37 38

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

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

59 60 61 62 63
    /**
     * The current user
     */
    var $user = null;

64 65 66 67 68
    /**
     * The notice being replied to
     */
    var $inreplyto = null;

69 70 71 72 73 74 75 76 77
    /**
     * Pre-filled location content of the form
     */

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

78 79 80 81
    /** select this group from the drop-down by default. */
    var $to_group;

    /** select this user from the drop-down by default. */
82
    var $to_profile;
83 84 85 86

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

87 88 89
    /**
     * Constructor
     *
90 91 92 93 94 95 96 97 98
     * @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
99
     */
100
    function __construct($action, $options=null)
101
    {
102
        parent::__construct($action);
103

104 105
        // When creating a notice form we don't want to collide with
        // possibly existing HTML elements, as naming conventions are similar.
106
        $this->id_suffix = rand();
107

108 109 110 111 112
        if (is_null($options)) {
            $options = array();
        }

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

114
        $prefill = array('content', 'inreplyto', 'lat', 
115 116
                         'lon', 'location_id', 'location_ns',
                         'to_group', 'to_profile', 'private');
117 118 119 120 121 122

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

124 125 126 127
        // Prefill the profile if we're replying

        if (empty($this->to_profile) &&
            !empty($this->inreplyto)) {
128
            $notice = Notice::getKV('id', $this->inreplyto);
129 130 131 132 133
            if (!empty($notice)) {
                $this->to_profile = $notice->getProfile();
            }
        }

134 135
        if (array_key_exists('user', $options)) {
            $this->user = $options['user'];
136 137 138
        } else {
            $this->user = common_current_user();
        }
139

140 141
        $this->profile = $this->user->getProfile();

142 143 144
        if (common_config('attachments', 'uploads')) {
            $this->enctype = 'multipart/form-data';
        }
145 146 147 148 149
    }

    /**
     * ID of the form
     *
150
     * @return string ID of the form
151 152 153 154
     */

    function id()
    {
155
        return 'form_notice_' . $this->id_suffix;
156 157
    }

158 159 160 161 162 163 164 165
   /**
     * Class of the form
     *
     * @return string class of the form
     */

    function formClass()
    {
166
        return 'form_notice ajax-notice';
167 168
    }

169 170 171 172 173 174 175 176 177 178 179
    /**
     * Action of the form
     *
     * @return string URL of the action
     */

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

180 181 182 183 184 185 186
    /**
     * Legend of the Form
     *
     * @return void
     */
    function formLegend()
    {
187
        // TRANS: Form legend for notice form.
188 189 190
        $this->out->element('legend', null, _('Send a notice'));
    }

191 192 193 194 195 196 197 198 199
    protected function placeholderText()
    {
        if ($this->inreplyto) {
            return _('Write a reply...');
        }

        return _('Share your status...');
    }

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

            $contentLimit = Notice::maxContent();

            if ($contentLimit > 0) {
224
                $this->out->element('span',
225
                                    array('class' => 'count'),
226 227 228 229
                                    $contentLimit);
            }

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

248
            $this->out->elementStart('div', 'to-selector');
249 250
            $toWidget = new ToSelector($this->out,
                                       $this->user,
251
                                       (!empty($this->to_group) ? $this->to_group : $this->to_profile));
252 253 254

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

256
            if ($this->profile->shareLocation()) {
257 258 259 260
                $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');
261

262
                $this->out->elementStart('div', array('class' => 'notice_data-geo_wrap',
263
                                                      'data-api' => common_local_url('geocode')));
264 265 266 267 268 269 270 271 272 273

                // @fixme checkbox method allows no way to change the id without changing the name
                //$this->out->checkbox('notice_data-geo', _('Share my location'), true);
                $this->out->element('input', array(
                    'name' => 'notice_data-geo',
                    'type' => 'checkbox',
                    'class' => 'checkbox',
                    'id' => $this->id() . '-notice_data-geo',
                    'checked' => true, // ?
                ));
274 275 276 277
                $this->out->element('label', array('class' => 'notice_data-geo',
                                                   'for'   => $this->id().'-notice_data-geo'),
                                    // TRANS: Checkbox label to allow sharing geo location in notices.
                                    _('Share my location'));
278
                               
279
                $this->out->elementEnd('div');
280 281 282 283
                // 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');
284
                $this->out->inlineScript(' var NoticeDataGeo_text = {'.
285 286
                    'ShareDisable: ' .json_encode($share_disable_text).','.
                    'ErrorTimeout: ' .json_encode($error_timeout_text).
287
                    '}');
288 289
            }

Sarven Capadisli's avatar
Sarven Capadisli committed
290
            Event::handle('EndShowNoticeFormData', array($this));
291
        }
292 293 294 295 296 297 298 299 300 301
    }

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

    function formActions()
    {
302 303
        $this->out->element('input', array('id' => 'notice_action-submit',
                                           'class' => 'submit',
304 305
                                           'name' => 'status_submit',
                                           'type' => 'submit',
306 307
                                           // TRANS: Button text for sending notice.
                                           'value' => _m('BUTTON', 'Send')));
308
    }
309
}