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