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 ManagedAction
54 protected $target = null; // Profile that we're showing
56 protected function prepare(array $args=array())
58 parent::prepare($args);
61 $nickname_arg = $this->arg('nickname');
62 $nickname = common_canonical_nickname($nickname_arg);
64 // Permanent redirect on non-canonical nickname
66 if ($nickname_arg != $nickname) {
67 $args = array('nickname' => $nickname);
68 if ($this->arg('page') && $this->arg('page') != 1) {
69 $args['page'] = $this->arg['page'];
71 common_redirect(common_local_url($this->getActionName(), $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);
80 $this->target = $this->user->getProfile();
81 } catch (NicknameException $e) {
82 $id = (int)$this->arg('id');
83 $this->target = Profile::getKV('id', $id);
85 if (!$this->target instanceof Profile) {
86 // TRANS: Error message displayed when referring to a user without a profile.
87 $this->serverError(_m('Profile ID does not exist.'));
90 if ($this->target->isLocal()) {
91 // For local users when accessed by id number, redirect to
92 // the same action but using the nickname as argument.
93 common_redirect(common_local_url($this->getActionName(),
94 array('nickname'=>$user->getNickname())));
98 if ($this->target->hasRole(Profile_role::SILENCED) &&
99 (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
100 throw new ClientException(_('This profile has been silenced by site moderators'), 403);
103 // backwards compatibility until all actions are fixed to use $this->target
104 $this->profile = $this->target;
106 $this->tag = $this->trimmed('tag');
107 $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
108 common_set_returnto($this->selfUrl());
112 function isReadOnly($args)
117 function showSections()
119 $this->showSubscriptions();
120 $this->showSubscribers();
123 $this->showStatistics();
127 * Convenience function for common pattern of links to subscription/groups sections.
129 * @param string $actionClass
130 * @param string $title
131 * @param string $cssClass
133 private function statsSectionLink($actionClass, $title, $cssClass='')
135 $this->element('a', array('href' => common_local_url($actionClass,
136 array('nickname' => $this->target->getNickname())),
137 'class' => $cssClass),
141 function showSubscriptions()
143 $this->elementStart('div', array('id' => 'entity_subscriptions',
144 'class' => 'section'));
145 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
146 $this->elementStart('h2');
147 // TRANS: H2 text for user subscription statistics.
148 $this->statsSectionLink('subscriptions', _('Following'));
150 $this->text($this->target->subscriptionCount());
151 $this->elementEnd('h2');
154 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
155 $pml = new ProfileMiniList($profile, $this);
157 } catch (NoResultException $e) {
158 // TRANS: Text for user subscription statistics if the user has no subscription
159 $this->element('p', null, _('(None)'));
162 Event::handle('EndShowSubscriptionsMiniList', array($this));
164 $this->elementEnd('div');
167 function showSubscribers()
169 $this->elementStart('div', array('id' => 'entity_subscribers',
170 'class' => 'section'));
172 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
174 $this->elementStart('h2');
175 // TRANS: H2 text for user subscriber statistics.
176 $this->statsSectionLink('subscribers', _('Followers'));
178 $this->text($this->target->subscriberCount());
179 $this->elementEnd('h2');
182 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
183 $sml = new SubscribersMiniList($profile, $this);
185 } catch (NoResultException $e) {
186 // TRANS: Text for user subscriber statistics if user has no subscribers.
187 $this->element('p', null, _('(None)'));
190 Event::handle('EndShowSubscribersMiniList', array($this));
193 $this->elementEnd('div');
196 function showStatistics()
198 $notice_count = $this->target->noticeCount();
199 $age_days = (time() - strtotime($this->target->created)) / 86400;
201 // Rather than extrapolating out to a bajillion...
204 $daily_count = round($notice_count / $age_days);
206 $this->elementStart('div', array('id' => 'entity_statistics',
207 'class' => 'section'));
209 // TRANS: H2 text for user statistics.
210 $this->element('h2', null, _('Statistics'));
212 $profile = $this->target;
213 $actionParams = array('nickname' => $profile->nickname);
217 // TRANS: Label for user statistics.
218 'label' => _('User ID'),
219 'value' => $profile->id,
222 'id' => 'member-since',
223 // TRANS: Label for user statistics.
224 'label' => _('Member since'),
225 'value' => date('j M Y', strtotime($profile->created))
229 // TRANS: Label for user statistics.
230 'label' => _('Notices'),
231 'value' => $notice_count,
234 'id' => 'daily_notices',
235 // TRANS: Label for user statistics.
236 // TRANS: Average count of posts made per day since account registration.
237 'label' => _('Daily average'),
238 'value' => $daily_count
242 // Give plugins a chance to add stats entries
243 Event::handle('ProfileStats', array($profile, &$stats));
245 foreach ($stats as $row) {
246 $this->showStatsRow($row);
248 $this->elementEnd('div');
251 private function showStatsRow($row)
253 $this->elementStart('dl', 'entity_' . $row['id']);
254 $this->elementStart('dt');
255 if (!empty($row['link'])) {
256 $this->element('a', array('href' => $row['link']), $row['label']);
258 $this->text($row['label']);
260 $this->elementEnd('dt');
261 $this->element('dd', null, $row['value']);
262 $this->elementEnd('dl');
265 function showGroups()
267 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
269 $this->elementStart('div', array('id' => 'entity_groups',
270 'class' => 'section'));
271 if (Event::handle('StartShowGroupsMiniList', array($this))) {
272 $this->elementStart('h2');
273 // TRANS: H2 text for user group membership statistics.
274 $this->statsSectionLink('usergroups', _('Groups'));
276 $this->text($this->target->getGroupCount());
277 $this->elementEnd('h2');
279 if ($groups instanceof User_group) {
280 $gml = new GroupMiniList($groups, $this->target, $this);
283 // TRANS: Text for user user group membership statistics if user is not a member of any group.
284 $this->element('p', null, _('(None)'));
287 Event::handle('EndShowGroupsMiniList', array($this));
289 $this->elementEnd('div');
294 $lists = $this->target->getLists($this->scoped);
297 $this->elementStart('div', array('id' => 'entity_lists',
298 'class' => 'section'));
300 if (Event::handle('StartShowListsMiniList', array($this))) {
302 $url = common_local_url('peopletagsbyuser',
303 array('nickname' => $this->target->getNickname()));
305 $this->elementStart('h2');
307 array('href' => $url),
308 // TRANS: H2 text for user list membership statistics.
311 $this->text($lists->N);
312 $this->elementEnd('h2');
314 $this->elementStart('ul');
319 while ($lists->fetch()) {
320 if (!empty($lists->mainpage)) {
321 $url = $lists->mainpage;
323 $url = common_local_url('showprofiletag',
324 array('tagger' => $this->target->getNickname(),
325 'tag' => $lists->tag));
333 $this->element('a', array('href' => $url),
337 $this->elementEnd('ul');
339 Event::handle('EndShowListsMiniList', array($this));
341 $this->elementEnd('div');
346 class SubscribersMiniList extends ProfileMiniList
348 function newListItem($profile)
350 return new SubscribersMiniListItem($profile, $this->action);
354 class SubscribersMiniListItem extends ProfileMiniListItem
356 function linkAttributes()
358 $aAttrs = parent::linkAttributes();
359 if (common_config('nofollow', 'subscribers')) {
360 $aAttrs['rel'] .= ' nofollow';