noticeform.php 11 KB

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