]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticeform.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / lib / noticeform.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Form for posting a notice
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Form
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @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  * Form for posting a notice
35  *
36  * Frequently-used form for posting a notice
37  *
38  * @category Form
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @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  * @see      HTMLOutputter
46  */
47 class NoticeForm extends Form
48 {
49     /**
50      * Current action, used for returning to this page.
51      */
52     var $actionName = null;
53
54     /**
55      * Pre-filled content of the form
56      */
57     var $content = null;
58
59     /**
60      * The current user
61      */
62     var $user = null;
63
64     /**
65      * The notice being replied to
66      */
67     var $inreplyto = null;
68
69     /**
70      * Pre-filled location content of the form
71      */
72
73     var $lat;
74     var $lon;
75     var $location_id;
76     var $location_ns;
77
78     /** select this group from the drop-down by default. */
79     var $to_group;
80
81     /** select this user from the drop-down by default. */
82     var $to_profile;
83
84     /** Pre-click the private checkbox. */
85     var $private;
86
87     /**
88      * Constructor
89      *
90      * @param Action $action  Action we're being embedded into
91      * @param array  $options Array of optional parameters
92      *                        'user' a user instead of current
93      *                        'content' notice content
94      *                        'inreplyto' ID of notice to reply to
95      *                        'lat' Latitude
96      *                        'lon' Longitude
97      *                        'location_id' ID of location
98      *                        'location_ns' Namespace of location
99      */
100     function __construct(Action $action, array $options = array())
101     {
102         parent::__construct($action);
103
104         // When creating a notice form we don't want to collide with
105         // possibly existing HTML elements, as naming conventions are similar.
106         $this->id_suffix = mt_rand();
107
108         $this->actionName  = $action->trimmed('action');
109
110         $prefill = array('content', 'inreplyto', 'lat', 
111                          'lon', 'location_id', 'location_ns',
112                          'to_group', 'to_profile', 'private');
113
114         foreach ($prefill as $fieldName) {
115             if (array_key_exists($fieldName, $options)) {
116                 $this->$fieldName = $options[$fieldName];
117             }
118         }
119
120         // Prefill the profile if we're replying
121
122         if (empty($this->to_profile) &&
123             !empty($this->inreplyto)) {
124             $notice = Notice::getKV('id', $this->inreplyto);
125             if (!empty($notice)) {
126                 $this->to_profile = $notice->getProfile();
127             }
128         }
129
130         if (array_key_exists('user', $options)) {
131             $this->user = $options['user'];
132         } else {
133             $this->user = common_current_user();
134         }
135
136         $this->profile = $this->user->getProfile();
137
138         if (common_config('attachments', 'uploads')) {
139             $this->enctype = 'multipart/form-data';
140         }
141     }
142
143     /**
144      * ID of the form
145      *
146      * @return string ID of the form
147      */
148
149     function id()
150     {
151         return 'form_notice_' . $this->id_suffix;
152     }
153
154    /**
155      * Class of the form
156      *
157      * @return string class of the form
158      */
159
160     function formClass()
161     {
162         return 'form_notice ajax-notice';
163     }
164
165     /**
166      * Action of the form
167      *
168      * @return string URL of the action
169      */
170
171     function action()
172     {
173         return common_local_url('newnotice');
174     }
175
176     /**
177      * Legend of the Form
178      *
179      * @return void
180      */
181     function formLegend()
182     {
183         // TRANS: Form legend for notice form.
184         $this->out->element('legend', null, _('Send a notice'));
185     }
186
187     protected function placeholderText()
188     {
189         if ($this->inreplyto) {
190             return _('Write a reply...');
191         }
192
193         return _('Share your status...');
194     }
195
196     /**
197      * Data elements
198      *
199      * @return void
200      */
201     function formData()
202     {
203         if (Event::handle('StartShowNoticeFormData', array($this))) {
204             $this->out->element('label', array('for' => 'notice_data-text',
205                                                'id' => 'notice_data-text-label'),
206                                 // TRANS: Title for notice label. %s is the user's nickname.
207                                 sprintf(_('What\'s up, %s?'), $this->user->nickname));
208             // XXX: vary by defined max size
209             $this->out->element('textarea', array('class' => 'notice_data-text',
210                                                   'required' => 'required',
211                                                   'placeholder' => $this->placeholderText(),
212                                                   'cols' => 35,
213                                                   'rows' => 4,
214                                                   'name' => 'status_textarea'),
215                                 ($this->content) ? $this->content : '');
216
217             $contentLimit = Notice::maxContent();
218
219             if ($contentLimit > 0) {
220                 $this->out->element('span',
221                                     array('class' => 'count'),
222                                     $contentLimit);
223             }
224
225             if (common_config('attachments', 'uploads')) {
226                 $this->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
227                 $this->out->element('label', array('class' => 'notice_data-attach',
228                                                    'for'   => $this->id().'-notice_data-attach'),
229                                     // TRANS: Input label in notice form for adding an attachment.
230                                     _('Attach'));
231                 // The actual input element tends to be hidden with CSS.
232                 $this->out->element('input', array('class' => 'notice_data-attach',
233                                                    'type' => 'file',
234                                                    'name' => 'attach',
235                                                    'id' => $this->id().'-notice_data-attach',
236                                                    // TRANS: Title for input field to attach a file to a notice.
237                                                    'title' => _('Attach a file.')));
238             }
239             if (!empty($this->actionName)) {
240                 $this->out->hidden('notice_return-to', $this->actionName, 'returnto');
241             }
242             $this->out->hidden('notice_in-reply-to', $this->inreplyto, 'inreplyto');
243
244             $this->out->elementStart('div', 'to-selector');
245             $toWidget = new ToSelector($this->out,
246                                        $this->user,
247                                        (!empty($this->to_group) ? $this->to_group : $this->to_profile));
248
249             $toWidget->show();
250             $this->out->elementEnd('div');
251
252             if ($this->profile->shareLocation()) {
253                 $this->out->hidden('notice_data-lat', empty($this->lat) ? (empty($this->profile->lat) ? null : $this->profile->lat) : $this->lat, 'lat');
254                 $this->out->hidden('notice_data-lon', empty($this->lon) ? (empty($this->profile->lon) ? null : $this->profile->lon) : $this->lon, 'lon');
255                 $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');
256                 $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');
257
258                 $this->out->elementStart('div', array('class' => 'notice_data-geo_wrap',
259                                                       'data-api' => common_local_url('geocode')));
260
261                 // @fixme checkbox method allows no way to change the id without changing the name
262                 //$this->out->checkbox('notice_data-geo', _('Share my location'), true);
263                 $this->out->element('input', array(
264                     'name' => 'notice_data-geo',
265                     'type' => 'checkbox',
266                     'class' => 'checkbox',
267                     'id' => $this->id() . '-notice_data-geo',
268                     'checked' => false, // Must be unchecked by default
269                 ));
270                 $this->out->element('label', array('class' => 'notice_data-geo',
271                                                    'for'   => $this->id().'-notice_data-geo'),
272                                     // TRANS: Checkbox label to allow sharing geo location in notices.
273                                     _('Share my location'));
274                                
275                 $this->out->elementEnd('div');
276                 // TRANS: Text to not share location for a notice in notice form.
277                 $share_disable_text = _('Do not share my location');
278                 // TRANS: Timeout error text for location retrieval in notice form.
279                 $error_timeout_text = _('Sorry, retrieving your geo location is taking longer than expected, please try again later');
280                 $this->out->inlineScript(' var NoticeDataGeo_text = {'.
281                     'ShareDisable: ' .json_encode($share_disable_text).','.
282                     'ErrorTimeout: ' .json_encode($error_timeout_text).
283                     '}');
284             }
285
286             Event::handle('EndShowNoticeFormData', array($this));
287         }
288     }
289
290     /**
291      * Action elements
292      *
293      * @return void
294      */
295
296     function formActions()
297     {
298         $this->out->element('input', array('id' => 'notice_action-submit',
299                                            'class' => 'submit',
300                                            'name' => 'status_submit',
301                                            'type' => 'submit',
302                                            // TRANS: Button text for sending notice.
303                                            'value' => _m('BUTTON', 'Send')));
304     }
305 }