messageform.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * GNUsocial implementation of Direct Messages
  18. *
  19. * @package GNUsocial
  20. * @author Mikael Nordfeldth <mmn@hethane.se>
  21. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Form for posting a direct message
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @author Sarven Capadisli <csarven@status.net>
  33. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class MessageForm extends Form
  37. {
  38. public $widgetOpts;
  39. public $scoped;
  40. protected $to = null;
  41. protected $content = null;
  42. /**
  43. * Constructor.
  44. *
  45. * @param HTMLOutputter $out output channel
  46. * @param array|null $formOpts
  47. */
  48. function __construct(HTMLOutputter $out = null, ?array $formOpts = null)
  49. {
  50. parent::__construct($out);
  51. if (isset($formOpts['to'])) {
  52. $this->to = $formOpts['to'];
  53. }
  54. $this->content = $formOpts['content'] ?? '';
  55. }
  56. /**
  57. * ID of the form.
  58. *
  59. * @return string ID of the form
  60. */
  61. function id(): string
  62. {
  63. return 'form_notice-direct';
  64. }
  65. /**
  66. * Class of the form.
  67. *
  68. * @return string class of the form
  69. */
  70. function formClass(): string
  71. {
  72. return 'form_notice ajax-notice';
  73. }
  74. /**
  75. * Action of the form.
  76. *
  77. * @return string URL of the action
  78. */
  79. function action(): string
  80. {
  81. return common_local_url('newmessage');
  82. }
  83. /**
  84. * Legend of the Form.
  85. *
  86. * @return void
  87. */
  88. function formLegend()
  89. {
  90. // TRANS: Form legend for direct notice.
  91. $this->out->element('legend', null, _m('Send a direct notice'));
  92. }
  93. /**
  94. * Data elements.
  95. *
  96. * @return void
  97. */
  98. function formData()
  99. {
  100. $user = common_current_user();
  101. $recipients = [];
  102. $default = 'default';
  103. $subs = $user->getSubscribed();
  104. $n_subs = 0;
  105. // Add local-subscriptions
  106. while ($subs->fetch()) {
  107. $n_subs++;
  108. if ($subs->isLocal()) {
  109. $value = 'profile:'.$subs->getID();
  110. try {
  111. $recipients[$value] = substr($subs->getAcctUri(), 5) . " [{$subs->getBestName()}]";
  112. } catch (ProfileNoAcctUriException $e) {
  113. $recipients[$value] = "[?@?] " . $e->profile->getBestName();
  114. }
  115. }
  116. }
  117. if (sizeof($recipients) < $n_subs) {
  118. // some subscriptions aren't local and therefore weren't added,
  119. // worth checking if others want to add them
  120. Event::handle('FillDirectMessageRecipients', [$user, &$recipients]);
  121. }
  122. // if we came from a profile page, then lets make the message receiver visible
  123. if (!is_null($this->to)) {
  124. if (isset($recipients['profile:'.$this->to->getID()])) {
  125. $default = 'profile' . $this->to->getID();
  126. } else {
  127. try {
  128. if ($this->to->isLocal()) {
  129. $this->content = "@{$this->to->getNickname()} {$this->content}";
  130. } else {
  131. $this->content = substr($this->to->getAcctUri(), 5) . " {$this->content}";
  132. }
  133. } catch (ProfileNoAcctUriException $e) {
  134. // well, I'm no magician
  135. }
  136. }
  137. }
  138. if ($default === 'default') {
  139. // TRANS: Label entry in drop-down selection box in direct-message inbox/outbox.
  140. // TRANS: This is the default entry in the drop-down box, doubling as instructions
  141. // TRANS: and a brake against accidental submissions with the first user in the list.
  142. $recipients[$default] = empty($recipients) ? _m('No subscriptions') : _m('Select recipient:');
  143. }
  144. asort($recipients);
  145. // TRANS: Dropdown label in direct notice form.
  146. $this->out->dropdown('to-box',
  147. _m('To'),
  148. $recipients,
  149. null,
  150. false,
  151. $default);
  152. $this->out->element('textarea',
  153. ['class' => 'notice_data-text',
  154. 'cols' => 35,
  155. 'rows' => 4,
  156. 'name' => 'content'],
  157. $this->content);
  158. $contentLimit = MessageModel::maxContent();
  159. if ($contentLimit > 0) {
  160. $this->out->element('span',
  161. ['class' => 'count'],
  162. $contentLimit);
  163. }
  164. }
  165. /**
  166. * Action elements.
  167. *
  168. * @return void
  169. */
  170. function formActions()
  171. {
  172. $this->out->element('input',
  173. ['id' => 'notice_action-submit',
  174. 'class' => 'submit',
  175. 'name' => 'message_send',
  176. 'type' => 'submit',
  177. // TRANS: Button text for sending a direct notice.
  178. 'value' => _m('Send button for direct notice', 'Send')]);
  179. }
  180. /**
  181. * Show the form.
  182. *
  183. * @return void
  184. */
  185. function show()
  186. {
  187. $this->elementStart('div', 'input_forms');
  188. $this->elementStart('div',
  189. ['id' => 'input_form_direct',
  190. 'class' => 'input_form current nonav']);
  191. parent::show();
  192. $this->elementEnd('div');
  193. $this->elementEnd('div');
  194. }
  195. }