]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/groupaction.php
Merge remote-tracking branch 'upstream/master' into social-master
[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     protected function prepare(array $args=array())
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         }
65
66         if (!$nickname) {
67             // TRANS: Client error displayed if no nickname argument was given requesting a group page.
68             $this->clientError(_('No nickname.'), 404);
69         }
70
71         $local = Local_group::getKV('nickname', $nickname);
72
73         if (!$local) {
74             $alias = Group_alias::getKV('alias', $nickname);
75             if ($alias) {
76                 $args = array('id' => $alias->group_id);
77                 if ($this->page != 1) {
78                     $args['page'] = $this->page;
79                 }
80                 common_redirect(common_local_url('groupbyid', $args), 301);
81             } else {
82                 common_log(LOG_NOTICE, "Couldn't find local group for nickname '$nickname'");
83                 // TRANS: Client error displayed if no remote group with a given name was found requesting group page.
84                 $this->clientError(_('No such group.'), 404);
85             }
86         }
87
88         $this->group = User_group::getKV('id', $local->group_id);
89
90         if (!$this->group) {
91             // TRANS: Client error displayed if no local group with a given name was found requesting group page.
92             $this->clientError(_('No such group.'), 404);
93         }
94     }
95
96     function showProfileBlock()
97     {
98         $block = new GroupProfileBlock($this, $this->group);
99         $block->show();
100     }
101
102     /**
103      * Fill in the sidebar.
104      *
105      * @return void
106      */
107     function showSections()
108     {
109         $this->showMembers();
110         $cur = common_current_user();
111         if ($cur && $cur->isAdmin($this->group)) {
112             $this->showPending();
113             $this->showBlocked();
114         }
115
116         $this->showAdmins();
117
118         if (!common_config('performance', 'high')) {
119             $cloud = new GroupTagCloudSection($this, $this->group);
120             $cloud->show();
121         }
122     }
123
124     /**
125      * Show mini-list of members
126      *
127      * @return void
128      */
129     function showMembers()
130     {
131         $member = $this->group->getMembers(0, MEMBERS_PER_SECTION);
132
133         if (!$member) {
134             return;
135         }
136
137         $this->elementStart('div', array('id' => 'entity_members',
138                                          'class' => 'section'));
139
140         if (Event::handle('StartShowGroupMembersMiniList', array($this))) {
141             $this->elementStart('h2');
142
143             $this->element('a', array('href' => common_local_url('groupmembers', array('nickname' =>
144                                                                                        $this->group->nickname))),
145                            // TRANS: Header for mini list of group members on a group page (h2).
146                            _('Members'));
147
148             $this->text(' ');
149
150             $this->text($this->group->getMemberCount());
151
152             $this->elementEnd('h2');
153
154             $gmml = new GroupMembersMiniList($member, $this);
155             $cnt = $gmml->show();
156             if ($cnt == 0) {
157                 // TRANS: Description for mini list of group members on a group page when the group has no members.
158                 $this->element('p', null, _('(None)'));
159             }
160
161             // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
162             //              for example http://identi.ca/group/statusnet. Broken?
163             if ($cnt > MEMBERS_PER_SECTION) {
164                 $this->element('a', array('href' => common_local_url('groupmembers',
165                                                                      array('nickname' => $this->group->nickname))),
166                                // TRANS: Link to all group members from mini list of group members if group has more than n members.
167                                _('All members'));
168             }
169
170             Event::handle('EndShowGroupMembersMiniList', array($this));
171         }
172
173         $this->elementEnd('div');
174     }
175
176     function showPending()
177     {
178         if ($this->group->join_policy != User_group::JOIN_POLICY_MODERATE) {
179             return;
180         }
181
182         $pending = $this->group->getQueueCount();
183
184         if (!$pending) {
185             return;
186         }
187
188         $request = $this->group->getRequests(0, MEMBERS_PER_SECTION);
189
190         if (!$request) {
191             return;
192         }
193
194         $this->elementStart('div', array('id' => 'entity_pending',
195                                          'class' => 'section'));
196
197         if (Event::handle('StartShowGroupPendingMiniList', array($this))) {
198
199             $this->elementStart('h2');
200
201             $this->element('a', array('href' => common_local_url('groupqueue', array('nickname' =>
202                                                                                      $this->group->nickname))),
203                            // TRANS: Header for mini list of users with a pending membership request on a group page (h2).
204                            _('Pending'));
205
206             $this->text(' ');
207
208             $this->text($pending);
209
210             $this->elementEnd('h2');
211
212             $gmml = new ProfileMiniList($request, $this);
213             $gmml->show();
214
215             Event::handle('EndShowGroupPendingMiniList', array($this));
216         }
217
218         $this->elementEnd('div');
219     }
220
221     function showBlocked()
222     {
223         $blocked = $this->group->getBlocked(0, MEMBERS_PER_SECTION);
224
225         $this->elementStart('div', array('id' => 'entity_blocked',
226                                          'class' => 'section'));
227
228         if (Event::handle('StartShowGroupBlockedMiniList', array($this))) {
229
230             $this->elementStart('h2');
231
232             $this->element('a', array('href' => common_local_url('blockedfromgroup', array('nickname' =>
233                                                                                            $this->group->nickname))),
234                            // TRANS: Header for mini list of users that are blocked in a group page (h2).
235                            _('Blocked'));
236
237             $this->text(' ');
238
239             $this->text($this->group->getBlockedCount());
240
241             $this->elementEnd('h2');
242
243             $gmml = new GroupBlockedMiniList($blocked, $this);
244             $cnt = $gmml->show();
245             if ($cnt == 0) {
246                 // TRANS: Description for mini list of group members on a group page when the group has no members.
247                 $this->element('p', null, _('(None)'));
248             }
249
250             // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
251             //              for example http://identi.ca/group/statusnet. Broken?
252             if ($cnt > MEMBERS_PER_SECTION) {
253                 $this->element('a', array('href' => common_local_url('blockedfromgroup',
254                                                                      array('nickname' => $this->group->nickname))),
255                                // TRANS: Link to all group members from mini list of group members if group has more than n members.
256                                _('All members'));
257             }
258
259             Event::handle('EndShowGroupBlockedMiniList', array($this));
260         }
261
262         $this->elementEnd('div');
263     }
264
265     /**
266      * Show list of admins
267      *
268      * @return void
269      */
270     function showAdmins()
271     {
272         $adminSection = new GroupAdminSection($this, $this->group);
273         $adminSection->show();
274     }
275
276     function noticeFormOptions()
277     {
278         $options = parent::noticeFormOptions();
279         $cur = common_current_user();
280
281         if (!empty($cur) && $cur->isMember($this->group)) {
282             $options['to_group'] =  $this->group;
283         }
284
285         return $options;
286     }
287
288     function getGroup()
289     {
290         return $this->group;
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 $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 $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 }