groupaction.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Base class for group actions
  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 Action
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2009-2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) {
  30. exit(1);
  31. }
  32. define('MEMBERS_PER_SECTION', 27);
  33. /**
  34. * Base class for group actions, similar to ProfileAction
  35. *
  36. * @category Action
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class GroupAction extends ShowstreamAction
  43. {
  44. protected $group;
  45. public function showProfileBlock()
  46. {
  47. $block = new GroupProfileBlock($this, $this->group);
  48. $block->show();
  49. }
  50. /**
  51. * Fill in the sidebar.
  52. *
  53. * @return void
  54. */
  55. public function showSections()
  56. {
  57. $this->showMembers();
  58. if ($this->scoped instanceof Profile && $this->scoped->isAdmin($this->group)) {
  59. $this->showPending();
  60. $this->showBlocked();
  61. }
  62. $this->showAdmins();
  63. }
  64. /**
  65. * Show mini-list of members
  66. *
  67. * @return void
  68. */
  69. public function showMembers()
  70. {
  71. $member = $this->group->getMembers(0, MEMBERS_PER_SECTION);
  72. if (!$member) {
  73. return;
  74. }
  75. $this->elementStart('div', array('id' => 'entity_members',
  76. 'class' => 'section'));
  77. if (Event::handle('StartShowGroupMembersMiniList', array($this))) {
  78. $this->elementStart('h2');
  79. $this->element(
  80. 'a',
  81. array('href' => common_local_url('groupmembers', array('nickname' =>
  82. $this->group->nickname))),
  83. // TRANS: Header for mini list of group members on a group page (h2).
  84. _('Members')
  85. );
  86. $this->text(' ');
  87. $this->text($this->group->getMemberCount());
  88. $this->elementEnd('h2');
  89. $gmml = new GroupMembersMiniList($member, $this);
  90. $cnt = $gmml->show();
  91. if ($cnt == 0) {
  92. // TRANS: Description for mini list of group members on a group page when the group has no members.
  93. $this->element('p', null, _('(None)'));
  94. }
  95. // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
  96. // for example http://identi.ca/group/statusnet. Broken?
  97. if ($cnt > MEMBERS_PER_SECTION) {
  98. $this->element(
  99. 'a',
  100. array('href' => common_local_url(
  101. 'groupmembers',
  102. array('nickname' => $this->group->nickname)
  103. )),
  104. // TRANS: Link to all group members from mini list of group members if group has more than n members.
  105. _('All members')
  106. );
  107. }
  108. Event::handle('EndShowGroupMembersMiniList', array($this));
  109. }
  110. $this->elementEnd('div');
  111. }
  112. public function showPending()
  113. {
  114. if ($this->group->join_policy != User_group::JOIN_POLICY_MODERATE) {
  115. return;
  116. }
  117. $pending = $this->group->getQueueCount();
  118. if (!$pending) {
  119. return;
  120. }
  121. $request = $this->group->getRequests(0, MEMBERS_PER_SECTION);
  122. if (!$request) {
  123. return;
  124. }
  125. $this->elementStart('div', array('id' => 'entity_pending',
  126. 'class' => 'section'));
  127. if (Event::handle('StartShowGroupPendingMiniList', array($this))) {
  128. $this->elementStart('h2');
  129. $this->element(
  130. 'a',
  131. array('href' => common_local_url('groupqueue', array('nickname' =>
  132. $this->group->nickname))),
  133. // TRANS: Header for mini list of users with a pending membership request on a group page (h2).
  134. _('Pending')
  135. );
  136. $this->text(' ');
  137. $this->text($pending);
  138. $this->elementEnd('h2');
  139. $gmml = new ProfileMiniList($request, $this);
  140. $gmml->show();
  141. Event::handle('EndShowGroupPendingMiniList', array($this));
  142. }
  143. $this->elementEnd('div');
  144. }
  145. public function showBlocked()
  146. {
  147. $blocked = $this->group->getBlocked(0, MEMBERS_PER_SECTION);
  148. $this->elementStart('div', array('id' => 'entity_blocked',
  149. 'class' => 'section'));
  150. if (Event::handle('StartShowGroupBlockedMiniList', array($this))) {
  151. $this->elementStart('h2');
  152. $this->element(
  153. 'a',
  154. array('href' => common_local_url('blockedfromgroup', array('nickname' =>
  155. $this->group->nickname))),
  156. // TRANS: Header for mini list of users that are blocked in a group page (h2).
  157. _('Blocked')
  158. );
  159. $this->text(' ');
  160. $this->text($this->group->getBlockedCount());
  161. $this->elementEnd('h2');
  162. $gmml = new GroupBlockedMiniList($blocked, $this);
  163. $cnt = $gmml->show();
  164. if ($cnt == 0) {
  165. // TRANS: Description for mini list of group members on a group page when the group has no members.
  166. $this->element('p', null, _('(None)'));
  167. }
  168. // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
  169. // for example http://identi.ca/group/statusnet. Broken?
  170. if ($cnt > MEMBERS_PER_SECTION) {
  171. $this->element(
  172. 'a',
  173. array('href' => common_local_url(
  174. 'blockedfromgroup',
  175. array('nickname' => $this->group->nickname)
  176. )),
  177. // TRANS: Link to all group members from mini list of group members if group has more than n members.
  178. _('All members')
  179. );
  180. }
  181. Event::handle('EndShowGroupBlockedMiniList', array($this));
  182. }
  183. $this->elementEnd('div');
  184. }
  185. /**
  186. * Show list of admins
  187. *
  188. * @return void
  189. */
  190. public function showAdmins()
  191. {
  192. $adminSection = new GroupAdminSection($this, $this->group);
  193. $adminSection->show();
  194. }
  195. public function noticeFormOptions()
  196. {
  197. $options = parent::noticeFormOptions();
  198. $cur = common_current_user();
  199. if (!empty($cur) && $cur->isMember($this->group)) {
  200. $options['to_group'] = $this->group;
  201. }
  202. return $options;
  203. }
  204. public function getGroup()
  205. {
  206. return $this->group;
  207. }
  208. /**
  209. * @return bool|void
  210. * @throws ClientException
  211. * @throws NicknameException
  212. */
  213. protected function doPreparation()
  214. {
  215. $nickname_arg = $this->arg('nickname');
  216. $nickname = common_canonical_nickname($nickname_arg);
  217. // Permanent redirect on non-canonical nickname
  218. if ($nickname_arg !== $nickname) {
  219. $args = ['nickname' => $nickname];
  220. if ($this->page != 1) {
  221. $args['page'] = $this->page;
  222. }
  223. common_redirect(common_local_url($this->getActionName(), $args), 301);
  224. return false;
  225. }
  226. if (!$nickname) {
  227. // TRANS: Client error displayed if no nickname argument was given requesting a group page.
  228. $this->clientError(_('No nickname.'), 404);
  229. }
  230. $local = Local_group::getKV('nickname', $nickname);
  231. if (!$local) {
  232. $alias = Group_alias::getKV('alias', $nickname);
  233. if ($alias) {
  234. $args = ['id' => $alias->group_id];
  235. if ($this->page != 1) {
  236. $args['page'] = $this->page;
  237. }
  238. common_redirect(common_local_url('groupbyid', $args), 301);
  239. return false;
  240. } else {
  241. common_log(LOG_NOTICE, "Couldn't find local group for nickname '$nickname'");
  242. // TRANS: Client error displayed if no remote group with a given name was found requesting group page.
  243. throw new ClientException(_('No such group.'), 404);
  244. }
  245. }
  246. $this->group = User_group::getKV('id', $local->group_id);
  247. $this->target = $this->group->getProfile();
  248. if (!$this->group instanceof User_group) {
  249. // TRANS: Client error displayed if no local group with a given name was found requesting group page.
  250. throw new ClientException(_('No such group.'), 404);
  251. }
  252. return true;
  253. }
  254. }