<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/view/admineditpollsview.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );
	
	/**
     * Delete polls action
     */
    class AdminDeletePollsAction extends AdminAction 
	{
	    var $_pollIds;
	    
        function AdminDeletePollsAction( $actionInfo, $request )
        {
        	$this->AdminAction( $actionInfo, $request );
        	
        	if( !$request['pollId'] ) {
        	    $this->registerFieldValidator( 'pollIds', new ArrayValidator() );
        	    $this->_pollIds = $this->_request->getValue( 'pollIds' );
        	} else {
        	    $this->_pollIds = array( $this->_request->getValue( 'pollId' ) );
        	}
            
        	// Create errors view object
        	$view = new AdminEditPollsView( $this->_blogInfo );
        	$view->setErrorMessage( $this->_locale->tr( 'error_deleting_polls' ));
        	$this->setValidationErrorView( $view );
            
        	$this->requirePermission( "manage_admin_plugins" );      
        }

        function perform()
        {
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/polls.class.php" );
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/poll.class.php" );
            
            // Create view object
            $this->_view = new AdminEditPollsView( $this->_blogInfo );
            
            $polls = new Polls();
            
            foreach( $this->_pollIds as $pollId ) {
                $poll = $polls->getPoll( $pollId );
                // Check if poll exist
                if( !$poll ) {
                    $errorMessage .= $this->_locale->pr( "error_deleting_poll2", $pollId );
                } else {
                    // Save poll text before delete for messages (error or success)
                    $pollText = $poll->getText();
                    // Check if this poll belong to this blog
                    if( $poll->getBlogId() != $this->_blogInfo->getId() ) {
                        $errorMessage .= $this->_locale->pr( "error_deleting_poll2", $pollId );
                        
                    } elseif( !$polls->deletePoll($pollId) ) {
                        $this->_view->setErrorMessage( $this->_locale->pr( "error_deleting_poll", $pollText ) );
                        
                    } else {
                        
                        $totalOk++;
                        if( $totalOk < 2 ) {
                            $successMessage .= $this->_locale->pr( "poll_deleted_ok", $pollText )."<br/>";
                        } else {
                            $successMessage = $this->_locale->pr( "polls_deleted_ok", $totalOk );
                        }
                    }
                }
            }
            if( $errorMessage ) {
                $this->_view->setErrorMessage( $errorMessage );
            }
            if( $successMessage ) {
                $this->_view->setSuccessMessage( $successMessage );
            }
            $this->setCommonData();
            return true;
        }
    }
?>