From f359ef79a8273d5edaf0cee06712357d75b65039 Mon Sep 17 00:00:00 2001
From: mattpiwik <matthieu.aubry@gmail.com>
Date: Sun, 3 Feb 2008 17:32:11 +0000
Subject: [PATCH] - plugins now run install() when first enabled - fixed bug in
 menu - deactivating also deactivates the LogStats plugins

git-svn-id: http://dev.piwik.org/svn/trunk@270 59fd770c-687e-43c8-a1e3-f5a4ff64c105
---
 config/global.ini.php         | 13 +++---
 libs/jquery/superfish.js      | 11 ++++-
 modules/Config.php            | 21 ++++++++--
 modules/FrontController.php   | 10 ++---
 modules/LogStats.php          |  9 +++--
 modules/PluginsManager.php    | 76 +++++++++++++++++++++++++++++++----
 plugins/Home/Controller.php   |  2 +-
 plugins/Provider/Provider.php |  2 +-
 8 files changed, 114 insertions(+), 30 deletions(-)

diff --git a/config/global.ini.php b/config/global.ini.php
index 4f6a776488..459b76b709 100755
--- a/config/global.ini.php
+++ b/config/global.ini.php
@@ -25,9 +25,8 @@ default			= en
 
 [Plugins]
 Plugins[] 		= PluginsAdmin
-
-;Plugins[] 		= API
-;Plugins[] 		= Widgetize
+Plugins[] 		= API
+Plugins[] 		= Widgetize
 Plugins[] 		= Home
 Plugins[] 		= Actions
 Plugins[] 		= Dashboard
@@ -44,11 +43,13 @@ Plugins[] 		= ExamplePlugin
 Plugins[] 		= Login
 Plugins[] 		= UsersManager
 Plugins[] 		= SitesManager
-
 Plugins[] 		= Installation
 
-[Plugins_LogStats]
-Plugins[] 		= Provider
+[PluginsInstalled]
+PluginsInstalled[] = Login
+PluginsInstalled[] = UsersManager
+PluginsInstalled[] = SitesManager
+PluginsInstalled[] = Installation
 
 [Debug]
 ; if set to true, the archiving process will always be triggered, even if the archive has already been computed
diff --git a/libs/jquery/superfish.js b/libs/jquery/superfish.js
index 1d568831fe..a9557b63fd 100644
--- a/libs/jquery/superfish.js
+++ b/libs/jquery/superfish.js
@@ -36,7 +36,16 @@
 				if($(this).find('ul li').size() == 0)
 				{
 					//console.log('clicked sub menu');
+					
+					// case we clicked the submenu
 					$.superfish.currentActiveMenu = $(this).parents('li');
+					
+					// case we clicked the main menu with NO submenu
+					if($.superfish.currentActiveMenu.size() == 0)
+					{
+						//console.log('clicked main menu with no submenu');
+						$.superfish.currentActiveMenu = $(this);
+					}
 				}
 				else
 				{
@@ -63,12 +72,12 @@
 				if (!$$.is('.'+bcClass) ) {
 					menu.sfTimer=setTimeout(function(){
 						
+						// if there is an active menu (a clicked menu)
 						if($.superfish.currentActiveMenu != 'init')
 						{
 							//console.log('showing '); console.log($.superfish.currentActiveMenu.html());
 							$.superfish.currentActiveMenu.siblings('.sfHover').removeClass('sfHover');
 							$.superfish.currentActiveMenu.showSuperfishUl().siblings().hideSuperfishUl();
-							
 						}
 						else
 						{
diff --git a/modules/Config.php b/modules/Config.php
index 137721de2a..9ca8aeec9a 100755
--- a/modules/Config.php
+++ b/modules/Config.php
@@ -80,18 +80,31 @@ class Piwik_Config
 //				print("<pre>saving $section => ".var_export($arraySection,true)." <br>");
 				
 				$configFile .= "[$section]\n";
+				echo "array section"; var_dump($arraySection);
+
 				foreach($arraySection as $name => $value)
 				{
-					// kind of hack 
-					// if the value is a simple array
 					if(is_numeric($name))
 					{
-						$name = $section."[]";
+						$name = $section;
+						$value = array($value);
+					}
+					
+					if(is_array($value))
+					{
+						foreach($value as $currentValue)
+						{
+							$configFile .= $name."[] = $currentValue\n";
+						}
+					}
+					else
+					{
+						$configFile .= $name." = $value\n";						
 					}
-					$configFile .= "$name = $value\n";
 				}
 				$configFile .= "\n";
 			}
+
 			chdir($this->correctCwd);
 			file_put_contents($this->getDefaultUserConfigPath(), $configFile );
 			
diff --git a/modules/FrontController.php b/modules/FrontController.php
index d1b9eee6bf..8e1ae0af63 100644
--- a/modules/FrontController.php
+++ b/modules/FrontController.php
@@ -266,11 +266,9 @@ class Piwik_FrontController
 				Piwik::prefixTable('log_profiling'),
 		);
 		
-//		Piwik::dropTables($doNotDrop);
-
-		Piwik::createTables();
-		
-		Piwik_PluginsManager::getInstance()->installPlugins();
+		//Piwik::dropTables($doNotDrop);
+		//Piwik::createTables();
+		//Piwik_PluginsManager::getInstance()->installPlugins();
 		
 		// Setup the auth object
 		Piwik_PostEvent('FrontController.authSetCredentials');
@@ -279,7 +277,7 @@ class Piwik_FrontController
 			$authAdapter = Zend_Registry::get('auth');
 		}
 		catch(Exception $e){
-			throw new Exception("Object 'auth' cannot be found in the Registry. Maybe the Login plugin is not available?");
+			throw new Exception("Object 'auth' cannot be found in the Registry. Maybe the Login plugin is not enabled?");
 		}
 		
 		// Perform the authentication query, saving the result
diff --git a/modules/LogStats.php b/modules/LogStats.php
index d4e488b92d..bc167b37f6 100644
--- a/modules/LogStats.php
+++ b/modules/LogStats.php
@@ -84,9 +84,12 @@ class Piwik_LogStats
 
 	private function initProcess()
 	{
-		Piwik_PluginsManager::getInstance()->setPluginsToLoad( 
-				Piwik_LogStats_Config::getInstance()->Plugins_LogStats['Plugins']
-			);
+		$pluginsLogStats = Piwik_LogStats_Config::getInstance()->Plugins_LogStats;
+		if(is_array($pluginsLogStats)
+			&& count($pluginsLogStats) != 0)
+		{
+			Piwik_PluginsManager::getInstance()->setPluginsToLoad( $pluginsLogStats['Plugins_LogStats'] );
+		}
 		
 		$saveStats = Piwik_LogStats_Config::getInstance()->LogStats['record_statistics'];
 		
diff --git a/modules/PluginsManager.php b/modules/PluginsManager.php
index 782ebadd0c..f6f038e945 100644
--- a/modules/PluginsManager.php
+++ b/modules/PluginsManager.php
@@ -88,28 +88,78 @@ class Piwik_PluginsManager
 	public function deactivatePlugin($pluginName)
 	{
 		$plugins = $this->pluginsToLoad;
-		
+	
 		$key = array_search($pluginName,$plugins);
 		if($key !== false)
 		{
 			unset($plugins[$key]);
 			Zend_Registry::get('config')->Plugins = $plugins;
 		}
+		
+		$pluginsLogStats = Zend_Registry::get('config')->Plugins_LogStats->Plugins_LogStats;
+		if(!is_null($pluginsLogStats))
+		{
+			$pluginsLogStats = $pluginsLogStats->toArray();
+			$key = array_search($pluginName,$pluginsLogStats);
+			if($key !== false)
+			{
+				unset($pluginsLogStats[$key]);
+				Zend_Registry::get('config')->Plugins_LogStats = $pluginsLogStats;
+			}
+		}
 	}
 	public function activatePlugin($pluginName)
 	{
+		$plugins = Zend_Registry::get('config')->Plugins->Plugins->toArray();
+		if(in_array($pluginName,$plugins))
+		{
+			throw new Exception("Plugin '$pluginName' already activated.");
+		}
+		
 		$existingPlugins = $this->readPluginsDirectory();
+		if( array_search($pluginName,$existingPlugins) === false)
+		{
+			throw new Exception("Unable to find the plugin '$pluginName'.");
+		}
 		
-		if( array_search($pluginName,$existingPlugins) !== false)
+		$plugin = $this->loadPlugin($pluginName);
+		
+		// is the plugin already installed or is it the first time we activate it?
+		$pluginsInstalled = Zend_Registry::get('config')->PluginsInstalled->PluginsInstalled->toArray();
+		if(!in_array($pluginName,$pluginsInstalled))
 		{
-			$plugins = Zend_Registry::get('config')->Plugins->Plugins->toArray();
-			
-			$plugins[] = $pluginName;
+			$this->installPlugin($plugin);
+			$pluginsInstalled[] = $pluginName;
+			Zend_Registry::get('config')->PluginsInstalled = $pluginsInstalled;	
+		}
+		
+		$information = $plugin->getInformation();
+		
+		// if the plugin is to be loaded during the statistics logging
+		if(isset($information['LogStatsPlugin'])
+			&& $information['LogStatsPlugin'] === true)
+		{
+			$pluginsLogStats = Zend_Registry::get('config')->Plugins_LogStats->Plugins_LogStats;
+			if(is_null($pluginsLogStats))
+			{
+				$pluginsLogStats = array();
+			}
+			else
+			{
+				$pluginsLogStats = $pluginsLogStats->toArray();
+			}
+			$pluginsLogStats[] = $pluginName;
+
+			// the config file will automatically be saved with the new plugin
+			Zend_Registry::get('config')->Plugins_LogStats = $pluginsLogStats;
 			
-			$plugins = array_unique($plugins);
-//			var_dump($plugins);exit;
-			Zend_Registry::get('config')->Plugins = $plugins;
 		}
+		
+		// we add the plugin to the list of activated plugins
+		$plugins[] = $pluginName;
+
+		// the config file will automatically be saved with the new plugin
+		Zend_Registry::get('config')->Plugins = $plugins;
 	}
 	
 	public function setPluginsToLoad( array $pluginsToLoad )
@@ -172,6 +222,7 @@ class Piwik_PluginsManager
 	 */
 	public function loadPlugins()
 	{
+		$this->pluginsToLoad = array_unique($this->pluginsToLoad);
 		foreach($this->pluginsToLoad as $pluginName)
 		{
 			$newPlugin = $this->loadPlugin($pluginName);
@@ -226,6 +277,15 @@ class Piwik_PluginsManager
 		}
 		return $newPlugin;
 	}
+
+	public function installPlugin( Piwik_Plugin $plugin )
+	{
+		try{
+			$plugin->install();
+		} catch(Exception $e) {
+			throw new Exception("There was a problem installing the plugin ". $plugin->getName() . " = " . $e->getMessage() );
+		}	
+	}
 	
 	public function installPlugins()
 	{
diff --git a/plugins/Home/Controller.php b/plugins/Home/Controller.php
index 235f71eab7..2e86d3a116 100644
--- a/plugins/Home/Controller.php
+++ b/plugins/Home/Controller.php
@@ -34,7 +34,7 @@ class Piwik_Home_Controller extends Piwik_Controller
 		// date
 		$view->date = $this->strDate;
 		$oDate = new Piwik_Date($this->strDate);
-		$view->prettyDate = $oDate->get("l jS F Y");
+		$view->prettyDate = $oDate->get("l j\<\s\u\p\>S\<\/\s\u\p\> F Y");
 		
 		// period
 		$currentPeriod = Piwik_Common::getRequestVar('period');
diff --git a/plugins/Provider/Provider.php b/plugins/Provider/Provider.php
index 0a2301c041..809f44b2c8 100644
--- a/plugins/Provider/Provider.php
+++ b/plugins/Provider/Provider.php
@@ -23,6 +23,7 @@ class Piwik_Provider extends Piwik_Plugin
 			'author' => 'Piwik',
 			'homepage' => 'http://piwik.org/',
 			'version' => '0.1',
+			'LogStatsPlugin' => true, // this plugin must be loaded during the stats logging
 		);
 		
 		return $info;
@@ -151,7 +152,6 @@ class Piwik_Provider extends Piwik_Plugin
 	
 }
 
-
 // when the plugin is loaded during LogStats these functions are not defined
 if(function_exists('Piwik_AddWidget'))
 {
-- 
GitLab