<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/moblog/class/view/adminmoblogpluginsettingsview.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/moblog/class/moblog/moblogconstants.properties.php" );
    lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );

	class AdminMoblogPluginUpdateSettingsAction extends AdminAction
	{
	
        var $_pluginEnabled;
        var $_categoryIds;
        var $_albumId;
        var $_previewType;
        var $_globalArticleCategoryId;
        
		function AdminMoblogPluginUpdateSettingsAction( $actionInfo, $request )
		{
			$this->AdminAction( $actionInfo, $request );
        	$this->registerFieldValidator("categoryIds", new ArrayValidator(new IntegerValidator()));
        	$this->registerFieldValidator("pluginEnabled", new IntegerValidator(), true);
        	$this->registerFieldValidator("albumId", new IntegerValidator());
        	$this->registerFieldValidator("resourcePreviewType", new IntegerValidator());
        	$this->registerFieldValidator("globalArticleCategoryId", new IntegerValidator());

			$view = new AdminMoblogPluginSettingsView( $this->_blogInfo, $this->_userInfo );
        	$view->setErrorMessage( $this->_locale->tr("moblog_error_updating_settings"));
        	$this->setValidationErrorView( $view );
		}
	
		function validate()
		{
            if(!parent::validate())
                return false;
            
            $this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
            $this->_pluginEnabled = ($this->_pluginEnabled != "" );

			$this->_categoryIds = $this->_request->getValue( "categoryIds" );
			$this->_albumId = $this->_request->getValue( "albumId" );
            $this->_globalArticleCategoryId = $this->_request->getValue( "globalArticleCategoryId" );

			// check how images should be embedded
			$this->_previewType = $this->_request->getValue( "resourcePreviewType" );
			if( $this->_previewType < MOBLOG_EMBED_SMALL_PREVIEW ||
			    $this->_previewType > MOBLOG_EMBED_FULL_SIZE_VIEW )
			    $this->_previewType = MOBLOG_EMBED_SMALL_PREVIEW;

			return true;
		}

		function perform()
		{		
			// save the settings
			$blogSettings = $this->_blogInfo->getSettings();
                // note, the following preference is singular (without the 's' for backward compatibility)
			$blogSettings->setValue( "plugin_moblog_article_category_id", $this->_categoryIds );
			$blogSettings->setValue( "plugin_moblog_global_article_category_id", $this->_globalArticleCategoryId );
			$blogSettings->setValue( "plugin_moblog_gallery_resource_album_id", $this->_albumId );
			$blogSettings->setValue( "plugin_moblog_resource_preview_type", $this->_previewType );					
			$blogSettings->setValue( "plugin_moblog_enabled", $this->_pluginEnabled );
			// update the settings in the database *and* in the session, otherwise we will
			// keep using the old settings!!
			$blogs = new Blogs();
			$this->_blogInfo->setSettings( $blogSettings );
			$blogs->updateBlog( $this->_blogInfo );
			$this->_session->setValue( "blogInfo", $this->_blogInfo );
			$this->saveSession();

			// show an informative message
			$this->_view = new AdminMoblogPluginSettingsView( $this->_blogInfo, $this->_userInfo );
			$this->_view->setSuccessMessage( $this->_locale->tr("moblog_plugin_settings_saved_ok"));
			$this->setCommonData();
		
			return true;
		}
	}
?>
