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