messageform.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. protected $to = null;
  39. protected $content = null;
  40. /**
  41. * Constructor.
  42. *
  43. * @param HTMLOutputter $out output channel
  44. * @param array|null $formOpts
  45. */
  46. function __construct(HTMLOutputter $out = null, ?array $formOpts = null)
  47. {
  48. parent::__construct($out);
  49. if (isset($formOpts['to'])) {
  50. $this->to = $formOpts['to'];
  51. }
  52. $this->content = $formOpts['content'] ?? '';
  53. }
  54. /**
  55. * ID of the form.
  56. *
  57. * @return string ID of the form
  58. */
  59. function id(): string
  60. {
  61. return 'form_notice-direct';
  62. }
  63. /**
  64. * Class of the form.
  65. *
  66. * @return string class of the form
  67. */
  68. function formClass(): string
  69. {
  70. return 'form_notice ajax-notice';
  71. }
  72. /**
  73. * Action of the form.
  74. *
  75. * @return string URL of the action
  76. */
  77. function action(): string
  78. {
  79. return common_local_url('newmessage');
  80. }
  81. /**
  82. * Legend of the Form.
  83. *
  84. * @return void
  85. */
  86. function formLegend()
  87. {
  88. // TRANS: Form legend for direct notice.
  89. $this->out->element('legend', null, _m('Send a direct notice'));
  90. }
  91. /**
  92. * Data elements.
  93. *
  94. * @return void
  95. */
  96. function formData()
  97. {
  98. $user = common_current_user();
  99. $recipients = [];
  100. $default = 'default';
  101. $subs = $user->getSubscribed();
  102. $n_subs = 0;
  103. // Add local-subscriptions
  104. while ($subs->fetch()) {
  105. $n_subs++;
  106. if ($subs->isLocal()) {
  107. $value = 'profile:'.$subs->getID();
  108. try {
  109. $recipients[$value] = substr($subs->getAcctUri(), 5) . " [{$subs->getBestName()}]";
  110. } catch (ProfileNoAcctUriException $e) {
  111. $recipients[$value] = "[?@?] " . $e->profile->getBestName();
  112. }
  113. }
  114. }
  115. if (sizeof($recipients) < $n_subs) {
  116. // some subscriptions aren't local and therefore weren't added,
  117. // worth checking if others want to add them
  118. Event::handle('FillDirectMessageRecipients', [$user, &$recipients]);
  119. }
  120. // if we came from a profile page, then lets make the message receiver visible
  121. if (!is_null($this->to)) {
  122. if (isset($recipients['profile:'.$this->to->getID()])) {
  123. $default = 'profile' . $this->to->getID();
  124. } else {
  125. try {
  126. if ($this->to->isLocal()) {
  127. $this->content = "@{$this->to->getNickname()} {$this->content}";
  128. } else {
  129. $this->content = substr($this->to->getAcctUri(), 5) . " {$this->content}";
  130. }
  131. } catch (ProfileNoAcctUriException $e) {
  132. // well, I'm no magician
  133. }
  134. }
  135. }
  136. if ($default === 'default') {
  137. // TRANS: Label entry in drop-down selection box in direct-message inbox/outbox.
  138. // TRANS: This is the default entry in the drop-down box, doubling as instructions
  139. // TRANS: and a brake against accidental submissions with the first user in the list.
  140. $recipients[$default] = empty($recipients) ? _m('No subscriptions') : _m('Select recipient:');
  141. }
  142. asort($recipients);
  143. // TRANS: Dropdown label in direct notice form.
  144. $this->out->dropdown('to-box',
  145. _m('To'),
  146. $recipients,
  147. null,
  148. false,
  149. $default);
  150. $this->out->element('textarea',
  151. ['class' => 'notice_data-text',
  152. 'cols' => 35,
  153. 'rows' => 4,
  154. 'name' => 'content'],
  155. $this->content);
  156. $contentLimit = MessageModel::maxContent();
  157. if ($contentLimit > 0) {
  158. $this->out->element('span',
  159. ['class' => 'count'],
  160. $contentLimit);
  161. }
  162. }
  163. /**
  164. * Action elements.
  165. *
  166. * @return void
  167. */
  168. function formActions()
  169. {
  170. $this->out->element('input',
  171. ['id' => 'notice_action-submit',
  172. 'class' => 'submit',
  173. 'name' => 'message_send',
  174. 'type' => 'submit',
  175. // TRANS: Button text for sending a direct notice.
  176. 'value' => _m('Send button for direct notice', 'Send')]);
  177. }
  178. /**
  179. * Show the form.
  180. *
  181. * @return void
  182. */
  183. function show()
  184. {
  185. $this->elementStart('div', 'input_forms');
  186. $this->elementStart('div',
  187. ['id' => 'input_form_direct',
  188. 'class' => 'input_form current nonav']);
  189. parent::show();
  190. $this->elementEnd('div');
  191. $this->elementEnd('div');
  192. }
  193. }