profileaction.php 11.7 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 22
 *
 * Common parent of Personal and Profile actions
 *
 * 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/>.
 *
 * @category  Personal
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('GNUSOCIAL')) { exit(1); }
32 33 34 35 36 37 38

/**
 * Profile action common superclass
 *
 * Abstracts out common code from profile and personal tabs
 *
 * @category Personal
39
 * @package  StatusNet
40
 * @author   Evan Prodromou <evan@status.net>
41
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42
 * @link     http://status.net/
43
 */
44
abstract class ProfileAction extends ManagedAction
45
{
46 47
    var $page    = null;
    var $tag     = null;
48

49 50
    protected $target  = null;    // Profile that we're showing

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    protected function doPreparation()
    {
        // showstream requires a nickname
        $nickname_arg = $this->trimmed('nickname');
        $nickname     = common_canonical_nickname($nickname_arg);

        // Permanent redirect on non-canonical nickname
        if ($nickname_arg != $nickname) {
            $args = array('nickname' => $nickname);
            if ($this->arg('page') && $this->arg('page') != 1) {
                $args['page'] = $this->arg['page'];
            }
            common_redirect(common_local_url($this->getActionName(), $args), 301);
        }

        try {
            $user = User::getByNickname($nickname);
        } catch (NoSuchUserException $e) {
            $group = Local_group::getKV('nickname', $nickname);
            if ($group instanceof Local_group) {
                common_redirect($group->getProfile()->getUrl());
            }

            // No user nor group found, throw the NoSuchUserException again
            throw $e;
        }

        $this->target = $user->getProfile();
    }

81
    protected function prepare(array $args=array())
82
    {
83
        // this will call ->doPreparation() which child classes use to set $this->target
84
        parent::prepare($args);
85

86 87
        if ($this->target->hasRole(Profile_role::SILENCED)
                && (!$this->scoped instanceof Profile || !$this->scoped->hasRight(Right::SILENCEUSER))) {
88 89 90
            throw new ClientException(_('This profile has been silenced by site moderators'), 403);
        }

91
        $this->tag = $this->trimmed('tag');
92 93
        $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
        common_set_returnto($this->selfUrl());
94

95
        return true;
96 97
    }

98 99
    public function getTarget()
    {
mmn's avatar
mmn committed
100 101 102
        if (!$this->target instanceof Profile) {
            throw new ServerException('No target profile in ProfileAction class');
        }
103 104 105
        return $this->target;
    }

106 107 108 109 110
    function isReadOnly($args)
    {
        return true;
    }

111 112 113 114 115
    function showSections()
    {
        $this->showSubscriptions();
        $this->showSubscribers();
        $this->showGroups();
116
        $this->showLists();
117 118 119
        $this->showStatistics();
    }

120 121 122 123 124 125 126 127 128 129
    /**
     * Convenience function for common pattern of links to subscription/groups sections.
     *
     * @param string $actionClass
     * @param string $title
     * @param string $cssClass
     */
    private function statsSectionLink($actionClass, $title, $cssClass='')
    {
        $this->element('a', array('href' => common_local_url($actionClass,
130
                                                             array('nickname' => $this->target->getNickname())),
131 132 133 134
                                  'class' => $cssClass),
                       $title);
    }

135 136 137 138
    function showSubscriptions()
    {
        $this->elementStart('div', array('id' => 'entity_subscriptions',
                                         'class' => 'section'));
139
        if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
140
            $this->elementStart('h2');
141
            // TRANS: H2 text for user subscription statistics.
142 143
            $this->statsSectionLink('subscriptions', _('Following'));
            $this->text(' ');
144
            $this->text($this->target->subscriptionCount());
145
            $this->elementEnd('h2');
146 147
        
            try {
148
                $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
149
                $pml = new ProfileMiniList($profile, $this);
150 151 152 153
                $pml->show();
            } catch (NoResultException $e) {
                // TRANS: Text for user subscription statistics if the user has no subscription
                $this->element('p', null, _('(None)'));
154 155
            }

156 157
            Event::handle('EndShowSubscriptionsMiniList', array($this));
        }
158 159 160 161 162 163 164 165
        $this->elementEnd('div');
    }

    function showSubscribers()
    {
        $this->elementStart('div', array('id' => 'entity_subscribers',
                                         'class' => 'section'));

166
        if (Event::handle('StartShowSubscribersMiniList', array($this))) {
167

168
            $this->elementStart('h2');
169
            // TRANS: H2 text for user subscriber statistics.
170 171
            $this->statsSectionLink('subscribers', _('Followers'));
            $this->text(' ');
172
            $this->text($this->target->subscriberCount());
173
            $this->elementEnd('h2');
174

175
            try {
176
                $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
177
                $sml = new SubscribersMiniList($profile, $this);
178 179 180 181
                $sml->show();
            } catch (NoResultException $e) {
                // TRANS: Text for user subscriber statistics if user has no subscribers.
                $this->element('p', null, _('(None)'));
182 183
            }

184
            Event::handle('EndShowSubscribersMiniList', array($this));
185 186 187 188 189 190 191
        }

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

    function showStatistics()
    {
192 193
        $notice_count = $this->target->noticeCount();
        $age_days     = (time() - strtotime($this->target->created)) / 86400;
194 195 196 197 198
        if ($age_days < 1) {
            // Rather than extrapolating out to a bajillion...
            $age_days = 1;
        }
        $daily_count = round($notice_count / $age_days);
199 200 201 202

        $this->elementStart('div', array('id' => 'entity_statistics',
                                         'class' => 'section'));

203
        // TRANS: H2 text for user statistics.
204 205
        $this->element('h2', null, _('Statistics'));

206
        $profile = $this->target;
207 208 209 210
        $actionParams = array('nickname' => $profile->nickname);
        $stats = array(
            array(
                'id' => 'user-id',
211
                // TRANS: Label for user statistics.
212 213 214 215 216
                'label' => _('User ID'),
                'value' => $profile->id,
            ),
            array(
                'id' => 'member-since',
217
                // TRANS: Label for user statistics.
218 219 220 221 222
                'label' => _('Member since'),
                'value' => date('j M Y', strtotime($profile->created))
            ),
            array(
                'id' => 'notices',
223
                // TRANS: Label for user statistics.
224 225 226 227 228
                'label' => _('Notices'),
                'value' => $notice_count,
            ),
            array(
                'id' => 'daily_notices',
229 230
                // TRANS: Label for user statistics.
                // TRANS: Average count of posts made per day since account registration.
231 232 233 234 235 236 237 238 239 240 241
                'label' => _('Daily average'),
                'value' => $daily_count
            )
        );

        // Give plugins a chance to add stats entries
        Event::handle('ProfileStats', array($profile, &$stats));

        foreach ($stats as $row) {
            $this->showStatsRow($row);
        }
242 243 244
        $this->elementEnd('div');
    }

245
    private function showStatsRow($row)
246
    {
247
        $this->elementStart('dl', 'entity_' . $row['id']);
248
        $this->elementStart('dt');
249 250
        if (!empty($row['link'])) {
            $this->element('a', array('href' => $row['link']), $row['label']);
251
        } else {
252
            $this->text($row['label']);
253 254
        }
        $this->elementEnd('dt');
255
        $this->element('dd', null, $row['value']);
256 257 258
        $this->elementEnd('dl');
    }

259 260
    function showGroups()
    {
261
        $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
262 263 264

        $this->elementStart('div', array('id' => 'entity_groups',
                                         'class' => 'section'));
265
        if (Event::handle('StartShowGroupsMiniList', array($this))) {
266
            $this->elementStart('h2');
267
            // TRANS: H2 text for user group membership statistics.
268
            $this->statsSectionLink('usergroups', _('Groups'));
269
            $this->text(' ');
270
            $this->text($this->target->getGroupCount());
271
            $this->elementEnd('h2');
272

273
            if ($groups instanceof User_group) {
274
                $gml = new GroupMiniList($groups, $this->target, $this);
275
                $cnt = $gml->show();
276 277 278
            } else {
                // TRANS: Text for user user group membership statistics if user is not a member of any group.
                $this->element('p', null, _('(None)'));
279
            }
280

281
            Event::handle('EndShowGroupsMiniList', array($this));
282
        }
283
            $this->elementEnd('div');
284
    }
285 286 287

    function showLists()
    {
288
        $lists = $this->target->getLists($this->scoped);
289 290 291 292 293 294 295

        if ($lists->N > 0) {
            $this->elementStart('div', array('id' => 'entity_lists',
                                             'class' => 'section'));

            if (Event::handle('StartShowListsMiniList', array($this))) {

296
                $url = common_local_url('peopletagsbyuser',
297
                                        array('nickname' => $this->target->getNickname()));
298

299
                $this->elementStart('h2');
300 301
                $this->element('a',
                               array('href' => $url),
302
                               // TRANS: H2 text for user list membership statistics.
303
                               _('Lists'));
304 305 306 307 308 309 310
                $this->text(' ');
                $this->text($lists->N);
                $this->elementEnd('h2');

                $this->elementStart('ul');


311 312
                $first = true;

313
                while ($lists->fetch()) {
314 315 316 317
                    if (!empty($lists->mainpage)) {
                        $url = $lists->mainpage;
                    } else {
                        $url = common_local_url('showprofiletag',
318
                                                array('nickname' => $this->target->getNickname(),
319
                                                      'tag'    => $lists->tag));
320
                    }
321 322 323 324 325 326 327 328
                    if (!$first) {
                        $this->text(', ');
                    } else {
                        $first = false;
                    }

                    $this->element('a', array('href' => $url),
                                   $lists->tag);
329 330 331 332 333 334 335 336 337
                }

                $this->elementEnd('ul');

                Event::handle('EndShowListsMiniList', array($this));
            }
            $this->elementEnd('div');
        }
    }
338
}