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
66 * Instructions for using this form.
68 * @return string instructions
71 function getInstructions()
73 return _('Design settings for this StatusNet site.');
77 * Get the default design and show the design admin panel form
84 $this->design = Design::siteDesign();
85 $form = new DesignAdminPanelForm($this);
91 * Save settings from the form
96 function saveSettings()
98 if ($this->arg('save')) {
99 $this->saveDesignSettings();
100 } else if ($this->arg('defaults')) {
101 $this->restoreDefaults();
103 $this->clientError(_('Unexpected form submission.'));
108 * Save the new design settings
113 function saveDesignSettings()
115 // Workaround for PHP returning empty $_POST and $_FILES when POST
116 // length > post_max_size in php.ini
120 && ($_SERVER['CONTENT_LENGTH'] > 0)
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']));
128 // check for an image upload
130 $bgimage = $this->saveBackgroundImage();
132 static $settings = array('theme', 'logo');
136 foreach ($settings as $setting) {
137 $values[$setting] = $this->trimmed($setting);
140 $this->validate($values);
142 $oldtheme = common_config('site', 'theme');
144 $config = new Config();
146 $config->query('BEGIN');
148 // Only update colors if the theme has not changed.
150 if ($oldtheme == $values['theme']) {
152 $bgcolor = new WebColor($this->trimmed('design_background'));
153 $ccolor = new WebColor($this->trimmed('design_content'));
154 $sbcolor = new WebColor($this->trimmed('design_sidebar'));
155 $tcolor = new WebColor($this->trimmed('design_text'));
156 $lcolor = new WebColor($this->trimmed('design_links'));
158 Config::save('design', 'backgroundcolor', $bgcolor->intValue());
159 Config::save('design', 'contentcolor', $ccolor->intValue());
160 Config::save('design', 'sidebarcolor', $sbcolor->intValue());
161 Config::save('design', 'textcolor', $tcolor->intValue());
162 Config::save('design', 'linkcolor', $lcolor->intValue());
165 $onoff = $this->arg('design_background-image_onoff');
170 if ($onoff == 'on') {
176 $tile = $this->boolean('design_background-image_repeat');
178 // Hack to use Design's bit setter
179 $scratch = new Design();
180 $scratch->setDisposition($on, $off, $tile);
182 Config::save('design', 'disposition', $scratch->disposition);
184 foreach ($settings as $setting) {
185 Config::save('site', $setting, $values[$setting]);
188 if (isset($bgimage)) {
189 Config::save('design', 'backgroundimage', $bgimage);
192 $config->query('COMMIT');
196 * Restore the default design
201 function restoreDefaults()
203 $this->deleteSetting('site', 'logo');
204 $this->deleteSetting('site', 'theme');
207 'theme', 'backgroundimage', 'backgroundcolor', 'contentcolor',
208 'sidebarcolor', 'textcolor', 'linkcolor', 'disposition'
211 foreach ($settings as $setting) {
212 $this->deleteSetting('design', $setting);
215 // XXX: Should we restore the default dir settings, etc.? --Z
217 // XXX: I can't get it to show the new settings without forcing
218 // this terrible reload -- FIX ME!
219 common_redirect(common_local_url('designadminpanel'), 303);
223 * Save the background image if the user uploaded one
225 * @return string $filename the filename of the image
228 function saveBackgroundImage()
232 if ($_FILES['design_background-image_file']['error'] ==
239 ImageFile::fromUpload('design_background-image_file');
240 } catch (Exception $e) {
241 $this->clientError('Unable to save background image.');
245 // Note: site design background image has a special filename
247 $filename = Design::filename('site-design-background',
248 image_type_to_extension($imagefile->type),
251 $filepath = Design::path($filename);
253 move_uploaded_file($imagefile->filepath, $filepath);
255 // delete any old backround img laying around
257 if (isset($this->design->backgroundimage)) {
258 @unlink(Design::path($design->backgroundimage));
266 * Attempt to validate setting values
271 function validate(&$values)
273 if (!empty($values['logo']) &&
274 !Validate::uri($values['logo'], array('allowed_schemes' => array('http', 'https')))) {
275 $this->clientError(_('Invalid logo URL.'));
278 if (!in_array($values['theme'], Theme::listAvailable())) {
279 $this->clientError(sprintf(_("Theme not available: %s."), $values['theme']));
284 * Add the Farbtastic stylesheet
289 function showStylesheets()
291 parent::showStylesheets();
292 $this->cssLink('js/farbtastic/farbtastic.css',null,'screen, projection, tv');
296 * Add the Farbtastic scripts
301 function showScripts()
303 parent::showScripts();
305 $this->script('farbtastic/farbtastic.js');
306 $this->script('userdesign.go.js');
308 $this->autofocus('design_background-image_file');
313 class DesignAdminPanelForm extends AdminForm
319 * @return int ID of the form
324 return 'form_design_admin_panel';
330 * @return string class of the form
335 return 'form_settings';
339 * HTTP method used to submit the form
341 * For image data we need to send multipart/form-data
342 * so we set that here too
344 * @return string the method to use for submitting
349 $this->enctype = 'multipart/form-data';
357 * @return string URL of the action
362 return common_local_url('designadminpanel');
366 * Data elements of the form
375 $this->showBackground();
381 $this->out->elementStart('fieldset', array('id' => 'settings_design_logo'));
382 $this->out->element('legend', null, _('Change logo'));
384 $this->out->elementStart('ul', 'form_data');
387 $this->input('logo', _('Site logo'), 'Logo for the site (full URL)');
390 $this->out->elementEnd('ul');
392 $this->out->elementEnd('fieldset');
398 $this->out->elementStart('fieldset', array('id' => 'settings_design_theme'));
399 $this->out->element('legend', null, _('Change theme'));
401 $this->out->elementStart('ul', 'form_data');
403 $themes = Theme::listAvailable();
405 // XXX: listAvailable() can return an empty list if you
406 // screw up your settings, so just in case:
408 if (empty($themes)) {
409 $themes = array('default', 'default');
413 $themes = array_combine($themes, $themes);
416 $this->out->dropdown('theme', _('Site theme'),
417 $themes, _('Theme for the site.'),
418 false, $this->value('theme'));
421 $this->out->elementEnd('ul');
423 $this->out->elementEnd('fieldset');
426 function showBackground()
428 $design = $this->out->design;
430 $this->out->elementStart('fieldset', array('id' =>
431 'settings_design_background-image'));
432 $this->out->element('legend', null, _('Change background image'));
433 $this->out->elementStart('ul', 'form_data');
436 $this->out->element('label', array('for' => 'design_background-image_file'),
438 $this->out->element('input', array('name' => 'design_background-image_file',
440 'id' => 'design_background-image_file'));
441 $this->out->element('p', 'form_guide',
442 sprintf(_('You can upload a background image for the site. ' .
443 'The maximum file size is %1$s.'), ImageFile::maxFileSize()));
444 $this->out->element('input', array('name' => 'MAX_FILE_SIZE',
446 'id' => 'MAX_FILE_SIZE',
447 'value' => ImageFile::maxFileSizeInt()));
450 if (!empty($design->backgroundimage)) {
452 $this->out->elementStart('li', array('id' =>
453 'design_background-image_onoff'));
455 $this->out->element('img', array('src' =>
456 Design::url($design->backgroundimage)));
458 $attrs = array('name' => 'design_background-image_onoff',
460 'id' => 'design_background-image_on',
464 if ($design->disposition & BACKGROUND_ON) {
465 $attrs['checked'] = 'checked';
468 $this->out->element('input', $attrs);
470 $this->out->element('label', array('for' => 'design_background-image_on',
474 $attrs = array('name' => 'design_background-image_onoff',
476 'id' => 'design_background-image_off',
480 if ($design->disposition & BACKGROUND_OFF) {
481 $attrs['checked'] = 'checked';
484 $this->out->element('input', $attrs);
486 $this->out->element('label', array('for' => 'design_background-image_off',
489 $this->out->element('p', 'form_guide', _('Turn background image on or off.'));
493 $this->out->checkbox('design_background-image_repeat',
494 _('Tile background image'),
495 ($design->disposition & BACKGROUND_TILE) ? true : false);
499 $this->out->elementEnd('ul');
500 $this->out->elementEnd('fieldset');
503 function showColors()
505 $this->out->elementStart('fieldset', array('id' => 'settings_design_color'));
506 $this->out->element('legend', null, _('Change colours'));
508 $this->out->elementStart('ul', 'form_data');
511 // @fixme avoid loop unrolling in non-performance-critical contexts like this
513 $bgcolor = new WebColor($design->backgroundcolor);
516 $this->out->element('label', array('for' => 'swatch-1'), _('Background'));
517 $this->out->element('input', array('name' => 'design_background',
526 $ccolor = new WebColor($design->contentcolor);
529 $this->out->element('label', array('for' => 'swatch-2'), _('Content'));
530 $this->out->element('input', array('name' => 'design_content',
539 $sbcolor = new WebColor($design->sidebarcolor);
542 $this->out->element('label', array('for' => 'swatch-3'), _('Sidebar'));
543 $this->out->element('input', array('name' => 'design_sidebar',
552 $tcolor = new WebColor($design->textcolor);
555 $this->out->element('label', array('for' => 'swatch-4'), _('Text'));
556 $this->out->element('input', array('name' => 'design_text',
565 $lcolor = new WebColor($design->linkcolor);
568 $this->out->element('label', array('for' => 'swatch-5'), _('Links'));
569 $this->out->element('input', array('name' => 'design_links',
578 } catch (WebColorException $e) {
579 // @fixme normalize them individually!
580 common_log(LOG_ERR, 'Bad color values in site design: ' .
584 $this->out->elementEnd('fieldset');
586 $this->out->elementEnd('ul');
595 function formActions()
597 $this->out->submit('defaults', _('Use defaults'), 'submit form_action-default',
598 'defaults', _('Restore default designs'));
600 $this->out->element('input', array('id' => 'settings_design_reset',
603 'class' => 'submit form_action-primary',
604 'title' => _('Reset back to default')));
606 $this->out->submit('save', _('Save'), 'submit form_action-secondary',
607 'save', _('Save design'));