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