<?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/stickyposts/class/view/pluginstickypostsconfigview.class.php" );	
		
	/**
	 * updates the plugin configuration
	 */
	class PluginStickyPostsUpdateConfigAction extends AdminAction
	{
		var $_pluginEnabled;
		var $_maxStickys;
		var $_maxAnnounces;
		var $_stickyCategoryId;
		var $_announceCategoryId;
		
		function PluginStickyPostsUpdateConfigAction( $actionInfo, $request )
		{
			$this->AdminAction( $actionInfo, $request );
		}
		
		function validate()
		{
            $errorMessages = array();
			$this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
            $this->_pluginEnabled = ($this->_pluginEnabled != "" );			
            
			$this->_maxStickys = $this->_request->getValue( "maxStickys" );
            if( $this->_maxStickys <= 0  || !ctype_digit($this->_maxStickys) ) {
                $errorMessages[] = "stickyposts_error_maxstickys";
				$isError = true;
			}
			
			$this->_maxAnnounces = $this->_request->getValue( "maxAnnounces" );
            if( $this->_maxAnnounces <= 0  || !ctype_digit($this->_maxAnnounces) ) {
                $errorMessages[] = "stickyposts_error_maxannounces";
				$isError = true;
			}
			
			$this->_stickyCategoryId = $this->_request->getValue( "stickyCategoryId" );
			if( $this->_stickyCategoryId == ""){
                $errorMessages[] = "stickyposts_error_stickyCategoryId";
				$isError = true;
			}
			
			$this->_announceCategoryId = $this->_request->getValue( "announceCategoryId" );
			if( $this->_announceCategoryId == ""){
                $errorMessages[] = "stickyposts_error_announceCategoryId";
				$isError = true;
			}

			if( count($errorMessages) > 0 ) {
                $this->_view = new PluginStickyPostsConfigView( $this->_blogInfo );
				$allerror='';
				foreach( $errorMessages as $error ){
					$allerror .= $this->_locale->tr($error).'<br />';
				}
		$this->_view->setErrorMessage( $this->_locale->tr($allerror));
                $this->setCommonData();
                return false;
            }        	                
			
			return true;
		}
		        
		function perform()
		{
            // // update the plugin configurations to blog setting
			$blogSettings = $this->_blogInfo->getSettings();
            $blogSettings->setValue( "plugin_stickyposts_enabled", $this->_pluginEnabled );
            $blogSettings->setValue( "plugin_stickyposts_maxstickys", $this->_maxStickys );
            $blogSettings->setValue( "plugin_stickyposts_maxannounces", $this->_maxAnnounces );
            $blogSettings->setValue( "plugin_stickyposts_stickycategoryid", $this->_stickyCategoryId );
            $blogSettings->setValue( "plugin_stickyposts_announcecategoryid", $this->_announceCategoryId );
            $this->_blogInfo->setSettings( $blogSettings ); 
		
			// save the blogs settings
			$blogs = new Blogs();
            if( !$blogs->updateBlog( $this->_blogInfo->getId(), $this->_blogInfo )) {
                $this->_view = new PluginStickyPostsConfigView( $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 PluginStickyPostsConfigView( $this->_blogInfo );
			$this->_view->setSuccessMessage( $this->_locale->tr("stickyposts_settings_saved_ok"));
			$this->setCommonData();
			
			// clear the cache
			CacheControl::resetBlogCache( $this->_blogInfo->getId());				
            
            return true;		
		}
	}
?>
