<?php

	include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	include_once( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
	include_once( PLOG_CLASS_PATH."plugins/rightschooser/class/view/pluginrightschooserconfigview.class.php" );	
		
	/**
	 * updates the plugin configuration
	 */
	class PluginRightsChooserUpdateConfigAction extends AdminAction
	{
		var $_pluginEnabled;
		var $_RightsIcon;
		var $_RightsText;
		var $_RightsCode;
		var $_RightsUrl;
		
		function PluginRightsChooserUpdateConfigAction( $actionInfo, $request )
		{
			$this->AdminAction( $actionInfo, $request );
		}
		
		function validate()
		{
            $this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
            $this->_pluginEnabled = ($this->_pluginEnabled != "" );
            $this->_RightsIcon = $this->_request->getValue( "RightsIcon" );
	        $this->_RightsText = $this->_request->getValue( "RightsText" );
	        $this->_RightsCode = $this->_request->getValue( "RightsCode" );
	        $this->_RightsUrl = $this->_request->getValue( "RightsUrl" );
			if( $this->_pluginEnabled && $this->_RightsUrl =="" ) {
                $this->_view = new PluginRightsChooserConfigView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("rightschooser_error_enablewithoutchoose"));
                $this->setCommonData();

                return false;
            }
			return true;
		}
		        
		function perform()
		{
            // // update the plugin configurations to blog setting
			$blogSettings = $this->_blogInfo->getSettings();
            $blogSettings->setValue( "plugin_rightschooser_enabled", $this->_pluginEnabled );
            $blogSettings->setValue( "plugin_rightschooser_rightsicon", $this->_RightsIcon );
	        $blogSettings->setValue( "plugin_rightschooser_rightstext", $this->_RightsText );
	        $blogSettings->setValue( "plugin_rightschooser_rightscode", $this->_RightsCode);
	        $blogSettings->setValue( "plugin_rightschooser_rightsurl", $this->_RightsUrl);
            $this->_blogInfo->setSettings( $blogSettings ); 
		
			// save the blogs settings
			$blogs = new Blogs();
            if( !$blogs->updateBlog( $this->_blogInfo->getId(), $this->_blogInfo )) {
                $this->_view = new PluginRightsChooserConfigView( $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 PluginRightsChooserConfigView( $this->_blogInfo );
			$this->_view->setSuccessMessage( $this->_locale->tr("rightschooser_settings_saved_ok"));
			$this->setCommonData();	
			
			// clear the cache
			CacheControl::resetBlogCache( $this->_blogInfo->getId());						
            
            return true;		
		}
	}
?>