]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profileaction.php
move follower/following/groups stats to those sections
[quix0rs-gnu-social.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->showPeopletagSubs();
101         $this->showPeopletags();
102         $this->showStatistics();
103     }
104
105     /**
106      * Convenience function for common pattern of links to subscription/groups sections.
107      *
108      * @param string $actionClass
109      * @param string $title
110      * @param string $cssClass
111      */
112     private function statsSectionLink($actionClass, $title, $cssClass='')
113     {
114         $this->element('a', array('href' => common_local_url($actionClass,
115                                                              array('nickname' => $this->profile->nickname)),
116                                   'class' => $cssClass),
117                        $title);
118     }
119
120     function showSubscriptions()
121     {
122         $profile = $this->profile->getSubscriptions(0, PROFILES_PER_MINILIST + 1);
123
124         $this->elementStart('div', array('id' => 'entity_subscriptions',
125                                          'class' => 'section'));
126         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
127             $this->elementStart('h2');
128             // TRANS: H2 text for user subscription statistics.
129             $this->statsSectionLink('subscriptions', _('Following'));
130             $this->text(' ');
131             $this->text($this->profile->subscriptionCount());
132             $this->elementEnd('h2');
133
134             $cnt = 0;
135
136             if (!empty($profile)) {
137                 $pml = new ProfileMiniList($profile, $this);
138                 $cnt = $pml->show();
139                 if ($cnt == 0) {
140                     // TRANS: Text for user subscription statistics if the user has no subscriptions.
141                     $this->element('p', null, _('(None)'));
142                 }
143             }
144
145             Event::handle('EndShowSubscriptionsMiniList', array($this));
146         }
147         $this->elementEnd('div');
148     }
149
150     function showSubscribers()
151     {
152         $profile = $this->profile->getSubscribers(0, PROFILES_PER_MINILIST + 1);
153
154         $this->elementStart('div', array('id' => 'entity_subscribers',
155                                          'class' => 'section'));
156
157         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
158
159             $this->elementStart('h2');
160             // TRANS: H2 text for user subscriber statistics.
161             $this->statsSectionLink('subscribers', _('Followers'));
162             $this->text(' ');
163             $this->text($this->profile->subscriberCount());
164             $this->elementEnd('h2');
165
166             $cnt = 0;
167
168             if (!empty($profile)) {
169                 $sml = new SubscribersMiniList($profile, $this);
170                 $cnt = $sml->show();
171                 if ($cnt == 0) {
172                     // TRANS: Text for user subscriber statistics if user has no subscribers.
173                     $this->element('p', null, _('(None)'));
174                 }
175             }
176
177             Event::handle('EndShowSubscribersMiniList', array($this));
178         }
179
180         $this->elementEnd('div');
181     }
182
183     function showPeopletagSubs()
184     {
185         $user = common_current_user();
186         if (!empty($user) && $this->profile->id == $user->id) {
187             if (Event::handle('StartShowPeopletagSubscriptionsSection', array($this))) {
188
189                 $profile = $user->getProfile();
190                 $section = new PeopletagSubscriptionsSection($this, $profile);
191                 $section->show();
192
193                 Event::handle('EndShowPeopletagSubscriptionsSection', array($this));
194             }
195         }
196     }
197
198     function showPeopletags()
199     {
200         if (Event::handle('StartShowPeopletagsSection', array($this))) {
201
202             $section = new PeopletagsForUserSection($this, $this->profile);
203             $section->show();
204
205             Event::handle('EndShowPeopletagsSection', array($this));
206         }
207     }
208
209     function showStatistics()
210     {
211         $notice_count = $this->profile->noticeCount();
212         $age_days     = (time() - strtotime($this->profile->created)) / 86400;
213         if ($age_days < 1) {
214             // Rather than extrapolating out to a bajillion...
215             $age_days = 1;
216         }
217         $daily_count = round($notice_count / $age_days);
218
219         $this->elementStart('div', array('id' => 'entity_statistics',
220                                          'class' => 'section'));
221
222         // TRANS: H2 text for user statistics.
223         $this->element('h2', null, _('Statistics'));
224
225         $profile = $this->profile;
226         $actionParams = array('nickname' => $profile->nickname);
227         $stats = array(
228             array(
229                 'id' => 'user-id',
230                 // TRANS: Label for user statistics.
231                 'label' => _('User ID'),
232                 'value' => $profile->id,
233             ),
234             array(
235                 'id' => 'member-since',
236                 // TRANS: Label for user statistics.
237                 'label' => _('Member since'),
238                 'value' => date('j M Y', strtotime($profile->created))
239             ),
240             array(
241                 'id' => 'notices',
242                 // TRANS: Label for user statistics.
243                 'label' => _('Notices'),
244                 'value' => $notice_count,
245             ),
246             array(
247                 'id' => 'daily_notices',
248                 // TRANS: Label for user statistics.
249                 // TRANS: Average count of posts made per day since account registration.
250                 'label' => _('Daily average'),
251                 'value' => $daily_count
252             )
253         );
254
255         // Give plugins a chance to add stats entries
256         Event::handle('ProfileStats', array($profile, &$stats));
257
258         foreach ($stats as $row) {
259             $this->showStatsRow($row);
260         }
261         $this->elementEnd('div');
262     }
263
264     private function showStatsRow($row)
265     {
266         $this->elementStart('dl', 'entity_' . $row['id']);
267         $this->elementStart('dt');
268         if (!empty($row['link'])) {
269             $this->element('a', array('href' => $row['link']), $row['label']);
270         } else {
271             $this->text($row['label']);
272         }
273         $this->elementEnd('dt');
274         $this->element('dd', null, $row['value']);
275         $this->elementEnd('dl');
276     }
277
278     function showGroups()
279     {
280         $groups = $this->profile->getGroups(0, GROUPS_PER_MINILIST + 1);
281
282         $this->elementStart('div', array('id' => 'entity_groups',
283                                          'class' => 'section'));
284         if (Event::handle('StartShowGroupsMiniList', array($this))) {
285             $this->elementStart('h2');
286             // TRANS: H2 text for user group membership statistics.
287             $this->statsSectionLink('usergroups', _('Groups'));
288             $this->text(' ');
289             $this->text($this->profile->getGroups()->N);
290             $this->elementEnd('h2');
291
292             if ($groups) {
293                 $gml = new GroupMiniList($groups, $this->profile, $this);
294                 $cnt = $gml->show();
295                 if ($cnt == 0) {
296                     // TRANS: Text for user user group membership statistics if user is not a member of any group.
297                     $this->element('p', null, _('(None)'));
298                 }
299             }
300
301             if ($cnt > GROUPS_PER_MINILIST) {
302                 $this->elementStart('p');
303                 // TRANS: Text for user group membership statistics if user has more subscriptions than displayed.
304                 $this->statsSectionLink('usergroups', _('All groups'), 'more');
305                 $this->elementEnd('p');
306             }
307
308             Event::handle('EndShowGroupsMiniList', array($this));
309         }
310             $this->elementEnd('div');
311     }
312 }
313
314 class SubscribersMiniList extends ProfileMiniList
315 {
316     function newListItem($profile)
317     {
318         return new SubscribersMiniListItem($profile, $this->action);
319     }
320 }
321
322 class SubscribersMiniListItem extends ProfileMiniListItem
323 {
324     function linkAttributes()
325     {
326         $aAttrs = parent::linkAttributes();
327         if (common_config('nofollow', 'subscribers')) {
328             $aAttrs['rel'] .= ' nofollow';
329         }
330         return $aAttrs;
331     }
332 }