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