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