]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/groupaction.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / lib / groupaction.php
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
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 define('MEMBERS_PER_SECTION', 27);
35
36 /**
37  * Base class for group actions, similar to ProfileAction
38  *
39  * @category Action
40  * @package  StatusNet
41  * @author   Zach Copley <zach@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  */
45 class GroupAction extends Action
46 {
47     protected $group;
48
49     function prepare($args)
50     {
51         parent::prepare($args);
52
53         $nickname_arg = $this->arg('nickname');
54         $nickname = common_canonical_nickname($nickname_arg);
55
56         // Permanent redirect on non-canonical nickname
57
58         if ($nickname_arg != $nickname) {
59             $args = array('nickname' => $nickname);
60             if ($this->page != 1) {
61                 $args['page'] = $this->page;
62             }
63             common_redirect(common_local_url('showgroup', $args), 301);
64             return false;
65         }
66
67         if (!$nickname) {
68             // TRANS: Client error displayed if no nickname argument was given requesting a group page.
69             $this->clientError(_('No nickname.'), 404);
70             return false;
71         }
72
73         $local = Local_group::staticGet('nickname', $nickname);
74
75         if (!$local) {
76             $alias = Group_alias::staticGet('alias', $nickname);
77             if ($alias) {
78                 $args = array('id' => $alias->group_id);
79                 if ($this->page != 1) {
80                     $args['page'] = $this->page;
81                 }
82                 common_redirect(common_local_url('groupbyid', $args), 301);
83                 return false;
84             } else {
85                 common_log(LOG_NOTICE, "Couldn't find local group for nickname '$nickname'");
86                 // TRANS: Client error displayed if no remote group with a given name was found requesting group page.
87                 $this->clientError(_('No such group.'), 404);
88                 return false;
89             }
90         }
91
92         $this->group = User_group::staticGet('id', $local->group_id);
93
94         if (!$this->group) {
95             // TRANS: Client error displayed if no local group with a given name was found requesting group page.
96             $this->clientError(_('No such group.'), 404);
97             return false;
98         }
99     }
100
101     function showProfileBlock()
102     {
103         $block = new GroupProfileBlock($this, $this->group);
104         $block->show();
105     }
106
107     /**
108      * Fill in the sidebar.
109      *
110      * @return void
111      */
112     function showSections()
113     {
114         $this->showMembers();
115         $cur = common_current_user();
116         if ($cur && $cur->isAdmin($this->group)) {
117             $this->showPending();
118             $this->showBlocked();
119         }
120         $this->showAdmins();
121         $cloud = new GroupTagCloudSection($this, $this->group);
122         $cloud->show();
123     }
124
125     /**
126      * Show mini-list of members
127      *
128      * @return void
129      */
130     function showMembers()
131     {
132         $member = $this->group->getMembers(0, MEMBERS_PER_SECTION);
133
134         if (!$member) {
135             return;
136         }
137
138         $this->elementStart('div', array('id' => 'entity_members',
139                                          'class' => 'section'));
140
141         if (Event::handle('StartShowGroupMembersMiniList', array($this))) {
142             $this->elementStart('h2');
143
144             $this->element('a', array('href' => common_local_url('groupmembers', array('nickname' =>
145                                                                                        $this->group->nickname))),
146                            // TRANS: Header for mini list of group members on a group page (h2).
147                            _('Members'));
148
149             $this->text(' ');
150
151             $this->text($this->group->getMemberCount());
152
153             $this->elementEnd('h2');
154
155             $gmml = new GroupMembersMiniList($member, $this);
156             $cnt = $gmml->show();
157             if ($cnt == 0) {
158                 // TRANS: Description for mini list of group members on a group page when the group has no members.
159                 $this->element('p', null, _('(None)'));
160             }
161
162             // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
163             //              for example http://identi.ca/group/statusnet. Broken?
164             if ($cnt > MEMBERS_PER_SECTION) {
165                 $this->element('a', array('href' => common_local_url('groupmembers',
166                                                                      array('nickname' => $this->group->nickname))),
167                                // TRANS: Link to all group members from mini list of group members if group has more than n members.
168                                _('All members'));
169             }
170
171             Event::handle('EndShowGroupMembersMiniList', array($this));
172         }
173
174         $this->elementEnd('div');
175     }
176
177     function showPending()
178     {
179         if ($this->group->join_policy != User_group::JOIN_POLICY_MODERATE) {
180             return;
181         }
182
183         $pending = $this->group->getQueueCount();
184
185         if (!$pending) {
186             return;
187         }
188
189         $request = $this->group->getRequests(0, MEMBERS_PER_SECTION);
190
191         if (!$request) {
192             return;
193         }
194
195         $this->elementStart('div', array('id' => 'entity_pending',
196                                          'class' => 'section'));
197
198         if (Event::handle('StartShowGroupPendingMiniList', array($this))) {
199
200             $this->elementStart('h2');
201
202             $this->element('a', array('href' => common_local_url('groupqueue', array('nickname' =>
203                                                                                      $this->group->nickname))),
204                            // TRANS: Header for mini list of users with a pending membership request on a group page (h2).
205                            _('Pending'));
206
207             $this->text(' ');
208
209             $this->text($pending);
210
211             $this->elementEnd('h2');
212
213             $gmml = new ProfileMiniList($request, $this);
214             $gmml->show();
215
216             Event::handle('EndShowGroupPendingMiniList', array($this));
217         }
218
219         $this->elementEnd('div');
220     }
221
222     function showBlocked()
223     {
224         $blocked = $this->group->getBlocked(0, MEMBERS_PER_SECTION);
225
226         if (!$blocked) {
227             return;
228         }
229
230         $this->elementStart('div', array('id' => 'entity_blocked',
231                                          'class' => 'section'));
232
233         if (Event::handle('StartShowGroupBlockedMiniList', array($this))) {
234
235             $this->elementStart('h2');
236
237             $this->element('a', array('href' => common_local_url('blockedfromgroup', array('nickname' =>
238                                                                                            $this->group->nickname))),
239                            // TRANS: Header for mini list of users that are blocked in a group page (h2).
240                            _('Blocked'));
241
242             $this->text(' ');
243
244             $this->text($this->group->getBlockedCount());
245
246             $this->elementEnd('h2');
247
248             $gmml = new GroupBlockedMiniList($blocked, $this);
249             $cnt = $gmml->show();
250             if ($cnt == 0) {
251                 // TRANS: Description for mini list of group members on a group page when the group has no members.
252                 $this->element('p', null, _('(None)'));
253             }
254
255             // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
256             //              for example http://identi.ca/group/statusnet. Broken?
257             if ($cnt > MEMBERS_PER_SECTION) {
258                 $this->element('a', array('href' => common_local_url('blockedfromgroup',
259                                                                      array('nickname' => $this->group->nickname))),
260                                // TRANS: Link to all group members from mini list of group members if group has more than n members.
261                                _('All members'));
262             }
263
264             Event::handle('EndShowGroupBlockedMiniList', array($this));
265         }
266
267         $this->elementEnd('div');
268     }
269
270     /**
271      * Show list of admins
272      *
273      * @return void
274      */
275     function showAdmins()
276     {
277         $adminSection = new GroupAdminSection($this, $this->group);
278         $adminSection->show();
279     }
280
281     function noticeFormOptions()
282     {
283         $options = parent::noticeFormOptions();
284         $cur = common_current_user();
285
286         if (!empty($cur) && $cur->isMember($this->group)) {
287             $options['to_group'] =  $this->group;
288         }
289
290         return $options;
291     }
292 }
293
294 class GroupAdminSection extends ProfileSection
295 {
296     var $group;
297
298     function __construct($out, $group)
299     {
300         parent::__construct($out);
301         $this->group = $group;
302     }
303
304     function getProfiles()
305     {
306         return $this->group->getAdmins();
307     }
308
309     function title()
310     {
311         // TRANS: Title for list of group administrators on a group page.
312         return _m('TITLE','Admins');
313     }
314
315     function divId()
316     {
317         return 'group_admins';
318     }
319
320     function moreUrl()
321     {
322         return null;
323     }
324 }
325
326 class GroupMembersMiniList extends ProfileMiniList
327 {
328     function newListItem($profile)
329     {
330         return new GroupMembersMiniListItem($profile, $this->action);
331     }
332 }
333
334 class GroupMembersMiniListItem extends ProfileMiniListItem
335 {
336     function linkAttributes()
337     {
338         $aAttrs = parent::linkAttributes();
339
340         if (common_config('nofollow', 'members')) {
341             $aAttrs['rel'] .= ' nofollow';
342         }
343
344         return $aAttrs;
345     }
346 }
347
348 class GroupBlockedMiniList extends ProfileMiniList
349 {
350     function newListItem($profile)
351     {
352         return new GroupBlockedMiniListItem($profile, $this->action);
353     }
354 }
355
356 class GroupBlockedMiniListItem extends ProfileMiniListItem
357 {
358     function linkAttributes()
359     {
360         $aAttrs = parent::linkAttributes();
361
362         if (common_config('nofollow', 'members')) {
363             $aAttrs['rel'] .= ' nofollow';
364         }
365
366         return $aAttrs;
367     }
368 }
369
370 class ThreadingGroupNoticeStream extends ThreadingNoticeStream
371 {
372     function __construct($group, $profile)
373     {
374         parent::__construct(new GroupNoticeStream($group, $profile));
375     }
376 }