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 // TRANS: Client error displayed when calling a profile action without specifying a user.
70 $this->clientError(_('No such user.'), 404);
73 $this->target = $this->user->getProfile();
74 } catch (NicknameException $e) {
75 $id = (int)$this->arg('id');
76 $this->target = Profile::getKV('id', $id);
78 if (!$this->target instanceof Profile) {
79 // TRANS: Error message displayed when referring to a user without a profile.
80 $this->serverError(_m('Profile ID does not exist.'));
83 if ($this->target->isLocal()) {
84 // For local users when accessed by id number, redirect to
85 // the same action but using the nickname as argument.
86 common_redirect(common_local_url($this->getActionName(),
87 array('nickname'=>$user->getNickname())));
91 if ($this->target->hasRole(Profile_role::SILENCED) &&
92 (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
93 throw new ClientException(_('This profile has been silenced by site moderators'), 403);
96 // backwards compatibility until all actions are fixed to use $this->target
97 $this->profile = $this->target;
99 $this->tag = $this->trimmed('tag');
100 $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
101 common_set_returnto($this->selfUrl());
103 return $this->profileActionPreparation();
106 protected function profileActionPreparation()
111 function isReadOnly($args)
116 function showSections()
118 $this->showSubscriptions();
119 $this->showSubscribers();
122 $this->showStatistics();
126 * Convenience function for common pattern of links to subscription/groups sections.
128 * @param string $actionClass
129 * @param string $title
130 * @param string $cssClass
132 private function statsSectionLink($actionClass, $title, $cssClass='')
134 $this->element('a', array('href' => common_local_url($actionClass,
135 array('nickname' => $this->target->getNickname())),
136 'class' => $cssClass),
140 function showSubscriptions()
142 $this->elementStart('div', array('id' => 'entity_subscriptions',
143 'class' => 'section'));
144 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
145 $this->elementStart('h2');
146 // TRANS: H2 text for user subscription statistics.
147 $this->statsSectionLink('subscriptions', _('Following'));
149 $this->text($this->target->subscriptionCount());
150 $this->elementEnd('h2');
153 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
154 $pml = new ProfileMiniList($profile, $this);
156 } catch (NoResultException $e) {
157 // TRANS: Text for user subscription statistics if the user has no subscription
158 $this->element('p', null, _('(None)'));
161 Event::handle('EndShowSubscriptionsMiniList', array($this));
163 $this->elementEnd('div');
166 function showSubscribers()
168 $this->elementStart('div', array('id' => 'entity_subscribers',
169 'class' => 'section'));
171 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
173 $this->elementStart('h2');
174 // TRANS: H2 text for user subscriber statistics.
175 $this->statsSectionLink('subscribers', _('Followers'));
177 $this->text($this->target->subscriberCount());
178 $this->elementEnd('h2');
181 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
182 $sml = new SubscribersMiniList($profile, $this);
184 } catch (NoResultException $e) {
185 // TRANS: Text for user subscriber statistics if user has no subscribers.
186 $this->element('p', null, _('(None)'));
189 Event::handle('EndShowSubscribersMiniList', array($this));
192 $this->elementEnd('div');
195 function showStatistics()
197 $notice_count = $this->target->noticeCount();
198 $age_days = (time() - strtotime($this->target->created)) / 86400;
200 // Rather than extrapolating out to a bajillion...
203 $daily_count = round($notice_count / $age_days);
205 $this->elementStart('div', array('id' => 'entity_statistics',
206 'class' => 'section'));
208 // TRANS: H2 text for user statistics.
209 $this->element('h2', null, _('Statistics'));
211 $profile = $this->target;
212 $actionParams = array('nickname' => $profile->nickname);
216 // TRANS: Label for user statistics.
217 'label' => _('User ID'),
218 'value' => $profile->id,
221 'id' => 'member-since',
222 // TRANS: Label for user statistics.
223 'label' => _('Member since'),
224 'value' => date('j M Y', strtotime($profile->created))
228 // TRANS: Label for user statistics.
229 'label' => _('Notices'),
230 'value' => $notice_count,
233 'id' => 'daily_notices',
234 // TRANS: Label for user statistics.
235 // TRANS: Average count of posts made per day since account registration.
236 'label' => _('Daily average'),
237 'value' => $daily_count
241 // Give plugins a chance to add stats entries
242 Event::handle('ProfileStats', array($profile, &$stats));
244 foreach ($stats as $row) {
245 $this->showStatsRow($row);
247 $this->elementEnd('div');
250 private function showStatsRow($row)
252 $this->elementStart('dl', 'entity_' . $row['id']);
253 $this->elementStart('dt');
254 if (!empty($row['link'])) {
255 $this->element('a', array('href' => $row['link']), $row['label']);
257 $this->text($row['label']);
259 $this->elementEnd('dt');
260 $this->element('dd', null, $row['value']);
261 $this->elementEnd('dl');
264 function showGroups()
266 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
268 $this->elementStart('div', array('id' => 'entity_groups',
269 'class' => 'section'));
270 if (Event::handle('StartShowGroupsMiniList', array($this))) {
271 $this->elementStart('h2');
272 // TRANS: H2 text for user group membership statistics.
273 $this->statsSectionLink('usergroups', _('Groups'));
275 $this->text($this->target->getGroupCount());
276 $this->elementEnd('h2');
278 if ($groups instanceof User_group) {
279 $gml = new GroupMiniList($groups, $this->target, $this);
282 // TRANS: Text for user user group membership statistics if user is not a member of any group.
283 $this->element('p', null, _('(None)'));
286 Event::handle('EndShowGroupsMiniList', array($this));
288 $this->elementEnd('div');
293 $lists = $this->target->getLists($this->scoped);
296 $this->elementStart('div', array('id' => 'entity_lists',
297 'class' => 'section'));
299 if (Event::handle('StartShowListsMiniList', array($this))) {
301 $url = common_local_url('peopletagsbyuser',
302 array('nickname' => $this->target->getNickname()));
304 $this->elementStart('h2');
306 array('href' => $url),
307 // TRANS: H2 text for user list membership statistics.
310 $this->text($lists->N);
311 $this->elementEnd('h2');
313 $this->elementStart('ul');
318 while ($lists->fetch()) {
319 if (!empty($lists->mainpage)) {
320 $url = $lists->mainpage;
322 $url = common_local_url('showprofiletag',
323 array('tagger' => $this->target->getNickname(),
324 'tag' => $lists->tag));
332 $this->element('a', array('href' => $url),
336 $this->elementEnd('ul');
338 Event::handle('EndShowListsMiniList', array($this));
340 $this->elementEnd('div');
345 class SubscribersMiniList extends ProfileMiniList
347 function newListItem($profile)
349 return new SubscribersMiniListItem($profile, $this->action);
353 class SubscribersMiniListItem extends ProfileMiniListItem
355 function linkAttributes()
357 $aAttrs = parent::linkAttributes();
358 if (common_config('nofollow', 'subscribers')) {
359 $aAttrs['rel'] .= ' nofollow';