<?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/flickr/class/view/pluginflickrconfigview.class.php" );
		
	/**
	 * updates the plugin configuration
	 */
	class PluginFlickrUpdateConfigAction extends AdminAction
	{
		var $_pluginEnabled;
		var $_email;
		var $_password;
		var $_aliasName;
		var $_albumThumbnailSize;
		var $_photoThumbnailSize;
        var $_showPrivate;
        var $_showNote;
        var $_expiredTime;
		
		function PluginFlickrUpdateConfigAction( $actionInfo, $request )
		{
			$this->AdminAction( $actionInfo, $request );
		}
		
		function validate()
		{
            $this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
            $this->_pluginEnabled = ($this->_pluginEnabled != "" );	
            
            $this->_email = $this->_request->getValue( "email" );
            if( $this->_email == "" ) {
                $this->_view = new PluginFlickrConfigView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("flickr_error_email"));
                $this->setCommonData();

                return false;                
            }  
                           
            $this->_password = $this->_request->getValue( "password" );
            if( $this->_password == "" ) {
                $this->_view = new PluginFlickrConfigView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("flickr_error_password"));
                $this->setCommonData();

                return false;                   
            }

            $this->_aliasName = $this->_request->getValue( "aliasName" );
            if( $this->_aliasName == "" ) {
                $this->_view = new PluginFlickrConfigView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("flickr_error_aliasname"));
                $this->setCommonData();

                return false;                
            }             
                        
            $this->_albumThumbnailSize = $this->_request->getValue( "albumThumbnailSize" );
            $this->_photoThumbnailSize = $this->_request->getValue( "photoThumbnailSize" );
            $this->_showPrivate = $this->_request->getValue( "showPrivate" );
            $this->_showNote = $this->_request->getValue( "showNote" );
            
            $this->_expiredTime = $this->_request->getValue( "expiredTime" );
            if( $this->_expiredTime < 3600 || !ctype_digit($this->_expiredTime) ) {
                $this->_view = new PluginFlickrConfigView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr("flickr_error_expiredtime"));
                $this->setCommonData();

                return false;                 
            }        	                
			return true;
		}
		        
		function perform()
		{
            // update the plugin configurations to blog setting
			$blogSettings = $this->_blogInfo->getSettings();
            $blogSettings->setValue( "plugin_flickr_enabled", $this->_pluginEnabled );
            $blogSettings->setValue( "plugin_flickr_email", $this->_email );
            $blogSettings->setValue( "plugin_flickr_password", $this->_password );
            $blogSettings->setValue( "plugin_flickr_aliasname", $this->_aliasName );
            $blogSettings->setValue( "plugin_flickr_albumthumbnailsize", $this->_albumThumbnailSize );
            $blogSettings->setValue( "plugin_flickr_photothumbnailsize", $this->_photoThumbnailSize );
            $blogSettings->setValue( "plugin_flickr_showprivate", $this->_showPrivate );
            $blogSettings->setValue( "plugin_flickr_shownote", $this->_showNote );
            $blogSettings->setValue( "plugin_flickr_expiredtime", $this->_expiredTime );
            $this->_blogInfo->setSettings( $blogSettings ); 
		
			// save the blogs settings
			$blogs = new Blogs();
            if( !$blogs->updateBlog( $this->_blogInfo->getId(), $this->_blogInfo )) {
                $this->_view = new PluginFlickrConfigView( $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 PluginFlickrConfigView( $this->_blogInfo );
			$this->_view->setSuccessMessage( $this->_locale->tr("flickr_settings_saved_ok"));
			$this->setCommonData();

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