editgroup.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Edit an existing group
  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 Group
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @copyright 2008-2011 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET') && !defined('LACONICA') && !defined('GNUSOCIAL')) {
  32. exit(1);
  33. }
  34. /**
  35. * Add a new group
  36. *
  37. * This is the form for adding a new group
  38. *
  39. * @category Group
  40. * @package StatusNet
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Zach Copley <zach@status.net>
  43. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. */
  47. class EditgroupAction extends GroupAction
  48. {
  49. public $message = null;
  50. public $success = null;
  51. protected $canPost = true;
  52. public function title()
  53. {
  54. // TRANS: Title for form to edit a group. %s is a group nickname.
  55. return sprintf(_('Edit %s group'), $this->group->nickname);
  56. }
  57. public function showContent()
  58. {
  59. $form = new GroupEditForm($this, $this->group);
  60. $form->show();
  61. }
  62. public function showPageNoticeBlock()
  63. {
  64. parent::showPageNoticeBlock();
  65. if ($this->message) {
  66. $this->element(
  67. 'p',
  68. ($this->success) ? 'success' : 'error',
  69. $this->message
  70. );
  71. } else {
  72. $this->element(
  73. 'p',
  74. 'instructions',
  75. // TRANS: Form instructions for group edit form.
  76. _('Use this form to edit the group.')
  77. );
  78. }
  79. }
  80. public function showScripts()
  81. {
  82. parent::showScripts();
  83. $this->autofocus('fullname');
  84. }
  85. /**
  86. * Prepare to run
  87. * @param array $args
  88. * @return bool
  89. * @throws ClientException
  90. * @throws NicknameException
  91. */
  92. protected function prepare(array $args = [])
  93. {
  94. parent::prepare($args);
  95. if (!common_logged_in()) {
  96. // TRANS: Client error displayed trying to edit a group while not logged in.
  97. $this->clientError(_('You must be logged in to create a group.'));
  98. }
  99. $nickname_arg = $this->trimmed('nickname');
  100. $nickname = common_canonical_nickname($nickname_arg);
  101. // Permanent redirect on non-canonical nickname
  102. if ($nickname_arg != $nickname) {
  103. $args = ['nickname' => $nickname];
  104. common_redirect(common_local_url('editgroup', $args), 301);
  105. }
  106. if (!$nickname) {
  107. // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
  108. $this->clientError(_('No nickname.'), 404);
  109. }
  110. $groupid = $this->trimmed('groupid');
  111. if ($groupid) {
  112. $this->group = User_group::getKV('id', $groupid);
  113. } else {
  114. $local = Local_group::getKV('nickname', $nickname);
  115. if ($local) {
  116. $this->group = User_group::getKV('id', $local->group_id);
  117. }
  118. }
  119. if (!$this->group) {
  120. // TRANS: Client error displayed trying to edit a non-existing group.
  121. $this->clientError(_('No such group.'), 404);
  122. }
  123. $cur = common_current_user();
  124. if (!$cur->isAdmin($this->group)) {
  125. // TRANS: Client error displayed trying to edit a group while not being a group admin.
  126. $this->clientError(_('You must be an admin to edit the group.'), 403);
  127. }
  128. return true;
  129. }
  130. protected function handlePost()
  131. {
  132. parent::handlePost();
  133. $cur = common_current_user();
  134. if (!$cur->isAdmin($this->group)) {
  135. // TRANS: Client error displayed trying to edit a group while not being a group admin.
  136. $this->clientError(_('You must be an admin to edit the group.'), 403);
  137. }
  138. if (Event::handle('StartGroupSaveForm', [$this])) {
  139. // $nickname will only be set if this changenick value is true.
  140. $nickname = null;
  141. if (common_config('profile', 'changenick') == true) {
  142. try {
  143. $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
  144. } catch (NicknameTakenException $e) {
  145. // Abort only if the nickname is occupied by _another_ group
  146. if ($e->profile->id != $this->group->profile_id) {
  147. $this->setMessage($e->getMessage(), true);
  148. return;
  149. }
  150. $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
  151. } catch (NicknameException $e) {
  152. $this->setMessage($e->getMessage(), true);
  153. return;
  154. }
  155. }
  156. $fullname = $this->trimmed('fullname');
  157. $homepage = $this->trimmed('homepage');
  158. $description = $this->trimmed('description');
  159. $location = $this->trimmed('location');
  160. $aliasstring = $this->trimmed('aliases');
  161. $private = $this->boolean('private');
  162. if ($private) {
  163. $force_scope = 1;
  164. $join_policy = User_group::JOIN_POLICY_MODERATE;
  165. } else {
  166. $force_scope = 0;
  167. $join_policy = User_group::JOIN_POLICY_OPEN;
  168. }
  169. if (!is_null($homepage) && (strlen($homepage) > 0) &&
  170. !common_valid_http_url($homepage)) {
  171. // TRANS: Group edit form validation error.
  172. $this->setMessage(_('Homepage is not a valid URL.'), true);
  173. return;
  174. } elseif (!is_null($fullname) && mb_strlen($fullname) > 255) {
  175. // TRANS: Group edit form validation error.
  176. $this->setMessage(_('Full name is too long (maximum 255 characters).'), true);
  177. return;
  178. } elseif (User_group::descriptionTooLong($description)) {
  179. $this->setMessage(sprintf(
  180. // TRANS: Group edit form validation error.
  181. _m(
  182. 'Description is too long (maximum %d character).',
  183. 'Description is too long (maximum %d characters).',
  184. User_group::maxDescription()
  185. ),
  186. User_group::maxDescription()
  187. ), true);
  188. return;
  189. } elseif (!is_null($location) && mb_strlen($location) > 255) {
  190. // TRANS: Group edit form validation error.
  191. $this->setMessage(_('Location is too long (maximum 255 characters).'), true);
  192. return;
  193. }
  194. if (!empty($aliasstring)) {
  195. $aliases = array_map(
  196. ['Nickname', 'normalize'],
  197. array_unique(preg_split('/[\s,]+/', $aliasstring))
  198. );
  199. } else {
  200. $aliases = [];
  201. }
  202. if (count($aliases) > common_config('group', 'maxaliases')) {
  203. // TRANS: Group edit form validation error.
  204. // TRANS: %d is the maximum number of allowed aliases.
  205. $this->setMessage(sprintf(
  206. _m(
  207. 'Too many aliases! Maximum %d allowed.',
  208. 'Too many aliases! Maximum %d allowed.',
  209. common_config('group', 'maxaliases')
  210. ),
  211. common_config('group', 'maxaliases')
  212. ), true);
  213. return;
  214. }
  215. $this->group->query('BEGIN');
  216. $orig = clone($this->group);
  217. if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
  218. assert(Nickname::normalize($nickname) === $nickname);
  219. common_debug("Changing group nickname from '{$this->group->nickname}' to '{$nickname}'.");
  220. $this->group->nickname = $nickname;
  221. $this->group->mainpage = common_local_url('showgroup', ['nickname' => $this->group->nickname]);
  222. }
  223. $this->group->fullname = $fullname;
  224. $this->group->homepage = $homepage;
  225. $this->group->description = $description;
  226. $this->group->location = $location;
  227. $this->group->join_policy = $join_policy;
  228. $this->group->force_scope = $force_scope;
  229. $result = $this->group->update($orig);
  230. if ($result === false) {
  231. common_log_db_error($this->group, 'UPDATE', __FILE__);
  232. // TRANS: Server error displayed when editing a group fails.
  233. $this->serverError(_('Could not update group.'));
  234. }
  235. $result = $this->group->setAliases($aliases);
  236. if (!$result) {
  237. // TRANS: Server error displayed when group aliases could not be added.
  238. $this->serverError(_('Could not create aliases.'));
  239. }
  240. $this->group->query('COMMIT');
  241. Event::handle('EndGroupSaveForm', [$this]);
  242. if ($this->group->nickname != $orig->nickname) {
  243. common_redirect(common_local_url('editgroup', ['nickname' => $this->group->nickname]), 303);
  244. }
  245. }
  246. // TRANS: Group edit form success message.
  247. $this->setMessage(_('Options saved.'));
  248. }
  249. public function setMessage($msg, $error = false)
  250. {
  251. $this->message = $msg;
  252. $this->success = !$error;
  253. }
  254. }