The official, canonical postActiv repository. http://www.postactiv.com

cancelgroup.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // !TODO: I WRITE HTML, REFACTOR FOR SMARTY
  3. /* ============================================================================
  4. * Title: CancelGroup
  5. * Leave a group
  6. *
  7. * postActiv:
  8. * the micro-blogging software
  9. *
  10. * Copyright:
  11. * Copyright (C) 2016-2018, Maiyannah Bishop
  12. *
  13. * Derived from code copyright various sources:
  14. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  15. * o StatusNet (C) 2008-2012, StatusNet, Inc
  16. * ----------------------------------------------------------------------------
  17. * License:
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as published by
  20. * the Free Software Foundation, either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. * <https://www.gnu.org/licenses/agpl.html>
  32. * ----------------------------------------------------------------------------
  33. * About:
  34. * Leave a group
  35. *
  36. * This is the action for leaving a group. It works more or less like the subscribe action
  37. * for users.
  38. *
  39. * PHP version:
  40. * Tested with PHP 7.0
  41. * ----------------------------------------------------------------------------
  42. * File Authors:
  43. * o Brion Vibber <brian@pobox.com>
  44. * o Siebrand Mazeland <s.mazeland@xs4all.nl>
  45. * o Mikael Nordfeldth <mmn@hethane.se>
  46. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  47. *
  48. * Web:
  49. * o postActiv <http://www.postactiv.com>
  50. * o GNU social <https://www.gnu.org/s/social/>
  51. * ============================================================================
  52. */
  53. // This file is formatted so that it provides useful documentation output in
  54. // NaturalDocs. Please be considerate of this before changing formatting.
  55. if (!defined('POSTACTIV')) { exit(1); }
  56. class CancelgroupAction extends Action
  57. {
  58. var $group = null;
  59. /**
  60. * Prepare to run
  61. */
  62. function prepare(array $args = array())
  63. {
  64. parent::prepare($args);
  65. if (!common_logged_in()) {
  66. // TRANS: Client error displayed when trying to leave a group while not logged in.
  67. $this->clientError(_('You must be logged in to leave a group.'));
  68. }
  69. $nickname_arg = $this->trimmed('nickname');
  70. $id = intval($this->arg('id'));
  71. if ($id) {
  72. $this->group = User_group::getKV('id', $id);
  73. } else if ($nickname_arg) {
  74. $nickname = common_canonical_nickname($nickname_arg);
  75. // Permanent redirect on non-canonical nickname
  76. if ($nickname_arg != $nickname) {
  77. $args = array('nickname' => $nickname);
  78. common_redirect(common_local_url('leavegroup', $args), 301);
  79. }
  80. $local = Local_group::getKV('nickname', $nickname);
  81. if (!$local) {
  82. // TRANS: Client error displayed when trying to leave a non-local group.
  83. $this->clientError(_('No such group.'), 404);
  84. }
  85. $this->group = User_group::getKV('id', $local->group_id);
  86. } else {
  87. // TRANS: Client error displayed when trying to leave a group without providing a group name or group ID.
  88. $this->clientError(_('No nickname or ID.'), 404);
  89. }
  90. if (!$this->group) {
  91. // TRANS: Client error displayed when trying to leave a non-existing group.
  92. $this->clientError(_('No such group.'), 404);
  93. }
  94. $cur = common_current_user();
  95. if (empty($cur)) {
  96. // TRANS: Client error displayed when trying to leave a group while not logged in.
  97. $this->clientError(_('Must be logged in.'), 403);
  98. }
  99. if ($this->arg('profile_id')) {
  100. if ($cur->isAdmin($this->group)) {
  101. $this->profile = Profile::getKV('id', $this->arg('profile_id'));
  102. } else {
  103. // TRANS: Client error displayed when trying to approve or cancel a group join request without
  104. // TRANS: being a group administrator.
  105. $this->clientError(_('Only group admin can approve or cancel join requests.'), 403);
  106. }
  107. } else {
  108. $this->profile = $cur->getProfile();
  109. }
  110. $this->request = Group_join_queue::pkeyGet(array('profile_id' => $this->profile->id,
  111. 'group_id' => $this->group->id));
  112. if (empty($this->request)) {
  113. // TRANS: Client error displayed when trying to approve a non-existing group join request.
  114. // TRANS: %s is a user nickname.
  115. $this->clientError(sprintf(_('%s is not in the moderation queue for this group.'), $this->profile->nickname), 403);
  116. }
  117. return true;
  118. }
  119. /**
  120. * Handle the request
  121. *
  122. * On POST, add the current user to the group
  123. *
  124. * @return void
  125. */
  126. function handle()
  127. {
  128. parent::handle();
  129. try {
  130. $this->request->abort();
  131. } catch (Exception $e) {
  132. common_log(LOG_ERR, "Exception canceling group sub: " . $e->getMessage());
  133. // TRANS: Server error displayed when cancelling a queued group join request fails.
  134. // TRANS: %1$s is the leaving user's nickname, $2$s is the group nickname for which the leave failed.
  135. $this->serverError(sprintf(_('Could not cancel request for user %1$s to join group %2$s.'),
  136. $this->profile->nickname, $this->group->nickname));
  137. return;
  138. }
  139. if ($this->boolean('ajax')) {
  140. $this->startHTML('text/xml;charset=utf-8');
  141. $this->elementStart('head');
  142. // TRANS: Title for leave group page after leaving.
  143. // TRANS: %s$s is the leaving user's name, %2$s is the group name.
  144. $this->element('title', null, sprintf(_m('TITLE','%1$s left group %2$s'),
  145. $this->profile->nickname,
  146. $this->group->nickname));
  147. $this->elementEnd('head');
  148. $this->elementStart('body');
  149. $jf = new JoinForm($this, $this->group);
  150. $jf->show();
  151. $this->elementEnd('body');
  152. $this->endHTML();
  153. } else {
  154. common_redirect(common_local_url('groupmembers', array('nickname' => $this->group->nickname)), 303);
  155. }
  156. }
  157. }
  158. // END OF FILE
  159. // ============================================================================
  160. ?>