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 prepare(array $args=array())
53 // this will call ->doPreparation() which child classes use to set $this->target
54 parent::prepare($args);
56 if ($this->target->hasRole(Profile_role::SILENCED)
57 && (!$this->scoped instanceof Profile || !$this->scoped->hasRight(Right::SILENCEUSER))) {
58 throw new ClientException(_('This profile has been silenced by site moderators'), 403);
61 // backwards compatibility until all actions are fixed to use $this->target
62 $this->profile = $this->target;
64 $this->tag = $this->trimmed('tag');
65 $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
66 common_set_returnto($this->selfUrl());
71 protected function profileActionPreparation()
73 // Nothing to do by default.
76 public function getTarget()
81 function isReadOnly($args)
86 function showSections()
88 $this->showSubscriptions();
89 $this->showSubscribers();
92 $this->showStatistics();
96 * Convenience function for common pattern of links to subscription/groups sections.
98 * @param string $actionClass
99 * @param string $title
100 * @param string $cssClass
102 private function statsSectionLink($actionClass, $title, $cssClass='')
104 $this->element('a', array('href' => common_local_url($actionClass,
105 array('nickname' => $this->target->getNickname())),
106 'class' => $cssClass),
110 function showSubscriptions()
112 $this->elementStart('div', array('id' => 'entity_subscriptions',
113 'class' => 'section'));
114 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
115 $this->elementStart('h2');
116 // TRANS: H2 text for user subscription statistics.
117 $this->statsSectionLink('subscriptions', _('Following'));
119 $this->text($this->target->subscriptionCount());
120 $this->elementEnd('h2');
123 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
124 $pml = new ProfileMiniList($profile, $this);
126 } catch (NoResultException $e) {
127 // TRANS: Text for user subscription statistics if the user has no subscription
128 $this->element('p', null, _('(None)'));
131 Event::handle('EndShowSubscriptionsMiniList', array($this));
133 $this->elementEnd('div');
136 function showSubscribers()
138 $this->elementStart('div', array('id' => 'entity_subscribers',
139 'class' => 'section'));
141 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
143 $this->elementStart('h2');
144 // TRANS: H2 text for user subscriber statistics.
145 $this->statsSectionLink('subscribers', _('Followers'));
147 $this->text($this->target->subscriberCount());
148 $this->elementEnd('h2');
151 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
152 $sml = new SubscribersMiniList($profile, $this);
154 } catch (NoResultException $e) {
155 // TRANS: Text for user subscriber statistics if user has no subscribers.
156 $this->element('p', null, _('(None)'));
159 Event::handle('EndShowSubscribersMiniList', array($this));
162 $this->elementEnd('div');
165 function showStatistics()
167 $notice_count = $this->target->noticeCount();
168 $age_days = (time() - strtotime($this->target->created)) / 86400;
170 // Rather than extrapolating out to a bajillion...
173 $daily_count = round($notice_count / $age_days);
175 $this->elementStart('div', array('id' => 'entity_statistics',
176 'class' => 'section'));
178 // TRANS: H2 text for user statistics.
179 $this->element('h2', null, _('Statistics'));
181 $profile = $this->target;
182 $actionParams = array('nickname' => $profile->nickname);
186 // TRANS: Label for user statistics.
187 'label' => _('User ID'),
188 'value' => $profile->id,
191 'id' => 'member-since',
192 // TRANS: Label for user statistics.
193 'label' => _('Member since'),
194 'value' => date('j M Y', strtotime($profile->created))
198 // TRANS: Label for user statistics.
199 'label' => _('Notices'),
200 'value' => $notice_count,
203 'id' => 'daily_notices',
204 // TRANS: Label for user statistics.
205 // TRANS: Average count of posts made per day since account registration.
206 'label' => _('Daily average'),
207 'value' => $daily_count
211 // Give plugins a chance to add stats entries
212 Event::handle('ProfileStats', array($profile, &$stats));
214 foreach ($stats as $row) {
215 $this->showStatsRow($row);
217 $this->elementEnd('div');
220 private function showStatsRow($row)
222 $this->elementStart('dl', 'entity_' . $row['id']);
223 $this->elementStart('dt');
224 if (!empty($row['link'])) {
225 $this->element('a', array('href' => $row['link']), $row['label']);
227 $this->text($row['label']);
229 $this->elementEnd('dt');
230 $this->element('dd', null, $row['value']);
231 $this->elementEnd('dl');
234 function showGroups()
236 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
238 $this->elementStart('div', array('id' => 'entity_groups',
239 'class' => 'section'));
240 if (Event::handle('StartShowGroupsMiniList', array($this))) {
241 $this->elementStart('h2');
242 // TRANS: H2 text for user group membership statistics.
243 $this->statsSectionLink('usergroups', _('Groups'));
245 $this->text($this->target->getGroupCount());
246 $this->elementEnd('h2');
248 if ($groups instanceof User_group) {
249 $gml = new GroupMiniList($groups, $this->target, $this);
252 // TRANS: Text for user user group membership statistics if user is not a member of any group.
253 $this->element('p', null, _('(None)'));
256 Event::handle('EndShowGroupsMiniList', array($this));
258 $this->elementEnd('div');
263 $lists = $this->target->getLists($this->scoped);
266 $this->elementStart('div', array('id' => 'entity_lists',
267 'class' => 'section'));
269 if (Event::handle('StartShowListsMiniList', array($this))) {
271 $url = common_local_url('peopletagsbyuser',
272 array('nickname' => $this->target->getNickname()));
274 $this->elementStart('h2');
276 array('href' => $url),
277 // TRANS: H2 text for user list membership statistics.
280 $this->text($lists->N);
281 $this->elementEnd('h2');
283 $this->elementStart('ul');
288 while ($lists->fetch()) {
289 if (!empty($lists->mainpage)) {
290 $url = $lists->mainpage;
292 $url = common_local_url('showprofiletag',
293 array('tagger' => $this->target->getNickname(),
294 'tag' => $lists->tag));
302 $this->element('a', array('href' => $url),
306 $this->elementEnd('ul');
308 Event::handle('EndShowListsMiniList', array($this));
310 $this->elementEnd('div');
315 class SubscribersMiniList extends ProfileMiniList
317 function newListItem($profile)
319 return new SubscribersMiniListItem($profile, $this->action);
323 class SubscribersMiniListItem extends ProfileMiniListItem
325 function linkAttributes()
327 $aAttrs = parent::linkAttributes();
328 if (common_config('nofollow', 'subscribers')) {
329 $aAttrs['rel'] .= ' nofollow';