3 * StatusNet, the distributed open-source microblogging tool
5 * Update a user's design colors
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.
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.
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/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2009 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/
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/lib/apiauth.php';
37 * Sets one or more hex values that control the color scheme of the
38 * authenticating user's design
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/
47 class ApiAccountUpdateProfileColorsAction extends ApiAuthAction
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;
57 * Take arguments for running
59 * @param array $args $_REQUEST args
61 * @return boolean success flag
65 function prepare($args)
67 parent::prepare($args);
69 $this->user = $this->auth_user;
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');
80 // XXX: we don't support changing the sidebar border color
83 $this->profile_sidebar_border_color
84 = $this->trimmed('profile_sidebar_border_color');
86 // XXX: Unlike Twitter, we do allow people to change the 'content color'
88 $this->profile_content_color = $this->trimmed('profile_content_color');
96 * Try to save the user's colors in her design. Create a new design
97 * if the user doesn't already have one.
99 * @param array $args $_REQUEST data (unused)
104 function handle($args)
106 parent::handle($args);
108 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
110 _('This method requires a POST.'),
116 if (!in_array($this->format, array('xml', 'json'))) {
118 _('API method not found.'),
125 $design = $this->user->getDesign();
127 if (!empty($design)) {
129 $original = clone($design);
132 $this->setColors($design);
133 } catch (WebColorException $e) {
134 $this->clientError($e->getMessage());
138 $result = $design->update($original);
140 if ($result === false) {
141 common_log_db_error($design, 'UPDATE', __FILE__);
142 $this->clientError(_('Could not update your design.'));
148 $this->user->query('BEGIN');
151 $design = new Design();
154 $this->setColors($design);
155 } catch (WebColorException $e) {
156 $this->clientError($e->getMessage());
160 $id = $design->insert();
163 common_log_db_error($id, 'INSERT', __FILE__);
164 $this->clientError(_('Unable to save your design settings.'));
168 $original = clone($this->user);
169 $this->user->design_id = $id;
170 $result = $this->user->update($original);
172 if (empty($result)) {
173 common_log_db_error($original, 'UPDATE', __FILE__);
174 $this->clientError(_('Unable to save your design settings.'));
175 $this->user->query('ROLLBACK');
179 $this->user->query('COMMIT');
182 $profile = $this->user->getProfile();
184 if (empty($profile)) {
185 $this->clientError(_('User has no profile.'));
189 $twitter_user = $this->twitterUserArray($profile, true);
191 if ($this->format == 'xml') {
192 $this->initDocument('xml');
193 $this->showTwitterXmlUser($twitter_user);
194 $this->endDocument('xml');
195 } elseif ($this->format == 'json') {
196 $this->initDocument('json');
197 $this->showJsonObjects($twitter_user);
198 $this->endDocument('json');
203 * Sets the user's design colors based on the request parameters
205 * @param Design $design the user's Design
210 function setColors($design)
212 $bgcolor = empty($this->profile_background_color) ?
213 null : new WebColor($this->profile_background_color);
214 $tcolor = empty($this->profile_text_color) ?
215 null : new WebColor($this->profile_text_color);
216 $sbcolor = empty($this->profile_sidebar_fill_color) ?
217 null : new WebColor($this->profile_sidebar_fill_color);
218 $lcolor = empty($this->profile_link_color) ?
219 null : new WebColor($this->profile_link_color);
220 $ccolor = empty($this->profile_content_color) ?
221 null : new WebColor($this->profile_content_color);
223 if (!empty($bgcolor)) {
224 $design->backgroundcolor = $bgcolor->intValue();
227 if (!empty($ccolor)) {
228 $design->contentcolor = $ccolor->intValue();
231 if (!empty($sbcolor)) {
232 $design->sidebarcolor = $sbcolor->intValue();
235 if (!empty($tcolor)) {
236 $design->textcolor = $tcolor->intValue();
239 if (!empty($lcolor)) {
240 $design->linkcolor = $lcolor->intValue();