<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/view/adminnewpollview.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );
	
	/**
     * Adds new poll action
     */
    class AdminAddPollAction extends AdminAction 
	{
        function AdminAddPollAction( $actionInfo, $request )
        {
        	$this->AdminAction( $actionInfo, $request );
        	
        	// Validation
        	$this->registerFieldValidator( 'pollText', new StringValidator() );
        	$this->registerFieldValidator( 'choice', new ArrayValidator() );
            
        	// Create error view object
        	$view = new AdminNewPollView( $this->_blogInfo );
        	$view->setErrorMessage( $this->_locale->tr( 'error_adding_poll' ));
        	$this->setValidationErrorView( $view );
            
        	// Check permissions
        	$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" );
            
            // Create view
            $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 )));
            }
            
            $polls = new Polls();
            
            // Add poll
            $poll = new Poll( $pollText, $this->_blogInfo->getId(), $choices );
            if( !$polls->addPoll( $poll ) ) {
                $this->_view->setErrorMessage( $this->_locale->tr( 'error_adding_poll' ) );
                $this->setCommonData();
                return false;
            }
            
            $this->_view->setSuccessMessage( $this->_locale->tr( 'poll_added_ok' ) );
            $this->setCommonData();
            return true;
        }
    }
?>