]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofilecolors.php
4fa85c68770f9bcf67b51e0da4ed213137e2aedf
[quix0rs-gnu-social.git] / actions / apiaccountupdateprofilecolors.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update a user's design colors
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  API
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009-2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/apiauth.php';
35
36 /**
37  * Sets one or more hex values that control the color scheme of the
38  * authenticating user's design
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class ApiAccountUpdateProfileColorsAction extends ApiAuthAction
47 {
48     var $profile_background_color     = null;
49     var $profile_text_color           = null;
50     var $profile_link_color           = null;
51     var $profile_sidebar_fill_color   = null;
52     var $profile_sidebar_border_color = null;
53
54     /**
55      * Take arguments for running
56      *
57      * @param array $args $_REQUEST args
58      *
59      * @return boolean success flag
60      */
61     function prepare($args)
62     {
63         parent::prepare($args);
64
65         $this->user   = $this->auth_user;
66
67         $this->profile_background_color
68             = $this->trimmed('profile_background_color');
69         $this->profile_text_color
70             = $this->trimmed('profile_text_color');
71         $this->profile_link_color
72             = $this->trimmed('profile_link_color');
73         $this->profile_sidebar_fill_color
74             = $this->trimmed('profile_sidebar_fill_color');
75
76         // XXX: we don't support changing the sidebar border color
77         // in our designs.
78
79         $this->profile_sidebar_border_color
80             = $this->trimmed('profile_sidebar_border_color');
81
82         // XXX: Unlike Twitter, we do allow people to change the 'content color'
83
84         $this->profile_content_color = $this->trimmed('profile_content_color');
85
86         return true;
87     }
88
89     /**
90      * Handle the request
91      *
92      * Try to save the user's colors in her design. Create a new design
93      * if the user doesn't already have one.
94      *
95      * @param array $args $_REQUEST data (unused)
96      *
97      * @return void
98      */
99     function handle($args)
100     {
101         parent::handle($args);
102
103         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
104             $this->clientError(
105                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
106                 _('This method requires a POST.'),
107                 400, $this->format
108             );
109             return;
110         }
111
112         if (!in_array($this->format, array('xml', 'json'))) {
113             $this->clientError(
114                 // TRANS: Client error displayed when coming across a non-supported API method.
115                 _('API method not found.'),
116                 404,
117                 $this->format
118             );
119             return;
120         }
121
122         $design = $this->user->getDesign();
123
124         if (!empty($design)) {
125             $original = clone($design);
126
127             try {
128                 $this->setColors($design);
129             } catch (WebColorException $e) {
130                 $this->clientError($e->getMessage(), 400, $this->format);
131                 return false;
132             }
133
134             $result = $design->update($original);
135
136             if ($result === false) {
137                 common_log_db_error($design, 'UPDATE', __FILE__);
138                 // TRANS: Client error displayed when a database error occurs updating profile colours.
139                 $this->clientError(_('Could not update your design.'));
140                 return;
141             }
142         } else {
143             $this->user->query('BEGIN');
144
145             // save new design
146             $design = new Design();
147
148             try {
149                 $this->setColors($design);
150             } catch (WebColorException $e) {
151                 $this->clientError($e->getMessage(), 400, $this->format);
152                 return false;
153             }
154
155             $id = $design->insert();
156
157             if (empty($id)) {
158                 common_log_db_error($id, 'INSERT', __FILE__);
159                 // TRANS: Client error displayed when a database error occurs inserting profile colours.
160                 $this->clientError(_('Unable to save your design settings.'));
161                 return;
162             }
163
164             $original              = clone($this->user);
165             $this->user->design_id = $id;
166             $result                = $this->user->update($original);
167
168             if (empty($result)) {
169                 common_log_db_error($original, 'UPDATE', __FILE__);
170                 // TRANS: Client error displayed when a database error occurs updating profile colours.
171                 $this->clientError(_('Unable to save your design settings.'));
172                 $this->user->query('ROLLBACK');
173                 return;
174             }
175
176             $this->user->query('COMMIT');
177         }
178
179         $profile = $this->user->getProfile();
180
181         if (empty($profile)) {
182             // TRANS: Error message displayed when referring to a user without a profile.
183             $this->clientError(_('User has no profile.'));
184             return;
185         }
186
187         $twitter_user = $this->twitterUserArray($profile, true);
188
189         if ($this->format == 'xml') {
190             $this->initDocument('xml');
191             $this->showTwitterXmlUser($twitter_user, 'user', true);
192             $this->endDocument('xml');
193         } elseif ($this->format == 'json') {
194             $this->initDocument('json');
195             $this->showJsonObjects($twitter_user);
196             $this->endDocument('json');
197         }
198     }
199
200     /**
201      * Sets the user's design colors based on the request parameters
202      *
203      * @param Design $design the user's Design
204      *
205      * @return void
206      */
207     function setColors($design)
208     {
209         $bgcolor = empty($this->profile_background_color) ?
210             null : new WebColor($this->profile_background_color);
211         $tcolor  = empty($this->profile_text_color) ?
212             null : new WebColor($this->profile_text_color);
213         $sbcolor = empty($this->profile_sidebar_fill_color) ?
214             null : new WebColor($this->profile_sidebar_fill_color);
215         $lcolor  = empty($this->profile_link_color) ?
216             null : new WebColor($this->profile_link_color);
217         $ccolor  = empty($this->profile_content_color) ?
218             null : new WebColor($this->profile_content_color);
219
220         if (!empty($bgcolor)) {
221             $design->backgroundcolor = $bgcolor->intValue();
222         }
223
224         if (!empty($ccolor)) {
225             $design->contentcolor = $ccolor->intValue();
226         }
227
228         if (!empty($sbcolor)) {
229             $design->sidebarcolor = $sbcolor->intValue();
230         }
231
232         if (!empty($tcolor)) {
233             $design->textcolor = $tcolor->intValue();
234         }
235
236         if (!empty($lcolor)) {
237             $design->linkcolor = $lcolor->intValue();
238         }
239
240         return true;
241     }
242 }