3 * StatusNet, the distributed open-source microblogging tool
5 * Design administration panel
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 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/
32 if (!defined('STATUSNET')) {
37 * Administer design settings
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/
48 class DesignadminpanelAction extends AdminPanelAction
51 /* The default site design */
55 * Returns the page title
57 * @return string page title
62 // TRANS: Message used as title for design settings for the site.
67 * Instructions for using this form.
69 * @return string instructions
72 function getInstructions()
74 return _('Design settings for this StatusNet site.');
78 * Get the default design and show the design admin panel form
85 $this->design = Design::siteDesign();
86 $form = new DesignAdminPanelForm($this);
92 * Save settings from the form
97 function saveSettings()
99 if ($this->arg('save')) {
100 $this->saveDesignSettings();
101 } else if ($this->arg('defaults')) {
102 $this->restoreDefaults();
104 $this->clientError(_('Unexpected form submission.'));
109 * Save the new design settings
114 function saveDesignSettings()
116 // Workaround for PHP returning empty $_POST and $_FILES when POST
117 // length > post_max_size in php.ini
121 && ($_SERVER['CONTENT_LENGTH'] > 0)
123 $msg = _('The server was unable to handle that much POST ' .
124 'data (%s bytes) due to its current configuration.');
125 $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
129 // check for file uploads
131 $bgimage = $this->saveBackgroundImage();
132 $customTheme = $this->saveCustomTheme();
134 $oldtheme = common_config('site', 'theme');
136 // This feels pretty hacky :D
137 $this->args['theme'] = $customTheme;
138 $themeChanged = true;
140 $themeChanged = ($this->trimmed('theme') != $oldtheme);
143 static $settings = array('theme', 'logo');
147 foreach ($settings as $setting) {
148 $values[$setting] = $this->trimmed($setting);
151 $this->validate($values);
153 $config = new Config();
155 $config->query('BEGIN');
157 // Only update colors if the theme has not changed.
159 if (!$themeChanged) {
161 $bgcolor = new WebColor($this->trimmed('design_background'));
162 $ccolor = new WebColor($this->trimmed('design_content'));
163 $sbcolor = new WebColor($this->trimmed('design_sidebar'));
164 $tcolor = new WebColor($this->trimmed('design_text'));
165 $lcolor = new WebColor($this->trimmed('design_links'));
167 Config::save('design', 'backgroundcolor', $bgcolor->intValue());
168 Config::save('design', 'contentcolor', $ccolor->intValue());
169 Config::save('design', 'sidebarcolor', $sbcolor->intValue());
170 Config::save('design', 'textcolor', $tcolor->intValue());
171 Config::save('design', 'linkcolor', $lcolor->intValue());
174 $onoff = $this->arg('design_background-image_onoff');
179 if ($onoff == 'on') {
185 $tile = $this->boolean('design_background-image_repeat');
187 // Hack to use Design's bit setter
188 $scratch = new Design();
189 $scratch->setDisposition($on, $off, $tile);
191 Config::save('design', 'disposition', $scratch->disposition);
193 foreach ($settings as $setting) {
194 Config::save('site', $setting, $values[$setting]);
197 if (isset($bgimage)) {
198 Config::save('design', 'backgroundimage', $bgimage);
201 if (common_config('custom_css', 'enabled')) {
202 $css = $this->arg('css');
203 if ($css != common_config('custom_css', 'css')) {
204 Config::save('custom_css', 'css', $css);
208 $config->query('COMMIT');
212 * Restore the default design
217 function restoreDefaults()
219 $this->deleteSetting('site', 'logo');
220 $this->deleteSetting('site', 'theme');
223 'theme', 'backgroundimage', 'backgroundcolor', 'contentcolor',
224 'sidebarcolor', 'textcolor', 'linkcolor', 'disposition'
227 foreach ($settings as $setting) {
228 $this->deleteSetting('design', $setting);
231 // XXX: Should we restore the default dir settings, etc.? --Z
233 // XXX: I can't get it to show the new settings without forcing
234 // this terrible reload -- FIX ME!
235 common_redirect(common_local_url('designadminpanel'), 303);
239 * Save the background image if the user uploaded one
241 * @return string $filename the filename of the image
244 function saveBackgroundImage()
248 if ($_FILES['design_background-image_file']['error'] ==
255 ImageFile::fromUpload('design_background-image_file');
256 } catch (Exception $e) {
257 $this->clientError('Unable to save background image.');
261 // Note: site design background image has a special filename
263 $filename = Design::filename('site-design-background',
264 image_type_to_extension($imagefile->type),
267 $filepath = Design::path($filename);
269 move_uploaded_file($imagefile->filepath, $filepath);
271 // delete any old backround img laying around
273 if (isset($this->design->backgroundimage)) {
274 @unlink(Design::path($design->backgroundimage));
282 * Save the custom theme if the user uploaded one.
284 * @return mixed custom theme name, if succesful, or null if no theme upload.
285 * @throws ClientException for invalid theme archives
286 * @throws ServerException if trouble saving the theme files
289 function saveCustomTheme()
291 if (common_config('theme_upload', 'enabled') &&
292 $_FILES['design_upload_theme']['error'] == UPLOAD_ERR_OK) {
294 $upload = ThemeUploader::fromUpload('design_upload_theme');
295 $basedir = common_config('local', 'dir');
296 if (empty($basedir)) {
297 $basedir = INSTALLDIR . '/local';
299 $name = 'custom'; // @todo allow multiples, custom naming?
300 $outdir = $basedir . '/theme/' . $name;
301 $upload->extract($outdir);
309 * Attempt to validate setting values
314 function validate(&$values)
316 if (!empty($values['logo']) &&
317 !Validate::uri($values['logo'], array('allowed_schemes' => array('http', 'https')))) {
318 $this->clientError(_('Invalid logo URL.'));
321 if (!in_array($values['theme'], Theme::listAvailable())) {
322 $this->clientError(sprintf(_("Theme not available: %s."), $values['theme']));
327 * Add the Farbtastic stylesheet
332 function showStylesheets()
334 parent::showStylesheets();
335 $this->cssLink('js/farbtastic/farbtastic.css',null,'screen, projection, tv');
339 * Add the Farbtastic scripts
344 function showScripts()
346 parent::showScripts();
348 $this->script('farbtastic/farbtastic.js');
349 $this->script('userdesign.go.js');
351 $this->autofocus('design_background-image_file');
356 class DesignAdminPanelForm extends AdminForm
362 * @return int ID of the form
367 return 'form_design_admin_panel';
373 * @return string class of the form
378 return 'form_settings';
382 * HTTP method used to submit the form
384 * For image data we need to send multipart/form-data
385 * so we set that here too
387 * @return string the method to use for submitting
392 $this->enctype = 'multipart/form-data';
400 * @return string URL of the action
405 return common_local_url('designadminpanel');
409 * Data elements of the form
418 $this->showBackground();
420 $this->showAdvanced();
425 $this->out->elementStart('fieldset', array('id' => 'settings_design_logo'));
426 $this->out->element('legend', null, _('Change logo'));
428 $this->out->elementStart('ul', 'form_data');
431 $this->input('logo', _('Site logo'), 'Logo for the site (full URL)');
434 $this->out->elementEnd('ul');
436 $this->out->elementEnd('fieldset');
442 $this->out->elementStart('fieldset', array('id' => 'settings_design_theme'));
443 $this->out->element('legend', null, _('Change theme'));
445 $this->out->elementStart('ul', 'form_data');
447 $themes = Theme::listAvailable();
449 // XXX: listAvailable() can return an empty list if you
450 // screw up your settings, so just in case:
452 if (empty($themes)) {
453 $themes = array('default', 'default');
457 $themes = array_combine($themes, $themes);
460 $this->out->dropdown('theme', _('Site theme'),
461 $themes, _('Theme for the site.'),
462 false, $this->value('theme'));
465 if (common_config('theme_upload', 'enabled')) {
467 $this->out->element('label', array('for' => 'design_upload_theme'), _('Custom theme'));
468 $this->out->element('input', array('id' => 'design_upload_theme',
469 'name' => 'design_upload_theme',
471 $this->out->element('p', 'form_guide', _('You can upload a custom StatusNet theme as a .ZIP archive.'));
475 $this->out->elementEnd('ul');
477 $this->out->elementEnd('fieldset');
480 function showBackground()
482 $design = $this->out->design;
484 $this->out->elementStart('fieldset', array('id' =>
485 'settings_design_background-image'));
486 $this->out->element('legend', null, _('Change background image'));
487 $this->out->elementStart('ul', 'form_data');
490 $this->out->element('label', array('for' => 'design_background-image_file'),
492 $this->out->element('input', array('name' => 'design_background-image_file',
494 'id' => 'design_background-image_file'));
495 $this->out->element('p', 'form_guide',
496 sprintf(_('You can upload a background image for the site. ' .
497 'The maximum file size is %1$s.'), ImageFile::maxFileSize()));
498 $this->out->element('input', array('name' => 'MAX_FILE_SIZE',
500 'id' => 'MAX_FILE_SIZE',
501 'value' => ImageFile::maxFileSizeInt()));
504 if (!empty($design->backgroundimage)) {
506 $this->out->elementStart('li', array('id' =>
507 'design_background-image_onoff'));
509 $this->out->element('img', array('src' =>
510 Design::url($design->backgroundimage)));
512 $attrs = array('name' => 'design_background-image_onoff',
514 'id' => 'design_background-image_on',
518 if ($design->disposition & BACKGROUND_ON) {
519 $attrs['checked'] = 'checked';
522 $this->out->element('input', $attrs);
524 $this->out->element('label', array('for' => 'design_background-image_on',
526 // TRANS: Used as radio button label to add a background image.
529 $attrs = array('name' => 'design_background-image_onoff',
531 'id' => 'design_background-image_off',
535 if ($design->disposition & BACKGROUND_OFF) {
536 $attrs['checked'] = 'checked';
539 $this->out->element('input', $attrs);
541 $this->out->element('label', array('for' => 'design_background-image_off',
543 // TRANS: Used as radio button label to not add a background image.
545 $this->out->element('p', 'form_guide', _('Turn background image on or off.'));
549 $this->out->checkbox('design_background-image_repeat',
550 _('Tile background image'),
551 ($design->disposition & BACKGROUND_TILE) ? true : false);
555 $this->out->elementEnd('ul');
556 $this->out->elementEnd('fieldset');
559 function showColors()
561 $design = $this->out->design;
563 $this->out->elementStart('fieldset', array('id' => 'settings_design_color'));
564 $this->out->element('legend', null, _('Change colours'));
566 $this->out->elementStart('ul', 'form_data');
569 // @fixme avoid loop unrolling in non-performance-critical contexts like this
571 $bgcolor = new WebColor($design->backgroundcolor);
574 $this->out->element('label', array('for' => 'swatch-1'), _('Background'));
575 $this->out->element('input', array('name' => 'design_background',
584 $ccolor = new WebColor($design->contentcolor);
587 $this->out->element('label', array('for' => 'swatch-2'), _('Content'));
588 $this->out->element('input', array('name' => 'design_content',
597 $sbcolor = new WebColor($design->sidebarcolor);
600 $this->out->element('label', array('for' => 'swatch-3'), _('Sidebar'));
601 $this->out->element('input', array('name' => 'design_sidebar',
610 $tcolor = new WebColor($design->textcolor);
613 $this->out->element('label', array('for' => 'swatch-4'), _('Text'));
614 $this->out->element('input', array('name' => 'design_text',
623 $lcolor = new WebColor($design->linkcolor);
626 $this->out->element('label', array('for' => 'swatch-5'), _('Links'));
627 $this->out->element('input', array('name' => 'design_links',
636 } catch (WebColorException $e) {
637 // @fixme normalize them individually!
638 common_log(LOG_ERR, 'Bad color values in site design: ' .
642 $this->out->elementEnd('fieldset');
644 $this->out->elementEnd('ul');
647 function showAdvanced()
649 if (common_config('custom_css', 'enabled')) {
650 $this->out->elementStart('fieldset', array('id' => 'settings_design_advanced'));
651 $this->out->element('legend', null, _('Advanced'));
652 $this->out->elementStart('ul', 'form_data');
655 $this->out->element('label', array('for' => 'css'), _('Custom CSS'));
656 $this->out->element('textarea', array('name' => 'css',
660 strval(common_config('custom_css', 'css')));
663 $this->out->elementEnd('fieldset');
664 $this->out->elementEnd('ul');
674 function formActions()
676 $this->out->submit('defaults', _('Use defaults'), 'submit form_action-default',
677 'defaults', _('Restore default designs'));
679 $this->out->element('input', array('id' => 'settings_design_reset',
682 'class' => 'submit form_action-primary',
683 'title' => _('Reset back to default')));
685 $this->out->submit('save', _('Save'), 'submit form_action-secondary',
686 'save', _('Save design'));