noticeform.php 11 KB

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