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('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/profileminilist.php';
36 require_once INSTALLDIR.'/lib/groupminilist.php';
39 * Profile action common superclass
41 * Abstracts out common code from profile and personal tabs
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/
49 class ProfileAction extends Action
55 function prepare($args)
57 parent::prepare($args);
59 $nickname_arg = $this->arg('nickname');
60 $nickname = common_canonical_nickname($nickname_arg);
62 // Permanent redirect on non-canonical nickname
64 if ($nickname_arg != $nickname) {
65 $args = array('nickname' => $nickname);
66 if ($this->arg('page') && $this->arg('page') != 1) {
67 $args['page'] = $this->arg['page'];
69 common_redirect(common_local_url($this->trimmed('action'), $args), 301);
73 $this->user = User::getKV('nickname', $nickname);
76 // TRANS: Client error displayed when calling a profile action without specifying a user.
77 $this->clientError(_('No such user.'), 404);
81 $this->profile = $this->user->getProfile();
83 if (!$this->profile) {
84 // TRANS: Error message displayed when referring to a user without a profile.
85 $this->serverError(_('User has no profile.'));
89 $user = common_current_user();
91 if ($this->profile->hasRole(Profile_role::SILENCED) &&
92 (empty($user) || !$user->hasRight(Right::SILENCEUSER))) {
93 throw new ClientException(_('This profile has been silenced by site moderators'), 403);
96 $this->tag = $this->trimmed('tag');
97 $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
98 common_set_returnto($this->selfUrl());
102 function showSections()
104 $this->showSubscriptions();
105 $this->showSubscribers();
108 $this->showStatistics();
112 * Convenience function for common pattern of links to subscription/groups sections.
114 * @param string $actionClass
115 * @param string $title
116 * @param string $cssClass
118 private function statsSectionLink($actionClass, $title, $cssClass='')
120 $this->element('a', array('href' => common_local_url($actionClass,
121 array('nickname' => $this->profile->nickname)),
122 'class' => $cssClass),
126 function showSubscriptions()
128 $this->elementStart('div', array('id' => 'entity_subscriptions',
129 'class' => 'section'));
130 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
131 $this->elementStart('h2');
132 // TRANS: H2 text for user subscription statistics.
133 $this->statsSectionLink('subscriptions', _('Following'));
135 $this->text($this->profile->subscriptionCount());
136 $this->elementEnd('h2');
139 $profile = $this->profile->getSubscribed(0, PROFILES_PER_MINILIST + 1);
140 $pml = new ProfileMiniList($profile, $this);
142 } catch (NoResultException $e) {
143 // TRANS: Text for user subscription statistics if the user has no subscription
144 $this->element('p', null, _('(None)'));
147 Event::handle('EndShowSubscriptionsMiniList', array($this));
149 $this->elementEnd('div');
152 function showSubscribers()
154 $this->elementStart('div', array('id' => 'entity_subscribers',
155 'class' => 'section'));
157 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
159 $this->elementStart('h2');
160 // TRANS: H2 text for user subscriber statistics.
161 $this->statsSectionLink('subscribers', _('Followers'));
163 $this->text($this->profile->subscriberCount());
164 $this->elementEnd('h2');
167 $profile = $this->profile->getSubscribers(0, PROFILES_PER_MINILIST + 1);
168 $sml = new SubscribersMiniList($profile, $this);
170 } catch (NoResultException $e) {
171 // TRANS: Text for user subscriber statistics if user has no subscribers.
172 $this->element('p', null, _('(None)'));
175 Event::handle('EndShowSubscribersMiniList', array($this));
178 $this->elementEnd('div');
181 function showStatistics()
183 $notice_count = $this->profile->noticeCount();
184 $age_days = (time() - strtotime($this->profile->created)) / 86400;
186 // Rather than extrapolating out to a bajillion...
189 $daily_count = round($notice_count / $age_days);
191 $this->elementStart('div', array('id' => 'entity_statistics',
192 'class' => 'section'));
194 // TRANS: H2 text for user statistics.
195 $this->element('h2', null, _('Statistics'));
197 $profile = $this->profile;
198 $actionParams = array('nickname' => $profile->nickname);
202 // TRANS: Label for user statistics.
203 'label' => _('User ID'),
204 'value' => $profile->id,
207 'id' => 'member-since',
208 // TRANS: Label for user statistics.
209 'label' => _('Member since'),
210 'value' => date('j M Y', strtotime($profile->created))
214 // TRANS: Label for user statistics.
215 'label' => _('Notices'),
216 'value' => $notice_count,
219 'id' => 'daily_notices',
220 // TRANS: Label for user statistics.
221 // TRANS: Average count of posts made per day since account registration.
222 'label' => _('Daily average'),
223 'value' => $daily_count
227 // Give plugins a chance to add stats entries
228 Event::handle('ProfileStats', array($profile, &$stats));
230 foreach ($stats as $row) {
231 $this->showStatsRow($row);
233 $this->elementEnd('div');
236 private function showStatsRow($row)
238 $this->elementStart('dl', 'entity_' . $row['id']);
239 $this->elementStart('dt');
240 if (!empty($row['link'])) {
241 $this->element('a', array('href' => $row['link']), $row['label']);
243 $this->text($row['label']);
245 $this->elementEnd('dt');
246 $this->element('dd', null, $row['value']);
247 $this->elementEnd('dl');
250 function showGroups()
252 $groups = $this->profile->getGroups(0, GROUPS_PER_MINILIST + 1);
254 $this->elementStart('div', array('id' => 'entity_groups',
255 'class' => 'section'));
256 if (Event::handle('StartShowGroupsMiniList', array($this))) {
257 $this->elementStart('h2');
258 // TRANS: H2 text for user group membership statistics.
259 $this->statsSectionLink('usergroups', _('Groups'));
261 $this->text($this->profile->getGroups(0, null)->N);
262 $this->elementEnd('h2');
265 $gml = new GroupMiniList($groups, $this->profile, $this);
268 // TRANS: Text for user user group membership statistics if user is not a member of any group.
269 $this->element('p', null, _('(None)'));
273 Event::handle('EndShowGroupsMiniList', array($this));
275 $this->elementEnd('div');
280 $cur = common_current_user();
282 $lists = $this->profile->getLists($cur);
285 $this->elementStart('div', array('id' => 'entity_lists',
286 'class' => 'section'));
288 if (Event::handle('StartShowListsMiniList', array($this))) {
290 $url = common_local_url('peopletagsbyuser',
291 array('nickname' => $this->profile->nickname));
293 $this->elementStart('h2');
295 array('href' => $url),
296 // TRANS: H2 text for user list membership statistics.
299 $this->text($lists->N);
300 $this->elementEnd('h2');
302 $this->elementStart('ul');
307 while ($lists->fetch()) {
308 if (!empty($lists->mainpage)) {
309 $url = $lists->mainpage;
311 $url = common_local_url('showprofiletag',
312 array('tagger' => $this->profile->nickname,
313 'tag' => $lists->tag));
321 $this->element('a', array('href' => $url),
325 $this->elementEnd('ul');
327 Event::handle('EndShowListsMiniList', array($this));
329 $this->elementEnd('div');
334 class SubscribersMiniList extends ProfileMiniList
336 function newListItem($profile)
338 return new SubscribersMiniListItem($profile, $this->action);
342 class SubscribersMiniListItem extends ProfileMiniListItem
344 function linkAttributes()
346 $aAttrs = parent::linkAttributes();
347 if (common_config('nofollow', 'subscribers')) {
348 $aAttrs['rel'] .= ' nofollow';