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