]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/adminprofileflag.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / plugins / UserFlag / adminprofileflag.php
1 <?php
2 /**
3  * Show latest and greatest profile flags
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Show the latest and greatest profile flags
36  *
37  * @category Action
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  */
43 class AdminprofileflagAction extends Action
44 {
45     var $page     = null;
46     var $profiles = null;
47
48     /**
49      * Take arguments for running
50      *
51      * @param array $args $_REQUEST args
52      *
53      * @return boolean success flag
54      */
55     function prepare($args)
56     {
57         parent::prepare($args);
58
59         $user = common_current_user();
60
61         // User must be logged in.
62
63         if (!common_logged_in()) {
64             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
65             $this->clientError(_m('Not logged in.'));
66             return;
67         }
68
69         $user = common_current_user();
70
71         // ...because they're logged in
72
73         assert(!empty($user));
74
75         // It must be a "real" login, not saved cookie login
76
77         if (!common_is_real_login()) {
78             // Cookie theft is too easy; we require automatic
79             // logins to re-authenticate before admining the site
80             common_set_returnto($this->selfUrl());
81             if (Event::handle('RedirectToLogin', array($this, $user))) {
82                 common_redirect(common_local_url('login'), 303);
83             }
84         }
85
86         // User must have the right to review flags
87
88         if (!$user->hasRight(UserFlagPlugin::REVIEWFLAGS)) {
89             // TRANS: Error message displayed when trying to review profile flags while not authorised.
90             $this->clientError(_m('You cannot review profile flags.'));
91             return false;
92         }
93
94         $this->page = $this->trimmed('page');
95
96         if (empty($this->page)) {
97             $this->page = 1;
98         }
99
100         $this->profiles = $this->getProfiles();
101
102         return true;
103     }
104
105     /**
106      * Handle request
107      *
108      * @param array $args $_REQUEST args; handled in prepare()
109      *
110      * @return void
111      */
112     function handle($args)
113     {
114         parent::handle($args);
115
116         $this->showPage();
117     }
118
119     /**
120      * Title of this page
121      *
122      * @return string Title of the page
123      */
124     function title()
125     {
126         // TRANS: Title for page with a list of profiles that were flagged for review.
127         return _m('Flagged profiles');
128     }
129
130     /**
131      * save the profile flag
132      *
133      * @return void
134      */
135     function showContent()
136     {
137         $pl = new FlaggedProfileList($this->profiles, $this);
138
139         $cnt = $pl->show();
140
141         $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE,
142                           $this->page, 'adminprofileflag');
143     }
144
145     /**
146      * Retrieve this action's profiles
147      *
148      * @return Profile $profile Profile query results
149      */
150     function getProfiles()
151     {
152         $ufp = new User_flag_profile();
153
154         $ufp->selectAdd();
155         $ufp->selectAdd('profile_id');
156         $ufp->selectAdd('count(*) as flag_count');
157
158         $ufp->whereAdd('cleared is NULL');
159
160         $ufp->groupBy('profile_id');
161         $ufp->orderBy('flag_count DESC, profile_id DESC');
162
163         $offset = ($this->page-1) * PROFILES_PER_PAGE;
164         $limit  = PROFILES_PER_PAGE + 1;
165
166         $ufp->limit($offset, $limit);
167
168         $profiles = array();
169
170         if ($ufp->find()) {
171             while ($ufp->fetch()) {
172                 $profile = Profile::getKV('id', $ufp->profile_id);
173                 if (!empty($profile)) {
174                     $profiles[] = $profile;
175                 }
176             }
177         }
178
179         $ufp->free();
180
181         return new ArrayWrapper($profiles);
182     }
183 }
184
185 /**
186  * Specialization of ProfileList to show flagging information
187  *
188  * Most of the hard part is done in FlaggedProfileListItem.
189  *
190  * @category Widget
191  * @package  StatusNet
192  * @author   Evan Prodromou <evan@status.net>
193  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
194  * @link     http://status.net/
195  */
196 class FlaggedProfileList extends ProfileList
197 {
198     /**
199      * Factory method for creating new list items
200      *
201      * @param Profile $profile Profile to create an item for
202      *
203      * @return ProfileListItem newly-created item
204      */
205     function newListItem($profile)
206     {
207         return new FlaggedProfileListItem($this->profile, $this->action);
208     }
209 }
210
211 /**
212  * Specialization of ProfileListItem to show flagging information
213  *
214  * @category Widget
215  * @package  StatusNet
216  * @author   Evan Prodromou <evan@status.net>
217  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
218  * @link     http://status.net/
219  */
220 class FlaggedProfileListItem extends ProfileListItem
221 {
222     const MAX_FLAGGERS = 5;
223
224     var $user   = null;
225     var $r2args = null;
226
227     /**
228      * Overload parent's action list with our own moderation-oriented buttons
229      *
230      * @return void
231      */
232     function showActions()
233     {
234         $this->user = common_current_user();
235
236         list($action, $this->r2args) = $this->out->returnToArgs();
237
238         $this->r2args['action'] = $action;
239
240         $this->startActions();
241         if (Event::handle('StartProfileListItemActionElements', array($this))) {
242             $this->out->elementStart('li', 'entity_moderation');
243             // TRANS: Header for moderation menu with action buttons for flagged profiles (like 'sandbox', 'silence', ...).
244             $this->out->element('p', null, _m('Moderate'));
245             $this->out->elementStart('ul');
246             $this->showSandboxButton();
247             $this->showSilenceButton();
248             $this->showDeleteButton();
249             $this->showClearButton();
250             $this->out->elementEnd('ul');
251             $this->out->elementEnd('li');
252             Event::handle('EndProfileListItemActionElements', array($this));
253         }
254         $this->endActions();
255     }
256
257     /**
258      * Show a button to sandbox the profile
259      *
260      * @return void
261      */
262     function showSandboxButton()
263     {
264         if ($this->user->hasRight(Right::SANDBOXUSER)) {
265             $this->out->elementStart('li', 'entity_sandbox');
266             if ($this->profile->isSandboxed()) {
267                 $usf = new UnSandboxForm($this->out, $this->profile, $this->r2args);
268                 $usf->show();
269             } else {
270                 $sf = new SandboxForm($this->out, $this->profile, $this->r2args);
271                 $sf->show();
272             }
273             $this->out->elementEnd('li');
274         }
275     }
276
277     /**
278      * Show a button to silence the profile
279      *
280      * @return void
281      */
282     function showSilenceButton()
283     {
284         if ($this->user->hasRight(Right::SILENCEUSER)) {
285             $this->out->elementStart('li', 'entity_silence');
286             if ($this->profile->isSilenced()) {
287                 $usf = new UnSilenceForm($this->out, $this->profile, $this->r2args);
288                 $usf->show();
289             } else {
290                 $sf = new SilenceForm($this->out, $this->profile, $this->r2args);
291                 $sf->show();
292             }
293             $this->out->elementEnd('li');
294         }
295     }
296
297     /**
298      * Show a button to delete user and profile
299      *
300      * @return void
301      */
302     function showDeleteButton()
303     {
304
305         if ($this->user->hasRight(Right::DELETEUSER)) {
306             $this->out->elementStart('li', 'entity_delete');
307             $df = new DeleteUserForm($this->out, $this->profile, $this->r2args);
308             $df->show();
309             $this->out->elementEnd('li');
310         }
311     }
312
313     /**
314      * Show a button to clear flags
315      *
316      * @return void
317      */
318     function showClearButton()
319     {
320         if ($this->user->hasRight(UserFlagPlugin::CLEARFLAGS)) {
321             $this->out->elementStart('li', 'entity_clear');
322             $cf = new ClearFlagForm($this->out, $this->profile, $this->r2args);
323             $cf->show();
324             $this->out->elementEnd('li');
325         }
326     }
327
328     /**
329      * Overload parent function to add flaggers list
330      *
331      * @return void
332      */
333     function endProfile()
334     {
335         $this->showFlaggersList();
336         parent::endProfile();
337     }
338
339     /**
340      * Show a list of people who've flagged this profile
341      *
342      * @return void
343      */
344     function showFlaggersList()
345     {
346         $flaggers = array();
347
348         $ufp = new User_flag_profile();
349
350         $ufp->selectAdd();
351         $ufp->selectAdd('user_id');
352         $ufp->profile_id = $this->profile->id;
353         $ufp->orderBy('created');
354
355         if ($ufp->find()) { // XXX: this should always happen
356             while ($ufp->fetch()) {
357                 $user = User::getKV('id', $ufp->user_id);
358                 if (!empty($user)) { // XXX: this would also be unusual
359                     $flaggers[] = clone($user);
360                 }
361             }
362         }
363
364         $cnt    = count($flaggers);
365         $others = 0;
366
367         if ($cnt > self::MAX_FLAGGERS) {
368             $flaggers = array_slice($flaggers, 0, self::MAX_FLAGGERS);
369             $others   = $cnt - self::MAX_FLAGGERS;
370         }
371
372         $lnks = array();
373
374         foreach ($flaggers as $flagger) {
375
376             $url = common_local_url('showstream',
377                                     array('nickname' => $flagger->nickname));
378
379             $lnks[] = XMLStringer::estring('a', array('href' => $url,
380                                                       'class' => 'flagger'),
381                                            $flagger->nickname);
382         }
383
384         if ($cnt > 0) {
385             if ($others > 0) {
386                 $flagging_users = implode(', ', $lnks);
387                 // TRANS: Message displayed on a profile if it has been flagged.
388                 // TRANS: %1$s is a comma separated list of at most 5 user nicknames that flagged.
389                 // TRANS: %2$d is a positive integer of additional flagging users. Also used for plural.
390                 $text .= sprintf(_m('Flagged by %1$s and %2$d other', 'Flagged by %1$s and %2$d others', $others), $flagging_users, $others);
391             } else {
392                 // TRANS: Message displayed on a profile if it has been flagged.
393                 // TRANS: %s is a comma separated list of at most 5 user nicknames that flagged.
394                 $text .= sprintf(_m('Flagged by %s'), $flagging_users);
395             }
396
397             $this->out->elementStart('p', array('class' => 'flaggers'));
398             $this->out->raw($text);
399             $this->out->elementEnd('p');
400         }
401     }
402 }