<?php

	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
	
    /**
     * Update poll settings action
     */
    class AdminUpdatePollSettingsAction extends AdminAction 
	{
        function AdminUpdatePollSettingsAction( $actionInfo, $request )
        {
	        $this->AdminAction( $actionInfo, $request );

	        $this->registerField('selectedPoll');
        	$this->registerField('styleId');

        	$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" );
            
            // Create view object
            $this->_view = new AdminEditPollsView( $this->_blogInfo );
            
            $selectedPoll = $this->_request->getValue( 'selectedPoll' );
            $styleId = $this->_request->getValue( 'styleId' );

            $polls = new Polls();
            if( $selectedPoll ) {
                $poll = $polls->getPoll( $selectedPoll );
                // Check if selected poll exists
                if( !$poll ) {
                    $this->_view->setErrorMessage( $this->_locale->tr( "error_updating_poll_settings" ) );
                    $this->setCommonData();
                    return false;
                }
                // Check if selected poll belong to this blog
                if( $poll->getBlogId() != $this->_blogInfo->getId() ) {
                    $this->_view->setErrorMessage( $this->_locale->tr("error_updating_poll_settings") );
                    $this->setCommonData();
                    return false;
                }
            }
            
            // Get polls settings
            $pollSettings = $polls->getPollsSettings( $this->_blogInfo->getId() );
            
            // If there is no poll settings for this blog then we will create one else 
            // we well update it.
            if( !$pollSettings ) {
                $result = $polls->addPollsSettings( $this->_blogInfo->getId(), array( 'selected_poll' => $selectedPoll, 'poll_style' => $styleId ) );
                if( !$result ) {
                    $this->_view->setErrorMessage( $this->_locale->tr("error_adding_poll_settings") );
                    $this->setCommonData();
                    return false;
                }
            } else {
                $result = $polls->updatePollsSettings( $this->_blogInfo->getId(), array( 'selected_poll' => $selectedPoll, 'poll_style' => $styleId ) );
                if( !$result ) {
                    $this->_view->setErrorMessage( $this->_locale->tr("error_updating_poll_settings") );
                    $this->setCommonData();
                    return false;
                }
            }
            
            $this->_view->setSuccessMessage( $this->_locale->tr("poll_settings_updated_ok") );
            $this->setCommonData();
            return true;
        }
    }
?>