<?php

	lt_include(PLOG_CLASS_PATH."class/action/admin/adminaction.class.php");
	lt_include(PLOG_CLASS_PATH."plugins/contact/class/view/admincontactpluginsettingsview.class.php");	
	lt_include(PLOG_CLASS_PATH."class/dao/blogs.class.php");
	lt_include(PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php");
	lt_include(PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php");

    class AdminContactPluginUpdateSettingsAction extends AdminAction 
	{
		var $_pluginEnabled;

        function AdminContactPluginUpdateSettingsAction($actionInfo, $request){
        	$this->AdminAction($actionInfo, $request);
			$this->requirePermission("manage_plugins");
        	$this->registerFieldValidator("pluginEnabled", new IntegerValidator(), true);
        	$this->registerFieldValidator("bannedKeywords", new StringValidator(), true);

			$view = new AdminContactPluginSettingsView($this->_blogInfo, $this->_userInfo);
        	$view->setErrorMessage($this->_locale->tr("plugin_contact_error_updating_settings"));
        	$this->setValidationErrorView($view);
        }
		
		function validate(){
            if(!parent::validate())
                return false;
            
            $this->_pluginEnabled = $this->_request->getValue("pluginEnabled");
            $this->_pluginEnabled = ($this->_pluginEnabled != "");
			return true;
		}
		
        function perform(){
        	// update the plugin configurations to blog setting
			$blogSettings = $this->_blogInfo->getSettings();
			$blogSettings->setValue("plugin_contact_enabled", $this->_pluginEnabled);

                // save word list, ignoring commas and leading and trailing spaces

            $bannedKeywords = explode(",", $this->_request->getValue("bannedKeywords"));
                // remove spaces and store in a more regexp-like fashion
            $bannedKeywordsExp = "";
            foreach($bannedKeywords as $key => $word){
                $word = trim($word);
                if($word){
                    if($bannedKeywordsExp)
                        $bannedKeywordsExp .= ",";
                    $bannedKeywordsExp .= $word;
                }
            }
			$blogSettings->setValue("plugin_contact_banned_keywords", $bannedKeywordsExp);


            
			$this->_blogInfo->setSettings($blogSettings);
			
			// update the settings in the db, and make sure that everything went fine
			$blogs = new Blogs();
			if(!$blogs->updateBlog($this->_blogInfo)) {
                $this->_view = new AdminContactPluginSettingsView($this->_blogInfo);
                $this->_view->setErrorMessage($this->_locale->tr("plugin_contact_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 AdminContactPluginSettingsView($this->_blogInfo);
			$this->_view->setSuccessMessage($this->_locale->tr("plugin_contact_settings_saved_ok"));
			$this->setCommonData();

			// clear the cache
			CacheControl::resetBlogCache($this->_blogInfo->getId());			
            
            return true;
        }
    }
?>
