]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profileaction.php
Allow showing profiles with UserbyidAction (/user/:id)
[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-2011 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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Profile action common superclass
35  *
36  * Abstracts out common code from profile and personal tabs
37  *
38  * @category Personal
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42  * @link     http://status.net/
43  */
44 abstract class ProfileAction extends ManagedAction
45 {
46     var $page    = null;
47     var $tag     = null;
48
49     protected $target  = null;    // Profile that we're showing
50
51     protected function prepare(array $args=array())
52     {
53         // this will call ->doPreparation() which lower classes can use
54         parent::prepare($args);
55
56         if ($this->target->hasRole(Profile_role::SILENCED)
57                 && (!$this->scoped instanceof Profile || !$this->scoped->hasRight(Right::SILENCEUSER))) {
58             throw new ClientException(_('This profile has been silenced by site moderators'), 403);
59         }
60
61         // backwards compatibility until all actions are fixed to use $this->target
62         $this->profile = $this->target;
63
64         $this->tag = $this->trimmed('tag');
65         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
66         common_set_returnto($this->selfUrl());
67
68         // fetch the actual stream stuff
69         $this->profileActionPreparation();
70
71         return true;
72     }
73
74     function isReadOnly($args)
75     {
76         return true;
77     }
78
79     function showSections()
80     {
81         $this->showSubscriptions();
82         $this->showSubscribers();
83         $this->showGroups();
84         $this->showLists();
85         $this->showStatistics();
86     }
87
88     /**
89      * Convenience function for common pattern of links to subscription/groups sections.
90      *
91      * @param string $actionClass
92      * @param string $title
93      * @param string $cssClass
94      */
95     private function statsSectionLink($actionClass, $title, $cssClass='')
96     {
97         $this->element('a', array('href' => common_local_url($actionClass,
98                                                              array('nickname' => $this->target->getNickname())),
99                                   'class' => $cssClass),
100                        $title);
101     }
102
103     function showSubscriptions()
104     {
105         $this->elementStart('div', array('id' => 'entity_subscriptions',
106                                          'class' => 'section'));
107         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
108             $this->elementStart('h2');
109             // TRANS: H2 text for user subscription statistics.
110             $this->statsSectionLink('subscriptions', _('Following'));
111             $this->text(' ');
112             $this->text($this->target->subscriptionCount());
113             $this->elementEnd('h2');
114         
115             try {
116                 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
117                 $pml = new ProfileMiniList($profile, $this);
118                 $pml->show();
119             } catch (NoResultException $e) {
120                 // TRANS: Text for user subscription statistics if the user has no subscription
121                 $this->element('p', null, _('(None)'));
122             }
123
124             Event::handle('EndShowSubscriptionsMiniList', array($this));
125         }
126         $this->elementEnd('div');
127     }
128
129     function showSubscribers()
130     {
131         $this->elementStart('div', array('id' => 'entity_subscribers',
132                                          'class' => 'section'));
133
134         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
135
136             $this->elementStart('h2');
137             // TRANS: H2 text for user subscriber statistics.
138             $this->statsSectionLink('subscribers', _('Followers'));
139             $this->text(' ');
140             $this->text($this->target->subscriberCount());
141             $this->elementEnd('h2');
142
143             try {
144                 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
145                 $sml = new SubscribersMiniList($profile, $this);
146                 $sml->show();
147             } catch (NoResultException $e) {
148                 // TRANS: Text for user subscriber statistics if user has no subscribers.
149                 $this->element('p', null, _('(None)'));
150             }
151
152             Event::handle('EndShowSubscribersMiniList', array($this));
153         }
154
155         $this->elementEnd('div');
156     }
157
158     function showStatistics()
159     {
160         $notice_count = $this->target->noticeCount();
161         $age_days     = (time() - strtotime($this->target->created)) / 86400;
162         if ($age_days < 1) {
163             // Rather than extrapolating out to a bajillion...
164             $age_days = 1;
165         }
166         $daily_count = round($notice_count / $age_days);
167
168         $this->elementStart('div', array('id' => 'entity_statistics',
169                                          'class' => 'section'));
170
171         // TRANS: H2 text for user statistics.
172         $this->element('h2', null, _('Statistics'));
173
174         $profile = $this->target;
175         $actionParams = array('nickname' => $profile->nickname);
176         $stats = array(
177             array(
178                 'id' => 'user-id',
179                 // TRANS: Label for user statistics.
180                 'label' => _('User ID'),
181                 'value' => $profile->id,
182             ),
183             array(
184                 'id' => 'member-since',
185                 // TRANS: Label for user statistics.
186                 'label' => _('Member since'),
187                 'value' => date('j M Y', strtotime($profile->created))
188             ),
189             array(
190                 'id' => 'notices',
191                 // TRANS: Label for user statistics.
192                 'label' => _('Notices'),
193                 'value' => $notice_count,
194             ),
195             array(
196                 'id' => 'daily_notices',
197                 // TRANS: Label for user statistics.
198                 // TRANS: Average count of posts made per day since account registration.
199                 'label' => _('Daily average'),
200                 'value' => $daily_count
201             )
202         );
203
204         // Give plugins a chance to add stats entries
205         Event::handle('ProfileStats', array($profile, &$stats));
206
207         foreach ($stats as $row) {
208             $this->showStatsRow($row);
209         }
210         $this->elementEnd('div');
211     }
212
213     private function showStatsRow($row)
214     {
215         $this->elementStart('dl', 'entity_' . $row['id']);
216         $this->elementStart('dt');
217         if (!empty($row['link'])) {
218             $this->element('a', array('href' => $row['link']), $row['label']);
219         } else {
220             $this->text($row['label']);
221         }
222         $this->elementEnd('dt');
223         $this->element('dd', null, $row['value']);
224         $this->elementEnd('dl');
225     }
226
227     function showGroups()
228     {
229         $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
230
231         $this->elementStart('div', array('id' => 'entity_groups',
232                                          'class' => 'section'));
233         if (Event::handle('StartShowGroupsMiniList', array($this))) {
234             $this->elementStart('h2');
235             // TRANS: H2 text for user group membership statistics.
236             $this->statsSectionLink('usergroups', _('Groups'));
237             $this->text(' ');
238             $this->text($this->target->getGroupCount());
239             $this->elementEnd('h2');
240
241             if ($groups instanceof User_group) {
242                 $gml = new GroupMiniList($groups, $this->target, $this);
243                 $cnt = $gml->show();
244             } else {
245                 // TRANS: Text for user user group membership statistics if user is not a member of any group.
246                 $this->element('p', null, _('(None)'));
247             }
248
249             Event::handle('EndShowGroupsMiniList', array($this));
250         }
251             $this->elementEnd('div');
252     }
253
254     function showLists()
255     {
256         $lists = $this->target->getLists($this->scoped);
257
258         if ($lists->N > 0) {
259             $this->elementStart('div', array('id' => 'entity_lists',
260                                              'class' => 'section'));
261
262             if (Event::handle('StartShowListsMiniList', array($this))) {
263
264                 $url = common_local_url('peopletagsbyuser',
265                                         array('nickname' => $this->target->getNickname()));
266
267                 $this->elementStart('h2');
268                 $this->element('a',
269                                array('href' => $url),
270                                // TRANS: H2 text for user list membership statistics.
271                                _('Lists'));
272                 $this->text(' ');
273                 $this->text($lists->N);
274                 $this->elementEnd('h2');
275
276                 $this->elementStart('ul');
277
278
279                 $first = true;
280
281                 while ($lists->fetch()) {
282                     if (!empty($lists->mainpage)) {
283                         $url = $lists->mainpage;
284                     } else {
285                         $url = common_local_url('showprofiletag',
286                                                 array('tagger' => $this->target->getNickname(),
287                                                       'tag'    => $lists->tag));
288                     }
289                     if (!$first) {
290                         $this->text(', ');
291                     } else {
292                         $first = false;
293                     }
294
295                     $this->element('a', array('href' => $url),
296                                    $lists->tag);
297                 }
298
299                 $this->elementEnd('ul');
300
301                 Event::handle('EndShowListsMiniList', array($this));
302             }
303             $this->elementEnd('div');
304         }
305     }
306 }
307
308 class SubscribersMiniList extends ProfileMiniList
309 {
310     function newListItem($profile)
311     {
312         return new SubscribersMiniListItem($profile, $this->action);
313     }
314 }
315
316 class SubscribersMiniListItem extends ProfileMiniListItem
317 {
318     function linkAttributes()
319     {
320         $aAttrs = parent::linkAttributes();
321         if (common_config('nofollow', 'subscribers')) {
322             $aAttrs['rel'] .= ' nofollow';
323         }
324         return $aAttrs;
325     }
326 }