deletegroup.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Delete a 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 Brion Vibber <brion@status.net>
  26. * @copyright 2008-2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. /**
  34. * Delete a group
  35. *
  36. * This is the action for deleting a group.
  37. *
  38. * @category Group
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Brion Vibber <brion@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. * @fixme merge more of this code with related variants
  45. */
  46. class DeletegroupAction extends RedirectingAction
  47. {
  48. var $group = null;
  49. /**
  50. * Prepare to run
  51. *
  52. * @fixme merge common setup code with other group actions
  53. * @fixme allow group admins to delete their own groups
  54. */
  55. function prepare(array $args = array())
  56. {
  57. parent::prepare($args);
  58. if (!common_logged_in()) {
  59. // TRANS: Client error when trying to delete group while not logged in.
  60. $this->clientError(_('You must be logged in to delete a group.'));
  61. }
  62. $nickname_arg = $this->trimmed('nickname');
  63. $id = intval($this->arg('id'));
  64. if ($id) {
  65. $this->group = User_group::getKV('id', $id);
  66. } else if ($nickname_arg) {
  67. $nickname = common_canonical_nickname($nickname_arg);
  68. // Permanent redirect on non-canonical nickname
  69. if ($nickname_arg != $nickname) {
  70. $args = array('nickname' => $nickname);
  71. common_redirect(common_local_url('leavegroup', $args), 301);
  72. }
  73. $local = Local_group::getKV('nickname', $nickname);
  74. if (!$local) {
  75. // TRANS: Client error when trying to delete a non-local group.
  76. $this->clientError(_('No such group.'), 404);
  77. }
  78. $this->group = User_group::getKV('id', $local->group_id);
  79. } else {
  80. // TRANS: Client error when trying to delete a group without providing a nickname or ID for the group.
  81. $this->clientError(_('No nickname or ID.'), 404);
  82. }
  83. if (!$this->group) {
  84. // TRANS: Client error when trying to delete a non-existing group.
  85. $this->clientError(_('No such group.'), 404);
  86. }
  87. $cur = common_current_user();
  88. if (!$cur->hasRight(Right::DELETEGROUP)) {
  89. // TRANS: Client error when trying to delete a group without having the rights to delete it.
  90. $this->clientError(_('You are not allowed to delete this group.'), 403);
  91. }
  92. return true;
  93. }
  94. /**
  95. * Handle the request
  96. *
  97. * On POST, delete the group.
  98. *
  99. * @param array $args unused
  100. *
  101. * @return void
  102. */
  103. function handle()
  104. {
  105. parent::handle();
  106. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  107. if ($this->arg('no')) {
  108. $this->returnToPrevious();
  109. return;
  110. } elseif ($this->arg('yes')) {
  111. $this->handlePost();
  112. return;
  113. }
  114. }
  115. $this->showPage();
  116. }
  117. function handlePost()
  118. {
  119. $cur = common_current_user();
  120. try {
  121. if (Event::handle('StartDeleteGroup', array($this->group))) {
  122. $this->group->delete();
  123. Event::handle('EndDeleteGroup', array($this->group));
  124. }
  125. } catch (Exception $e) {
  126. // TRANS: Server error displayed if a group could not be deleted.
  127. // TRANS: %s is the name of the group that could not be deleted.
  128. $this->serverError(sprintf(_('Could not delete group %s.'),
  129. $this->group->nickname));
  130. }
  131. if ($this->boolean('ajax')) {
  132. $this->startHTML('text/xml;charset=utf-8');
  133. $this->elementStart('head');
  134. // TRANS: Message given after deleting a group.
  135. // TRANS: %s is the deleted group's name.
  136. $this->element('title', null, sprintf(_('Deleted group %s'),
  137. $this->group->nickname));
  138. $this->elementEnd('head');
  139. $this->elementStart('body');
  140. // @fixme add a sensible AJAX response form!
  141. $this->elementEnd('body');
  142. $this->endHTML();
  143. } else {
  144. // @fixme if we could direct to the page on which this group
  145. // would have shown... that would be awesome
  146. common_redirect(common_local_url('groups'), 303);
  147. }
  148. }
  149. function title() {
  150. // TRANS: Title of delete group page.
  151. return _('Delete group');
  152. }
  153. function showContent() {
  154. $this->areYouSureForm();
  155. $block = new GroupProfileBlock($this, $this->group);
  156. $block->show();
  157. }
  158. /**
  159. * Confirm with user.
  160. * Ripped from DeleteuserAction
  161. *
  162. * Shows a confirmation form.
  163. *
  164. * @fixme refactor common code for things like this
  165. * @return void
  166. */
  167. function areYouSureForm()
  168. {
  169. $id = $this->group->id;
  170. $this->elementStart('form', array('id' => 'deletegroup-' . $id,
  171. 'method' => 'post',
  172. 'class' => 'form_settings form_entity_block',
  173. 'action' => common_local_url('deletegroup', array('id' => $this->group->id))));
  174. $this->elementStart('fieldset');
  175. $this->hidden('token', common_session_token());
  176. // TRANS: Form legend for deleting a group.
  177. $this->element('legend', _('Delete group'));
  178. if (Event::handle('StartDeleteGroupForm', array($this, $this->group))) {
  179. $this->element('p', null,
  180. // TRANS: Warning in form for deleleting a group.
  181. _('Are you sure you want to delete this group? '.
  182. 'This will clear all data about the group from the '.
  183. 'database, without a backup. ' .
  184. 'Public posts to this group will still appear in ' .
  185. 'individual timelines.'));
  186. foreach ($this->args as $k => $v) {
  187. if (substr($k, 0, 9) == 'returnto-') {
  188. $this->hidden($k, $v);
  189. }
  190. }
  191. Event::handle('EndDeleteGroupForm', array($this, $this->group));
  192. }
  193. $this->submit('form_action-no',
  194. // TRANS: Button label on the delete group form.
  195. _m('BUTTON','No'),
  196. 'submit form_action-primary',
  197. 'no',
  198. // TRANS: Submit button title for 'No' when deleting a group.
  199. _('Do not delete this group.'));
  200. $this->submit('form_action-yes',
  201. // TRANS: Button label on the delete group form.
  202. _m('BUTTON','Yes'),
  203. 'submit form_action-secondary',
  204. 'yes',
  205. // TRANS: Submit button title for 'Yes' when deleting a group.
  206. _('Delete this group.'));
  207. $this->elementEnd('fieldset');
  208. $this->elementEnd('form');
  209. }
  210. }