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