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