newnotice.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Handler for posting new notices
  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 Personal
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @author Sarven Capadisli <csarven@status.net>
  27. * @copyright 2008-2009 StatusNet, Inc.
  28. * @copyright 2013 Free Software Foundation, Inc.
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://status.net/
  31. */
  32. if (!defined('STATUSNET')) {
  33. exit(1);
  34. }
  35. /**
  36. * Action for posting new notices
  37. *
  38. * @category Personal
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Zach Copley <zach@status.net>
  42. * @author Sarven Capadisli <csarven@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. */
  46. class NewnoticeAction extends FormAction
  47. {
  48. protected $form = 'Notice';
  49. /**
  50. * Title of the page
  51. *
  52. * Note that this usually doesn't get called unless something went wrong
  53. *
  54. * @return string page title
  55. */
  56. function title()
  57. {
  58. if ($this->getInfo() && $this->stored instanceof Notice) {
  59. // TRANS: Page title after sending a notice.
  60. return _('Notice posted');
  61. }
  62. // TRANS: Page title for sending a new notice.
  63. return _m('TITLE','New notice');
  64. }
  65. protected function doPreparation()
  66. {
  67. foreach(array('inreplyto') as $opt) {
  68. if ($this->trimmed($opt)) {
  69. $this->formOpts[$opt] = $this->trimmed($opt);
  70. }
  71. }
  72. // Backwards compatibility for "share this" widget things.
  73. // If no 'content', use 'status_textarea'
  74. $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
  75. }
  76. /**
  77. * This handlePost saves a new notice, based on arguments
  78. *
  79. * If successful, will show the notice, or return an Ajax-y result.
  80. * If not, it will show an error message -- possibly Ajax-y.
  81. *
  82. * Also, if the notice input looks like a command, it will run the
  83. * command and show the results -- again, possibly ajaxy.
  84. *
  85. * @return void
  86. */
  87. protected function handlePost()
  88. {
  89. parent::handlePost();
  90. assert($this->scoped); // XXX: maybe an error instead...
  91. $user = $this->scoped->getUser();
  92. $content = $this->trimmed('status_textarea');
  93. $options = array();
  94. Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
  95. if (!$content) {
  96. // TRANS: Client error displayed trying to send a notice without content.
  97. $this->clientError(_('No content!'));
  98. }
  99. $inter = new CommandInterpreter();
  100. $cmd = $inter->handle_command($user, $content);
  101. if ($cmd) {
  102. if (StatusNet::isAjax()) {
  103. $cmd->execute(new AjaxWebChannel($this));
  104. } else {
  105. $cmd->execute(new WebChannel($this));
  106. }
  107. return;
  108. }
  109. $content_shortened = $user->shortenLinks($content);
  110. if (Notice::contentTooLong($content_shortened)) {
  111. // TRANS: Client error displayed when the parameter "status" is missing.
  112. // TRANS: %d is the maximum number of character for a notice.
  113. $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
  114. 'That\'s too long. Maximum notice size is %d characters.',
  115. Notice::maxContent()),
  116. Notice::maxContent()));
  117. }
  118. $replyto = intval($this->trimmed('inreplyto'));
  119. if ($replyto) {
  120. $options['reply_to'] = $replyto;
  121. }
  122. $upload = null;
  123. try {
  124. // throws exception on failure
  125. $upload = MediaFile::fromUpload('attach', $this->scoped);
  126. if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
  127. $content_shortened .= ' ' . $upload->shortUrl();
  128. }
  129. Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
  130. if (Notice::contentTooLong($content_shortened)) {
  131. $upload->delete();
  132. // TRANS: Client error displayed exceeding the maximum notice length.
  133. // TRANS: %d is the maximum length for a notice.
  134. $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
  135. 'Maximum notice size is %d characters, including attachment URL.',
  136. Notice::maxContent()),
  137. Notice::maxContent()));
  138. }
  139. } catch (NoUploadedMediaException $e) {
  140. // simply no attached media to the new notice
  141. }
  142. if ($this->scoped->shareLocation()) {
  143. // use browser data if checked; otherwise profile data
  144. if ($this->arg('notice_data-geo')) {
  145. $locOptions = Notice::locationOptions($this->trimmed('lat'),
  146. $this->trimmed('lon'),
  147. $this->trimmed('location_id'),
  148. $this->trimmed('location_ns'),
  149. $this->scoped);
  150. } else {
  151. $locOptions = Notice::locationOptions(null,
  152. null,
  153. null,
  154. null,
  155. $this->scoped);
  156. }
  157. $options = array_merge($options, $locOptions);
  158. }
  159. $author_id = $this->scoped->id;
  160. $text = $content_shortened;
  161. // Does the heavy-lifting for getting "To:" information
  162. ToSelector::fillOptions($this, $options);
  163. if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
  164. $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
  165. if ($upload instanceof MediaFile) {
  166. $upload->attachToNotice($this->stored);
  167. }
  168. Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
  169. }
  170. Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
  171. if (!StatusNet::isAjax()) {
  172. $url = common_local_url('shownotice', array('notice' => $this->stored->id));
  173. common_redirect($url, 303);
  174. }
  175. return _('Saved the notice!');
  176. }
  177. protected function showContent()
  178. {
  179. if ($this->getInfo() && $this->stored instanceof Notice) {
  180. $this->showNotice($this->stored);
  181. } elseif (!$this->getError()) {
  182. parent::showContent();
  183. }
  184. }
  185. /**
  186. * Output a notice
  187. *
  188. * Used to generate the notice code for Ajax results.
  189. *
  190. * @param Notice $notice Notice that was saved
  191. *
  192. * @return void
  193. */
  194. function showNotice(Notice $notice)
  195. {
  196. $nli = new NoticeListItem($notice, $this);
  197. $nli->show();
  198. }
  199. public function showNoticeForm()
  200. {
  201. // pass
  202. }
  203. }