]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/adminprofileflag.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[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
44 class AdminprofileflagAction extends Action
45 {
46     var $page     = null;
47     var $profiles = null;
48
49     /**
50      * Take arguments for running
51      *
52      * @param array $args $_REQUEST args
53      *
54      * @return boolean success flag
55      */
56
57     function prepare($args)
58     {
59         parent::prepare($args);
60
61         $user = common_current_user();
62
63         // User must be logged in.
64
65         if (!common_logged_in()) {
66             $this->clientError(_('Not logged in.'));
67             return;
68         }
69
70         $user = common_current_user();
71
72         // ...because they're logged in
73
74         assert(!empty($user));
75
76         // It must be a "real" login, not saved cookie login
77
78         if (!common_is_real_login()) {
79             // Cookie theft is too easy; we require automatic
80             // logins to re-authenticate before admining the site
81             common_set_returnto($this->selfUrl());
82             if (Event::handle('RedirectToLogin', array($this, $user))) {
83                 common_redirect(common_local_url('login'), 303);
84             }
85         }
86
87         // User must have the right to review flags
88
89         if (!$user->hasRight(UserFlagPlugin::REVIEWFLAGS)) {
90             $this->clientError(_('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
113     function handle($args)
114     {
115         parent::handle($args);
116
117         $this->showPage();
118     }
119
120     /**
121      * Title of this page
122      *
123      * @return string Title of the page
124      */
125
126     function title()
127     {
128         return _('Flagged profiles');
129     }
130
131     /**
132      * save the profile flag
133      *
134      * @return void
135      */
136
137     function showContent()
138     {
139         $pl = new FlaggedProfileList($this->profiles, $this);
140
141         $cnt = $pl->show();
142
143         $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE,
144                           $this->page, 'adminprofileflag');
145     }
146
147     /**
148      * Retrieve this action's profiles
149      *
150      * @return Profile $profile Profile query results
151      */
152
153     function getProfiles()
154     {
155         $ufp = new User_flag_profile();
156
157         $ufp->selectAdd();
158         $ufp->selectAdd('profile_id');
159         $ufp->selectAdd('count(*) as flag_count');
160
161         $ufp->whereAdd('cleared is NULL');
162
163         $ufp->groupBy('profile_id');
164         $ufp->orderBy('flag_count DESC, profile_id DESC');
165
166         $offset = ($this->page-1) * PROFILES_PER_PAGE;
167         $limit  = PROFILES_PER_PAGE + 1;
168
169         $ufp->limit($offset, $limit);
170
171         $profiles = array();
172
173         if ($ufp->find()) {
174             while ($ufp->fetch()) {
175                 $profile = Profile::staticGet('id', $ufp->profile_id);
176                 if (!empty($profile)) {
177                     $profiles[] = $profile;
178                 }
179             }
180         }
181
182         $ufp->free();
183
184         return new ArrayWrapper($profiles);
185     }
186 }
187
188 /**
189  * Specialization of ProfileList to show flagging information
190  *
191  * Most of the hard part is done in FlaggedProfileListItem.
192  *
193  * @category Widget
194  * @package  StatusNet
195  * @author   Evan Prodromou <evan@status.net>
196  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
197  * @link     http://status.net/
198  */
199
200 class FlaggedProfileList extends ProfileList
201 {
202     /**
203      * Factory method for creating new list items
204      *
205      * @param Profile $profile Profile to create an item for
206      *
207      * @return ProfileListItem newly-created item
208      */
209
210     function newListItem($profile)
211     {
212         return new FlaggedProfileListItem($this->profile, $this->action);
213     }
214 }
215
216 /**
217  * Specialization of ProfileListItem to show flagging information
218  *
219  * @category Widget
220  * @package  StatusNet
221  * @author   Evan Prodromou <evan@status.net>
222  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
223  * @link     http://status.net/
224  */
225
226 class FlaggedProfileListItem extends ProfileListItem
227 {
228     const MAX_FLAGGERS = 5;
229
230     var $user   = null;
231     var $r2args = null;
232
233     /**
234      * Overload parent's action list with our own moderation-oriented buttons
235      *
236      * @return void
237      */
238
239     function showActions()
240     {
241         $this->user = common_current_user();
242
243         list($action, $this->r2args) = $this->out->returnToArgs();
244
245         $this->r2args['action'] = $action;
246
247         $this->startActions();
248         if (Event::handle('StartProfileListItemActionElements', array($this))) {
249             $this->out->elementStart('li', 'entity_moderation');
250             $this->out->element('p', null, _('Moderate'));
251             $this->out->elementStart('ul');
252             $this->showSandboxButton();
253             $this->showSilenceButton();
254             $this->showDeleteButton();
255             $this->showClearButton();
256             $this->out->elementEnd('ul');
257             $this->out->elementEnd('li');
258             Event::handle('EndProfileListItemActionElements', array($this));
259         }
260         $this->endActions();
261     }
262
263     /**
264      * Show a button to sandbox the profile
265      *
266      * @return void
267      */
268
269     function showSandboxButton()
270     {
271         if ($this->user->hasRight(Right::SANDBOXUSER)) {
272             $this->out->elementStart('li', 'entity_sandbox');
273             if ($this->profile->isSandboxed()) {
274                 $usf = new UnSandboxForm($this->out, $this->profile, $this->r2args);
275                 $usf->show();
276             } else {
277                 $sf = new SandboxForm($this->out, $this->profile, $this->r2args);
278                 $sf->show();
279             }
280             $this->out->elementEnd('li');
281         }
282     }
283
284     /**
285      * Show a button to silence the profile
286      *
287      * @return void
288      */
289
290     function showSilenceButton()
291     {
292         if ($this->user->hasRight(Right::SILENCEUSER)) {
293             $this->out->elementStart('li', 'entity_silence');
294             if ($this->profile->isSilenced()) {
295                 $usf = new UnSilenceForm($this->out, $this->profile, $this->r2args);
296                 $usf->show();
297             } else {
298                 $sf = new SilenceForm($this->out, $this->profile, $this->r2args);
299                 $sf->show();
300             }
301             $this->out->elementEnd('li');
302         }
303     }
304
305     /**
306      * Show a button to delete user and profile
307      *
308      * @return void
309      */
310
311     function showDeleteButton()
312     {
313
314         if ($this->user->hasRight(Right::DELETEUSER)) {
315             $this->out->elementStart('li', 'entity_delete');
316             $df = new DeleteUserForm($this->out, $this->profile, $this->r2args);
317             $df->show();
318             $this->out->elementEnd('li');
319         }
320     }
321
322     /**
323      * Show a button to clear flags
324      *
325      * @return void
326      */
327
328     function showClearButton()
329     {
330         if ($this->user->hasRight(UserFlagPlugin::CLEARFLAGS)) {
331             $this->out->elementStart('li', 'entity_clear');
332             $cf = new ClearFlagForm($this->out, $this->profile, $this->r2args);
333             $cf->show();
334             $this->out->elementEnd('li');
335         }
336     }
337
338     /**
339      * Overload parent function to add flaggers list
340      *
341      * @return void
342      */
343
344     function endProfile()
345     {
346         $this->showFlaggersList();
347         parent::endProfile();
348     }
349
350     /**
351      * Show a list of people who've flagged this profile
352      *
353      * @return void
354      */
355
356     function showFlaggersList()
357     {
358         $flaggers = array();
359
360         $ufp = new User_flag_profile();
361
362         $ufp->selectAdd();
363         $ufp->selectAdd('user_id');
364         $ufp->profile_id = $this->profile->id;
365         $ufp->orderBy('created');
366
367         if ($ufp->find()) { // XXX: this should always happen
368             while ($ufp->fetch()) {
369                 $user = User::staticGet('id', $ufp->user_id);
370                 if (!empty($user)) { // XXX: this would also be unusual
371                     $flaggers[] = clone($user);
372                 }
373             }
374         }
375
376         $cnt    = count($flaggers);
377         $others = 0;
378
379         if ($cnt > self::MAX_FLAGGERS) {
380             $flaggers = array_slice($flaggers, 0, self::MAX_FLAGGERS);
381             $others   = $cnt - self::MAX_FLAGGERS;
382         }
383
384         $lnks = array();
385
386         foreach ($flaggers as $flagger) {
387
388             $url = common_local_url('showstream',
389                                     array('nickname' => $flagger->nickname));
390
391             $lnks[] = XMLStringer::estring('a', array('href' => $url,
392                                                       'class' => 'flagger'),
393                                            $flagger->nickname);
394         }
395
396         if ($cnt > 0) {
397             $text = _('Flagged by ');
398
399             $text .= implode(', ', $lnks);
400
401             if ($others > 0) {
402                 $text .= sprintf(_(' and %d others'), $others);
403             }
404
405             $this->out->elementStart('p', array('class' => 'flaggers'));
406             $this->out->raw($text);
407             $this->out->elementEnd('p');
408         }
409     }
410 }