no All groups link
[statusnet:mainline.git] / lib / profileaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Common parent of Personal and Profile actions
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 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/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/profileminilist.php';
36 require_once INSTALLDIR.'/lib/groupminilist.php';
37
38 /**
39  * Profile action common superclass
40  *
41  * Abstracts out common code from profile and personal tabs
42  *
43  * @category Personal
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class ProfileAction extends OwnerDesignAction
50 {
51     var $page    = null;
52     var $profile = null;
53     var $tag     = null;
54
55     function prepare($args)
56     {
57         parent::prepare($args);
58
59         $nickname_arg = $this->arg('nickname');
60         $nickname     = common_canonical_nickname($nickname_arg);
61
62         // Permanent redirect on non-canonical nickname
63
64         if ($nickname_arg != $nickname) {
65             $args = array('nickname' => $nickname);
66             if ($this->arg('page') && $this->arg('page') != 1) {
67                 $args['page'] = $this->arg['page'];
68             }
69             common_redirect(common_local_url($this->trimmed('action'), $args), 301);
70             return false;
71         }
72
73         $this->user = User::staticGet('nickname', $nickname);
74
75         if (!$this->user) {
76             // TRANS: Client error displayed when calling a profile action without specifying a user.
77             $this->clientError(_('No such user.'), 404);
78             return false;
79         }
80
81         $this->profile = $this->user->getProfile();
82
83         if (!$this->profile) {
84             // TRANS: Server error displayed when calling a profile action while the specified user does not have a profile.
85             $this->serverError(_('User has no profile.'));
86             return false;
87         }
88
89         $this->tag = $this->trimmed('tag');
90         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
91         common_set_returnto($this->selfUrl());
92         return true;
93     }
94
95     function showSections()
96     {
97         $this->showSubscriptions();
98         $this->showSubscribers();
99         $this->showGroups();
100         $this->showLists();
101         $this->showStatistics();
102     }
103
104     /**
105      * Convenience function for common pattern of links to subscription/groups sections.
106      *
107      * @param string $actionClass
108      * @param string $title
109      * @param string $cssClass
110      */
111     private function statsSectionLink($actionClass, $title, $cssClass='')
112     {
113         $this->element('a', array('href' => common_local_url($actionClass,
114                                                              array('nickname' => $this->profile->nickname)),
115                                   'class' => $cssClass),
116                        $title);
117     }
118
119     function showSubscriptions()
120     {
121         $profile = $this->profile->getSubscriptions(0, PROFILES_PER_MINILIST + 1);
122
123         $this->elementStart('div', array('id' => 'entity_subscriptions',
124                                          'class' => 'section'));
125         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
126             $this->elementStart('h2');
127             // TRANS: H2 text for user subscription statistics.
128             $this->statsSectionLink('subscriptions', _('Following'));
129             $this->text(' ');
130             $this->text($this->profile->subscriptionCount());
131             $this->elementEnd('h2');
132
133             $cnt = 0;
134
135             if (!empty($profile)) {
136                 $pml = new ProfileMiniList($profile, $this);
137                 $cnt = $pml->show();
138                 if ($cnt == 0) {
139                     // TRANS: Text for user subscription statistics if the user has no subscriptions.
140                     $this->element('p', null, _('(None)'));
141                 }
142             }
143
144             Event::handle('EndShowSubscriptionsMiniList', array($this));
145         }
146         $this->elementEnd('div');
147     }
148
149     function showSubscribers()
150     {
151         $profile = $this->profile->getSubscribers(0, PROFILES_PER_MINILIST + 1);
152
153         $this->elementStart('div', array('id' => 'entity_subscribers',
154                                          'class' => 'section'));
155
156         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
157
158             $this->elementStart('h2');
159             // TRANS: H2 text for user subscriber statistics.
160             $this->statsSectionLink('subscribers', _('Followers'));
161             $this->text(' ');
162             $this->text($this->profile->subscriberCount());
163             $this->elementEnd('h2');
164
165             $cnt = 0;
166
167             if (!empty($profile)) {
168                 $sml = new SubscribersMiniList($profile, $this);
169                 $cnt = $sml->show();
170                 if ($cnt == 0) {
171                     // TRANS: Text for user subscriber statistics if user has no subscribers.
172                     $this->element('p', null, _('(None)'));
173                 }
174             }
175
176             Event::handle('EndShowSubscribersMiniList', array($this));
177         }
178
179         $this->elementEnd('div');
180     }
181
182     function showStatistics()
183     {
184         $notice_count = $this->profile->noticeCount();
185         $age_days     = (time() - strtotime($this->profile->created)) / 86400;
186         if ($age_days < 1) {
187             // Rather than extrapolating out to a bajillion...
188             $age_days = 1;
189         }
190         $daily_count = round($notice_count / $age_days);
191
192         $this->elementStart('div', array('id' => 'entity_statistics',
193                                          'class' => 'section'));
194
195         // TRANS: H2 text for user statistics.
196         $this->element('h2', null, _('Statistics'));
197
198         $profile = $this->profile;
199         $actionParams = array('nickname' => $profile->nickname);
200         $stats = array(
201             array(
202                 'id' => 'user-id',
203                 // TRANS: Label for user statistics.
204                 'label' => _('User ID'),
205                 'value' => $profile->id,
206             ),
207             array(
208                 'id' => 'member-since',
209                 // TRANS: Label for user statistics.
210                 'label' => _('Member since'),
211                 'value' => date('j M Y', strtotime($profile->created))
212             ),
213             array(
214                 'id' => 'notices',
215                 // TRANS: Label for user statistics.
216                 'label' => _('Notices'),
217                 'value' => $notice_count,
218             ),
219             array(
220                 'id' => 'daily_notices',
221                 // TRANS: Label for user statistics.
222                 // TRANS: Average count of posts made per day since account registration.
223                 'label' => _('Daily average'),
224                 'value' => $daily_count
225             )
226         );
227
228         // Give plugins a chance to add stats entries
229         Event::handle('ProfileStats', array($profile, &$stats));
230
231         foreach ($stats as $row) {
232             $this->showStatsRow($row);
233         }
234         $this->elementEnd('div');
235     }
236
237     private function showStatsRow($row)
238     {
239         $this->elementStart('dl', 'entity_' . $row['id']);
240         $this->elementStart('dt');
241         if (!empty($row['link'])) {
242             $this->element('a', array('href' => $row['link']), $row['label']);
243         } else {
244             $this->text($row['label']);
245         }
246         $this->elementEnd('dt');
247         $this->element('dd', null, $row['value']);
248         $this->elementEnd('dl');
249     }
250
251     function showGroups()
252     {
253         $groups = $this->profile->getGroups(0, GROUPS_PER_MINILIST + 1);
254
255         $this->elementStart('div', array('id' => 'entity_groups',
256                                          'class' => 'section'));
257         if (Event::handle('StartShowGroupsMiniList', array($this))) {
258             $this->elementStart('h2');
259             // TRANS: H2 text for user group membership statistics.
260             $this->statsSectionLink('usergroups', _('Groups'));
261             $this->text(' ');
262             $this->text($this->profile->getGroups()->N);
263             $this->elementEnd('h2');
264
265             if ($groups) {
266                 $gml = new GroupMiniList($groups, $this->profile, $this);
267                 $cnt = $gml->show();
268                 if ($cnt == 0) {
269                     // TRANS: Text for user user group membership statistics if user is not a member of any group.
270                     $this->element('p', null, _('(None)'));
271                 }
272             }
273
274             Event::handle('EndShowGroupsMiniList', array($this));
275         }
276             $this->elementEnd('div');
277     }
278
279     function showLists()
280     {
281         $lists = $this->profile->getLists();
282
283         if ($lists->N > 0) {
284             $this->elementStart('div', array('id' => 'entity_lists',
285                                              'class' => 'section'));
286
287             if (Event::handle('StartShowListsMiniList', array($this))) {
288
289                 $this->elementStart('h2');
290                 // TRANS: H2 text for user list membership statistics.
291                 $this->statsSectionLink('userlists', _('Lists'));
292                 $this->text(' ');
293                 $this->text($lists->N);
294                 $this->elementEnd('h2');
295
296                 $this->elementStart('ul');
297
298                 $cur = common_current_user();
299
300                 $first = true;
301
302                 while ($lists->fetch()) {
303                     if (!$lists->private ||
304                         ($lists->private && !empty($cur) && $cur->id == $profile->id)) {
305                         if (!empty($lists->mainpage)) {
306                             $url = $lists->mainpage;
307                         } else {
308                             $url = common_local_url('showprofiletag',
309                                                     array('tagger' => $this->profile->nickname,
310                                                           'tag'    => $lists->tag));
311                         }
312                         if (!$first) {
313                             $this->text(', ');
314                         } else {
315                             $first = false;
316                         }
317
318                         $this->element('a', array('href' => $url),
319                                        $lists->tag);
320                     }
321                 }
322
323                 $this->elementEnd('ul');
324
325                 Event::handle('EndShowListsMiniList', array($this));
326             }
327             $this->elementEnd('div');
328         }
329     }
330 }
331
332 class SubscribersMiniList extends ProfileMiniList
333 {
334     function newListItem($profile)
335     {
336         return new SubscribersMiniListItem($profile, $this->action);
337     }
338 }
339
340 class SubscribersMiniListItem extends ProfileMiniListItem
341 {
342     function linkAttributes()
343     {
344         $aAttrs = parent::linkAttributes();
345         if (common_config('nofollow', 'subscribers')) {
346             $aAttrs['rel'] .= ' nofollow';
347         }
348         return $aAttrs;
349     }
350 }