]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/designadminpanel.php
refactor the common parts of usermap and allmap into a common base class
[quix0rs-gnu-social.git] / actions / designadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Design administration panel
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    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * Administer design settings
38  *
39  * @category Admin
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Zach Copley <zach@status.net>
43  * @author   Sarven Capadisli <csarven@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class DesignadminpanelAction extends AdminPanelAction
49 {
50
51     /* The default site design */
52     var $design = null;
53
54     /**
55      * Returns the page title
56      *
57      * @return string page title
58      */
59
60     function title()
61     {
62         return _('Design');
63     }
64
65     /**
66      * Instructions for using this form.
67      *
68      * @return string instructions
69      */
70
71     function getInstructions()
72     {
73         return _('Design settings for this StatusNet site.');
74     }
75
76     /**
77      * Get the default design and show the design admin panel form
78      *
79      * @return void
80      */
81
82     function showForm()
83     {
84         $this->design = Design::siteDesign();
85         $form = new DesignAdminPanelForm($this);
86         $form->show();
87         return;
88     }
89
90     /**
91      * Save settings from the form
92      *
93      * @return void
94      */
95
96     function saveSettings()
97     {
98         if ($this->arg('save')) {
99             $this->saveDesignSettings();
100         } else if ($this->arg('defaults')) {
101             $this->restoreDefaults();
102         } else {
103             $this->clientError(_('Unexpected form submission.'));
104         }
105     }
106
107     /**
108      * Save the new design settings
109      *
110      * @return void
111      */
112
113     function saveDesignSettings()
114     {
115         // Workaround for PHP returning empty $_POST and $_FILES when POST
116         // length > post_max_size in php.ini
117
118         if (empty($_FILES)
119             && empty($_POST)
120             && ($_SERVER['CONTENT_LENGTH'] > 0)
121         ) {
122             $msg = _('The server was unable to handle that much POST ' .
123                 'data (%s bytes) due to its current configuration.');
124             $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
125             return;
126         }
127
128         // check for an image upload
129
130         $bgimage = $this->saveBackgroundImage();
131
132         common_debug("background image: $bgimage");
133
134         static $settings = array('theme', 'logo');
135
136         $values = array();
137
138         foreach ($settings as $setting) {
139             $values[$setting] = $this->trimmed($setting);
140         }
141
142         $this->validate($values);
143
144         // assert(all values are valid);
145
146         $bgcolor = new WebColor($this->trimmed('design_background'));
147         $ccolor  = new WebColor($this->trimmed('design_content'));
148         $sbcolor = new WebColor($this->trimmed('design_sidebar'));
149         $tcolor  = new WebColor($this->trimmed('design_text'));
150         $lcolor  = new WebColor($this->trimmed('design_links'));
151
152         $onoff = $this->arg('design_background-image_onoff');
153
154         $on   = false;
155         $off  = false;
156
157         if ($onoff == 'on') {
158             $on = true;
159         } else {
160             $off = true;
161         }
162
163         $tile = $this->boolean('design_background-image_repeat');
164
165         $config = new Config();
166
167         $config->query('BEGIN');
168
169         foreach ($settings as $setting) {
170             Config::save('site', $setting, $values[$setting]);
171         }
172
173         if (isset($bgimage)) {
174             Config::save('design', 'backgroundimage', $bgimage);
175         }
176
177         Config::save('design', 'backgroundcolor', $bgcolor->intValue());
178         Config::save('design', 'contentcolor', $ccolor->intValue());
179         Config::save('design', 'sidebarcolor', $sbcolor->intValue());
180         Config::save('design', 'textcolor', $tcolor->intValue());
181         Config::save('design', 'linkcolor', $lcolor->intValue());
182
183         // Hack to use Design's bit setter
184         $scratch = new Design();
185         $scratch->setDisposition($on, $off, $tile);
186
187         Config::save('design', 'disposition', $scratch->disposition);
188
189         $config->query('COMMIT');
190
191         return;
192     }
193
194     /**
195       * Restore the default design
196       *
197       * @return void
198       */
199
200     function restoreDefaults()
201     {
202         $this->deleteSetting('site', 'logo');
203         $this->deleteSetting('site', 'theme');
204
205         $settings = array(
206             'theme', 'backgroundimage', 'backgroundcolor', 'contentcolor',
207             'sidebarcolor', 'textcolor', 'linkcolor', 'disposition'
208         );
209
210         foreach ($settings as $setting) {
211             $this->deleteSetting('design', $setting);
212         }
213
214         // XXX: Should we restore the default dir settings, etc.? --Z
215     }
216
217     /**
218      * Save the background image if the user uploaded one
219      *
220      * @return string $filename the filename of the image
221      */
222
223     function saveBackgroundImage()
224     {
225         $filename = null;
226
227         if ($_FILES['design_background-image_file']['error'] ==
228             UPLOAD_ERR_OK) {
229
230             $filepath = null;
231
232             try {
233                 $imagefile =
234                     ImageFile::fromUpload('design_background-image_file');
235             } catch (Exception $e) {
236                 $this->clientError('Unable to save background image.');
237                 return;
238             }
239
240             // Note: site design background image has a special filename
241
242             $filename = Design::filename('site-design-background',
243                 image_type_to_extension($imagefile->type),
244                     common_timestamp());
245
246             $filepath = Design::path($filename);
247
248             move_uploaded_file($imagefile->filepath, $filepath);
249
250             // delete any old backround img laying around
251
252             if (isset($this->design->backgroundimage)) {
253                 @unlink(Design::path($design->backgroundimage));
254             }
255
256             return $filename;
257         }
258     }
259
260     /**
261      * Attempt to validate setting values
262      *
263      * @return void
264      */
265
266     function validate(&$values)
267     {
268         if (!empty($values['logo']) &&
269             !Validate::uri($values['logo'], array('allowed_schemes' => array('http', 'https')))) {
270             $this->clientError(_("Invalid logo URL."));
271         }
272
273         if (!in_array($values['theme'], Theme::listAvailable())) {
274             $this->clientError(sprintf(_("Theme not available: %s"), $values['theme']));
275         }
276     }
277
278     /**
279      * Add the Farbtastic stylesheet
280      *
281      * @return void
282      */
283
284     function showStylesheets()
285     {
286         parent::showStylesheets();
287         $this->cssLink('css/farbtastic.css','base','screen, projection, tv');
288     }
289
290     /**
291      * Add the Farbtastic scripts
292      *
293      * @return void
294      */
295
296     function showScripts()
297     {
298         parent::showScripts();
299
300         $this->script('js/farbtastic/farbtastic.js');
301         $this->script('js/userdesign.go.js');
302
303         $this->autofocus('design_background-image_file');
304     }
305
306 }
307
308 class DesignAdminPanelForm extends AdminForm
309 {
310
311     /**
312      * ID of the form
313      *
314      * @return int ID of the form
315      */
316
317     function id()
318     {
319         return 'form_design_admin_panel';
320     }
321
322     /**
323      * class of the form
324      *
325      * @return string class of the form
326      */
327
328     function formClass()
329     {
330         return 'form_settings';
331     }
332
333     /**
334      * HTTP method used to submit the form
335      *
336      * For image data we need to send multipart/form-data
337      * so we set that here too
338      *
339      * @return string the method to use for submitting
340      */
341
342     function method()
343     {
344         $this->enctype = 'multipart/form-data';
345
346         return 'post';
347     }
348
349     /**
350      * Action of the form
351      *
352      * @return string URL of the action
353      */
354
355     function action()
356     {
357         return common_local_url('designadminpanel');
358     }
359
360     /**
361      * Data elements of the form
362      *
363      * @return void
364      */
365
366     function formData()
367     {
368
369         $this->out->elementStart('fieldset', array('id' => 'settings_design_logo'));
370         $this->out->element('legend', null, _('Change logo'));
371
372         $this->out->elementStart('ul', 'form_data');
373
374         $this->li();
375         $this->input('logo', _('Site logo'), 'Logo for the site (full URL)');
376         $this->unli();
377
378         $this->out->elementEnd('ul');
379
380         $this->out->elementEnd('fieldset');
381         $this->out->elementStart('fieldset', array('id' => 'settings_design_theme'));
382         $this->out->element('legend', null, _('Change theme'));
383
384         $this->out->elementStart('ul', 'form_data');
385
386         $themes = Theme::listAvailable();
387
388         // XXX: listAvailable() can return an empty list if you
389         // screw up your settings, so just in case:
390
391         if (empty($themes)) {
392             $themes = array('default', 'default');
393         }
394
395         asort($themes);
396         $themes = array_combine($themes, $themes);
397
398         $this->li();
399         $this->out->dropdown('theme', _('Site theme'),
400                              $themes, _('Theme for the site.'),
401                              false, $this->value('theme'));
402         $this->unli();
403
404         $this->out->elementEnd('ul');
405
406         $this->out->elementEnd('fieldset');
407
408         $design = $this->out->design;
409
410         $this->out->elementStart('fieldset', array('id' =>
411             'settings_design_background-image'));
412         $this->out->element('legend', null, _('Change background image'));
413         $this->out->elementStart('ul', 'form_data');
414
415         $this->li();
416         $this->out->element('label', array('for' => 'design_background-image_file'),
417                                 _('Background'));
418         $this->out->element('input', array('name' => 'design_background-image_file',
419                                      'type' => 'file',
420                                      'id' => 'design_background-image_file'));
421         $this->out->element('p', 'form_guide',
422             sprintf(_('You can upload a background image for the site. ' .
423               'The maximum file size is %1$s.'), ImageFile::maxFileSize()));
424         $this->out->element('input', array('name' => 'MAX_FILE_SIZE',
425                                           'type' => 'hidden',
426                                           'id' => 'MAX_FILE_SIZE',
427                                           'value' => ImageFile::maxFileSizeInt()));
428         $this->unli();
429
430         if (!empty($design->backgroundimage)) {
431
432             $this->out->elementStart('li', array('id' =>
433                 'design_background-image_onoff'));
434
435             $this->out->element('img', array('src' =>
436                 Design::url($design->backgroundimage)));
437
438             $attrs = array('name' => 'design_background-image_onoff',
439                            'type' => 'radio',
440                            'id' => 'design_background-image_on',
441                            'class' => 'radio',
442                            'value' => 'on');
443
444             if ($design->disposition & BACKGROUND_ON) {
445                 $attrs['checked'] = 'checked';
446             }
447
448             $this->out->element('input', $attrs);
449
450             $this->out->element('label', array('for' => 'design_background-image_on',
451                                           'class' => 'radio'),
452                                           _('On'));
453
454             $attrs = array('name' => 'design_background-image_onoff',
455                            'type' => 'radio',
456                            'id' => 'design_background-image_off',
457                            'class' => 'radio',
458                            'value' => 'off');
459
460             if ($design->disposition & BACKGROUND_OFF) {
461                 $attrs['checked'] = 'checked';
462             }
463
464             $this->out->element('input', $attrs);
465
466             $this->out->element('label', array('for' => 'design_background-image_off',
467                                           'class' => 'radio'),
468                                           _('Off'));
469             $this->out->element('p', 'form_guide', _('Turn background image on or off.'));
470             $this->unli();
471
472             $this->li();
473             $this->out->checkbox('design_background-image_repeat',
474                             _('Tile background image'),
475                             ($design->disposition & BACKGROUND_TILE) ? true : false);
476             $this->unli();
477         }
478
479         $this->out->elementEnd('ul');
480         $this->out->elementEnd('fieldset');
481
482         $this->out->elementStart('fieldset', array('id' => 'settings_design_color'));
483         $this->out->element('legend', null, _('Change colours'));
484
485         $this->out->elementStart('ul', 'form_data');
486
487         try {
488
489             $bgcolor = new WebColor($design->backgroundcolor);
490
491             $this->li();
492             $this->out->element('label', array('for' => 'swatch-1'), _('Background'));
493             $this->out->element('input', array('name' => 'design_background',
494                                           'type' => 'text',
495                                           'id' => 'swatch-1',
496                                           'class' => 'swatch',
497                                           'maxlength' => '7',
498                                           'size' => '7',
499                                           'value' => ''));
500             $this->unli();
501
502             $ccolor = new WebColor($design->contentcolor);
503
504             $this->li();
505             $this->out->element('label', array('for' => 'swatch-2'), _('Content'));
506             $this->out->element('input', array('name' => 'design_content',
507                                           'type' => 'text',
508                                           'id' => 'swatch-2',
509                                           'class' => 'swatch',
510                                           'maxlength' => '7',
511                                           'size' => '7',
512                                           'value' => ''));
513             $this->unli();
514
515             $sbcolor = new WebColor($design->sidebarcolor);
516
517             $this->li();
518             $this->out->element('label', array('for' => 'swatch-3'), _('Sidebar'));
519             $this->out->element('input', array('name' => 'design_sidebar',
520                                         'type' => 'text',
521                                         'id' => 'swatch-3',
522                                         'class' => 'swatch',
523                                         'maxlength' => '7',
524                                         'size' => '7',
525                                         'value' => ''));
526             $this->unli();
527
528             $tcolor = new WebColor($design->textcolor);
529
530             $this->li();
531             $this->out->element('label', array('for' => 'swatch-4'), _('Text'));
532             $this->out->element('input', array('name' => 'design_text',
533                                         'type' => 'text',
534                                         'id' => 'swatch-4',
535                                         'class' => 'swatch',
536                                         'maxlength' => '7',
537                                         'size' => '7',
538                                         'value' => ''));
539             $this->unli();
540
541             $lcolor = new WebColor($design->linkcolor);
542
543             $this->li();
544             $this->out->element('label', array('for' => 'swatch-5'), _('Links'));
545             $this->out->element('input', array('name' => 'design_links',
546                                          'type' => 'text',
547                                          'id' => 'swatch-5',
548                                          'class' => 'swatch',
549                                          'maxlength' => '7',
550                                          'size' => '7',
551                                          'value' => ''));
552             $this->unli();
553
554         } catch (WebColorException $e) {
555             common_log(LOG_ERR, 'Bad color values in site design: ' .
556                 $e->getMessage());
557         }
558
559         $this->out->elementEnd('fieldset');
560
561         $this->out->elementEnd('ul');
562     }
563
564     /**
565      * Action elements
566      *
567      * @return void
568      */
569
570     function formActions()
571     {
572         $this->out->submit('defaults', _('Use defaults'), 'submit form_action-default',
573                 'defaults', _('Restore default designs'));
574
575         $this->out->element('input', array('id' => 'settings_design_reset',
576                                          'type' => 'reset',
577                                          'value' => 'Reset',
578                                          'class' => 'submit form_action-primary',
579                                          'title' => _('Reset back to default')));
580
581         $this->out->submit('save', _('Save'), 'submit form_action-secondary',
582                 'save', _('Save design'));
583     }
584
585 }