<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/view/admineditpollsview.class.php" );
	
	/**
     * Edit polls action
     */
    class AdminEditPollAction extends AdminAction 
	{
        function AdminEditPollAction( $actionInfo, $request )
        {
        	$this->AdminAction( $actionInfo, $request );
        	
        	// Validation
        	$this->registerFieldValidator( "pollId", new IntegerValidator() );
        	
        	// Create error view object
        	$view = new AdminEditPollsView( $this->_blogInfo );
        	$view->setErrorMessage( $this->_locale->tr( "error_editing_poll" ));
        	$this->setValidationErrorView( $view );
        	
            $this->requirePermission( "manage_admin_plugins" );
        }

        function perform()
        {
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/view/admineditpollview.class.php" );
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/polls.class.php" );
            
            // Get parameters
            $pollId = $this->_request->getValue( 'pollId' );
            
            $polls = new Polls();
            $poll = $polls->getPoll( $pollId );
            
            // Check if poll object exist
            if( !$poll ) {
                $this->_view = new AdminEditPollsView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr( "error_editing_poll" ) );
                $this->setCommonData();
                return false;
            }
            // Check if this poll belong to this blog
            if( $poll->getBlogId() != $this->_blogInfo->getId() ) {
                $this->_view = new AdminEditPollsView( $this->_blogInfo );
                $this->_view->setErrorMessage( $this->_locale->tr( "error_editing_poll" ) );
                $this->setCommonData();
                return false;
            }
            
            $this->_view = new AdminEditPollView( $this->_blogInfo );
            $this->_view->setValue( "poll", $poll );
            $this->setCommonData();
            return true;
        }
    }
?>