From 2dd160ff9c1fa8ff8216fec0658ea81a6a9084f5 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 18 Dec 2017 11:50:54 +0100 Subject: [PATCH 1/2] Unset selected, should not be handled with select attributes --- inc/html.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inc/html.class.php b/inc/html.class.php index 9f902f536db..eead4fb790e 100644 --- a/inc/html.class.php +++ b/inc/html.class.php @@ -4542,7 +4542,11 @@ static function input($fieldName, $options = []) { * @return string */ static function select($name, array $values, $options = []) { - $selected = (isset($options['selected'])) ? $options['selected'] : false; + $selected = false; + if (isset($options['selected'])) { + $selected = $options['selected']; + unset ($options['selected']); + } $select = sprintf( '"; - foreach ($themes_files as $key => $file) { - if (strpos($file, ".css") !== false) { - $name = substr($file, 0, -4); - $selected = ""; - if ($data["palette"] == $name) { - $selected = "selected='selected'"; - } - echo ""; - } - } + echo Html::select( + 'palette', + $this->getPalettes(), + [ + 'id' => 'theme-selector', + 'selected' => $data['palette'] + ] + ); echo Html::scriptBlock(" function formatThemes(theme) { if (!theme.id) { @@ -2810,4 +2806,27 @@ public static function getCache($optname, $context = 'core') { //Toolbox::logDebug("CACHE $optname", get_class($cache)); return $cache; } + + /** + * Get available palettes + * + * @return array + */ + public function getPalettes() { + $themes_files = scandir(GLPI_ROOT."/css/palettes/"); + $themes = []; + foreach ($themes_files as $file) { + if (strpos($file, ".css") !== false) { + $name = substr($file, 0, -4); + if (strpos($name, '.min') !== false) { + $name = substr($name, 0, -4); + if (isset($themes[$name])) { + continue; + } + } + $themes[$name] = ucfirst($name); + } + } + return $themes; + } } diff --git a/tests/units/Config.php b/tests/units/Config.php index 0008332abc6..4e95d3156e0 100644 --- a/tests/units/Config.php +++ b/tests/units/Config.php @@ -393,4 +393,30 @@ public function testGetRights() { UPDATE => 'Update' ]); } + + public function testGetPalettes() { + $expected = [ + 'aerialgreen' => 'Aerialgreen', + 'auror' => 'Auror', + 'automn' => 'Automn', + 'classic' => 'Classic', + 'clockworkorange' => 'Clockworkorange', + 'dark' => 'Dark', + 'flood' => 'Flood', + 'greenflat' => 'Greenflat', + 'hipster' => 'Hipster', + 'icecream' => 'Icecream', + 'lightblue' => 'Lightblue', + 'premiumred' => 'Premiumred', + 'purplehaze' => 'Purplehaze', + 'teclib' => 'Teclib', + 'vintage' => 'Vintage' + ]; + $this + ->if($this->newTestedInstance) + ->then + ->array($this->testedInstance->getPalettes()) + ->isIdenticalTo($expected); + + } }