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