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(
133 'site' => array('theme', 'logo'),
134 'theme' => array('server', 'dir', 'path'),
135 'avatar' => array('server', 'dir', 'path'),
136 'background' => array('server', 'dir', 'path')
141 foreach ($settings as $section => $parts) {
142 foreach ($parts as $setting) {
143 $values[$section][$setting] = $this->trimmed("$section-$setting");
147 $this->validate($values);
149 // assert(all values are valid);
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'));
157 $onoff = $this->arg('design_background-image_onoff');
162 if ($onoff == 'on') {
168 $tile = $this->boolean('design_background-image_repeat');
170 $config = new Config();
172 $config->query('BEGIN');
174 foreach ($settings as $section => $parts) {
175 foreach ($parts as $setting) {
176 Config::save($section, $setting, $values[$section][$setting]);
180 if (isset($bgimage)) {
181 Config::save('design', 'backgroundimage', $bgimage);
184 Config::save('design', 'backgroundcolor', $bgcolor->intValue());
185 Config::save('design', 'contentcolor', $ccolor->intValue());
186 Config::save('design', 'sidebarcolor', $sbcolor->intValue());
187 Config::save('design', 'textcolor', $tcolor->intValue());
188 Config::save('design', 'linkcolor', $lcolor->intValue());
190 // Hack to use Design's bit setter
191 $scratch = new Design();
192 $scratch->setDisposition($on, $off, $tile);
194 Config::save('design', 'disposition', $scratch->disposition);
196 $config->query('COMMIT');
202 * Restore the default design
207 function restoreDefaults()
209 $this->deleteSetting('site', 'logo');
210 $this->deleteSetting('site', 'theme');
213 'theme', 'backgroundimage', 'backgroundcolor', 'contentcolor',
214 'sidebarcolor', 'textcolor', 'linkcolor', 'disposition'
217 foreach ($settings as $setting) {
218 $this->deleteSetting('design', $setting);
221 // XXX: Should we restore the default dir settings, etc.? --Z
225 * Save the background image if the user uploaded one
227 * @return string $filename the filename of the image
230 function saveBackgroundImage()
234 if ($_FILES['design_background-image_file']['error'] ==
241 ImageFile::fromUpload('design_background-image_file');
242 } catch (Exception $e) {
243 $this->clientError('Unable to save background image.');
247 // Note: site design background image has a special filename
249 $filename = Design::filename('site-design-background',
250 image_type_to_extension($imagefile->type),
253 $filepath = Design::path($filename);
255 move_uploaded_file($imagefile->filepath, $filepath);
257 // delete any old backround img laying around
259 if (isset($this->design->backgroundimage)) {
260 @unlink(Design::path($design->backgroundimage));
268 * Attempt to validate setting values
273 function validate(&$values)
276 if (!empty($values['site']['logo']) &&
277 !Validate::uri($values['site']['logo'], array('allowed_schemes' => array('http', 'https')))) {
278 $this->clientError(_("Invalid logo URL."));
281 if (!in_array($values['site']['theme'], Theme::listAvailable())) {
282 $this->clientError(sprintf(_("Theme not available: %s"), $values['site']['theme']));
285 // Make sure the directories are there
287 if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
288 $this->clientError(sprintf(_("Theme directory not readable: %s"), $values['theme']['dir']));
291 if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
292 $this->clientError(sprintf(_("Avatar directory not writable: %s"), $values['avatar']['dir']));
295 if (empty($values['background']['dir']) || !is_writable($values['background']['dir'])) {
296 $this->clientError(sprintf(_("Background directory not writable: %s"), $values['background']['dir']));
299 // Do we need to do anything else but validate the
300 // other fields for length? Design settings are
301 // validated elsewhere --Z
303 static $settings = array(
304 'theme' => array('server', 'path'),
305 'avatar' => array('server', 'path'),
306 'background' => array('server', 'path')
309 foreach ($settings as $section => $parts) {
310 foreach ($parts as $setting) {
311 if (mb_strlen($values[$section][$setting]) > 255) {
312 $this->clientError(sprintf(_("Max length for %s %s is 255 characters."),
313 $section, $setting));
321 * Add the Farbtastic stylesheet
326 function showStylesheets()
328 parent::showStylesheets();
329 $this->cssLink('css/farbtastic.css','base','screen, projection, tv');
333 * Add the Farbtastic scripts
338 function showScripts()
340 parent::showScripts();
342 $this->script('js/farbtastic/farbtastic.js');
343 $this->script('js/userdesign.go.js');
345 $this->autofocus('design_background-image_file');
350 class DesignAdminPanelForm extends AdminForm
356 * @return int ID of the form
361 return 'form_design_admin_panel';
367 * @return string class of the form
372 return 'form_settings';
376 * HTTP method used to submit the form
378 * For image data we need to send multipart/form-data
379 * so we set that here too
381 * @return string the method to use for submitting
386 $this->enctype = 'multipart/form-data';
394 * @return string URL of the action
399 return common_local_url('designadminpanel');
403 * Data elements of the form
411 $this->out->elementStart('fieldset', array('id' => 'settings_logo'));
412 $this->out->element('legend', null, _('Change logo'));
414 $this->out->elementStart('ul', 'form_data');
417 $this->input('logo', _('Site logo'), 'Logo for the site (full URL)', 'site');
420 $this->out->elementEnd('ul');
422 $this->out->elementEnd('fieldset');
423 $this->out->elementStart('fieldset', array('id' => 'settings_theme'));
424 $this->out->element('legend', null, _('Change theme'));
426 $this->out->elementStart('ul', 'form_data');
428 $themes = Theme::listAvailable();
430 // XXX: listAvailable() can return an empty list if you
431 // screw up your settings, so just in case:
433 if (empty($themes)) {
434 $themes = array('default', 'default');
438 $themes = array_combine($themes, $themes);
441 $this->out->dropdown('site-theme', _('Site theme'),
442 $themes, _('Theme for the site.'),
443 false, $this->value('theme', 'site'));
447 $this->input('server', _('Theme server'), 'Server for themes', 'theme');
451 $this->input('path', _('Theme path'), 'Web path to themes', 'theme');
455 $this->input('dir', _('Theme directory'), 'Directory where themes are located', 'theme');
458 $this->out->elementEnd('ul');
460 $this->out->elementEnd('fieldset');
461 $this->out->elementStart('fieldset', array('id' => 'settings_avatar'));
462 $this->out->element('legend', null, _('Avatar Settings'));
464 $this->out->elementStart('ul', 'form_data');
467 $this->input('server', _('Avatar server'), 'Server for avatars', 'avatar');
471 $this->input('path', _('Avatar path'), 'Web path to avatars', 'avatar');
475 $this->input('dir', _('Avatar directory'), 'Directory where avatars are located', 'avatar');
478 $this->out->elementEnd('ul');
480 $this->out->elementEnd('fieldset');
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',
528 $attrs = array('name' => 'design_background-image_onoff',
530 'id' => 'design_background-image_off',
534 if ($design->disposition & BACKGROUND_OFF) {
535 $attrs['checked'] = 'checked';
538 $this->out->element('input', $attrs);
540 $this->out->element('label', array('for' => 'design_background-image_off',
543 $this->out->element('p', 'form_guide', _('Turn background image on or off.'));
547 $this->out->checkbox('design_background-image_repeat',
548 _('Tile background image'),
549 ($design->disposition & BACKGROUND_TILE) ? true : false);
554 $this->input('server', _('Background server'), 'Server for backgrounds', 'background');
558 $this->input('path', _('Background path'), 'Web path to backgrounds', 'background');
562 $this->input('dir', _('Background directory'), 'Directory where backgrounds are located', 'background');
565 $this->out->elementEnd('ul');
566 $this->out->elementEnd('fieldset');
568 $this->out->elementStart('fieldset', array('id' => 'settings_design_color'));
569 $this->out->element('legend', null, _('Change colours'));
571 $this->out->elementStart('ul', 'form_data');
575 $bgcolor = new WebColor($design->backgroundcolor);
578 $this->out->element('label', array('for' => 'swatch-1'), _('Background'));
579 $this->out->element('input', array('name' => 'design_background',
588 $ccolor = new WebColor($design->contentcolor);
591 $this->out->element('label', array('for' => 'swatch-2'), _('Content'));
592 $this->out->element('input', array('name' => 'design_content',
601 $sbcolor = new WebColor($design->sidebarcolor);
604 $this->out->element('label', array('for' => 'swatch-3'), _('Sidebar'));
605 $this->out->element('input', array('name' => 'design_sidebar',
614 $tcolor = new WebColor($design->textcolor);
617 $this->out->element('label', array('for' => 'swatch-4'), _('Text'));
618 $this->out->element('input', array('name' => 'design_text',
627 $lcolor = new WebColor($design->linkcolor);
630 $this->out->element('label', array('for' => 'swatch-5'), _('Links'));
631 $this->out->element('input', array('name' => 'design_links',
640 } catch (WebColorException $e) {
641 common_log(LOG_ERR, 'Bad color values in site design: ' .
645 $this->out->elementEnd('fieldset');
647 $this->out->elementEnd('ul');
656 function formActions()
658 $this->out->submit('defaults', _('Use defaults'), 'submit form_action-default',
659 'defaults', _('Restore default designs'));
661 $this->out->element('input', array('id' => 'settings_design_reset',
664 'class' => 'submit form_action-primary',
665 'title' => _('Reset back to default')));
667 $this->out->submit('save', _('Save'), 'submit form_action-secondary',
668 'save', _('Save design'));
673 * Utility to simplify some of the duplicated code around
674 * params and settings. Overriding the input() in the base class
675 * to handle a whole bunch of cases of settings with the same
676 * name under different sections.
678 * @param string $setting Name of the setting
679 * @param string $title Title to use for the input
680 * @param string $instructions Instructions for this field
681 * @param string $section config section, default = 'site'
686 function input($setting, $title, $instructions, $section='site')
688 $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);