]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showgroup.php
Resolve Group Aliases in showgroup.php
[quix0rs-gnu-social.git] / actions / showgroup.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Group main page
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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Sarven Capadisli <csarven@controlyourself.ca>
26  * @copyright 2008-2009 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/noticelist.php';
36 require_once INSTALLDIR.'/lib/feedlist.php';
37
38 define('MEMBERS_PER_SECTION', 27);
39
40 /**
41  * Group main page
42  *
43  * @category Group
44  * @package  Laconica
45  * @author   Evan Prodromou <evan@controlyourself.ca>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://laconi.ca/
48  */
49
50 class ShowgroupAction extends GroupDesignAction
51 {
52
53     /** page we're viewing. */
54     var $page = null;
55
56     /**
57      * Is this page read-only?
58      *
59      * @return boolean true
60      */
61
62     function isReadOnly($args)
63     {
64         return true;
65     }
66
67     /**
68      * Title of the page
69      *
70      * @return string page title, with page number
71      */
72
73     function title()
74     {
75         if (!empty($this->group->fullname)) {
76             $base = $this->group->fullname . ' (' . $this->group->nickname . ')';
77         } else {
78             $base = $this->group->nickname;
79         }
80
81         if ($this->page == 1) {
82             return sprintf(_("%s group"), $base);
83         } else {
84             return sprintf(_("%s group, page %d"),
85                            $base,
86                            $this->page);
87         }
88     }
89
90     /**
91      * Prepare the action
92      *
93      * Reads and validates arguments and instantiates the attributes.
94      *
95      * @param array $args $_REQUEST args
96      *
97      * @return boolean success flag
98      */
99
100     function prepare($args)
101     {
102         parent::prepare($args);
103
104         if (!common_config('inboxes','enabled')) {
105             $this->serverError(_('Inboxes must be enabled for groups to work'));
106             return false;
107         }
108
109         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
110
111         $nickname_arg = $this->arg('nickname');
112         $nickname = common_canonical_nickname($nickname_arg);
113
114         // Permanent redirect on non-canonical nickname
115
116         if ($nickname_arg != $nickname) {
117             $args = array('nickname' => $nickname);
118             if ($this->page != 1) {
119                 $args['page'] = $this->page;
120             }
121             common_redirect(common_local_url('showgroup', $args), 301);
122             return false;
123         }
124
125         if (!$nickname) {
126             $this->clientError(_('No nickname'), 404);
127             return false;
128         }
129
130         $this->group = User_group::staticGet('nickname', $nickname);
131
132         if (!$this->group) {
133             $alias = Group_alias::staticGet('alias', $nickname);
134             if ($alias) {
135                 $args = array('id' => $alias->group_id);
136                 if ($this->page != 1) {
137                     $args['page'] = $this->page;
138                 }
139                 common_redirect(common_local_url('groupbyid', $args), 301);
140                 return false;
141             } else {
142                 $this->clientError(_('No such group'), 404);
143                 return false;
144             }
145         }
146
147         common_set_returnto($this->selfUrl());
148
149         return true;
150     }
151
152     /**
153      * Handle the request
154      *
155      * Shows a profile for the group, some controls, and a list of
156      * group notices.
157      *
158      * @return void
159      */
160
161     function handle($args)
162     {
163         $this->showPage();
164     }
165
166     /**
167      * Local menu
168      *
169      * @return void
170      */
171
172     function showLocalNav()
173     {
174         $nav = new GroupNav($this, $this->group);
175         $nav->show();
176     }
177
178     /**
179      * Show the page content
180      *
181      * Shows a group profile and a list of group notices
182      */
183
184     function showContent()
185     {
186         $this->showGroupProfile();
187         $this->showGroupNotices();
188     }
189
190     /**
191      * Show the group notices
192      *
193      * @return void
194      */
195
196     function showGroupNotices()
197     {
198         $notice = $this->group->getNotices(($this->page-1)*NOTICES_PER_PAGE,
199                                            NOTICES_PER_PAGE + 1);
200
201         $nl = new NoticeList($notice, $this);
202         $cnt = $nl->show();
203
204         $this->pagination($this->page > 1,
205                           $cnt > NOTICES_PER_PAGE,
206                           $this->page,
207                           'showgroup',
208                           array('nickname' => $this->group->nickname));
209     }
210
211     /**
212      * Show the group profile
213      *
214      * Information about the group
215      *
216      * @return void
217      */
218
219     function showGroupProfile()
220     {
221         $this->elementStart('div', 'entity_profile vcard author');
222
223         $this->element('h2', null, _('Group profile'));
224
225         $this->elementStart('dl', 'entity_depiction');
226         $this->element('dt', null, _('Avatar'));
227         $this->elementStart('dd');
228
229         $logo = ($this->group->homepage_logo) ?
230           $this->group->homepage_logo : User_group::defaultLogo(AVATAR_PROFILE_SIZE);
231
232         $this->element('img', array('src' => $logo,
233                                     'class' => 'photo avatar',
234                                     'width' => AVATAR_PROFILE_SIZE,
235                                     'height' => AVATAR_PROFILE_SIZE,
236                                     'alt' => $this->group->nickname));
237         $this->elementEnd('dd');
238         $this->elementEnd('dl');
239
240         $this->elementStart('dl', 'entity_nickname');
241         $this->element('dt', null, _('Nickname'));
242         $this->elementStart('dd');
243         $hasFN = ($this->group->fullname) ? 'nickname url uid' : 'fn org nickname url uid';
244         $this->element('a', array('href' => $this->group->homeUrl(),
245                                   'rel' => 'me', 'class' => $hasFN),
246                             $this->group->nickname);
247         $this->elementEnd('dd');
248         $this->elementEnd('dl');
249
250         if ($this->group->fullname) {
251             $this->elementStart('dl', 'entity_fn');
252             $this->element('dt', null, _('Full name'));
253             $this->elementStart('dd');
254             $this->element('span', 'fn org', $this->group->fullname);
255             $this->elementEnd('dd');
256             $this->elementEnd('dl');
257         }
258
259         if ($this->group->location) {
260             $this->elementStart('dl', 'entity_location');
261             $this->element('dt', null, _('Location'));
262             $this->element('dd', 'label', $this->group->location);
263             $this->elementEnd('dl');
264         }
265
266         if ($this->group->homepage) {
267             $this->elementStart('dl', 'entity_url');
268             $this->element('dt', null, _('URL'));
269             $this->elementStart('dd');
270             $this->element('a', array('href' => $this->group->homepage,
271                                       'rel' => 'me', 'class' => 'url'),
272                            $this->group->homepage);
273             $this->elementEnd('dd');
274             $this->elementEnd('dl');
275         }
276
277         if ($this->group->description) {
278             $this->elementStart('dl', 'entity_note');
279             $this->element('dt', null, _('Note'));
280             $this->element('dd', 'note', $this->group->description);
281             $this->elementEnd('dl');
282         }
283
284         if (common_config('group', 'maxaliases') > 0) {
285             $aliases = $this->group->getAliases();
286
287             if (!empty($aliases)) {
288                 $this->elementStart('dl', 'entity_aliases');
289                 $this->element('dt', null, _('Aliases'));
290                 $this->element('dd', 'aliases', implode(' ', $aliases));
291                 $this->elementEnd('dl');
292             }
293         }
294
295         $this->elementEnd('div');
296
297         $this->elementStart('div', 'entity_actions');
298         $this->element('h2', null, _('Group actions'));
299         $this->elementStart('ul');
300         $this->elementStart('li', 'entity_subscribe');
301         $cur = common_current_user();
302         if ($cur) {
303             if ($cur->isMember($this->group)) {
304                 $lf = new LeaveForm($this, $this->group);
305                 $lf->show();
306             } else if (!Group_block::isBlocked($this->group, $cur->getProfile())) {
307                 $jf = new JoinForm($this, $this->group);
308                 $jf->show();
309             }
310         }
311
312         $this->elementEnd('li');
313
314         $this->elementEnd('ul');
315         $this->elementEnd('div');
316     }
317
318     /**
319      * Get a list of the feeds for this page
320      *
321      * @return void
322      */
323
324     function getFeeds()
325     {
326         $url =
327           common_local_url('grouprss',
328                            array('nickname' => $this->group->nickname));
329
330         return array(new Feed(Feed::RSS1,
331                               common_local_url('grouprss',
332                                                array('nickname' => $this->group->nickname)),
333                               sprintf(_('Notice feed for %s group (RSS 1.0)'),
334                                       $this->group->nickname)),
335                      new Feed(Feed::RSS2,
336                               common_local_url('api',
337                                                array('apiaction' => 'groups',
338                                                      'method' => 'timeline',
339                                                      'argument' => $this->group->nickname.'.rss')),
340                               sprintf(_('Notice feed for %s group (RSS 2.0)'),
341                                       $this->group->nickname)),
342                      new Feed(Feed::ATOM,
343                               common_local_url('api',
344                                                array('apiaction' => 'groups',
345                                                      'method' => 'timeline',
346                                                      'argument' => $this->group->nickname.'.atom')),
347                               sprintf(_('Notice feed for %s group (Atom)'),
348                                       $this->group->nickname)));
349     }
350
351     /**
352      * Fill in the sidebar.
353      *
354      * @return void
355      */
356
357     function showSections()
358     {
359         $this->showMembers();
360         $this->showStatistics();
361         $this->showAdmins();
362         $cloud = new GroupTagCloudSection($this, $this->group);
363         $cloud->show();
364     }
365
366     /**
367      * Show mini-list of members
368      *
369      * @return void
370      */
371
372     function showMembers()
373     {
374         $member = $this->group->getMembers(0, MEMBERS_PER_SECTION);
375
376         if (!$member) {
377             return;
378         }
379
380         $this->elementStart('div', array('id' => 'entity_members',
381                                          'class' => 'section'));
382
383         $this->element('h2', null, _('Members'));
384
385         $pml = new ProfileMiniList($member, $this);
386         $cnt = $pml->show();
387         if ($cnt == 0) {
388              $this->element('p', null, _('(None)'));
389         }
390
391         if ($cnt > MEMBERS_PER_SECTION) {
392             $this->element('a', array('href' => common_local_url('groupmembers',
393                                                                  array('nickname' => $this->group->nickname))),
394                            _('All members'));
395         }
396
397         $this->elementEnd('div');
398     }
399
400     /**
401      * Show list of admins
402      *
403      * @return void
404      */
405
406     function showAdmins()
407     {
408         $adminSection = new GroupAdminSection($this, $this->group);
409         $adminSection->show();
410     }
411
412     /**
413      * Show some statistics
414      *
415      * @return void
416      */
417
418     function showStatistics()
419     {
420         // XXX: WORM cache this
421         $members = $this->group->getMembers();
422         $members_count = 0;
423         /** $member->count() doesn't work. */
424         while ($members->fetch()) {
425             $members_count++;
426         }
427
428         $this->elementStart('div', array('id' => 'entity_statistics',
429                                          'class' => 'section'));
430
431         $this->element('h2', null, _('Statistics'));
432
433         $this->elementStart('dl', 'entity_created');
434         $this->element('dt', null, _('Created'));
435         $this->element('dd', null, date('j M Y',
436                                                  strtotime($this->group->created)));
437         $this->elementEnd('dl');
438
439         $this->elementStart('dl', 'entity_members');
440         $this->element('dt', null, _('Members'));
441         $this->element('dd', null, (is_int($members_count)) ? $members_count : '0');
442         $this->elementEnd('dl');
443
444         $this->elementEnd('div');
445     }
446
447     function showAnonymousMessage()
448     {
449         if (!(common_config('site','closed') || common_config('site','inviteonly'))) {
450             $m = sprintf(_('**%s** is a user group on %%%%site.name%%%%, a [micro-blogging](http://en.wikipedia.org/wiki/Micro-blogging) service ' .
451                 'based on the Free Software [Laconica](http://laconi.ca/) tool. Its members share ' .
452                 'short messages about their life and interests. '.
453                 '[Join now](%%%%action.%s%%%%) to become part of this group and many more! ([Read more](%%%%doc.help%%%%))'),
454                      $this->group->nickname,
455                      (!common_config('site','openidonly')) ? 'register' : 'openidlogin');
456         } else {
457             $m = sprintf(_('**%s** is a user group on %%%%site.name%%%%, a [micro-blogging](http://en.wikipedia.org/wiki/Micro-blogging) service ' .
458                 'based on the Free Software [Laconica](http://laconi.ca/) tool. Its members share ' .
459                 'short messages about their life and interests. '),
460                      $this->group->nickname);
461         }
462         $this->elementStart('div', array('id' => 'anon_notice'));
463         $this->raw(common_markup_to_html($m));
464         $this->elementEnd('div');
465     }
466 }
467
468 class GroupAdminSection extends ProfileSection
469 {
470     var $group;
471
472     function __construct($out, $group)
473     {
474         parent::__construct($out);
475         $this->group = $group;
476     }
477
478     function getProfiles()
479     {
480         return $this->group->getAdmins();
481     }
482
483     function title()
484     {
485         return _('Admins');
486     }
487
488     function divId()
489     {
490         return 'group_admins';
491     }
492
493     function moreUrl()
494     {
495         return null;
496     }
497 }