<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/plugoo/class/view/adminplugoopluginsettingsview.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );


    class AdminPlugooPluginUpdateSettingsAction extends AdminAction 
	{
	
		var $_pluginEnabled;
		var $_key;
    	/**
         * Constructor. If nothing else, it also has to call the constructor of the parent
         * class, BlogAction with the same parameters
         */
        function AdminPlugooPluginUpdateSettingsAction( $actionInfo, $request )
        {
        	$this->AdminAction( $actionInfo, $request );
        }
		
		function validate()
		{
			$this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
			
			if( $this->_pluginEnabled == "" )
				$this->_pluginEnabled = false;
			else
				$this->_pluginEnabled = true;
				
			// get the parameters from the request
			$this->_url  = $this->_request->getValue( "pluginUrl" );
			
			return true;
		}
		

        /**
         * Carries out the specified action
         */
        function perform()
        {
			// Check data
			if ($this->_pluginEnabled && $this->_url == "") {
                $this->_view = new AdminPlugooPluginSettingsView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("plugoo_pluginurl_error"));
                $this->setCommonData();

                return false; 				
			}
			
			// save the plugin settings to the db
			$blogSettings = $this->_blogInfo->getSettings();
			$blogSettings->setValue( "plugin_plugoo_enabled", $this->_pluginEnabled );
			$blogSettings->setValue( "plugin_plugoo_url", $this->_url );
			$this->_blogInfo->setSettings( $blogSettings );
			$blogs = new Blogs();
			
			// update the settings in the db, and make sure that everything went fine
            if( !$blogs->updateBlog( $this->_blogInfo )) {
                $this->_view = new AdminPlugooPluginSettingsView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("error_updating_settings"));
                $this->setCommonData();

                return false; 				
			}
			

			// if everything went ok...
            $this->_blogInfo->setSettings( $blogSettings );
            $this->_session->setValue( "blogInfo", $this->_blogInfo );
            $this->saveSession();
			
			$this->_view = new AdminPlugooPluginSettingsView( $this->_blogInfo );
			$this->_view->setSuccessMessage( $this->_locale->tr("plugoo_settings_saved_ok"));			
			$this->setCommonData();
			
			// clear the cache
			CacheControl::resetBlogCache( $this->_blogInfo->getId());			
            
            return true;
        }
    }
?>