editgroup.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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')) {
  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. * @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 EditgroupAction extends GroupAction
  47. {
  48. var $msg;
  49. function title()
  50. {
  51. // TRANS: Title for form to edit a group. %s is a group nickname.
  52. return sprintf(_('Edit %s group'), $this->group->nickname);
  53. }
  54. /**
  55. * Prepare to run
  56. */
  57. protected function prepare(array $args=array())
  58. {
  59. parent::prepare($args);
  60. if (!common_logged_in()) {
  61. // TRANS: Client error displayed trying to edit a group while not logged in.
  62. $this->clientError(_('You must be logged in to create a group.'));
  63. }
  64. $nickname_arg = $this->trimmed('nickname');
  65. $nickname = common_canonical_nickname($nickname_arg);
  66. // Permanent redirect on non-canonical nickname
  67. if ($nickname_arg != $nickname) {
  68. $args = array('nickname' => $nickname);
  69. common_redirect(common_local_url('editgroup', $args), 301);
  70. }
  71. if (!$nickname) {
  72. // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
  73. $this->clientError(_('No nickname.'), 404);
  74. }
  75. $groupid = $this->trimmed('groupid');
  76. if ($groupid) {
  77. $this->group = User_group::getKV('id', $groupid);
  78. } else {
  79. $local = Local_group::getKV('nickname', $nickname);
  80. if ($local) {
  81. $this->group = User_group::getKV('id', $local->group_id);
  82. }
  83. }
  84. if (!$this->group) {
  85. // TRANS: Client error displayed trying to edit a non-existing group.
  86. $this->clientError(_('No such group.'), 404);
  87. }
  88. $cur = common_current_user();
  89. if (!$cur->isAdmin($this->group)) {
  90. // TRANS: Client error displayed trying to edit a group while not being a group admin.
  91. $this->clientError(_('You must be an admin to edit the group.'), 403);
  92. }
  93. return true;
  94. }
  95. /**
  96. * Handle the request
  97. *
  98. * On GET, show the form. On POST, try to save the group.
  99. *
  100. * @return void
  101. */
  102. protected function handle()
  103. {
  104. parent::handle();
  105. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  106. $this->trySave();
  107. } else {
  108. $this->showForm();
  109. }
  110. }
  111. function showForm($msg=null)
  112. {
  113. $this->msg = $msg;
  114. $this->showPage();
  115. }
  116. function showContent()
  117. {
  118. $form = new GroupEditForm($this, $this->group);
  119. $form->show();
  120. }
  121. function showPageNotice()
  122. {
  123. if ($this->msg) {
  124. $this->element('p', 'error', $this->msg);
  125. } else {
  126. $this->element('p', 'instructions',
  127. // TRANS: Form instructions for group edit form.
  128. _('Use this form to edit the group.'));
  129. }
  130. }
  131. function showScripts()
  132. {
  133. parent::showScripts();
  134. $this->autofocus('fullname');
  135. }
  136. function trySave()
  137. {
  138. $cur = common_current_user();
  139. if (!$cur->isAdmin($this->group)) {
  140. // TRANS: Client error displayed trying to edit a group while not being a group admin.
  141. $this->clientError(_('You must be an admin to edit the group.'), 403);
  142. }
  143. if (Event::handle('StartGroupSaveForm', array($this))) {
  144. // $nickname will only be set if this changenick value is true.
  145. if (common_config('profile', 'changenick') == true) {
  146. try {
  147. $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
  148. } catch (NicknameTakenException $e) {
  149. // Abort only if the nickname is occupied by _another_ group
  150. if ($e->profile->id != $this->group->profile_id) {
  151. $this->showForm($e->getMessage());
  152. return;
  153. }
  154. $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
  155. } catch (NicknameException $e) {
  156. $this->showForm($e->getMessage());
  157. return;
  158. }
  159. }
  160. $fullname = $this->trimmed('fullname');
  161. $homepage = $this->trimmed('homepage');
  162. $description = $this->trimmed('description');
  163. $location = $this->trimmed('location');
  164. $aliasstring = $this->trimmed('aliases');
  165. $private = $this->boolean('private');
  166. if ($private) {
  167. $force_scope = 1;
  168. $join_policy = User_group::JOIN_POLICY_MODERATE;
  169. } else {
  170. $force_scope = 0;
  171. $join_policy = User_group::JOIN_POLICY_OPEN;
  172. }
  173. if (!is_null($homepage) && (strlen($homepage) > 0) &&
  174. !common_valid_http_url($homepage)) {
  175. // TRANS: Group edit form validation error.
  176. $this->showForm(_('Homepage is not a valid URL.'));
  177. return;
  178. } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
  179. // TRANS: Group edit form validation error.
  180. $this->showForm(_('Full name is too long (maximum 255 characters).'));
  181. return;
  182. } else if (User_group::descriptionTooLong($description)) {
  183. $this->showForm(sprintf(
  184. // TRANS: Group edit form validation error.
  185. _m('Description is too long (maximum %d character).',
  186. 'Description is too long (maximum %d characters).',
  187. User_group::maxDescription()),
  188. User_group::maxDescription()));
  189. return;
  190. } else if (!is_null($location) && mb_strlen($location) > 255) {
  191. // TRANS: Group edit form validation error.
  192. $this->showForm(_('Location is too long (maximum 255 characters).'));
  193. return;
  194. }
  195. if (!empty($aliasstring)) {
  196. $aliases = array_map(array('Nickname', 'normalize'),
  197. array_unique(preg_split('/[\s,]+/', $aliasstring)));
  198. } else {
  199. $aliases = array();
  200. }
  201. if (count($aliases) > common_config('group', 'maxaliases')) {
  202. // TRANS: Group edit form validation error.
  203. // TRANS: %d is the maximum number of allowed aliases.
  204. $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
  205. 'Too many aliases! Maximum %d allowed.',
  206. common_config('group', 'maxaliases')),
  207. common_config('group', 'maxaliases')));
  208. return;
  209. }
  210. $this->group->query('BEGIN');
  211. $orig = clone($this->group);
  212. if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
  213. assert(Nickname::normalize($nickname)===$nickname);
  214. common_debug("Changing group nickname from '{$profile->nickname}' to '{$nickname}'.");
  215. $this->group->nickname = $nickname;
  216. $this->group->mainpage = common_local_url('showgroup', array('nickname' => $this->group->nickname));
  217. }
  218. $this->group->fullname = $fullname;
  219. $this->group->homepage = $homepage;
  220. $this->group->description = $description;
  221. $this->group->location = $location;
  222. $this->group->join_policy = $join_policy;
  223. $this->group->force_scope = $force_scope;
  224. $result = $this->group->update($orig);
  225. if ($result === false) {
  226. common_log_db_error($this->group, 'UPDATE', __FILE__);
  227. // TRANS: Server error displayed when editing a group fails.
  228. $this->serverError(_('Could not update group.'));
  229. }
  230. $result = $this->group->setAliases($aliases);
  231. if (!$result) {
  232. // TRANS: Server error displayed when group aliases could not be added.
  233. $this->serverError(_('Could not create aliases.'));
  234. }
  235. $this->group->query('COMMIT');
  236. Event::handle('EndGroupSaveForm', array($this));
  237. }
  238. if ($this->group->nickname != $orig->nickname) {
  239. common_redirect(common_local_url('editgroup', array('nickname' => $this->group->nickname)), 303);
  240. } else {
  241. // TRANS: Group edit form success message.
  242. $this->showForm(_('Options saved.'));
  243. }
  244. }
  245. }