newgroupmessage.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Action for adding a new group message
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Cache
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Action for adding a new group message
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class NewgroupmessageAction extends Action
  46. {
  47. var $group;
  48. var $user;
  49. var $text;
  50. /**
  51. * For initializing members of the class.
  52. *
  53. * @param array $argarray misc. arguments
  54. *
  55. * @return boolean true
  56. */
  57. function prepare($argarray)
  58. {
  59. parent::prepare($argarray);
  60. $this->user = common_current_user();
  61. if (empty($this->user)) {
  62. // TRANS: Client exception thrown when trying to send a private group message while not logged in.
  63. throw new ClientException(_m('Must be logged in.'), 403);
  64. }
  65. if (!$this->user->hasRight(Right::NEWMESSAGE)) {
  66. // TRANS: Exception thrown when user %s is not allowed to send a private group message.
  67. throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'),
  68. $this->user->nickname));
  69. }
  70. $nicknameArg = $this->trimmed('nickname');
  71. $nickname = common_canonical_nickname($nicknameArg);
  72. if ($nickname != $nicknameArg) {
  73. $url = common_local_url('newgroupmessage', array('nickname' => $nickname));
  74. common_redirect($url, 301);
  75. }
  76. $localGroup = Local_group::getKV('nickname', $nickname);
  77. if (empty($localGroup)) {
  78. // TRANS: Client exception thrown when trying to send a private group message to a non-existing group.
  79. throw new ClientException(_m('No such group.'), 404);
  80. }
  81. $this->group = User_group::getKV('id', $localGroup->group_id);
  82. if (empty($this->group)) {
  83. // TRANS: Client exception thrown when trying to send a private group message to a non-existing group.
  84. throw new ClientException(_m('No such group.'), 404);
  85. }
  86. // This throws an exception on error
  87. Group_privacy_settings::ensurePost($this->user, $this->group);
  88. // If we're posted to, check session token and get text
  89. if ($this->isPost()) {
  90. $this->checkSessionToken();
  91. $this->text = $this->trimmed('content');
  92. }
  93. return true;
  94. }
  95. /**
  96. * Handler method
  97. *
  98. * @param array $argarray is ignored since it's now passed in in prepare()
  99. *
  100. * @return void
  101. */
  102. function handle($argarray=null)
  103. {
  104. if ($this->isPost()) {
  105. $this->sendNewMessage();
  106. } else {
  107. $this->showPage();
  108. }
  109. }
  110. function showNoticeForm()
  111. {
  112. $form = new GroupMessageForm($this, $this->group);
  113. $form->show();
  114. }
  115. function sendNewMessage()
  116. {
  117. $gm = Group_message::send($this->user, $this->group, $this->text);
  118. if ($this->boolean('ajax')) {
  119. $this->startHTML('text/xml;charset=utf-8');
  120. $this->elementStart('head');
  121. // TRANS: Title after sending a private group message.
  122. $this->element('title', null, _m('Message sent'));
  123. $this->elementEnd('head');
  124. $this->elementStart('body');
  125. $this->element('p',
  126. array('id' => 'command_result'),
  127. // TRANS: Succes text after sending a direct message to group %s.
  128. sprintf(_m('Direct message to %s sent.'),
  129. $this->group->nickname));
  130. $this->elementEnd('body');
  131. $this->endHTML();
  132. } else {
  133. common_redirect($gm->url, 303);
  134. }
  135. }
  136. function title()
  137. {
  138. // TRANS: Title of form for new private group message.
  139. return sprintf(_m('New message to group %s'), $this->group->nickname);
  140. }
  141. }