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 implements NoticestreamAction
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());
68 // fetch the actual stream stuff
69 $stream = $this->getStream();
70 $this->notice = $stream->getNotices(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
72 if ($this->page > 1 && $this->notice->N == 0) {
73 // TRANS: Client error when page not found (404).
74 $this->clientError(_('No such page.'), 404);
81 protected function profileActionPreparation()
83 // Nothing to do by default.
86 public function getTarget()
91 function isReadOnly(array $args=array())
96 function showSections()
98 $this->showSubscriptions();
99 $this->showSubscribers();
102 $this->showStatistics();
106 * Convenience function for common pattern of links to subscription/groups sections.
108 * @param string $actionClass
109 * @param string $title
110 * @param string $cssClass
112 private function statsSectionLink($actionClass, $title, $cssClass='')
114 $this->element('a', array('href' => common_local_url($actionClass,
115 array('nickname' => $this->target->getNickname())),
116 'class' => $cssClass),
120 function showSubscriptions()
122 $this->elementStart('div', array('id' => 'entity_subscriptions',
123 'class' => 'section'));
124 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
125 $this->elementStart('h2');
126 // TRANS: H2 text for user subscription statistics.
127 $this->statsSectionLink('subscriptions', _('Following'));
129 $this->text($this->target->subscriptionCount());
130 $this->elementEnd('h2');
133 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
134 $pml = new ProfileMiniList($profile, $this);
136 } catch (NoResultException $e) {
137 // TRANS: Text for user subscription statistics if the user has no subscription
138 $this->element('p', null, _('(None)'));
141 Event::handle('EndShowSubscriptionsMiniList', array($this));
143 $this->elementEnd('div');
146 function showSubscribers()
148 $this->elementStart('div', array('id' => 'entity_subscribers',
149 'class' => 'section'));
151 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
153 $this->elementStart('h2');
154 // TRANS: H2 text for user subscriber statistics.
155 $this->statsSectionLink('subscribers', _('Followers'));
157 $this->text($this->target->subscriberCount());
158 $this->elementEnd('h2');
161 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
162 $sml = new SubscribersMiniList($profile, $this);
164 } catch (NoResultException $e) {
165 // TRANS: Text for user subscriber statistics if user has no subscribers.
166 $this->element('p', null, _('(None)'));
169 Event::handle('EndShowSubscribersMiniList', array($this));
172 $this->elementEnd('div');
175 function showStatistics()
177 $notice_count = $this->target->noticeCount();
178 $age_days = (time() - strtotime($this->target->created)) / 86400;
180 // Rather than extrapolating out to a bajillion...
183 $daily_count = round($notice_count / $age_days);
185 $this->elementStart('div', array('id' => 'entity_statistics',
186 'class' => 'section'));
188 // TRANS: H2 text for user statistics.
189 $this->element('h2', null, _('Statistics'));
191 $actionParams = array('nickname' => $this->target->nickname);
195 // TRANS: Label for user statistics.
196 'label' => _('User ID'),
197 'value' => $this->target->id,
200 'id' => 'member-since',
201 // TRANS: Label for user statistics.
202 'label' => _('Member since'),
203 'value' => date('j M Y', strtotime($this->target->created))
207 // TRANS: Label for user statistics.
208 'label' => _('Notices'),
209 'value' => $notice_count,
212 'id' => 'daily_notices',
213 // TRANS: Label for user statistics.
214 // TRANS: Average count of posts made per day since account registration.
215 'label' => _('Daily average'),
216 'value' => $daily_count
220 // Give plugins a chance to add stats entries
221 Event::handle('ProfileStats', array($this->target, &$stats));
223 foreach ($stats as $row) {
224 $this->showStatsRow($row);
226 $this->elementEnd('div');
229 private function showStatsRow($row)
231 $this->elementStart('dl', 'entity_' . $row['id']);
232 $this->elementStart('dt');
233 if (!empty($row['link'])) {
234 $this->element('a', array('href' => $row['link']), $row['label']);
236 $this->text($row['label']);
238 $this->elementEnd('dt');
239 $this->element('dd', null, $row['value']);
240 $this->elementEnd('dl');
243 function showGroups()
245 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
247 $this->elementStart('div', array('id' => 'entity_groups',
248 'class' => 'section'));
249 if (Event::handle('StartShowGroupsMiniList', array($this))) {
250 $this->elementStart('h2');
251 // TRANS: H2 text for user group membership statistics.
252 $this->statsSectionLink('usergroups', _('Groups'));
254 $this->text($this->target->getGroupCount());
255 $this->elementEnd('h2');
257 if ($groups instanceof User_group) {
258 $gml = new GroupMiniList($groups, $this->target, $this);
261 // TRANS: Text for user user group membership statistics if user is not a member of any group.
262 $this->element('p', null, _('(None)'));
265 Event::handle('EndShowGroupsMiniList', array($this));
267 $this->elementEnd('div');
272 $lists = $this->target->getLists($this->scoped);
275 $this->elementStart('div', array('id' => 'entity_lists',
276 'class' => 'section'));
278 if (Event::handle('StartShowListsMiniList', array($this))) {
280 $url = common_local_url('peopletagsbyuser',
281 array('nickname' => $this->target->getNickname()));
283 $this->elementStart('h2');
285 array('href' => $url),
286 // TRANS: H2 text for user list membership statistics.
289 $this->text($lists->N);
290 $this->elementEnd('h2');
292 $this->elementStart('ul');
297 while ($lists->fetch()) {
298 if (!empty($lists->mainpage)) {
299 $url = $lists->mainpage;
301 $url = common_local_url('showprofiletag',
302 array('tagger' => $this->target->getNickname(),
303 'tag' => $lists->tag));
311 $this->element('a', array('href' => $url),
315 $this->elementEnd('ul');
317 Event::handle('EndShowListsMiniList', array($this));
319 $this->elementEnd('div');
324 class SubscribersMiniList extends ProfileMiniList
326 function newListItem(Profile $profile)
328 return new SubscribersMiniListItem($profile, $this->action);
332 class SubscribersMiniListItem extends ProfileMiniListItem
334 function linkAttributes()
336 $aAttrs = parent::linkAttributes();
337 if (common_config('nofollow', 'subscribers')) {
338 $aAttrs['rel'] .= ' nofollow';