]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofilecolors.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[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
47 class ApiAccountUpdateProfileColorsAction extends ApiAuthAction
48 {
49
50     var $profile_background_color     = null;
51     var $profile_text_color           = null;
52     var $profile_link_color           = null;
53     var $profile_sidebar_fill_color   = null;
54     var $profile_sidebar_border_color = null;
55
56     /**
57      * Take arguments for running
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
62      *
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         $this->user   = $this->auth_user;
70
71         $this->profile_background_color
72             = $this->trimmed('profile_background_color');
73         $this->profile_text_color
74             = $this->trimmed('profile_text_color');
75         $this->profile_link_color
76             = $this->trimmed('profile_link_color');
77         $this->profile_sidebar_fill_color
78             = $this->trimmed('profile_sidebar_fill_color');
79
80         // XXX: we don't support changing the sidebar border color
81         // in our designs.
82
83         $this->profile_sidebar_border_color
84             = $this->trimmed('profile_sidebar_border_color');
85
86         // XXX: Unlike Twitter, we do allow people to change the 'content color'
87
88         $this->profile_content_color = $this->trimmed('profile_content_color');
89
90         return true;
91     }
92
93     /**
94      * Handle the request
95      *
96      * Try to save the user's colors in her design. Create a new design
97      * if the user doesn't already have one.
98      *
99      * @param array $args $_REQUEST data (unused)
100      *
101      * @return void
102      */
103
104     function handle($args)
105     {
106         parent::handle($args);
107
108         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
109             $this->clientError(
110                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
111                 _('This method requires a POST.'),
112                 400, $this->format
113             );
114             return;
115         }
116
117         if (!in_array($this->format, array('xml', 'json'))) {
118             $this->clientError(
119                 _('API method not found.'),
120                 404,
121                 $this->format
122             );
123             return;
124         }
125
126         $design = $this->user->getDesign();
127
128         if (!empty($design)) {
129
130             $original = clone($design);
131
132             try {
133                 $this->setColors($design);
134             } catch (WebColorException $e) {
135                 $this->clientError($e->getMessage(), 400, $this->format);
136                 return false;
137             }
138
139             $result = $design->update($original);
140
141             if ($result === false) {
142                 common_log_db_error($design, 'UPDATE', __FILE__);
143                 $this->clientError(_('Could not update your design.'));
144                 return;
145             }
146
147         } else {
148
149             $this->user->query('BEGIN');
150
151             // save new design
152             $design = new Design();
153
154             try {
155                 $this->setColors($design);
156             } catch (WebColorException $e) {
157                 $this->clientError($e->getMessage(), 400, $this->format);
158                 return false;
159             }
160
161             $id = $design->insert();
162
163             if (empty($id)) {
164                 common_log_db_error($id, 'INSERT', __FILE__);
165                 $this->clientError(_('Unable to save your design settings.'));
166                 return;
167             }
168
169             $original              = clone($this->user);
170             $this->user->design_id = $id;
171             $result                = $this->user->update($original);
172
173             if (empty($result)) {
174                 common_log_db_error($original, 'UPDATE', __FILE__);
175                 $this->clientError(_('Unable to save your design settings.'));
176                 $this->user->query('ROLLBACK');
177                 return;
178             }
179
180             $this->user->query('COMMIT');
181         }
182
183         $profile = $this->user->getProfile();
184
185         if (empty($profile)) {
186             $this->clientError(_('User has no profile.'));
187             return;
188         }
189
190         $twitter_user = $this->twitterUserArray($profile, true);
191
192         if ($this->format == 'xml') {
193             $this->initDocument('xml');
194             $this->showTwitterXmlUser($twitter_user);
195             $this->endDocument('xml');
196         } elseif ($this->format == 'json') {
197             $this->initDocument('json');
198             $this->showJsonObjects($twitter_user);
199             $this->endDocument('json');
200         }
201     }
202
203     /**
204      * Sets the user's design colors based on the request parameters
205      *
206      * @param Design $design the user's Design
207      *
208      * @return void
209      */
210
211     function setColors($design)
212     {
213         $bgcolor = empty($this->profile_background_color) ?
214             null : new WebColor($this->profile_background_color);
215         $tcolor  = empty($this->profile_text_color) ?
216             null : new WebColor($this->profile_text_color);
217         $sbcolor = empty($this->profile_sidebar_fill_color) ?
218             null : new WebColor($this->profile_sidebar_fill_color);
219         $lcolor  = empty($this->profile_link_color) ?
220             null : new WebColor($this->profile_link_color);
221         $ccolor  = empty($this->profile_content_color) ?
222             null : new WebColor($this->profile_content_color);
223
224         if (!empty($bgcolor)) {
225             $design->backgroundcolor = $bgcolor->intValue();
226         }
227
228         if (!empty($ccolor)) {
229             $design->contentcolor = $ccolor->intValue();
230         }
231
232         if (!empty($sbcolor)) {
233             $design->sidebarcolor = $sbcolor->intValue();
234         }
235
236         if (!empty($tcolor)) {
237             $design->textcolor = $tcolor->intValue();
238         }
239
240         if (!empty($lcolor)) {
241             $design->linkcolor = $lcolor->intValue();
242         }
243
244         return true;
245     }
246
247 }