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 lower classes can use
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 $this->profileActionPreparation();
74 protected function profileActionPreparation()
76 // Nothing to do by default.
79 public function getTarget()
84 function isReadOnly($args)
89 function showSections()
91 $this->showSubscriptions();
92 $this->showSubscribers();
95 $this->showStatistics();
99 * Convenience function for common pattern of links to subscription/groups sections.
101 * @param string $actionClass
102 * @param string $title
103 * @param string $cssClass
105 private function statsSectionLink($actionClass, $title, $cssClass='')
107 $this->element('a', array('href' => common_local_url($actionClass,
108 array('nickname' => $this->target->getNickname())),
109 'class' => $cssClass),
113 function showSubscriptions()
115 $this->elementStart('div', array('id' => 'entity_subscriptions',
116 'class' => 'section'));
117 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
118 $this->elementStart('h2');
119 // TRANS: H2 text for user subscription statistics.
120 $this->statsSectionLink('subscriptions', _('Following'));
122 $this->text($this->target->subscriptionCount());
123 $this->elementEnd('h2');
126 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
127 $pml = new ProfileMiniList($profile, $this);
129 } catch (NoResultException $e) {
130 // TRANS: Text for user subscription statistics if the user has no subscription
131 $this->element('p', null, _('(None)'));
134 Event::handle('EndShowSubscriptionsMiniList', array($this));
136 $this->elementEnd('div');
139 function showSubscribers()
141 $this->elementStart('div', array('id' => 'entity_subscribers',
142 'class' => 'section'));
144 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
146 $this->elementStart('h2');
147 // TRANS: H2 text for user subscriber statistics.
148 $this->statsSectionLink('subscribers', _('Followers'));
150 $this->text($this->target->subscriberCount());
151 $this->elementEnd('h2');
154 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
155 $sml = new SubscribersMiniList($profile, $this);
157 } catch (NoResultException $e) {
158 // TRANS: Text for user subscriber statistics if user has no subscribers.
159 $this->element('p', null, _('(None)'));
162 Event::handle('EndShowSubscribersMiniList', array($this));
165 $this->elementEnd('div');
168 function showStatistics()
170 $notice_count = $this->target->noticeCount();
171 $age_days = (time() - strtotime($this->target->created)) / 86400;
173 // Rather than extrapolating out to a bajillion...
176 $daily_count = round($notice_count / $age_days);
178 $this->elementStart('div', array('id' => 'entity_statistics',
179 'class' => 'section'));
181 // TRANS: H2 text for user statistics.
182 $this->element('h2', null, _('Statistics'));
184 $profile = $this->target;
185 $actionParams = array('nickname' => $profile->nickname);
189 // TRANS: Label for user statistics.
190 'label' => _('User ID'),
191 'value' => $profile->id,
194 'id' => 'member-since',
195 // TRANS: Label for user statistics.
196 'label' => _('Member since'),
197 'value' => date('j M Y', strtotime($profile->created))
201 // TRANS: Label for user statistics.
202 'label' => _('Notices'),
203 'value' => $notice_count,
206 'id' => 'daily_notices',
207 // TRANS: Label for user statistics.
208 // TRANS: Average count of posts made per day since account registration.
209 'label' => _('Daily average'),
210 'value' => $daily_count
214 // Give plugins a chance to add stats entries
215 Event::handle('ProfileStats', array($profile, &$stats));
217 foreach ($stats as $row) {
218 $this->showStatsRow($row);
220 $this->elementEnd('div');
223 private function showStatsRow($row)
225 $this->elementStart('dl', 'entity_' . $row['id']);
226 $this->elementStart('dt');
227 if (!empty($row['link'])) {
228 $this->element('a', array('href' => $row['link']), $row['label']);
230 $this->text($row['label']);
232 $this->elementEnd('dt');
233 $this->element('dd', null, $row['value']);
234 $this->elementEnd('dl');
237 function showGroups()
239 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
241 $this->elementStart('div', array('id' => 'entity_groups',
242 'class' => 'section'));
243 if (Event::handle('StartShowGroupsMiniList', array($this))) {
244 $this->elementStart('h2');
245 // TRANS: H2 text for user group membership statistics.
246 $this->statsSectionLink('usergroups', _('Groups'));
248 $this->text($this->target->getGroupCount());
249 $this->elementEnd('h2');
251 if ($groups instanceof User_group) {
252 $gml = new GroupMiniList($groups, $this->target, $this);
255 // TRANS: Text for user user group membership statistics if user is not a member of any group.
256 $this->element('p', null, _('(None)'));
259 Event::handle('EndShowGroupsMiniList', array($this));
261 $this->elementEnd('div');
266 $lists = $this->target->getLists($this->scoped);
269 $this->elementStart('div', array('id' => 'entity_lists',
270 'class' => 'section'));
272 if (Event::handle('StartShowListsMiniList', array($this))) {
274 $url = common_local_url('peopletagsbyuser',
275 array('nickname' => $this->target->getNickname()));
277 $this->elementStart('h2');
279 array('href' => $url),
280 // TRANS: H2 text for user list membership statistics.
283 $this->text($lists->N);
284 $this->elementEnd('h2');
286 $this->elementStart('ul');
291 while ($lists->fetch()) {
292 if (!empty($lists->mainpage)) {
293 $url = $lists->mainpage;
295 $url = common_local_url('showprofiletag',
296 array('tagger' => $this->target->getNickname(),
297 'tag' => $lists->tag));
305 $this->element('a', array('href' => $url),
309 $this->elementEnd('ul');
311 Event::handle('EndShowListsMiniList', array($this));
313 $this->elementEnd('div');
318 class SubscribersMiniList extends ProfileMiniList
320 function newListItem($profile)
322 return new SubscribersMiniListItem($profile, $this->action);
326 class SubscribersMiniListItem extends ProfileMiniListItem
328 function linkAttributes()
330 $aAttrs = parent::linkAttributes();
331 if (common_config('nofollow', 'subscribers')) {
332 $aAttrs['rel'] .= ' nofollow';