From 196f952db81e60f124f707681b3f4452d543fad5 Mon Sep 17 00:00:00 2001
From: Cameron <e107inc@gmail.com>
Date: Wed, 6 Apr 2022 08:37:13 -0700
Subject: [PATCH] Closes #4514 Theme developers can now set the default style,
 just as they would with the default layout.

---
 e107_admin/theme.php                       |  3 ++
 e107_handlers/form_handler.php             |  4 +-
 e107_handlers/theme_handler.php            | 52 +++++++++++++++++-----
 e107_tests/tests/unit/themeHandlerTest.php | 16 ++++++-
 e107_themes/bootstrap3/admin_style.css     |  4 +-
 e107_themes/bootstrap5/theme.xml           |  4 +-
 e107_themes/bootstrap5/theme_config.php    | 34 +-------------
 7 files changed, 65 insertions(+), 52 deletions(-)

diff --git a/e107_admin/theme.php b/e107_admin/theme.php
index 218c05536..dbe0b3621 100644
--- a/e107_admin/theme.php
+++ b/e107_admin/theme.php
@@ -433,12 +433,15 @@ class theme_admin_ui extends e_admin_ui
 				{
 					$mes->addError($message);
 				}
+
+				$this->redirectAction('main');
 			}
 
 			if(!empty($_POST['selectadmin']))
 			{
 				$id = key($_POST['selectadmin']);
 				$this->setAdminTheme($id);
+				$this->redirectAction('admin');
 			}
 
 
diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php
index 0aad12489..9d8a1bd6b 100644
--- a/e107_handlers/form_handler.php
+++ b/e107_handlers/form_handler.php
@@ -7171,11 +7171,11 @@ var_dump($select_options);*/
 		{
 
 			$thumbnail = $this->tp->toImage($val['thumbnail'], $parms);
-			$active = ($key === $value) ? ' active' : '';
+		//	$active = ($key === $value) ? ' active' : '';
 
 			$text .= "<div class='e-image-radio " . $class . "' >
 							<label" . $this->attributes([
-					'class' => "theme-selection$active",
+					'class' => "theme-selection",
 					'title' => varset($val['title']),
 				]) . "><input" . $this->attributes([
 					'type'     => 'radio',
diff --git a/e107_handlers/theme_handler.php b/e107_handlers/theme_handler.php
index 181efe805..eb8bccbe5 100644
--- a/e107_handlers/theme_handler.php
+++ b/e107_handlers/theme_handler.php
@@ -1198,6 +1198,7 @@ class e_theme
 					"name"          => $val['@attributes']['file'],
 					"info"          => $val['@attributes']['name'],
 					"nonadmin"      => $notadmin,
+					'default'       => vartrue($val['@attributes']['default'], false),
 					'scope'         => vartrue($val['@attributes']['scope'], 'front'),
 					'exclude'       => vartrue($val['@attributes']['exclude']),
 					'description'   => vartrue($val['@attributes']['description']),
@@ -3102,15 +3103,16 @@ class themeHandler
 		
 		$themeArray = e107::getTheme()->getList("id");
 		
-		$name = ($name) ? $name : vartrue($themeArray[$this->id]);
-		$layout = $pref['sitetheme_layouts'] = is_array($this->themeArray[$name]['layouts']) ? $this->themeArray[$name]['layouts'] : array();
-		$deflayout = $this->findDefault($name);
+		$name        = ($name) ? $name : vartrue($themeArray[$this->id]);
+		$layout      = $pref['sitetheme_layouts'] = is_array($this->themeArray[$name]['layouts']) ? $this->themeArray[$name]['layouts'] : array();
+		$deflayout   = $this->findDefault($name);
 		$customPages = $this->themeArray[$name]['custompages'];
-		$version = $this->themeArray[$name]['version'];
-		$glyphs = $this->themeArray[$name]['glyphs'];
-		
+		$version     = $this->themeArray[$name]['version'];
+		$glyphs      = $this->themeArray[$name]['glyphs'];
+		$style       = $this->findDefaultCSS($name);
+
 		$core->set('sitetheme', $name);
-		$core->set('themecss', 'style.css');
+		$core->set('themecss', $style);
 		$core->set('sitetheme_layouts', $layout);
 		$core->set('sitetheme_deflayout', $deflayout);
 		$core->set('sitetheme_custompages', $customPages);
@@ -3167,6 +3169,7 @@ class themeHandler
 		e107::getCache()->clearAll('js');
 		e107::getCache()->clearAll('css');
 		e107::getCache()->clearAll('library');
+		e107::getCache()->clearAll('browser');
 		
 		if($core->save())
 		{
@@ -3260,6 +3263,7 @@ class themeHandler
 
 
 	/**
+	 * Find the default layout as marked in theme.xml
 	 * @param $theme
 	 * @return int|string
 	 */
@@ -3269,16 +3273,16 @@ class themeHandler
 		{
 			return e107::getParser()->filter($_POST['layout_default'], 'w');
 		}
-		
+
 	//	$l = $this->themeArray[$theme];
-		
+
 	//	if(!$l)
 		{
 			$l = e107::getTheme($theme)->get(); // $this->getThemeInfo($theme);
 		}
 
-		
-		if($l['layouts'])
+
+		if(!empty($l['layouts']))
 		{
 			foreach ($l['layouts'] as $key=>$val)
 			{
@@ -3293,6 +3297,32 @@ class themeHandler
 			return "";
 		}
 	}
+
+	/**
+	 * Find the default css style as defined in theme.xml.  When not found, use 'style.css'.
+	 * @param string $theme theme-folder name.
+	 * @return string
+	 */
+	function findDefaultCSS($theme)
+	{
+		$l = e107::getTheme($theme)->get();
+
+		if(empty($l['css']))
+		{
+			return 'style.css';
+		}
+
+		foreach($l['css'] as $item)
+		{
+			if(!empty($item['default']) && $item['default'] === 'true')
+			{
+				return $item['name'];
+			}
+
+		}
+
+		return 'style.css';
+	}
 	/*
 	function setAdminTheme()
 	{
diff --git a/e107_tests/tests/unit/themeHandlerTest.php b/e107_tests/tests/unit/themeHandlerTest.php
index 24dcb8eb1..833efed1f 100644
--- a/e107_tests/tests/unit/themeHandlerTest.php
+++ b/e107_tests/tests/unit/themeHandlerTest.php
@@ -21,7 +21,7 @@
 			}
 			catch(Exception $e)
 			{
-				$this->assertTrue(false, "Couldn't load themeHandler object");
+				$this->fail("Couldn't load themeHandler object");
 			}
 
 		}
@@ -67,12 +67,24 @@
 		{
 
 		}
-
+*/
 		public function testFindDefault()
 		{
+			$result = $this->th->findDefault('bootstrap3');
+			$this->assertSame('jumbotron_sidebar_right', $result);
 
 		}
 
+		public function testFindDefaultCSS()
+		{
+			$result = $this->th->findDefaultCSS('voux');
+			$this->assertSame('style.css', $result);
+
+			$result = $this->th->findDefaultCSS('bootstrap5');
+			$this->assertSame('https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/quartz/bootstrap.min.css', $result);
+
+		}
+/*
 		public function testGetThemes()
 		{
 
diff --git a/e107_themes/bootstrap3/admin_style.css b/e107_themes/bootstrap3/admin_style.css
index c5d06624a..ddaa00d76 100644
--- a/e107_themes/bootstrap3/admin_style.css
+++ b/e107_themes/bootstrap3/admin_style.css
@@ -114,8 +114,8 @@ cursor: help;
 
 		.e-image-radio label > input { visibility: hidden;  position: absolute; 	}
 		.e-image-radio label > input + div{  cursor:pointer;  border:3px solid silver; border-radius:4px;/*height: 60px;padding: 5px;*/  vertical-align: middle; }
-		.e-image-radio label > input:checked + div {    border:3px solid #337ab7; 	}
-		.e-image-radio label > input + div span { visibility: hidden; float:right; margin-right:10px; color:#337ab7	}
+		.e-image-radio label > input:checked + div {    border:3px solid rgb(91, 192, 222); 	}
+		.e-image-radio label > input + div span { visibility: hidden; float:right; margin-right:10px; color:rgb(91, 192, 222)	}
 		.e-image-radio label > input:checked + div span { visibility: initial;	}
 
 
diff --git a/e107_themes/bootstrap5/theme.xml b/e107_themes/bootstrap5/theme.xml
index f21b65b90..dee620bb3 100644
--- a/e107_themes/bootstrap5/theme.xml
+++ b/e107_themes/bootstrap5/theme.xml
@@ -26,10 +26,10 @@
         <library name="animate.css" scope="front" />
 	</libraries>
 	<stylesheets>
-		<css file="style.css" name="Default" thumbnail='preview_frontend.png' scope='front' />
+		<css file="style.css" name="Basic" thumbnail='preview_frontend.png' scope='front' />
 		<css file="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/lux/bootstrap.min.css" name="Lux" thumbnail='images/lux.png' scope='front' exclude='bootstrap' />
 		<css file="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/sketchy/bootstrap.min.css" name="Sketchy" thumbnail='images/sketchy.png' scope='front' exclude='bootstrap' />
-        <css file="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/quartz/bootstrap.min.css" name="Quartz" thumbnail='images/quartz.png' scope='front' exclude='bootstrap' />
+        <css file="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/quartz/bootstrap.min.css" default="true" name="Quartz" thumbnail='images/quartz.png' scope='front' exclude='bootstrap' />
 		<css file="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/slate/bootstrap.min.css" name="Slate" thumbnail='images/slate.png' scope='front' exclude='bootstrap' />
         <css file="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/5.1.3/superhero/bootstrap.min.css" name="Superhero" thumbnail='images/superhero.png' scope='front' exclude='bootstrap' />
 	</stylesheets>
diff --git a/e107_themes/bootstrap5/theme_config.php b/e107_themes/bootstrap5/theme_config.php
index 9bbe92d6f..6c471684e 100644
--- a/e107_themes/bootstrap5/theme_config.php
+++ b/e107_themes/bootstrap5/theme_config.php
@@ -7,8 +7,6 @@ $sitetheme = e107::getPref('sitetheme');
 e107::themeLan('admin', $sitetheme, true);
 
 
-
-
 class theme_config implements e_theme_config
 {
 
@@ -20,41 +18,11 @@ class theme_config implements e_theme_config
 
 	function config()
 	{
-		// v2.2.2  
-		$bootswatch = array(
-			"cerulean"  => 'Cerulean',
-			"cosmo"     => 'Cosmo',
-			"cyborg"    => 'Cyborg',
-			"darkly"    => 'Darkly',
-			"flatly"    => 'Flatly',
-			"journal"   => 'Journal',
-			"litera"    => 'Litera',
-			"lumen"     => 'Lumen',
-			"lux"       => 'Lux',
-			"materia"   => 'Materia',
-			"minty"     => 'Minty',
-			'morph'     => 'Morph',
-			"pulse"     => 'Pulse',
-			'quartz'    => 'Quartz',
-			"sandstone" => 'Sandstone',
-			"simplex"   => 'Simplex',
-			"sketchy"   => 'Sketchy',
-			"slate"     => 'Slate',
-			"solar"     => 'Solar',
-			"spacelab"  => 'Spacelab',
-			"superhero" => 'Superhero',
-			"united"    => 'United',
-			"yeti"      => 'Yeti',
-			'zephyr'    => 'Zephyr',
-		);
-		
-		$previewLink = " <a class='btn btn-default btn-secondary e-modal' data-modal-caption=\"Use the 'Themes' menu to view the selection.\" href='http://bootswatch.com/default/'>".LAN_PREVIEW."</a>";
 
 		return array(
-			//'bootswatch'        => array('title'=>LAN_THEMEPREF_01, 'type'=>'dropdown', 'writeParms'=>array('optArray'=> $bootswatch, 'post'=>$previewLink, 'default'=>LAN_DEFAULT)),
 			'cardmenu_look' => array('title' => LAN_THEMEPREF_02, 'type'=>'boolean', 'writeParms'=>array(),'help'=>''),
 			'login_iframe' => array('title' => LAN_THEMEPREF_03, 'type'=>'boolean', 'writeParms'=>array(),'help'=>''),
-			);
+		);
 
 	}