]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/designadminpanel.php
Create a bundled & minified JS file for Mapstraction's common case (using OpenLayers...
[quix0rs-gnu-social.git] / actions / designadminpanel.php
index 6b40ae02145d6bd4cb8d42a44e856c8307afa84e..321a8ee5ebcfad1dcb65e52b59acbaa915851944 100644 (file)
@@ -59,6 +59,7 @@ class DesignadminpanelAction extends AdminPanelAction
 
     function title()
     {
+        // TRANS: Message used as title for design settings for the site.
         return _('Design');
     }
 
@@ -70,11 +71,11 @@ class DesignadminpanelAction extends AdminPanelAction
 
     function getInstructions()
     {
-        return _('Design settings for this StatusNet site.');
+        return _('Design settings for this StatusNet site');
     }
 
     /**
-     * Show the site admin panel form
+     * Get the default design and show the design admin panel form
      *
      * @return void
      */
@@ -82,7 +83,6 @@ class DesignadminpanelAction extends AdminPanelAction
     function showForm()
     {
         $this->design = Design::siteDesign();
-
         $form = new DesignAdminPanelForm($this);
         $form->show();
         return;
@@ -101,8 +101,7 @@ class DesignadminpanelAction extends AdminPanelAction
         } else if ($this->arg('defaults')) {
             $this->restoreDefaults();
         } else {
-            $this->success = false;
-            $this->message = 'Unexpected form submission.';
+            $this->clientError(_('Unexpected form submission.'));
         }
     }
 
@@ -114,7 +113,6 @@ class DesignadminpanelAction extends AdminPanelAction
 
     function saveDesignSettings()
     {
-
         // Workaround for PHP returning empty $_POST and $_FILES when POST
         // length > post_max_size in php.ini
 
@@ -122,35 +120,71 @@ class DesignadminpanelAction extends AdminPanelAction
             && empty($_POST)
             && ($_SERVER['CONTENT_LENGTH'] > 0)
         ) {
-            $msg = _('The server was unable to handle that much POST ' .
-                'data (%s bytes) due to its current configuration.');
-            $this->success = false;
-            $this->msg     = $e->getMessage(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
+            // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
+            // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
+            $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
+                      'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
+                      intval($_SERVER['CONTENT_LENGTH']));
+            $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
             return;
         }
 
-        // check for an image upload
+        // check for file uploads
 
         $bgimage = $this->saveBackgroundImage();
+        $customTheme = $this->saveCustomTheme();
+
+        $oldtheme = common_config('site', 'theme');
+        if ($customTheme) {
+            // This feels pretty hacky :D
+            $this->args['theme'] = $customTheme;
+            $themeChanged = true;
+        } else {
+            $themeChanged = ($this->trimmed('theme') != $oldtheme);
+        }
+
+        static $settings = array('theme', 'logo', 'ssllogo');
 
-        static $settings = array('theme');
         $values = array();
 
         foreach ($settings as $setting) {
             $values[$setting] = $this->trimmed($setting);
         }
 
-        // This throws an exception on validation errors
-        try {
+        $this->validate($values);
+
+        $config = new Config();
+
+        $config->query('BEGIN');
+
+        if ($themeChanged) {
+            // If the theme has changed, reset custom colors and let them pick
+            // up the new theme's defaults.
+            $colors = array('background', 'content', 'sidebar', 'text', 'link');
+            foreach ($colors as $colorKey) {
+                // Clear from global config so we see defaults on this page...
+                $GLOBALS['config']['design'][$colorKey . 'color'] = false;
+
+                // And remove old settings from DB...
+                $this->deleteSetting('design', $colorKey . 'color');
+            }
+        } else {
+            // Only save colors from the form if the theme has not changed.
+            //
+            // @fixme a future more ajaxy form should allow theme switch
+            // and color customization in one step.
+
             $bgcolor = new WebColor($this->trimmed('design_background'));
             $ccolor  = new WebColor($this->trimmed('design_content'));
             $sbcolor = new WebColor($this->trimmed('design_sidebar'));
             $tcolor  = new WebColor($this->trimmed('design_text'));
             $lcolor  = new WebColor($this->trimmed('design_links'));
-        } catch (WebColorException $e) {
-            $this->success = false;
-            $this->msg = $e->getMessage();
-            return;
+
+            Config::save('design', 'backgroundcolor', $bgcolor->intValue());
+            Config::save('design', 'contentcolor', $ccolor->intValue());
+            Config::save('design', 'sidebarcolor', $sbcolor->intValue());
+            Config::save('design', 'textcolor', $tcolor->intValue());
+            Config::save('design', 'linkcolor', $lcolor->intValue());
         }
 
         $onoff = $this->arg('design_background-image_onoff');
@@ -166,13 +200,11 @@ class DesignadminpanelAction extends AdminPanelAction
 
         $tile = $this->boolean('design_background-image_repeat');
 
-        $this->validate($values);
-
-        // assert(all values are valid);
-
-        $config = new Config();
+        // Hack to use Design's bit setter
+        $scratch = new Design();
+        $scratch->setDisposition($on, $off, $tile);
 
-        $config->query('BEGIN');
+        Config::save('design', 'disposition', $scratch->disposition);
 
         foreach ($settings as $setting) {
             Config::save('site', $setting, $values[$setting]);
@@ -182,47 +214,14 @@ class DesignadminpanelAction extends AdminPanelAction
             Config::save('design', 'backgroundimage', $bgimage);
         }
 
-        Config::save('design', 'backgroundcolor', $bgcolor->intValue());
-        Config::save('design', 'contentcolor', $ccolor->intValue());
-        Config::save('design', 'sidebarcolor', $sbcolor->intValue());
-        Config::save('design', 'textcolor', $tcolor->intValue());
-        Config::save('design', 'linkcolor', $lcolor->intValue());
-
-        // Hack to use Design's bit setter
-        $scratch = new Design();
-        $scratch->setDisposition($on, $off, $tile);
-
-        Config::save('design', 'disposition', $scratch->disposition);
-
-        $config->query('COMMIT');
-
-        return;
-
-    }
-
-    /**
-     * Delete a design setting
-     *
-     * @return mixed $result false if something didn't work
-     */
-
-    function deleteSetting($section, $setting)
-    {
-        $config = new Config();
-
-        $config->section = $section;
-        $config->setting = $setting;
-
-        if ($config->find(true)) {
-            $result = $config->delete();
-            if (!$result) {
-                common_log_db_error($config, 'DELETE', __FILE__);
-                $this->clientError(_("Unable to delete design setting."));
-                return null;
+        if (common_config('custom_css', 'enabled')) {
+            $css = $this->arg('css');
+            if ($css != common_config('custom_css', 'css')) {
+                Config::save('custom_css', 'css', $css);
             }
         }
 
-        return $result;
+        $config->query('COMMIT');
     }
 
     /**
@@ -233,6 +232,8 @@ class DesignadminpanelAction extends AdminPanelAction
 
     function restoreDefaults()
     {
+        $this->deleteSetting('site', 'logo');
+        $this->deleteSetting('site', 'ssllogo');
         $this->deleteSetting('site', 'theme');
 
         $settings = array(
@@ -243,6 +244,12 @@ class DesignadminpanelAction extends AdminPanelAction
         foreach ($settings as $setting) {
             $this->deleteSetting('design', $setting);
         }
+
+        // XXX: Should we restore the default dir settings, etc.? --Z
+
+        // XXX: I can't get it to show the new settings without forcing
+        // this terrible reload -- FIX ME!
+        common_redirect(common_local_url('designadminpanel'), 303);
     }
 
     /**
@@ -254,8 +261,8 @@ class DesignadminpanelAction extends AdminPanelAction
     function saveBackgroundImage()
     {
         $filename = null;
-
-        if ($_FILES['design_background-image_file']['error'] ==
+        if (isset($_FILES['design_background-image_file']['error']) &&
+            $_FILES['design_background-image_file']['error'] ==
             UPLOAD_ERR_OK) {
 
             $filepath = null;
@@ -264,8 +271,7 @@ class DesignadminpanelAction extends AdminPanelAction
                 $imagefile =
                     ImageFile::fromUpload('design_background-image_file');
             } catch (Exception $e) {
-                $this->success = false;
-                $this->msg     = $e->getMessage();
+                $this->clientError('Unable to save background image.');
                 return;
             }
 
@@ -289,6 +295,33 @@ class DesignadminpanelAction extends AdminPanelAction
         }
     }
 
+    /**
+     * Save the custom theme if the user uploaded one.
+     *
+     * @return mixed custom theme name, if succesful, or null if no theme upload.
+     * @throws ClientException for invalid theme archives
+     * @throws ServerException if trouble saving the theme files
+     */
+
+    function saveCustomTheme()
+    {
+        if (common_config('theme_upload', 'enabled') &&
+            $_FILES['design_upload_theme']['error'] == UPLOAD_ERR_OK) {
+
+            $upload = ThemeUploader::fromUpload('design_upload_theme');
+            $basedir = common_config('local', 'dir');
+            if (empty($basedir)) {
+                $basedir = INSTALLDIR . '/local';
+            }
+            $name = 'custom'; // @todo allow multiples, custom naming?
+            $outdir = $basedir . '/theme/' . $name;
+            $upload->extract($outdir);
+            return $name;
+        } else {
+            return null;
+        }
+    }
+
     /**
      * Attempt to validate setting values
      *
@@ -297,8 +330,18 @@ class DesignadminpanelAction extends AdminPanelAction
 
     function validate(&$values)
     {
+        if (!empty($values['logo']) &&
+            !Validate::uri($values['logo'], array('allowed_schemes' => array('http', 'https')))) {
+            $this->clientError(_('Invalid logo URL.'));
+        }
+
+        if (!empty($values['ssllogo']) &&
+            !Validate::uri($values['ssllogo'], array('allowed_schemes' => array('https')))) {
+            $this->clientError(_('Invalid SSL logo URL.'));
+        }
+
         if (!in_array($values['theme'], Theme::listAvailable())) {
-            $this->clientError(sprintf(_("Theme not available: %s"), $values['theme']));
+            $this->clientError(sprintf(_("Theme not available: %s."), $values['theme']));
         }
     }
 
@@ -311,7 +354,7 @@ class DesignadminpanelAction extends AdminPanelAction
     function showStylesheets()
     {
         parent::showStylesheets();
-        $this->cssLink('css/farbtastic.css','base','screen, projection, tv');
+        $this->cssLink('js/farbtastic/farbtastic.css',null,'screen, projection, tv');
     }
 
     /**
@@ -324,15 +367,15 @@ class DesignadminpanelAction extends AdminPanelAction
     {
         parent::showScripts();
 
-        $this->script('js/farbtastic/farbtastic.js');
-        $this->script('js/userdesign.go.js');
+        $this->script('farbtastic/farbtastic.js');
+        $this->script('userdesign.go.js');
 
         $this->autofocus('design_background-image_file');
     }
 
 }
 
-class DesignAdminPanelForm extends Form
+class DesignAdminPanelForm extends AdminForm
 {
 
     /**
@@ -392,24 +435,84 @@ class DesignAdminPanelForm extends Form
 
     function formData()
     {
+        $this->showLogo();
+        $this->showTheme();
+        $this->showBackground();
+        $this->showColors();
+        $this->showAdvanced();
+    }
 
-        $design = $this->out->design;
+    function showLogo()
+    {
+        $this->out->elementStart('fieldset', array('id' => 'settings_design_logo'));
+        $this->out->element('legend', null, _('Change logo'));
 
-        $themes = Theme::listAvailable();
+        $this->out->elementStart('ul', 'form_data');
 
-        asort($themes);
+        $this->li();
+        $this->input('logo', _('Site logo'), 'Logo for the site (full URL)');
+        $this->unli();
 
-        $themes = array_combine($themes, $themes);
+        $this->li();
+        $this->input('ssllogo', _('SSL logo'), 'Logo to show on SSL pages');
+        $this->unli();
+
+        $this->out->elementEnd('ul');
+
+        $this->out->elementEnd('fieldset');
+
+    }
+
+    function showTheme()
+    {
+        $this->out->elementStart('fieldset', array('id' => 'settings_design_theme'));
+        $this->out->element('legend', null, _('Change theme'));
 
         $this->out->elementStart('ul', 'form_data');
 
-        $this->out->elementStart('li');
-        $this->out->dropdown('theme', _('Theme'),
+        $themes = Theme::listAvailable();
+
+        // XXX: listAvailable() can return an empty list if you
+        // screw up your settings, so just in case:
+
+        if (empty($themes)) {
+            $themes = array('default', 'default');
+        }
+
+        asort($themes);
+        $themes = array_combine($themes, $themes);
+
+        $this->li();
+        $this->out->dropdown('theme', _('Site theme'),
                              $themes, _('Theme for the site.'),
                              false, $this->value('theme'));
-        $this->out->elementEnd('li');
+        $this->unli();
+
+        if (common_config('theme_upload', 'enabled')) {
+            $this->li();
+            $this->out->element('label', array('for' => 'design_upload_theme'), _('Custom theme'));
+            $this->out->element('input', array('id' => 'design_upload_theme',
+                                               'name' => 'design_upload_theme',
+                                               'type' => 'file'));
+            $this->out->element('p', 'form_guide', _('You can upload a custom StatusNet theme as a .ZIP archive.'));
+            $this->unli();
+        }
+
+        $this->out->elementEnd('ul');
 
-        $this->out->elementStart('li');
+        $this->out->elementEnd('fieldset');
+    }
+
+    function showBackground()
+    {
+        $design = $this->out->design;
+
+        $this->out->elementStart('fieldset', array('id' =>
+            'settings_design_background-image'));
+        $this->out->element('legend', null, _('Change background image'));
+        $this->out->elementStart('ul', 'form_data');
+
+        $this->li();
         $this->out->element('label', array('for' => 'design_background-image_file'),
                                 _('Background'));
         $this->out->element('input', array('name' => 'design_background-image_file',
@@ -422,7 +525,7 @@ class DesignAdminPanelForm extends Form
                                           'type' => 'hidden',
                                           'id' => 'MAX_FILE_SIZE',
                                           'value' => ImageFile::maxFileSizeInt()));
-        $this->out->elementEnd('li');
+        $this->unli();
 
         if (!empty($design->backgroundimage)) {
 
@@ -446,6 +549,7 @@ class DesignAdminPanelForm extends Form
 
             $this->out->element('label', array('for' => 'design_background-image_on',
                                           'class' => 'radio'),
+                                          // TRANS: Used as radio button label to add a background image.
                                           _('On'));
 
             $attrs = array('name' => 'design_background-image_onoff',
@@ -462,28 +566,37 @@ class DesignAdminPanelForm extends Form
 
             $this->out->element('label', array('for' => 'design_background-image_off',
                                           'class' => 'radio'),
+                                          // TRANS: Used as radio button label to not add a background image.
                                           _('Off'));
             $this->out->element('p', 'form_guide', _('Turn background image on or off.'));
-            $this->out->elementEnd('li');
+            $this->unli();
 
-            $this->out->elementStart('li');
+            $this->li();
             $this->out->checkbox('design_background-image_repeat',
                             _('Tile background image'),
                             ($design->disposition & BACKGROUND_TILE) ? true : false);
-            $this->out->elementEnd('li');
+            $this->unli();
         }
 
         $this->out->elementEnd('ul');
+        $this->out->elementEnd('fieldset');
+    }
+
+    function showColors()
+    {
+        $design = $this->out->design;
 
         $this->out->elementStart('fieldset', array('id' => 'settings_design_color'));
         $this->out->element('legend', null, _('Change colours'));
+
         $this->out->elementStart('ul', 'form_data');
 
         try {
+            // @fixme avoid loop unrolling in non-performance-critical contexts like this
 
             $bgcolor = new WebColor($design->backgroundcolor);
 
-            $this->out->elementStart('li');
+            $this->li();
             $this->out->element('label', array('for' => 'swatch-1'), _('Background'));
             $this->out->element('input', array('name' => 'design_background',
                                           'type' => 'text',
@@ -492,11 +605,11 @@ class DesignAdminPanelForm extends Form
                                           'maxlength' => '7',
                                           'size' => '7',
                                           'value' => ''));
-            $this->out->elementEnd('li');
+            $this->unli();
 
             $ccolor = new WebColor($design->contentcolor);
 
-            $this->out->elementStart('li');
+            $this->li();
             $this->out->element('label', array('for' => 'swatch-2'), _('Content'));
             $this->out->element('input', array('name' => 'design_content',
                                           'type' => 'text',
@@ -505,11 +618,11 @@ class DesignAdminPanelForm extends Form
                                           'maxlength' => '7',
                                           'size' => '7',
                                           'value' => ''));
-            $this->out->elementEnd('li');
+            $this->unli();
 
             $sbcolor = new WebColor($design->sidebarcolor);
 
-            $this->out->elementStart('li');
+            $this->li();
             $this->out->element('label', array('for' => 'swatch-3'), _('Sidebar'));
             $this->out->element('input', array('name' => 'design_sidebar',
                                         'type' => 'text',
@@ -518,11 +631,11 @@ class DesignAdminPanelForm extends Form
                                         'maxlength' => '7',
                                         'size' => '7',
                                         'value' => ''));
-            $this->out->elementEnd('li');
+            $this->unli();
 
             $tcolor = new WebColor($design->textcolor);
 
-            $this->out->elementStart('li');
+            $this->li();
             $this->out->element('label', array('for' => 'swatch-4'), _('Text'));
             $this->out->element('input', array('name' => 'design_text',
                                         'type' => 'text',
@@ -531,11 +644,11 @@ class DesignAdminPanelForm extends Form
                                         'maxlength' => '7',
                                         'size' => '7',
                                         'value' => ''));
-            $this->out->elementEnd('li');
+            $this->unli();
 
             $lcolor = new WebColor($design->linkcolor);
 
-            $this->out->elementStart('li');
+            $this->li();
             $this->out->element('label', array('for' => 'swatch-5'), _('Links'));
             $this->out->element('input', array('name' => 'design_links',
                                          'type' => 'text',
@@ -544,49 +657,38 @@ class DesignAdminPanelForm extends Form
                                          'maxlength' => '7',
                                          'size' => '7',
                                          'value' => ''));
-            $this->out->elementEnd('li');
+            $this->unli();
 
         } catch (WebColorException $e) {
+            // @fixme normalize them individually!
             common_log(LOG_ERR, 'Bad color values in site design: ' .
                 $e->getMessage());
         }
 
-        $this->out->elementEnd('ul');
         $this->out->elementEnd('fieldset');
 
+        $this->out->elementEnd('ul');
     }
 
-    /**
-     * Utility to simplify some of the duplicated code around
-     * params and settings.
-     *
-     * @param string $setting      Name of the setting
-     * @param string $title        Title to use for the input
-     * @param string $instructions Instructions for this field
-     *
-     * @return void
-     */
-
-    function input($setting, $title, $instructions)
-    {
-        $this->out->input($setting, $title, $this->value($setting), $instructions);
-    }
-
-    /**
-     * Utility to simplify getting the posted-or-stored setting value
-     *
-     * @param string $setting Name of the setting
-     *
-     * @return string param value if posted, or current config value
-     */
-
-    function value($setting)
+    function showAdvanced()
     {
-        $value = $this->out->trimmed($setting);
-        if (empty($value)) {
-            $value = common_config('site', $setting);
+        if (common_config('custom_css', 'enabled')) {
+            $this->out->elementStart('fieldset', array('id' => 'settings_design_advanced'));
+            $this->out->element('legend', null, _('Advanced'));
+            $this->out->elementStart('ul', 'form_data');
+
+            $this->li();
+            $this->out->element('label', array('for' => 'css'), _('Custom CSS'));
+            $this->out->element('textarea', array('name' => 'css',
+                                            'id' => 'css',
+                                            'cols' => '50',
+                                            'rows' => '10'),
+                                strval(common_config('custom_css', 'css')));
+            $this->unli();
+
+            $this->out->elementEnd('fieldset');
+            $this->out->elementEnd('ul');
         }
-        return $value;
     }
 
     /**
@@ -607,5 +709,7 @@ class DesignAdminPanelForm extends Form
                                          'title' => _('Reset back to default')));
 
         $this->out->submit('save', _('Save'), 'submit form_action-secondary',
-                'save', _('Save design'));    }
+                'save', _('Save design'));
+    }
+
 }