noticeform.php 11 KB

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