showgroup.php 15.1 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Group main page
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
22
 * @category  Group
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
26
 * @copyright 2008-2011 StatusNet, Inc.
27
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28
 * @link      http://status.net/
29 30
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
32 33 34 35 36 37
    exit(1);
}

require_once INSTALLDIR.'/lib/noticelist.php';
require_once INSTALLDIR.'/lib/feedlist.php';

38
define('MEMBERS_PER_SECTION', 27);
39

40 41 42
/**
 * Group main page
 *
43
 * @category Group
44
 * @package  StatusNet
45
 * @author   Evan Prodromou <evan@status.net>
46
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47
 * @link     http://status.net/
48
 */
49
class ShowgroupAction extends Action
50 51 52
{
    /** page we're viewing. */
    var $page = null;
53 54
    var $userProfile = null;
    var $notice = null;
55

56 57 58 59 60
    /**
     * Is this page read-only?
     *
     * @return boolean true
     */
61
    function isReadOnly($args)
62 63 64 65
    {
        return true;
    }

66 67 68 69 70 71 72
    /**
     * Title of the page
     *
     * @return string page title, with page number
     */
    function title()
    {
73
        $base = $this->group->getFancyName();
74

75
        if ($this->page == 1) {
76
            // TRANS: Page title for first group page. %s is a group name.
77
            return sprintf(_('%s group'), $base);
78
        } else {
79 80
            // TRANS: Page title for any but first group page.
            // TRANS: %1$s is a group name, $2$s is a page number.
81
            return sprintf(_('%1$s group, page %2$d'),
82
                           $base,
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
                           $this->page);
        }
    }

    /**
     * Prepare the action
     *
     * Reads and validates arguments and instantiates the attributes.
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
    function prepare($args)
    {
        parent::prepare($args);

        $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;

        $nickname_arg = $this->arg('nickname');
        $nickname = common_canonical_nickname($nickname_arg);

        // Permanent redirect on non-canonical nickname

        if ($nickname_arg != $nickname) {
            $args = array('nickname' => $nickname);
            if ($this->page != 1) {
                $args['page'] = $this->page;
            }
            common_redirect(common_local_url('showgroup', $args), 301);
            return false;
        }

        if (!$nickname) {
117
            // TRANS: Client error displayed if no nickname argument was given requesting a group page.
118
            $this->clientError(_('No nickname.'), 404);
119 120 121
            return false;
        }

122 123 124
        $local = Local_group::staticGet('nickname', $nickname);

        if (!$local) {
125 126 127 128 129 130 131 132 133
            $alias = Group_alias::staticGet('alias', $nickname);
            if ($alias) {
                $args = array('id' => $alias->group_id);
                if ($this->page != 1) {
                    $args['page'] = $this->page;
                }
                common_redirect(common_local_url('groupbyid', $args), 301);
                return false;
            } else {
134
                common_log(LOG_NOTICE, "Couldn't find local group for nickname '$nickname'");
135
                // TRANS: Client error displayed if no remote group with a given name was found requesting group page.
136
                $this->clientError(_('No such group.'), 404);
137 138
                return false;
            }
139 140
        }

141 142 143
        $this->group = User_group::staticGet('id', $local->group_id);

        if (!$this->group) {
144
                // TRANS: Client error displayed if no local group with a given name was found requesting group page.
145 146 147 148
            $this->clientError(_('No such group.'), 404);
            return false;
        }

149
        $this->userProfile = Profile::current();
150

151
        $stream = new ThreadingGroupNoticeStream($this->group, $this->userProfile);
152 153 154 155

        $this->notice = $stream->getNotices(($this->page-1)*NOTICES_PER_PAGE,
                                            NOTICES_PER_PAGE + 1);

156 157
        common_set_returnto($this->selfUrl());

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        return true;
    }

    /**
     * Handle the request
     *
     * Shows a profile for the group, some controls, and a list of
     * group notices.
     *
     * @return void
     */
    function handle($args)
    {
        $this->showPage();
    }

174 175 176 177 178
    /**
     * Local menu
     *
     * @return void
     */
179
    function showObjectNav()
180 181 182 183 184
    {
        $nav = new GroupNav($this, $this->group);
        $nav->show();
    }

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    /**
     * Show the page content
     *
     * Shows a group profile and a list of group notices
     */
    function showContent()
    {
        $this->showGroupNotices();
    }

    /**
     * Show the group notices
     *
     * @return void
     */
    function showGroupNotices()
    {
202
        $nl = new ThreadedNoticeList($this->notice, $this, $this->userProfile);
203 204 205 206 207 208 209 210 211 212
        $cnt = $nl->show();

        $this->pagination($this->page > 1,
                          $cnt > NOTICES_PER_PAGE,
                          $this->page,
                          'showgroup',
                          array('nickname' => $this->group->nickname));
    }

    /**
213
     * Get a list of the feeds for this page
214 215 216
     *
     * @return void
     */
217
    function getFeeds()
218 219 220 221 222
    {
        $url =
          common_local_url('grouprss',
                           array('nickname' => $this->group->nickname));

223 224 225 226 227 228 229 230
        return array(new Feed(Feed::JSON,
                              common_local_url('ApiTimelineGroup',
                                               array('format' => 'as',
                                                     'id' => $this->group->id)),
                              // TRANS: Tooltip for feed link. %s is a group nickname.
                              sprintf(_('Notice feed for %s group (Activity Streams JSON)'),
                                      $this->group->nickname)),
                    new Feed(Feed::RSS1,
231 232
                              common_local_url('grouprss',
                                               array('nickname' => $this->group->nickname)),
233
                              // TRANS: Tooltip for feed link. %s is a group nickname.
234 235 236
                              sprintf(_('Notice feed for %s group (RSS 1.0)'),
                                      $this->group->nickname)),
                     new Feed(Feed::RSS2,
237 238
                              common_local_url('ApiTimelineGroup',
                                               array('format' => 'rss',
239
                                                     'id' => $this->group->id)),
240
                              // TRANS: Tooltip for feed link. %s is a group nickname.
241 242 243
                              sprintf(_('Notice feed for %s group (RSS 2.0)'),
                                      $this->group->nickname)),
                     new Feed(Feed::ATOM,
244 245
                              common_local_url('ApiTimelineGroup',
                                               array('format' => 'atom',
246
                                                     'id' => $this->group->id)),
247
                              // TRANS: Tooltip for feed link. %s is a group nickname.
248
                              sprintf(_('Notice feed for %s group (Atom)'),
Toby Inkster's avatar
Toby Inkster committed
249 250 251 252
                                      $this->group->nickname)),
                     new Feed(Feed::FOAF,
                              common_local_url('foafgroup',
                                               array('nickname' => $this->group->nickname)),
253
                              // TRANS: Tooltip for feed link. %s is a group nickname.
Toby Inkster's avatar
Toby Inkster committed
254 255
                              sprintf(_('FOAF for %s group'),
                                       $this->group->nickname)));
256
    }
257 258 259 260 261 262 263 264 265

    /**
     * Fill in the sidebar.
     *
     * @return void
     */
    function showSections()
    {
        $this->showMembers();
266
        $this->showStatistics();
267
        $this->showAdmins();
268 269
        $cloud = new GroupTagCloudSection($this, $this->group);
        $cloud->show();
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    }

    /**
     * Show mini-list of members
     *
     * @return void
     */
    function showMembers()
    {
        $member = $this->group->getMembers(0, MEMBERS_PER_SECTION);

        if (!$member) {
            return;
        }

sarven's avatar
sarven committed
285
        $this->elementStart('div', array('id' => 'entity_members',
286 287
                                         'class' => 'section'));

288
        if (Event::handle('StartShowGroupMembersMiniList', array($this))) {
289

290
            // TRANS: Header for mini list of group members on a group page (h2).
291 292
            $this->element('h2', null, _('Members'));

293 294
            $gmml = new GroupMembersMiniList($member, $this);
            $cnt = $gmml->show();
295
            if ($cnt == 0) {
296
                // TRANS: Description for mini list of group members on a group page when the group has no members.
297 298 299
                $this->element('p', null, _('(None)'));
            }

300 301
            // @todo FIXME: Should be shown if a group has more than 27 members, but I do not see it displayed at
            //              for example http://identi.ca/group/statusnet. Broken?
302 303 304
            if ($cnt > MEMBERS_PER_SECTION) {
                $this->element('a', array('href' => common_local_url('groupmembers',
                                                                     array('nickname' => $this->group->nickname))),
305
                               // TRANS: Link to all group members from mini list of group members if group has more than n members.
306 307
                               _('All members'));
            }
308

309
            Event::handle('EndShowGroupMembersMiniList', array($this));
310
        }
sarven's avatar
sarven committed
311 312

        $this->elementEnd('div');
313
    }
314

315 316 317 318 319 320 321 322 323 324 325
    /**
     * Show list of admins
     *
     * @return void
     */
    function showAdmins()
    {
        $adminSection = new GroupAdminSection($this, $this->group);
        $adminSection->show();
    }

326 327 328 329 330 331 332 333 334 335
    /**
     * Show some statistics
     *
     * @return void
     */
    function showStatistics()
    {
        $this->elementStart('div', array('id' => 'entity_statistics',
                                         'class' => 'section'));

336
        // TRANS: Header for group statistics on a group page (h2).
337 338
        $this->element('h2', null, _('Statistics'));

339
        $this->elementStart('dl');
340 341

        // TRANS: Label for group creation date.
342
        $this->element('dt', null, _m('LABEL','Created'));
343
        $this->element('dd', 'entity_created', date('j M Y',
344
                                                 strtotime($this->group->created)));
345 346 347
        // @todo FIXME: i18n issue. This label gets a colon added from somewhere. Should be part of the message.
        // TRANS: Label for member count in statistics on group page.
        $this->element('dt', null, _m('LABEL','Members'));
348
        $this->element('dd', null, $this->group->getMemberCount());
349 350 351 352 353
        $this->elementEnd('dl');

        $this->elementEnd('div');
    }

354 355
    function showAnonymousMessage()
    {
356
        if (!(common_config('site','closed') || common_config('site','inviteonly'))) {
357 358 359 360 361
            // @todo FIXME: use group full name here if available instead of (uglier) primary alias.
            // TRANS: Notice on group pages for anonymous users for StatusNet sites that accept new registrations.
            // TRANS: **%s** is the group alias, %%%%site.name%%%% is the site name,
            // TRANS: %%%%action.register%%%% is the URL for registration, %%%%doc.help%%%% is a URL to help.
            // TRANS: This message contains Markdown links. Ensure they are formatted correctly: [Description](link).
362
            $m = sprintf(_('**%s** is a user group on %%%%site.name%%%%, a [micro-blogging](http://en.wikipedia.org/wiki/Micro-blogging) service ' .
363
                'based on the Free Software [StatusNet](http://status.net/) tool. Its members share ' .
364
                'short messages about their life and interests. '.
365 366
                '[Join now](%%%%action.register%%%%) to become part of this group and many more! ([Read more](%%%%doc.help%%%%))'),
                     $this->group->nickname);
367
        } else {
368 369 370 371
            // @todo FIXME: use group full name here if available instead of (uglier) primary alias.
            // TRANS: Notice on group pages for anonymous users for StatusNet sites that accept no new registrations.
            // TRANS: **%s** is the group alias, %%%%site.name%%%% is the site name,
            // TRANS: This message contains Markdown links. Ensure they are formatted correctly: [Description](link).
372
            $m = sprintf(_('**%s** is a user group on %%%%site.name%%%%, a [micro-blogging](http://en.wikipedia.org/wiki/Micro-blogging) service ' .
373
                'based on the Free Software [StatusNet](http://status.net/) tool. Its members share ' .
374 375 376
                'short messages about their life and interests. '),
                     $this->group->nickname);
        }
sarven's avatar
sarven committed
377
        $this->elementStart('div', array('id' => 'anon_notice'));
378
        $this->raw(common_markup_to_html($m));
379
        $this->elementEnd('div');
380
    }
381 382 383 384 385 386 387 388 389 390 391 392

    function noticeFormOptions()
    {
        $options = parent::noticeFormOptions();
        $cur = common_current_user();

        if (!empty($cur) && $cur->isMember($this->group)) {
            $options['to_group'] =  $this->group;
        }

        return $options;
    }
393
}
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

class GroupAdminSection extends ProfileSection
{
    var $group;

    function __construct($out, $group)
    {
        parent::__construct($out);
        $this->group = $group;
    }

    function getProfiles()
    {
        return $this->group->getAdmins();
    }

    function title()
    {
412 413
        // TRANS: Title for list of group administrators on a group page.
        return _m('TITLE','Admins');
414 415 416 417 418 419 420 421 422 423 424
    }

    function divId()
    {
        return 'group_admins';
    }

    function moreUrl()
    {
        return null;
    }
425
}
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

class GroupMembersMiniList extends ProfileMiniList
{
    function newListItem($profile)
    {
        return new GroupMembersMiniListItem($profile, $this->action);
    }
}

class GroupMembersMiniListItem extends ProfileMiniListItem
{
    function linkAttributes()
    {
        $aAttrs = parent::linkAttributes();

        if (common_config('nofollow', 'members')) {
            $aAttrs['rel'] .= ' nofollow';
        }

        return $aAttrs;
    }
}
448 449 450 451 452 453 454 455

class ThreadingGroupNoticeStream extends ThreadingNoticeStream
{
    function __construct($group, $profile)
    {
        parent::__construct(new GroupNoticeStream($group, $profile));
    }
}