noticeform.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Form for posting a notice
  33. *
  34. * Frequently-used form for posting a notice
  35. *
  36. * @category Form
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @author Sarven Capadisli <csarven@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. *
  43. * @see HTMLOutputter
  44. */
  45. class NoticeForm extends Form
  46. {
  47. public $widgetOpts;
  48. public $scoped;
  49. public $id_suffix;
  50. public $profile;
  51. /**
  52. * Current action, used for returning to this page.
  53. */
  54. var $actionName = null;
  55. /**
  56. * Pre-filled content of the form
  57. */
  58. var $content = null;
  59. /**
  60. * The current user
  61. */
  62. var $user = null;
  63. /**
  64. * The notice being replied to
  65. */
  66. var $inreplyto = null;
  67. /**
  68. * Pre-filled location content of the form
  69. */
  70. var $lat;
  71. var $lon;
  72. var $location_id;
  73. var $location_ns;
  74. /** select this group from the drop-down by default. */
  75. var $to_group;
  76. /** select this user from the drop-down by default. */
  77. var $to_profile;
  78. /** Pre-click the private checkbox. */
  79. var $private;
  80. /**
  81. * Constructor
  82. *
  83. * @param Action $action Action we're being embedded into
  84. * @param array $options Array of optional parameters
  85. * 'user' a user instead of current
  86. * 'content' notice content
  87. * 'inreplyto' ID of notice to reply to
  88. * 'lat' Latitude
  89. * 'lon' Longitude
  90. * 'location_id' ID of location
  91. * 'location_ns' Namespace of location
  92. * @throws UserNoProfileException
  93. */
  94. function __construct(Action $action, array $options = [])
  95. {
  96. parent::__construct($action);
  97. // When creating a notice form we don't want to collide with
  98. // possibly existing HTML elements, as naming conventions are similar.
  99. $this->id_suffix = rand();
  100. $this->actionName = $action->trimmed('action');
  101. $prefill = array('content', 'inreplyto', 'lat',
  102. 'lon', 'location_id', 'location_ns',
  103. 'to_group', 'to_profile', 'private');
  104. foreach ($prefill as $fieldName) {
  105. if (array_key_exists($fieldName, $options)) {
  106. $this->$fieldName = $options[$fieldName];
  107. }
  108. }
  109. // Prefill the profile if we're replying
  110. if (empty($this->to_profile) &&
  111. !empty($this->inreplyto)) {
  112. $notice = Notice::getKV('id', $this->inreplyto);
  113. if (!empty($notice)) {
  114. $this->to_profile = $notice->getProfile();
  115. }
  116. }
  117. if (array_key_exists('user', $options)) {
  118. $this->user = $options['user'];
  119. } else {
  120. $this->user = common_current_user();
  121. }
  122. $this->profile = $this->user->getProfile();
  123. if (common_config('attachments', 'uploads')) {
  124. $this->enctype = 'multipart/form-data';
  125. }
  126. }
  127. /**
  128. * ID of the form
  129. *
  130. * @return string ID of the form
  131. */
  132. function id()
  133. {
  134. return 'form_notice_' . $this->id_suffix;
  135. }
  136. /**
  137. * Class of the form
  138. *
  139. * @return string class of the form
  140. */
  141. function formClass()
  142. {
  143. return 'form_notice ajax-notice';
  144. }
  145. /**
  146. * Action of the form
  147. *
  148. * @return string URL of the action
  149. */
  150. function action()
  151. {
  152. return common_local_url('newnotice');
  153. }
  154. /**
  155. * Legend of the Form
  156. *
  157. * @return void
  158. */
  159. function formLegend()
  160. {
  161. // TRANS: Form legend for notice form.
  162. $this->out->element('legend', null, _('Send a notice'));
  163. }
  164. protected function placeholderText()
  165. {
  166. if ($this->inreplyto) {
  167. return _('Write a reply...');
  168. }
  169. return _('Share your status...');
  170. }
  171. /**
  172. * Data elements
  173. *
  174. * @return void
  175. */
  176. function formData()
  177. {
  178. if (Event::handle('StartShowNoticeFormData', array($this))) {
  179. $this->out->element('label', array('for' => 'notice_data-text',
  180. 'id' => 'notice_data-text-label'),
  181. // TRANS: Title for notice label. %s is the user's nickname.
  182. sprintf(_('What\'s up, %s?'), $this->user->nickname));
  183. // XXX: vary by defined max size
  184. $this->out->element('textarea', array('class' => 'notice_data-text',
  185. 'required' => 'required',
  186. 'placeholder' => $this->placeholderText(),
  187. 'cols' => 35,
  188. 'rows' => 4,
  189. 'name' => 'status_textarea'),
  190. ($this->content) ? $this->content : '');
  191. $contentLimit = Notice::maxContent();
  192. if ($contentLimit > 0) {
  193. $this->out->element('span',
  194. array('class' => 'count'),
  195. $contentLimit);
  196. }
  197. if (common_config('attachments', 'uploads')) {
  198. $this->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
  199. $this->out->element('label', array('class' => 'notice_data-attach',
  200. 'for' => $this->id().'-notice_data-attach'),
  201. // TRANS: Input label in notice form for adding an attachment.
  202. _('Attach'));
  203. // The actual input element tends to be hidden with CSS.
  204. $this->out->element('input', array('class' => 'notice_data-attach',
  205. 'type' => 'file',
  206. 'name' => 'attach',
  207. 'id' => $this->id().'-notice_data-attach',
  208. // TRANS: Title for input field to attach a file to a notice.
  209. 'title' => _('Attach a file.')));
  210. }
  211. if (!empty($this->actionName)) {
  212. $this->out->hidden('notice_return-to', $this->actionName, 'returnto');
  213. }
  214. $this->out->hidden('notice_in-reply-to', $this->inreplyto, 'inreplyto');
  215. $this->out->elementStart('div', 'to-selector');
  216. $toWidget = new ToSelector($this->out,
  217. $this->user,
  218. (!empty($this->to_group) ? $this->to_group : $this->to_profile));
  219. $toWidget->show();
  220. $this->out->elementEnd('div');
  221. if ($this->profile->shareLocation()) {
  222. $this->out->hidden('notice_data-lat', empty($this->lat) ? (empty($this->profile->lat) ? null : $this->profile->lat) : $this->lat, 'lat');
  223. $this->out->hidden('notice_data-lon', empty($this->lon) ? (empty($this->profile->lon) ? null : $this->profile->lon) : $this->lon, 'lon');
  224. $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');
  225. $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');
  226. $this->out->elementStart('div', array('class' => 'notice_data-geo_wrap',
  227. 'data-api' => common_local_url('geocode')));
  228. // @fixme checkbox method allows no way to change the id without changing the name
  229. //$this->out->checkbox('notice_data-geo', _('Share my location'), true);
  230. $this->out->element('input', array(
  231. 'name' => 'notice_data-geo',
  232. 'type' => 'checkbox',
  233. 'class' => 'checkbox',
  234. 'id' => $this->id() . '-notice_data-geo',
  235. 'checked' => true, // ?
  236. ));
  237. $this->out->element('label', array('class' => 'notice_data-geo',
  238. 'for' => $this->id().'-notice_data-geo'),
  239. // TRANS: Checkbox label to allow sharing geo location in notices.
  240. _('Share my location'));
  241. $this->out->elementEnd('div');
  242. // TRANS: Text to not share location for a notice in notice form.
  243. $share_disable_text = _('Do not share my location');
  244. // TRANS: Timeout error text for location retrieval in notice form.
  245. $error_timeout_text = _('Sorry, retrieving your geo location is taking longer than expected, please try again later');
  246. $this->out->inlineScript(' var NoticeDataGeo_text = {'.
  247. 'ShareDisable: ' .json_encode($share_disable_text).','.
  248. 'ErrorTimeout: ' .json_encode($error_timeout_text).
  249. '}');
  250. }
  251. Event::handle('EndShowNoticeFormData', array($this));
  252. }
  253. }
  254. /**
  255. * Action elements
  256. *
  257. * @return void
  258. */
  259. function formActions()
  260. {
  261. $this->out->element('input', array('id' => 'notice_action-submit',
  262. 'class' => 'submit',
  263. 'name' => 'status_submit',
  264. 'type' => 'submit',
  265. // TRANS: Button text for sending notice.
  266. 'value' => _m('BUTTON', 'Send')));
  267. }
  268. }