<?php

    lt_include( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
	
    /**
     * Read poll action
     */
    class ReadPollAction extends BlogAction 
    {
        function ReadPollAction( $actionInfo, $request )
        {
            $this->BlogAction( $actionInfo, $request );
        }

        function perform()
        {
            lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/polls.class.php" );
            lt_include( PLOG_CLASS_PATH."class/net/client.class.php" );

            $polls = new Polls();
            $locale = $this->_blogInfo->getLocale();

            $pollSettings = $polls->getPollsSettings( $this->_blogInfo->getId());
            if( $pollSettings['selected_poll'] ){
                $poll = $polls->getPoll( $pollSettings['selected_poll'] );
            }
            if( $poll ){
                $clientIp = Client::getIp();
                $config = Config::getConfig();
                $baseUrl = $config->getValue( 'base_url' );

                if( $polls->validateVoteByIp( $clientIp, $poll->getId() ) ) {
                    include( PLOG_CLASS_PATH."plugins/crystalpoll/class/styles/pollstyles.properties.php" );

                    $pollStyleKey = $pollSettings['poll_style'];
                    if( !$pollStyleKey ) {
                        $pollStyleKey = 'default';
                    }
                    $pollStyle = $poll2dStyle[$pollStyleKey];
                    if( !$pollStyle ) {
                        $pollStyle = $poll3dStyle[$pollStyleKey];
                    }
                    $choices = $poll->getChoices();
                    $votes = $polls->getChoicesVotesCount( $poll->getId() );
                    $totalVotes = array_sum( $votes );

                    $html = "<div id='pollDiv'>";
                    $html .= "<label>".$poll->getText()."</label><br/><br/>";
                    foreach( $choices as $choiceId => $choiceText ) {
                        $html .= "<div>";
                        if( $votes[$choiceId] ) {
                            $percent = ($votes[$choiceId] / $totalVotes) * 100;
                        } else {
                            $percent = 0;
                        }
                        $html .= $choiceText.' ( '.number_format($percent, 1).' % )<br/>';
                        $percent = ( !$percent ? 1 : $percent );
                        $html .= '<img src="'.$baseUrl.'/'.$pollStyle.'" style="width:'.number_format((($percent/100) *80), 0).'%; height:10px; margin: 2px;"/>';
                        $html .= "</div>";
                    }
                    $html .= $locale->tr('totalVotes')." ($totalVotes)</div>";
                } else {
                    $choices = $poll->getChoices();

                    $html = "<form id='poll'>
                             <div id=pollDiv><label>".$poll->getText()."</label><br/>";
                        foreach( $choices as $choiceKey => $choiceValue ) {
                            $html .= "<input type='radio' id='choice_$choiceKey' name='choice' value='$choiceKey' onClick='selectChoice(\"$choiceKey\");'/>
                                      <label for='choice_$choiceKey'>$choiceValue</label><br/>";
                        }
                    $html .= "<input type='hidden' id='pollId' name='pollId' value='".$poll->getId()."'/>
                              <input id='vote' value='".$locale->tr('vote')."' type='button' onClick='addVote();'/>
                              </div></form>";
                }
            }
            print $html;
            exit;
        }
    }
?>
