3 * StatusNet, the distributed open-source microblogging tool
5 * Common parent of Personal and Profile actions
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.
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.
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/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @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/
31 if (!defined('GNUSOCIAL')) { exit(1); }
34 * Profile action common superclass
36 * Abstracts out common code from profile and personal tabs
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/
44 abstract class ProfileAction extends ManagedAction
49 protected $target = null; // Profile that we're showing
51 protected function doPreparation()
54 $nickname_arg = $this->arg('nickname');
55 $nickname = common_canonical_nickname($nickname_arg);
57 // Permanent redirect on non-canonical nickname
59 if ($nickname_arg != $nickname) {
60 $args = array('nickname' => $nickname);
61 if ($this->arg('page') && $this->arg('page') != 1) {
62 $args['page'] = $this->arg['page'];
64 common_redirect(common_local_url($this->getActionName(), $args), 301);
66 $this->user = User::getKV('nickname', $nickname);
69 $group = Local_group::getKV('nickname', $nickname);
70 if ($group instanceof Local_group) {
71 common_redirect($group->getProfile()->getUrl());
73 // TRANS: Client error displayed when calling a profile action without specifying a user.
74 $this->clientError(_('No such user.'), 404);
77 $this->target = $this->user->getProfile();
78 } catch (NicknameException $e) {
79 $id = (int)$this->arg('id');
80 $this->target = Profile::getKV('id', $id);
82 if (!$this->target instanceof Profile) {
83 // TRANS: Error message displayed when referring to a user without a profile.
84 $this->serverError(_m('Profile ID does not exist.'));
87 if ($this->target->isLocal()) {
88 // For local users when accessed by id number, redirect to
89 // the same action but using the nickname as argument.
90 common_redirect(common_local_url($this->getActionName(),
91 array('nickname'=>$user->getNickname())));
95 if ($this->target->hasRole(Profile_role::SILENCED) &&
96 (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
97 throw new ClientException(_('This profile has been silenced by site moderators'), 403);
100 // backwards compatibility until all actions are fixed to use $this->target
101 $this->profile = $this->target;
103 $this->tag = $this->trimmed('tag');
104 $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
105 common_set_returnto($this->selfUrl());
107 return $this->profileActionPreparation();
110 protected function profileActionPreparation()
115 function isReadOnly($args)
120 function showSections()
122 $this->showSubscriptions();
123 $this->showSubscribers();
126 $this->showStatistics();
130 * Convenience function for common pattern of links to subscription/groups sections.
132 * @param string $actionClass
133 * @param string $title
134 * @param string $cssClass
136 private function statsSectionLink($actionClass, $title, $cssClass='')
138 $this->element('a', array('href' => common_local_url($actionClass,
139 array('nickname' => $this->target->getNickname())),
140 'class' => $cssClass),
144 function showSubscriptions()
146 $this->elementStart('div', array('id' => 'entity_subscriptions',
147 'class' => 'section'));
148 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
149 $this->elementStart('h2');
150 // TRANS: H2 text for user subscription statistics.
151 $this->statsSectionLink('subscriptions', _('Following'));
153 $this->text($this->target->subscriptionCount());
154 $this->elementEnd('h2');
157 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
158 $pml = new ProfileMiniList($profile, $this);
160 } catch (NoResultException $e) {
161 // TRANS: Text for user subscription statistics if the user has no subscription
162 $this->element('p', null, _('(None)'));
165 Event::handle('EndShowSubscriptionsMiniList', array($this));
167 $this->elementEnd('div');
170 function showSubscribers()
172 $this->elementStart('div', array('id' => 'entity_subscribers',
173 'class' => 'section'));
175 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
177 $this->elementStart('h2');
178 // TRANS: H2 text for user subscriber statistics.
179 $this->statsSectionLink('subscribers', _('Followers'));
181 $this->text($this->target->subscriberCount());
182 $this->elementEnd('h2');
185 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
186 $sml = new SubscribersMiniList($profile, $this);
188 } catch (NoResultException $e) {
189 // TRANS: Text for user subscriber statistics if user has no subscribers.
190 $this->element('p', null, _('(None)'));
193 Event::handle('EndShowSubscribersMiniList', array($this));
196 $this->elementEnd('div');
199 function showStatistics()
201 $notice_count = $this->target->noticeCount();
202 $age_days = (time() - strtotime($this->target->created)) / 86400;
204 // Rather than extrapolating out to a bajillion...
207 $daily_count = round($notice_count / $age_days);
209 $this->elementStart('div', array('id' => 'entity_statistics',
210 'class' => 'section'));
212 // TRANS: H2 text for user statistics.
213 $this->element('h2', null, _('Statistics'));
215 $profile = $this->target;
216 $actionParams = array('nickname' => $profile->nickname);
220 // TRANS: Label for user statistics.
221 'label' => _('User ID'),
222 'value' => $profile->id,
225 'id' => 'member-since',
226 // TRANS: Label for user statistics.
227 'label' => _('Member since'),
228 'value' => date('j M Y', strtotime($profile->created))
232 // TRANS: Label for user statistics.
233 'label' => _('Notices'),
234 'value' => $notice_count,
237 'id' => 'daily_notices',
238 // TRANS: Label for user statistics.
239 // TRANS: Average count of posts made per day since account registration.
240 'label' => _('Daily average'),
241 'value' => $daily_count
245 // Give plugins a chance to add stats entries
246 Event::handle('ProfileStats', array($profile, &$stats));
248 foreach ($stats as $row) {
249 $this->showStatsRow($row);
251 $this->elementEnd('div');
254 private function showStatsRow($row)
256 $this->elementStart('dl', 'entity_' . $row['id']);
257 $this->elementStart('dt');
258 if (!empty($row['link'])) {
259 $this->element('a', array('href' => $row['link']), $row['label']);
261 $this->text($row['label']);
263 $this->elementEnd('dt');
264 $this->element('dd', null, $row['value']);
265 $this->elementEnd('dl');
268 function showGroups()
270 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
272 $this->elementStart('div', array('id' => 'entity_groups',
273 'class' => 'section'));
274 if (Event::handle('StartShowGroupsMiniList', array($this))) {
275 $this->elementStart('h2');
276 // TRANS: H2 text for user group membership statistics.
277 $this->statsSectionLink('usergroups', _('Groups'));
279 $this->text($this->target->getGroupCount());
280 $this->elementEnd('h2');
282 if ($groups instanceof User_group) {
283 $gml = new GroupMiniList($groups, $this->target, $this);
286 // TRANS: Text for user user group membership statistics if user is not a member of any group.
287 $this->element('p', null, _('(None)'));
290 Event::handle('EndShowGroupsMiniList', array($this));
292 $this->elementEnd('div');
297 $lists = $this->target->getLists($this->scoped);
300 $this->elementStart('div', array('id' => 'entity_lists',
301 'class' => 'section'));
303 if (Event::handle('StartShowListsMiniList', array($this))) {
305 $url = common_local_url('peopletagsbyuser',
306 array('nickname' => $this->target->getNickname()));
308 $this->elementStart('h2');
310 array('href' => $url),
311 // TRANS: H2 text for user list membership statistics.
314 $this->text($lists->N);
315 $this->elementEnd('h2');
317 $this->elementStart('ul');
322 while ($lists->fetch()) {
323 if (!empty($lists->mainpage)) {
324 $url = $lists->mainpage;
326 $url = common_local_url('showprofiletag',
327 array('tagger' => $this->target->getNickname(),
328 'tag' => $lists->tag));
336 $this->element('a', array('href' => $url),
340 $this->elementEnd('ul');
342 Event::handle('EndShowListsMiniList', array($this));
344 $this->elementEnd('div');
349 class SubscribersMiniList extends ProfileMiniList
351 function newListItem($profile)
353 return new SubscribersMiniListItem($profile, $this->action);
357 class SubscribersMiniListItem extends ProfileMiniListItem
359 function linkAttributes()
361 $aAttrs = parent::linkAttributes();
362 if (common_config('nofollow', 'subscribers')) {
363 $aAttrs['rel'] .= ' nofollow';