]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profileaction.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.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
50 class ProfileAction extends OwnerDesignAction
51 {
52     var $page    = null;
53     var $profile = null;
54     var $tag     = null;
55
56     function prepare($args)
57     {
58         parent::prepare($args);
59
60         $nickname_arg = $this->arg('nickname');
61         $nickname     = common_canonical_nickname($nickname_arg);
62
63         // Permanent redirect on non-canonical nickname
64
65         if ($nickname_arg != $nickname) {
66             $args = array('nickname' => $nickname);
67             if ($this->arg('page') && $this->arg('page') != 1) {
68                 $args['page'] = $this->arg['page'];
69             }
70             common_redirect(common_local_url($this->trimmed('action'), $args), 301);
71             return false;
72         }
73
74         $this->user = User::staticGet('nickname', $nickname);
75
76         if (!$this->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             $this->serverError(_('User has no profile.'));
85             return false;
86         }
87
88         $this->tag = $this->trimmed('tag');
89         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
90         common_set_returnto($this->selfUrl());
91         return true;
92     }
93
94     function showSections()
95     {
96         $this->showSubscriptions();
97         $this->showSubscribers();
98         $this->showGroups();
99         $this->showStatistics();
100     }
101
102     /**
103      * Convenience function for common pattern of links to subscription/groups sections.
104      *
105      * @param string $actionClass
106      * @param string $title
107      * @param string $cssClass
108      */
109     private function statsSectionLink($actionClass, $title, $cssClass='')
110     {
111         $this->element('a', array('href' => common_local_url($actionClass,
112                                                              array('nickname' => $this->profile->nickname)),
113                                   'class' => $cssClass),
114                        $title);
115     }
116
117     function showSubscriptions()
118     {
119         $profile = $this->profile->getSubscriptions(0, PROFILES_PER_MINILIST + 1);
120
121         $this->elementStart('div', array('id' => 'entity_subscriptions',
122                                          'class' => 'section'));
123         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
124             $this->elementStart('h2');
125             $this->statsSectionLink('subscriptions', _('Subscriptions'));
126             $this->elementEnd('h2');
127
128             $cnt = 0;
129
130             if (!empty($profile)) {
131                 $pml = new ProfileMiniList($profile, $this);
132                 $cnt = $pml->show();
133                 if ($cnt == 0) {
134                     $this->element('p', null, _('(None)'));
135                 }
136             }
137
138             if ($cnt > PROFILES_PER_MINILIST) {
139                 $this->elementStart('p');
140                 $this->statsSectionLink('subscriptions', _('All subscriptions'), 'more');
141                 $this->elementEnd('p');
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             $this->statsSectionLink('subscribers', _('Subscribers'));
160             $this->elementEnd('h2');
161
162             $cnt = 0;
163
164             if (!empty($profile)) {
165                 $sml = new SubscribersMiniList($profile, $this);
166                 $cnt = $sml->show();
167                 if ($cnt == 0) {
168                     $this->element('p', null, _('(None)'));
169                 }
170             }
171
172             if ($cnt > PROFILES_PER_MINILIST) {
173                 $this->elementStart('p');
174                 $this->statsSectionLink('subscribers', _('All subscribers'), 'more');
175                 $this->elementEnd('p');
176             }
177
178             Event::handle('EndShowSubscribersMiniList', array($this));
179         }
180
181         $this->elementEnd('div');
182     }
183
184     function showStatistics()
185     {
186         $notice_count = $this->profile->noticeCount();
187         $age_days     = (time() - strtotime($this->profile->created)) / 86400;
188         if ($age_days < 1) {
189             // Rather than extrapolating out to a bajillion...
190             $age_days = 1;
191         }
192         $daily_count = round($notice_count / $age_days);
193
194         $this->elementStart('div', array('id' => 'entity_statistics',
195                                          'class' => 'section'));
196
197         $this->element('h2', null, _('Statistics'));
198
199         $profile = $this->profile;
200         $actionParams = array('nickname' => $profile->nickname);
201         $stats = array(
202             array(
203                 'id' => 'user-id',
204                 'label' => _('User ID'),
205                 'value' => $profile->id,
206             ),
207             array(
208                 'id' => 'member-since',
209                 'label' => _('Member since'),
210                 'value' => date('j M Y', strtotime($profile->created))
211             ),
212             array(
213                 'id' => 'subscriptions',
214                 'label' => _('Subscriptions'),
215                 'link' => common_local_url('subscriptions', $actionParams),
216                 'value' => $profile->subscriptionCount(),
217             ),
218             array(
219                 'id' => 'subscribers',
220                 'label' => _('Subscribers'),
221                 'link' => common_local_url('subscribers', $actionParams),
222                 'value' => $profile->subscriberCount(),
223             ),
224             array(
225                 'id' => 'groups',
226                 'label' => _('Groups'),
227                 'link' => common_local_url('usergroups', $actionParams),
228                 'value' => $profile->getGroups()->N,
229             ),
230             array(
231                 'id' => 'notices',
232                 'label' => _('Notices'),
233                 'value' => $notice_count,
234             ),
235             array(
236                 'id' => 'daily_notices',
237                 // TRANS: Average count of posts made per day since account registration
238                 'label' => _('Daily average'),
239                 'value' => $daily_count
240             )
241         );
242
243         // Give plugins a chance to add stats entries
244         Event::handle('ProfileStats', array($profile, &$stats));
245
246         foreach ($stats as $row) {
247             $this->showStatsRow($row);
248         }
249         $this->elementEnd('div');
250     }
251
252     private function showStatsRow($row)
253     {
254         $this->elementStart('dl', 'entity_' . $row['id']);
255         $this->elementStart('dt');
256         if (!empty($row['link'])) {
257             $this->element('a', array('href' => $row['link']), $row['label']);
258         } else {
259             $this->text($row['label']);
260         }
261         $this->elementEnd('dt');
262         $this->element('dd', null, $row['value']);
263         $this->elementEnd('dl');
264     }
265
266     function showGroups()
267     {
268         $groups = $this->profile->getGroups(0, GROUPS_PER_MINILIST + 1);
269
270         $this->elementStart('div', array('id' => 'entity_groups',
271                                          'class' => 'section'));
272         if (Event::handle('StartShowGroupsMiniList', array($this))) {
273             $this->elementStart('h2');
274             $this->statsSectionLink('usergroups', _('Groups'));
275             $this->elementEnd('h2');
276
277             if ($groups) {
278                 $gml = new GroupMiniList($groups, $this->profile, $this);
279                 $cnt = $gml->show();
280                 if ($cnt == 0) {
281                     $this->element('p', null, _('(None)'));
282                 }
283             }
284
285             if ($cnt > GROUPS_PER_MINILIST) {
286                 $this->elementStart('p');
287                 $this->statsSectionLink('usergroups', _('All groups'), 'more');
288                 $this->elementEnd('p');
289             }
290
291             Event::handle('EndShowGroupsMiniList', array($this));
292         }
293             $this->elementEnd('div');
294     }
295 }
296
297 class SubscribersMiniList extends ProfileMiniList
298 {
299     function newListItem($profile)
300     {
301         return new SubscribersMiniListItem($profile, $this->action);
302     }
303 }
304
305 class SubscribersMiniListItem extends ProfileMiniListItem
306 {
307     function linkAttributes()
308     {
309         $aAttrs = parent::linkAttributes();
310         if (common_config('nofollow', 'subscribers')) {
311             $aAttrs['rel'] .= ' nofollow';
312         }
313         return $aAttrs;
314     }
315 }
316