<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/view/admineditpollview.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
	
	/**
     * Update poll action
     */
    class AdminUpdatePollAction extends AdminAction 
	{
        function AdminUpdatePollAction( $actionInfo, $request )
        {
	        $this->AdminAction( $actionInfo, $request );
	        
	        // validation
        	$this->registerFieldValidator('pollText', new StringValidator());
        	$this->registerFieldValidator('choice', new ArrayValidator());
            $this->registerFieldValidator('pollId', new IntegerValidator());
            
            // Create error view object
        	$view = new AdminEditPollView( $this->_blogInfo );
        	$view->setErrorMessage( $this->_locale->tr( "error_updating_poll" ));
        	$this->setValidationErrorView( $view );
            
        	// check for permission
        	$this->requirePermission( "manage_admin_plugins" );            
        }

        function perform()
        {
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/view/admineditpollsview.class.php" );
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/polls.class.php" );
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/poll.class.php" );
            lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
            
            $this->_view = new AdminEditPollsView( $this->_blogInfo );
            
            // text filtering
            $pollText = trim(Textfilter::xhtmlize(Textfilter::filterAllHTML($this->_request->getValue('pollText'))));
            $choices = $this->_request->getValue('choice');
            foreach( $choices as $key => $value ) {
                $choices[$key] = trim(Textfilter::xhtmlize(Textfilter::filterAllHTML($value)));
            }
            
            // Get poll id from request
            $pollId = $this->_request->getValue('pollId');
            $polls = new Polls();
            
            $poll = $polls->getPoll($pollId);
            
            // Check if valid poll
            if( !$poll ) {
                $this->_view->setErrorMessage( $this->_locale->tr("error_updating_poll") );
                $this->setCommonData();
                return false;
            }
            
            // Check if this poll belong to this blog
            if( $poll->getBlogId() != $this->_blogInfo->getId() ) {
                $this->_view->setErrorMessage( $this->_locale->pr( "error_updating_poll2", $pollId ) );
                $this->setCommonData();
                return false;
            }
            
            $poll->setText( $pollText );
            $poll->setChoices( $choices );
            
            if( !$polls->updatePoll($poll) ) {
                $this->_view->setErrorMessage( $this->_locale->tr( "error_updating_poll" ) );
                $this->setCommonData();
                return false;
            }
            
            $this->_view->setSuccessMessage( $this->_locale->tr( "poll_updated_ok" ) );
            $this->setCommonData();
            return true;
        }
    }
?>