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(array $args=array())
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 $actionParams = array('nickname' => $this->target->nickname);
185 // TRANS: Label for user statistics.
186 'label' => _('User ID'),
187 'value' => $this->target->id,
190 'id' => 'member-since',
191 // TRANS: Label for user statistics.
192 'label' => _('Member since'),
193 'value' => date('j M Y', strtotime($this->target->created))
197 // TRANS: Label for user statistics.
198 'label' => _('Notices'),
199 'value' => $notice_count,
202 'id' => 'daily_notices',
203 // TRANS: Label for user statistics.
204 // TRANS: Average count of posts made per day since account registration.
205 'label' => _('Daily average'),
206 'value' => $daily_count
210 // Give plugins a chance to add stats entries
211 Event::handle('ProfileStats', array($this->target, &$stats));
213 foreach ($stats as $row) {
214 $this->showStatsRow($row);
216 $this->elementEnd('div');
219 private function showStatsRow($row)
221 $this->elementStart('dl', 'entity_' . $row['id']);
222 $this->elementStart('dt');
223 if (!empty($row['link'])) {
224 $this->element('a', array('href' => $row['link']), $row['label']);
226 $this->text($row['label']);
228 $this->elementEnd('dt');
229 $this->element('dd', null, $row['value']);
230 $this->elementEnd('dl');
233 function showGroups()
235 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
237 $this->elementStart('div', array('id' => 'entity_groups',
238 'class' => 'section'));
239 if (Event::handle('StartShowGroupsMiniList', array($this))) {
240 $this->elementStart('h2');
241 // TRANS: H2 text for user group membership statistics.
242 $this->statsSectionLink('usergroups', _('Groups'));
244 $this->text($this->target->getGroupCount());
245 $this->elementEnd('h2');
247 if ($groups instanceof User_group) {
248 $gml = new GroupMiniList($groups, $this->target, $this);
251 // TRANS: Text for user user group membership statistics if user is not a member of any group.
252 $this->element('p', null, _('(None)'));
255 Event::handle('EndShowGroupsMiniList', array($this));
257 $this->elementEnd('div');
262 $lists = $this->target->getLists($this->scoped);
265 $this->elementStart('div', array('id' => 'entity_lists',
266 'class' => 'section'));
268 if (Event::handle('StartShowListsMiniList', array($this))) {
270 $url = common_local_url('peopletagsbyuser',
271 array('nickname' => $this->target->getNickname()));
273 $this->elementStart('h2');
275 array('href' => $url),
276 // TRANS: H2 text for user list membership statistics.
279 $this->text($lists->N);
280 $this->elementEnd('h2');
282 $this->elementStart('ul');
287 while ($lists->fetch()) {
288 if (!empty($lists->mainpage)) {
289 $url = $lists->mainpage;
291 $url = common_local_url('showprofiletag',
292 array('tagger' => $this->target->getNickname(),
293 'tag' => $lists->tag));
301 $this->element('a', array('href' => $url),
305 $this->elementEnd('ul');
307 Event::handle('EndShowListsMiniList', array($this));
309 $this->elementEnd('div');
314 class SubscribersMiniList extends ProfileMiniList
316 function newListItem(Profile $profile)
318 return new SubscribersMiniListItem($profile, $this->action);
322 class SubscribersMiniListItem extends ProfileMiniListItem
324 function linkAttributes()
326 $aAttrs = parent::linkAttributes();
327 if (common_config('nofollow', 'subscribers')) {
328 $aAttrs['rel'] .= ' nofollow';