]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/userdesignsettings.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / actions / userdesignsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Change user password
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  Settings
23  * @package   StatusNet
24  * @author    Sarven Capadisli <csarven@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR . '/lib/designsettings.php';
36
37 /**
38  * Set a user's design
39  *
40  * Saves a design for a given user
41  *
42  * @category Settings
43  * @package  StatusNet
44  * @author   Zach Copley <zach@status.net>
45  * @author   Sarven Capadisli <csarven@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class UserDesignSettingsAction extends DesignSettingsAction
50 {
51     /**
52      * Sets the right action for the form, and passes request args into
53      * the base action
54      *
55      * @param array $args misc. arguments
56      *
57      * @return boolean true
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63         $this->submitaction = common_local_url('userdesignsettings');
64         return true;
65     }
66
67     /**
68      * Title of the page
69      *
70      * @return string Title of the page
71      */
72     function title()
73     {
74         return _('Profile design');
75     }
76
77     /**
78      * Instructions for use
79      *
80      * @return instructions for use
81      */
82     function getInstructions()
83     {
84         return _('Customize the way your profile looks ' .
85         'with a background image and a colour palette of your choice.');
86     }
87
88     /**
89      * Get the design we want to edit
90      *
91      * @return Design
92      */
93     function getWorkingDesign()
94     {
95         $user   = common_current_user();
96         $design = $user->getDesign();
97         return $design;
98     }
99
100     /**
101      * Content area of the page
102      *
103      * Shows a form for changing the design
104      *
105      * @return void
106      */
107     function showContent()
108     {
109         $design = $this->getWorkingDesign();
110
111         if (empty($design)) {
112             $design = Design::siteDesign();
113         }
114
115         $this->showDesignForm($design);
116     }
117
118     /**
119      * Shows the design settings form
120      *
121      * @param Design $design a working design to show
122      *
123      * @return nothing
124      */
125
126     function showDesignForm($design)
127     {
128         $form = new UserDesignForm($this, $design, $this->submitaction);
129         $form->show();
130     }
131
132     /**
133      * Save or update the user's design settings
134      *
135      * @return void
136      */
137     function saveDesign()
138     {
139         $this->saveDesignPreferences();
140
141         foreach ($this->args as $key => $val) {
142             if (preg_match('/(#ho|ho)Td.*g/i', $val)) {
143                 $this->sethd();
144                 return;
145             }
146         }
147
148         try {
149             $bgcolor = new WebColor($this->trimmed('design_background'));
150             $ccolor  = new WebColor($this->trimmed('design_content'));
151             $sbcolor = new WebColor($this->trimmed('design_sidebar'));
152             $tcolor  = new WebColor($this->trimmed('design_text'));
153             $lcolor  = new WebColor($this->trimmed('design_links'));
154         } catch (WebColorException $e) {
155             $this->showForm($e->getMessage());
156             return;
157         }
158
159         $onoff = $this->arg('design_background-image_onoff');
160
161         $on   = false;
162         $off  = false;
163         $tile = false;
164
165         if ($onoff == 'on') {
166             $on = true;
167         } else {
168             $off = true;
169         }
170
171         $repeat = $this->boolean('design_background-image_repeat');
172
173         if ($repeat) {
174             $tile = true;
175         }
176
177         $user = common_current_user();
178
179         $design = $user->getDesign();
180
181         if (!empty($design)) {
182             $original = clone($design);
183
184             $design->backgroundcolor = $bgcolor->intValue();
185             $design->contentcolor    = $ccolor->intValue();
186             $design->sidebarcolor    = $sbcolor->intValue();
187             $design->textcolor       = $tcolor->intValue();
188             $design->linkcolor       = $lcolor->intValue();
189
190             $design->setDisposition($on, $off, $tile);
191
192             $result = $design->update($original);
193
194             if ($result === false) {
195                 common_log_db_error($design, 'UPDATE', __FILE__);
196                 $this->showForm(_('Could not update your design.'));
197                 return;
198             }
199             // update design
200         } else {
201             $user->query('BEGIN');
202
203             // save new design
204             $design = new Design();
205
206             $design->backgroundcolor = $bgcolor->intValue();
207             $design->contentcolor    = $ccolor->intValue();
208             $design->sidebarcolor    = $sbcolor->intValue();
209             $design->textcolor       = $tcolor->intValue();
210             $design->linkcolor       = $lcolor->intValue();
211
212             $design->setDisposition($on, $off, $tile);
213
214             $id = $design->insert();
215
216             if (empty($id)) {
217                 common_log_db_error($id, 'INSERT', __FILE__);
218                 $this->showForm(_('Unable to save your design settings.'));
219                 return;
220             }
221
222             $original        = clone($user);
223             $user->design_id = $id;
224             $result          = $user->update($original);
225
226             if (empty($result)) {
227                 common_log_db_error($original, 'UPDATE', __FILE__);
228                 $this->showForm(_('Unable to save your design settings.'));
229                 $user->query('ROLLBACK');
230                 return;
231             }
232
233             $user->query('COMMIT');
234
235         }
236
237         $this->saveBackgroundImage($design);
238
239         $this->showForm(_('Design preferences saved.'), true);
240     }
241
242     /**
243      * Alternate default colors
244      *
245      * @return nothing
246      */
247     function sethd()
248     {
249
250         $user   = common_current_user();
251         $design = $user->getDesign();
252
253         $user->query('BEGIN');
254
255         // alternate colors
256         $design = new Design();
257
258         $design->backgroundcolor = 16184329;
259         $design->contentcolor    = 16059904;
260         $design->sidebarcolor    = 16059904;
261         $design->textcolor       = 0;
262         $design->linkcolor       = 16777215;
263
264         $design->setDisposition(false, true, false);
265
266         $id = $design->insert();
267
268         if (empty($id)) {
269             common_log_db_error($id, 'INSERT', __FILE__);
270             $this->showForm(_('Unable to save your design settings.'));
271             return;
272         }
273
274         $original        = clone($user);
275         $user->design_id = $id;
276         $result          = $user->update($original);
277
278         if (empty($result)) {
279             common_log_db_error($original, 'UPDATE', __FILE__);
280             $this->showForm(_('Unable to save your design settings.'));
281             $user->query('ROLLBACK');
282             return;
283         }
284
285         $user->query('COMMIT');
286
287         $this->saveBackgroundImage($design);
288
289         $this->showForm(_('Enjoy your hotdog!'), true);
290     }
291
292     function saveDesignPreferences()
293     {
294         $viewdesigns = $this->boolean('viewdesigns');
295
296         $user = common_current_user();
297
298         $original = clone($user);
299
300         $user->viewdesigns = $viewdesigns;
301
302         $result = $user->update($original);
303
304         if ($result === false) {
305             common_log_db_error($user, 'UPDATE', __FILE__);
306             throw new ServerException(_('Couldn\'t update user.'));
307         }
308     }
309 }
310
311 class UserDesignForm extends DesignForm
312 {
313     function __construct($out, $design, $actionurl)
314     {
315         parent::__construct($out, $design, $actionurl);
316     }
317
318     /**
319      * Legend of the Form
320      *
321      * @return void
322      */
323     function formLegend()
324     {
325         $this->out->element('legend', null, _('Design settings'));
326     }
327
328     /**
329      * Data elements of the form
330      *
331      * @return void
332      */
333
334     function formData()
335     {
336         $user = common_current_user();
337
338         $this->out->elementStart('ul', 'form_data');
339         $this->out->elementStart('li');
340         $this->out->checkbox('viewdesigns', _('View profile designs'),
341                          -                        $user->viewdesigns, _('Show or hide profile designs.'));
342         $this->out->elementEnd('li');
343         $this->out->elementEnd('ul');
344
345         $this->out->elementEnd('fieldset');
346
347         $this->out->elementStart('fieldset');
348         $this->out->element('legend', null, _('Background file'));
349
350         parent::formData();
351     }
352 }