]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profileaction.php
only variables can be sent by reference (strict standards)
[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 implements NoticestreamAction
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 child classes use to set $this->target
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         $stream = $this->getStream();
70         $this->notice = $stream->getNotices(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
71
72         if ($this->page > 1 && $this->notice->N == 0) {
73             // TRANS: Client error when page not found (404).
74             $this->clientError(_('No such page.'), 404);
75         }
76
77
78         return true;
79     }
80
81     protected function profileActionPreparation()
82     {
83         // Nothing to do by default.
84     }
85
86     public function getTarget()
87     {
88         return $this->target;
89     }
90
91     function isReadOnly($args)
92     {
93         return true;
94     }
95
96     function showSections()
97     {
98         $this->showSubscriptions();
99         $this->showSubscribers();
100         $this->showGroups();
101         $this->showLists();
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->target->getNickname())),
116                                   'class' => $cssClass),
117                        $title);
118     }
119
120     function showSubscriptions()
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', _('Following'));
128             $this->text(' ');
129             $this->text($this->target->subscriptionCount());
130             $this->elementEnd('h2');
131         
132             try {
133                 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
134                 $pml = new ProfileMiniList($profile, $this);
135                 $pml->show();
136             } catch (NoResultException $e) {
137                 // TRANS: Text for user subscription statistics if the user has no subscription
138                 $this->element('p', null, _('(None)'));
139             }
140
141             Event::handle('EndShowSubscriptionsMiniList', array($this));
142         }
143         $this->elementEnd('div');
144     }
145
146     function showSubscribers()
147     {
148         $this->elementStart('div', array('id' => 'entity_subscribers',
149                                          'class' => 'section'));
150
151         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
152
153             $this->elementStart('h2');
154             // TRANS: H2 text for user subscriber statistics.
155             $this->statsSectionLink('subscribers', _('Followers'));
156             $this->text(' ');
157             $this->text($this->target->subscriberCount());
158             $this->elementEnd('h2');
159
160             try {
161                 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
162                 $sml = new SubscribersMiniList($profile, $this);
163                 $sml->show();
164             } catch (NoResultException $e) {
165                 // TRANS: Text for user subscriber statistics if user has no subscribers.
166                 $this->element('p', null, _('(None)'));
167             }
168
169             Event::handle('EndShowSubscribersMiniList', array($this));
170         }
171
172         $this->elementEnd('div');
173     }
174
175     function showStatistics()
176     {
177         $notice_count = $this->target->noticeCount();
178         $age_days     = (time() - strtotime($this->target->created)) / 86400;
179         if ($age_days < 1) {
180             // Rather than extrapolating out to a bajillion...
181             $age_days = 1;
182         }
183         $daily_count = round($notice_count / $age_days);
184
185         $this->elementStart('div', array('id' => 'entity_statistics',
186                                          'class' => 'section'));
187
188         // TRANS: H2 text for user statistics.
189         $this->element('h2', null, _('Statistics'));
190
191         $profile = $this->target;
192         $actionParams = array('nickname' => $profile->nickname);
193         $stats = array(
194             array(
195                 'id' => 'user-id',
196                 // TRANS: Label for user statistics.
197                 'label' => _('User ID'),
198                 'value' => $profile->id,
199             ),
200             array(
201                 'id' => 'member-since',
202                 // TRANS: Label for user statistics.
203                 'label' => _('Member since'),
204                 'value' => date('j M Y', strtotime($profile->created))
205             ),
206             array(
207                 'id' => 'notices',
208                 // TRANS: Label for user statistics.
209                 'label' => _('Notices'),
210                 'value' => $notice_count,
211             ),
212             array(
213                 'id' => 'daily_notices',
214                 // TRANS: Label for user statistics.
215                 // TRANS: Average count of posts made per day since account registration.
216                 'label' => _('Daily average'),
217                 'value' => $daily_count
218             )
219         );
220
221         // Give plugins a chance to add stats entries
222         Event::handle('ProfileStats', array($profile, &$stats));
223
224         foreach ($stats as $row) {
225             $this->showStatsRow($row);
226         }
227         $this->elementEnd('div');
228     }
229
230     private function showStatsRow($row)
231     {
232         $this->elementStart('dl', 'entity_' . $row['id']);
233         $this->elementStart('dt');
234         if (!empty($row['link'])) {
235             $this->element('a', array('href' => $row['link']), $row['label']);
236         } else {
237             $this->text($row['label']);
238         }
239         $this->elementEnd('dt');
240         $this->element('dd', null, $row['value']);
241         $this->elementEnd('dl');
242     }
243
244     function showGroups()
245     {
246         $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
247
248         $this->elementStart('div', array('id' => 'entity_groups',
249                                          'class' => 'section'));
250         if (Event::handle('StartShowGroupsMiniList', array($this))) {
251             $this->elementStart('h2');
252             // TRANS: H2 text for user group membership statistics.
253             $this->statsSectionLink('usergroups', _('Groups'));
254             $this->text(' ');
255             $this->text($this->target->getGroupCount());
256             $this->elementEnd('h2');
257
258             if ($groups instanceof User_group) {
259                 $gml = new GroupMiniList($groups, $this->target, $this);
260                 $cnt = $gml->show();
261             } else {
262                 // TRANS: Text for user user group membership statistics if user is not a member of any group.
263                 $this->element('p', null, _('(None)'));
264             }
265
266             Event::handle('EndShowGroupsMiniList', array($this));
267         }
268             $this->elementEnd('div');
269     }
270
271     function showLists()
272     {
273         $lists = $this->target->getLists($this->scoped);
274
275         if ($lists->N > 0) {
276             $this->elementStart('div', array('id' => 'entity_lists',
277                                              'class' => 'section'));
278
279             if (Event::handle('StartShowListsMiniList', array($this))) {
280
281                 $url = common_local_url('peopletagsbyuser',
282                                         array('nickname' => $this->target->getNickname()));
283
284                 $this->elementStart('h2');
285                 $this->element('a',
286                                array('href' => $url),
287                                // TRANS: H2 text for user list membership statistics.
288                                _('Lists'));
289                 $this->text(' ');
290                 $this->text($lists->N);
291                 $this->elementEnd('h2');
292
293                 $this->elementStart('ul');
294
295
296                 $first = true;
297
298                 while ($lists->fetch()) {
299                     if (!empty($lists->mainpage)) {
300                         $url = $lists->mainpage;
301                     } else {
302                         $url = common_local_url('showprofiletag',
303                                                 array('tagger' => $this->target->getNickname(),
304                                                       'tag'    => $lists->tag));
305                     }
306                     if (!$first) {
307                         $this->text(', ');
308                     } else {
309                         $first = false;
310                     }
311
312                     $this->element('a', array('href' => $url),
313                                    $lists->tag);
314                 }
315
316                 $this->elementEnd('ul');
317
318                 Event::handle('EndShowListsMiniList', array($this));
319             }
320             $this->elementEnd('div');
321         }
322     }
323 }
324
325 class SubscribersMiniList extends ProfileMiniList
326 {
327     function newListItem($profile)
328     {
329         return new SubscribersMiniListItem($profile, $this->action);
330     }
331 }
332
333 class SubscribersMiniListItem extends ProfileMiniListItem
334 {
335     function linkAttributes()
336     {
337         $aAttrs = parent::linkAttributes();
338         if (common_config('nofollow', 'subscribers')) {
339             $aAttrs['rel'] .= ' nofollow';
340         }
341         return $aAttrs;
342     }
343 }