]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/flagprofile.php
77c86b233bd50e800a5e44baa69ab12e9d2795fb
[quix0rs-gnu-social.git] / plugins / UserFlag / flagprofile.php
1 <?php
2 /**
3  * Add a flag to a profile
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  * Action to flag a profile.
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 FlagprofileAction extends Action
45 {
46     var $profile = null;
47     var $flag    = 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         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
62             throw new ClientException(_('Action only accepts POST'));
63         }
64
65         if (!common_logged_in()) {
66             $this->clientError(_('Not logged in.'));
67             return false;
68         }
69
70         $id = $this->trimmed('flagprofileto');
71
72         if (!$id) {
73             $this->clientError(_('No profile specified.'));
74             return false;
75         }
76
77         $this->profile = Profile::staticGet('id', $id);
78
79         if (empty($this->profile)) {
80             $this->clientError(_('No profile with that ID.'));
81             return false;
82         }
83
84         $user = common_current_user();
85
86         assert(!empty($user)); // checked above
87
88         if (User_flag_profile::exists($this->profile->id,
89                                       $user->id))
90         {
91             $this->clientError(_('Flag already exists.'));
92             return false;
93         }
94
95         return true;
96     }
97
98     /**
99      * Handle request
100      *
101      * @param array $args $_REQUEST args; handled in prepare()
102      *
103      * @return void
104      */
105
106     function handle($args)
107     {
108         parent::handle($args);
109
110         $this->flagProfile();
111         
112         if ($this->boolean('ajax')) {
113             header('Content-Type: text/xml;charset=utf-8');
114             $this->xw->startDocument('1.0', 'UTF-8');
115             $this->elementStart('html');
116             $this->elementStart('head');
117             $this->element('title', null, _('Flagged for review'));
118             $this->elementEnd('head');
119             $this->elementStart('body');
120             $this->element('p', 'flagged', _('Flagged'));
121             $this->elementEnd('body');
122             $this->elementEnd('html');
123         } else {
124             $this->returnTo();
125         }
126     }
127
128     function title() {
129         return _('Flag profile');
130     }
131
132     /**
133      * save the profile flag
134      *
135      * @return void
136      */
137
138     function flagProfile()
139     {
140         $user = common_current_user();
141
142         assert(!empty($user));
143         assert(!empty($this->profile));
144
145         $ufp = new User_flag_profile();
146
147         $ufp->profile_id = $this->profile->id;
148         $ufp->user_id    = $user->id;
149         $ufp->created    = common_sql_now();
150
151         if (!$ufp->insert()) {
152             throw new ServerException(sprintf(_("Couldn't flag profile '%s' with flag '%s'."),
153                                               $this->profile->nickname, $this->flag));
154         }
155
156         $ufp->free();
157     }
158
159     function returnTo()
160     {
161         // Now, gotta figure where we go back to
162         foreach ($this->args as $k => $v) {
163             if ($k == 'returnto-action') {
164                 $action = $v;
165             } elseif (substr($k, 0, 9) == 'returnto-') {
166                 $args[substr($k, 9)] = $v;
167             }
168         }
169
170         common_redirect(common_local_url($action, $args), 303);
171     }
172 }
173