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 $this->tag = $this->trimmed('tag');
62 $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
63 common_set_returnto($this->selfUrl());
68 protected function profileActionPreparation()
70 // Nothing to do by default.
73 public function getTarget()
78 function isReadOnly($args)
83 function showSections()
85 $this->showSubscriptions();
86 $this->showSubscribers();
89 $this->showStatistics();
93 * Convenience function for common pattern of links to subscription/groups sections.
95 * @param string $actionClass
96 * @param string $title
97 * @param string $cssClass
99 private function statsSectionLink($actionClass, $title, $cssClass='')
101 $this->element('a', array('href' => common_local_url($actionClass,
102 array('nickname' => $this->target->getNickname())),
103 'class' => $cssClass),
107 function showSubscriptions()
109 $this->elementStart('div', array('id' => 'entity_subscriptions',
110 'class' => 'section'));
111 if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
112 $this->elementStart('h2');
113 // TRANS: H2 text for user subscription statistics.
114 $this->statsSectionLink('subscriptions', _('Following'));
116 $this->text($this->target->subscriptionCount());
117 $this->elementEnd('h2');
120 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
121 $pml = new ProfileMiniList($profile, $this);
123 } catch (NoResultException $e) {
124 // TRANS: Text for user subscription statistics if the user has no subscription
125 $this->element('p', null, _('(None)'));
128 Event::handle('EndShowSubscriptionsMiniList', array($this));
130 $this->elementEnd('div');
133 function showSubscribers()
135 $this->elementStart('div', array('id' => 'entity_subscribers',
136 'class' => 'section'));
138 if (Event::handle('StartShowSubscribersMiniList', array($this))) {
140 $this->elementStart('h2');
141 // TRANS: H2 text for user subscriber statistics.
142 $this->statsSectionLink('subscribers', _('Followers'));
144 $this->text($this->target->subscriberCount());
145 $this->elementEnd('h2');
148 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
149 $sml = new SubscribersMiniList($profile, $this);
151 } catch (NoResultException $e) {
152 // TRANS: Text for user subscriber statistics if user has no subscribers.
153 $this->element('p', null, _('(None)'));
156 Event::handle('EndShowSubscribersMiniList', array($this));
159 $this->elementEnd('div');
162 function showStatistics()
164 $notice_count = $this->target->noticeCount();
165 $age_days = (time() - strtotime($this->target->created)) / 86400;
167 // Rather than extrapolating out to a bajillion...
170 $daily_count = round($notice_count / $age_days);
172 $this->elementStart('div', array('id' => 'entity_statistics',
173 'class' => 'section'));
175 // TRANS: H2 text for user statistics.
176 $this->element('h2', null, _('Statistics'));
178 $profile = $this->target;
179 $actionParams = array('nickname' => $profile->nickname);
183 // TRANS: Label for user statistics.
184 'label' => _('User ID'),
185 'value' => $profile->id,
188 'id' => 'member-since',
189 // TRANS: Label for user statistics.
190 'label' => _('Member since'),
191 'value' => date('j M Y', strtotime($profile->created))
195 // TRANS: Label for user statistics.
196 'label' => _('Notices'),
197 'value' => $notice_count,
200 'id' => 'daily_notices',
201 // TRANS: Label for user statistics.
202 // TRANS: Average count of posts made per day since account registration.
203 'label' => _('Daily average'),
204 'value' => $daily_count
208 // Give plugins a chance to add stats entries
209 Event::handle('ProfileStats', array($profile, &$stats));
211 foreach ($stats as $row) {
212 $this->showStatsRow($row);
214 $this->elementEnd('div');
217 private function showStatsRow($row)
219 $this->elementStart('dl', 'entity_' . $row['id']);
220 $this->elementStart('dt');
221 if (!empty($row['link'])) {
222 $this->element('a', array('href' => $row['link']), $row['label']);
224 $this->text($row['label']);
226 $this->elementEnd('dt');
227 $this->element('dd', null, $row['value']);
228 $this->elementEnd('dl');
231 function showGroups()
233 $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
235 $this->elementStart('div', array('id' => 'entity_groups',
236 'class' => 'section'));
237 if (Event::handle('StartShowGroupsMiniList', array($this))) {
238 $this->elementStart('h2');
239 // TRANS: H2 text for user group membership statistics.
240 $this->statsSectionLink('usergroups', _('Groups'));
242 $this->text($this->target->getGroupCount());
243 $this->elementEnd('h2');
245 if ($groups instanceof User_group) {
246 $gml = new GroupMiniList($groups, $this->target, $this);
249 // TRANS: Text for user user group membership statistics if user is not a member of any group.
250 $this->element('p', null, _('(None)'));
253 Event::handle('EndShowGroupsMiniList', array($this));
255 $this->elementEnd('div');
260 $lists = $this->target->getLists($this->scoped);
263 $this->elementStart('div', array('id' => 'entity_lists',
264 'class' => 'section'));
266 if (Event::handle('StartShowListsMiniList', array($this))) {
268 $url = common_local_url('peopletagsbyuser',
269 array('nickname' => $this->target->getNickname()));
271 $this->elementStart('h2');
273 array('href' => $url),
274 // TRANS: H2 text for user list membership statistics.
277 $this->text($lists->N);
278 $this->elementEnd('h2');
280 $this->elementStart('ul');
285 while ($lists->fetch()) {
286 if (!empty($lists->mainpage)) {
287 $url = $lists->mainpage;
289 $url = common_local_url('showprofiletag',
290 array('tagger' => $this->target->getNickname(),
291 'tag' => $lists->tag));
299 $this->element('a', array('href' => $url),
303 $this->elementEnd('ul');
305 Event::handle('EndShowListsMiniList', array($this));
307 $this->elementEnd('div');
312 class SubscribersMiniList extends ProfileMiniList
314 function newListItem($profile)
316 return new SubscribersMiniListItem($profile, $this->action);
320 class SubscribersMiniListItem extends ProfileMiniListItem
322 function linkAttributes()
324 $aAttrs = parent::linkAttributes();
325 if (common_config('nofollow', 'subscribers')) {
326 $aAttrs['rel'] .= ' nofollow';