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