Browse Source

Improve CSS stylesheet change/upload

- Add theme parameter in function return_logo(), ChamiloApi::getPlatformLogo
- Use key/value array in function api_get_themes()
jmontoyaa 8 years ago
parent
commit
029776f32d

+ 54 - 85
main/admin/settings.lib.php

@@ -242,10 +242,8 @@ function handlePlugins()
  */
 function handleStylesheets()
 {
-    // Current style.
-    $currentstyle = api_get_setting('stylesheets');
-
     $is_style_changeable = isStyleChangeable();
+    $allowedFileTypes = ['png'];
 
     $form = new FormValidator(
         'stylesheet_upload',
@@ -334,77 +332,29 @@ function handleStylesheets()
         }
     }
 
-    $form_change = new FormValidator(
-        'stylesheet_upload',
-        'post',
-        api_get_self().'?category=Stylesheets',
-        null,
-        array('id' => 'stylesheets_id')
-    );
-
-    $list_of_names = array();
-    $selected = '';
-    $dirpath = '';
-    $safe_style_dir = '';
-
-    if ($handle = @opendir(CSS_UPLOAD_PATH)) {
-        $counter = 1;
-        while (false !== ($style_dir = readdir($handle))) {
-            if (substr($style_dir, 0, 1) == '.') {
-                // Skip directories starting with a '.'
-                continue;
-            }
-            $dirpath = CSS_UPLOAD_PATH.$style_dir;
-
-            if (is_dir($dirpath)) {
-                if ($style_dir != '.' && $style_dir != '..') {
-                    if (isset($_POST['style']) &&
-                        (isset($_POST['preview']) || isset($_POST['download'])) &&
-                        $_POST['style'] == $style_dir
-                    ) {
-                        $safe_style_dir = $style_dir;
-                    } else {
-                        if ($currentstyle == $style_dir || ($style_dir == 'chamilo' && !$currentstyle)) {
-                            if (isset($_POST['style'])) {
-                                $selected = Database::escape_string($_POST['style']);
-                            } else {
-                                $selected = $style_dir;
-                            }
-                        }
-                    }
-                    $show_name = ucwords(str_replace('_', ' ', $style_dir));
-
-                    if ($is_style_changeable) {
-                        $list_of_names[$style_dir]  = $show_name;
-                    }
-                    $counter++;
-                }
-            }
-        }
-        closedir($handle);
+    // Submit stylesheets.
+    if (isset($_POST['save'])) {
+        storeStylesheets();
+        Display::display_normal_message(get_lang('Saved'));
     }
 
-    // Sort styles in alphabetical order.
-    asort($list_of_names);
-    $select_list = array();
-    foreach ($list_of_names as $style_dir => $item) {
-        $select_list[$style_dir] = $item;
+    // Current style.
+    $selected = $currentStyle = api_get_setting('stylesheets');
+    $styleFromDatabase = api_get_settings_params_simple(['variable = ?' => 'stylesheets']);
+    if ($styleFromDatabase) {
+        $selected = $currentStyle = $styleFromDatabase['selected_value'];
     }
 
-    $styles = &$form_change->addElement('select', 'style', get_lang('NameStylesheet'), $select_list);
-    $styles->setSelected($selected);
-
-    if ($form_change->validate()) {
-        // Submit stylesheets.
-        if (isset($_POST['save'])) {
-            storeStylesheets();
-            Display::display_normal_message(get_lang('Saved'));
-        }
-        if (isset($_POST['download'])) {
-            generateCSSDownloadLink($safe_style_dir);
-        }
+    if (isset($_POST['preview'])) {
+        $selected = $currentStyle = Security::remove_XSS($_POST['style']);
     }
 
+    $dir = api_get_path(SYS_PUBLIC_PATH).'css/themes/' . $selected . '/images/';
+    $url = api_get_path(WEB_CSS_PATH).'themes/' . $selected . '/images/';
+    $logoFileName = 'header-logo.png';
+    $newLogoFileName = 'header-logo-custom' . api_get_current_access_url_id() . '.png';
+    $webPlatformLogoPath = ChamiloApi::getWebPlatformLogoPath($selected);
+
     $logoForm = new FormValidator(
         'logo_upload',
         'post',
@@ -412,24 +362,31 @@ function handleStylesheets()
     );
 
     $logoForm->addHtml(
-        Display::return_message(sprintf(get_lang('TheLogoMustBeSizeXAndFormatY'), '250 x 70', 'PNG'), 'info')
+        Display::return_message(
+            sprintf(
+                get_lang('TheLogoMustBeSizeXAndFormatY'),
+                '250 x 70',
+                'PNG'
+            ),
+            'info'
+        )
     );
 
-    $dir = api_get_path(SYS_PUBLIC_PATH).'css/themes/' . $selected . '/images/';
-    $url = api_get_path(WEB_CSS_PATH).'themes/' . $selected . '/images/';
-    $logoFileName = 'header-logo.png';
-    $newLogoFileName = 'header-logo-custom' . api_get_current_access_url_id() . '.png';
-    $webPlatformLogoPath = ChamiloApi::getWebPlatformLogoPath();
-
     if ($webPlatformLogoPath !== null) {
         $logoForm->addLabel(
             get_lang('CurrentLogo'),
             '<img id="header-logo-custom" src="' . $webPlatformLogoPath . '?' . time() . '">'
         );
     }
-
     $logoForm->addFile('new_logo', get_lang('UpdateLogo'));
-    $allowedFileTypes = ['png'];
+    if ($is_style_changeable) {
+        $logoGroup = [
+            $logoForm->addButtonUpload(get_lang('Upload'), 'logo_upload', true),
+            $logoForm->addButtonCancel(get_lang('Reset'), 'logo_reset', true)
+        ];
+
+        $logoForm->addGroup($logoGroup);
+    }
 
     if (isset($_POST['logo_reset'])) {
         if (is_file($dir.$newLogoFileName)) {
@@ -468,6 +425,25 @@ function handleStylesheets()
         }
     }
 
+    if (isset($_POST['download'])) {
+        generateCSSDownloadLink($selected);
+    }
+
+    $form_change = new FormValidator(
+        'stylesheet_upload',
+        'post',
+        api_get_self().'?category=Stylesheets',
+        null,
+        array('id' => 'stylesheets_id')
+    );
+
+    $styles = $form_change->addElement(
+        'selectTheme',
+        'style',
+        get_lang('NameStylesheet')
+    );
+    $styles->setSelected($currentStyle);
+
     if ($is_style_changeable) {
         $group = [
             $form_change->addButtonSave(get_lang('SaveSettings'), 'save', true),
@@ -477,13 +453,6 @@ function handleStylesheets()
 
         $form_change->addGroup($group);
 
-        $logoGroup = [
-            $logoForm->addButtonUpload(get_lang('Upload'), 'logo_upload', true),
-            $logoForm->addButtonCancel(get_lang('Reset'), 'logo_reset', true)
-        ];
-
-        $logoForm->addGroup($logoGroup);
-
         if ($show_upload_form) {
             echo '<script>
             $(function() {
@@ -498,7 +467,7 @@ function handleStylesheets()
             $form_change->display();
         }
 
-        //Little hack to update the logo image in update form when submiting
+        // Little hack to update the logo image in update form when submiting
         if (isset($_POST['logo_reset'])) {
             echo '<script>'
                     . '$("#header-logo-custom").attr("src","'.$url.$logoFileName.'");'

+ 10 - 10
main/admin/settings.php

@@ -177,7 +177,6 @@ if (!empty($_GET['category']) &&
                 $locked_settings = api_get_locked_settings();
                 foreach ($values as $key => $value) {
                     if (!in_array($key, $locked_settings)) {
-
                         $changeable = 0;
                         if ($mark_all) {
                             $changeable = 1;
@@ -369,9 +368,7 @@ if (!empty($_GET['category']) &&
         exit;
     }
 }
-
-$htmlHeadXtra[] = '<script>
-    
+$htmlHeadXtra[] = '<script>    
     var hide_icon = "'.api_get_path(WEB_IMG_PATH).'/icons/32/shared_setting_na.png";
     var show_icon = "'.api_get_path(WEB_IMG_PATH).'/icons/32/shared_setting.png";
     var url       = "'.api_get_path(WEB_AJAX_PATH).'admin.ajax.php?a=update_changeable_setting";
@@ -402,9 +399,6 @@ $htmlHeadXtra[] = '<script>
     });
 </script>';
 
-// Including the header (banner).
-Display :: display_header($tool_name);
-
 // The action images.
 $action_images['platform'] = 'platform.png';
 $action_images['course'] = 'course.png';
@@ -474,10 +468,8 @@ foreach ($resultcategories as $row) {
     $action_array[] = $url;
 }
 
-echo Display::actions($action_array);
-echo '<br />';
-echo $form_search_html;
 
+ob_start();
 if (!empty($_GET['category'])) {
     switch ($_GET['category']) {
         case 'Regions':
@@ -551,5 +543,13 @@ if (!empty($_GET['category'])) {
             }
     }
 }
+$content = ob_get_clean();
+
+// Including the header (banner).
+Display :: display_header($tool_name);
+echo Display::actions($action_array);
+echo '<br />';
+echo $form_search_html;
+echo $content;
 
 Display :: display_footer();

+ 6 - 7
main/inc/lib/api.lib.php

@@ -4351,11 +4351,10 @@ function api_get_visual_theme()
  * @return array        List of themes directories from the css folder
  * Note: Directory names (names of themes) in the file system should contain ASCII-characters only.
  */
-function api_get_themes() {
+function api_get_themes()
+{
     $cssdir = api_get_path(SYS_CSS_PATH) . 'themes/';
-    $list_dir = array();
-    $list_name = array();
-
+    $list = [];
     if (is_dir($cssdir)) {
         $themes = @scandir($cssdir);
 
@@ -4368,8 +4367,8 @@ function api_get_themes() {
                         continue;
                     } else {
                         if (is_dir($cssdir.$theme)) {
-                            $list_dir[] = $theme;
-                            $list_name[] = ucwords(str_replace('_', ' ', $theme));
+                            $name = ucwords(str_replace('_', ' ', $theme));
+                            $list[$theme] = $name;
                         }
                     }
                 }
@@ -4377,7 +4376,7 @@ function api_get_themes() {
         }
     }
 
-    return array($list_dir, $list_name);
+    return $list;
 }
 
 /**

+ 9 - 6
main/inc/lib/banner.lib.php

@@ -171,15 +171,18 @@ function getCustomTabs()
  * @param string $theme The name of the theme folder from web/css/themes/
  * @return string HTML string with logo as an HTML element
  */
-function return_logo()
+function return_logo($theme = '')
 {
     $siteName = api_get_setting('siteName');
 
-    return ChamiloApi::getPlatformLogo([
-        'title' => $siteName,
-        'class' => 'img-responsive',
-        'id' => 'header-logo'
-    ]);
+    return ChamiloApi::getPlatformLogo(
+        $theme,
+        [
+            'title' => $siteName,
+            'class' => 'img-responsive',
+            'id' => 'header-logo',
+        ]
+    );
 }
 
 /**

+ 1 - 1
main/inc/lib/display.lib.php

@@ -66,7 +66,6 @@ class Display
 
         self::$global_template = new Template($tool_name, $showHeader, $showHeader);
 
-
         // Fixing tools with any help it takes xxx part of main/xxx/index.php
         if (empty($help)) {
             $currentURL = api_get_self();
@@ -85,6 +84,7 @@ class Display
         }
 
         self::$global_template->setHelp($help);
+
         if (!empty(self::$preview_style)) {
             self::$global_template->preview_theme = self::$preview_style;
             self::$global_template->setCssFiles();

+ 2 - 2
main/inc/lib/formvalidator/Element/SelectTheme.php

@@ -21,8 +21,8 @@ class SelectTheme extends HTML_QuickForm_select
         $this->_options = array();
         $this->_values = array();
         $this->addOption('--', ''); // no theme select
-        for ($i = 0; $i < count($themes[0]); $i++) {
-            $this->addOption($themes[1][$i], $themes[0][$i]);
+        foreach ($themes as $themeValue => $themeName) {
+            $this->addOption($themeName, $themeValue);
         }
     }
 }

+ 1 - 1
main/inc/lib/template.lib.php

@@ -649,7 +649,7 @@ class Template
         }
 
         // Logo
-        $logo = return_logo();
+        $logo = return_logo($this->theme);
         $this->assign('logo', $logo);
         $this->assign('show_media_element', 1);
     }

+ 4 - 4
src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php

@@ -67,9 +67,9 @@ class ChamiloApi
      * Get the platform logo path
      * @return null|string
      */
-    public static function getWebPlatformLogoPath()
+    public static function getWebPlatformLogoPath($theme = '')
     {
-        $theme = api_get_visual_theme();
+        $theme = empty($theme) ? api_get_visual_theme() : $theme;
         $accessUrlId = api_get_current_access_url_id();
         $customLogoPath = "themes/$theme/images/header-logo-custom$accessUrlId.png";
 
@@ -92,9 +92,9 @@ class ChamiloApi
      * @param array $imageAttributes Optional.
      * @return string
      */
-    public static function getPlatformLogo($imageAttributes = [])
+    public static function getPlatformLogo($theme = '', $imageAttributes = [])
     {
-        $logoPath = self::getWebPlatformLogoPath();
+        $logoPath = self::getWebPlatformLogoPath($theme);
         $institution = api_get_setting('Institution');
         $institutionUrl = api_get_setting('InstitutionUrl');
         $siteName = api_get_setting('siteName');