/**
 * Return the HTML code for an unordered list showing opinions that can be voted
 * If the user has already voted, then a message appears
 * 
 * @param  BaseObject  $object   Propel object instance to vote
 * @param  string      $domid    unique css identifier for the block (div) containing the voter tool
 * @param  string      $message  a message string to be displayed in the voting-message block
 * @param  array       $options  Array of HTML options to apply on the HTML list
 * @return string
 **/
function depp_voter($object, $domid = 'depp-voter-block', $message = '', $options = array())
{
    if (is_null($object)) {
        sfLogger::getInstance()->debug('A NULL object cannot be voted');
        return '';
    }
    $user_id = deppPropelActAsVotableBehaviorToolkit::getUserId();
    // anonymous votes
    if ((is_null($user_id) || $user_id == '') && !$object->allowsAnonymousVoting()) {
        return __('Anonymous voting is not allowed') . ", " . __('please') . " " . link_to('login', '/login');
    }
    try {
        $voting_range = $object->getVotingRange();
        $options = _parse_attributes($options);
        if (!isset($options['id'])) {
            $options = array_merge($options, array('id' => 'voting-items'));
        }
        if ($object instanceof sfOutputEscaperObjectDecorator) {
            $object_class = get_class($object->getRawValue());
        } else {
            $object_class = get_class($object);
        }
        $object_id = $object->getReferenceKey();
        $token = deppPropelActAsVotableBehaviorToolkit::addTokenToSession($object_class, $object_id);
        // already voted
        if ($object->hasBeenVotedByUser($user_id)) {
            $message .= " " . link_to_remote(__('Take your vote back'), array('url' => sprintf('deppVoting/unvote?domid=%s&token=%s', $domid, $token), 'update' => $domid, 'script' => true, 'complete' => visual_effect('appear', $domid) . visual_effect('highlight', $domid)));
        }
        $list_content = '';
        for ($i = -1 * $voting_range; $i <= $voting_range; $i++) {
            if ($i == 0 && !$object->allowsNeutralPosition()) {
                continue;
            }
            $text = sprintf("[%d]", $i);
            $label = sprintf(__('Vote %d!'), $i);
            if ($object->hasBeenVotedByUser($user_id) && $object->getUserVoting($user_id) == $i) {
                $list_content .= content_tag('li', $text);
            } else {
                $list_content .= '  <li>' . link_to_remote($text, array('url' => sprintf('deppVoting/vote?domid=%s&token=%s&voting=%d', $domid, $token, $i), 'update' => $domid, 'script' => true, 'complete' => visual_effect('appear', $domid) . visual_effect('highlight', $domid)), array('title' => $label)) . '</li>';
            }
        }
        $results = get_component('deppVoting', 'votingDetails', array('object' => $object));
        return content_tag('ul', $list_content, $options) . content_tag('div', $message, array('id' => 'voting-message')) . content_tag('div', $results, array('id' => 'voting-results'));
    } catch (Exception $e) {
        sfLogger::getInstance()->err('Exception catched from sf_rater helper: ' . $e->getMessage());
    }
}
/**
 * Return the HTML code for an unordered list showing opinions that can be voted (no AJAX)
 * If the user has already voted, then a message appears
 * 
 * @param  BaseObject  $object   Propel object instance to vote
 * @param  string      $message  a message string to be displayed in the voting-message block
 * @param  array       $options  Array of HTML options to apply on the HTML list
 * @return string
 **/
function depp_voter_no_ajax($object, $message = '', $options = array())
{
    if (is_null($object)) {
        sfLogger::getInstance()->debug('A NULL object cannot be voted');
        return '';
    }
    $results = get_component('deppVoting', 'votingDetails', array('object' => $object));
    $user_id = deppPropelActAsVotableBehaviorToolkit::getUserId();
    // anonymous votes
    if ((is_null($user_id) || $user_id == '') && !$object->allowsAnonymousVoting()) {
        $msg = "<ul id='voting-items'>" . link_to('<li>' . image_tag('/images/btn-voted-yes.png', array('alt' => 'effettua il login per votare')) . '</li><li>' . image_tag('/images/btn-voted-no.png', array('alt' => 'effettua il login per votare')) . '</li></ul><br />effettua il login per votare', '/login');
        return content_tag('div', $msg, array('id' => 'voting-message')) . content_tag('div', $results, array('id' => 'voting-results'));
    }
    try {
        $options = _parse_attributes($options);
        if (!isset($options['id'])) {
            $options = array_merge($options, array('id' => 'voting-items'));
        }
        if ($object instanceof sfOutputEscaperObjectDecorator) {
            $object_class = get_class($object->getRawValue());
        } else {
            $object_class = get_class($object);
        }
        $object_id = $object->getReferenceKey();
        $token = deppPropelActAsVotableBehaviorToolkit::addTokenToSession($object_class, $object_id);
        // already voted
        if ($object->hasBeenVotedByUser($user_id)) {
            $message .= "&nbsp;" . link_to(__('Take your vote back'), sprintf('deppVoting/unvoteNoAjax?token=%s', $token), array('post' => true));
        }
        $list_content = '';
        for ($i = 1; $i >= -1; $i -= 2) {
            $text = $i == 1 ? 'yes' : 'no';
            $label = sprintf(__('Vote %s!'), $text);
            if ($object->hasBeenVotedByUser($user_id)) {
                $image_name = $object->getUserVoting($user_id) == $i ? "btn-has-voted-{$text}.png" : "btn-non-voted-{$text}.png";
                $list_content .= content_tag('li', image_tag($image_name, $label));
            } else {
                $list_content .= content_tag('li', link_to(image_tag("btn-vote-{$text}.png", $label), sprintf('deppVoting/voteNoAjax?token=%s&voting=%d', $token, $i), array('post' => true)));
            }
        }
        return content_tag('ul', $list_content, $options) . content_tag('div', $message, array('id' => 'voting-message')) . content_tag('div', $results, array('id' => 'voting-results'));
    } catch (Exception $e) {
        sfLogger::getInstance()->err('Exception catched from sf_rater helper: ' . $e->getMessage());
    }
}
 public function executeVotingDetailsSmall()
 {
     $object_class = get_class($this->object);
     $object_id = $this->object->getReferenceKey();
     $this->token = deppPropelActAsVotableBehaviorToolkit::addTokenToSession($object_class, $object_id);
 }
 /**
  * <p>Vote a propel object, un-ajax style</p>
  * 
  * @see  deppPropelActAsVotableBehavior API
  */
 public function executeVoteNoAjax()
 {
     try {
         if ($this->getRequest()->getMethod() !== sfRequest::POST) {
             $this->setError($this->messages['post_only']);
         }
         // Retrieve parameters from request
         $token = $this->getRequestParameter('token');
         $voting = $this->getRequestParameter('voting');
         // Retrieve ratable propel object
         if (is_null($token) or is_null($voting)) {
             $this->setError($this->messages['missing_params']);
         }
         $object = deppPropelActAsVotableBehaviorToolkit::retrieveFromToken($token);
         if (is_null($object)) {
             $this->setError($this->message['votable_error']);
         }
         // User retrieval
         $user_id = deppPropelActAsVotableBehaviorToolkit::getUserId();
         if (is_null($user_id) || $user_id == '') {
             if (!$object->allowsAnonymousVoting()) {
                 $msg = $this->messages['anonymous_not_allowed'];
                 sfLogger::getInstance()->warning($msg);
                 $this->setError($msg);
             } else {
                 // anonymous votes are allowed and are cookie based
                 $cookie_name = sprintf('%s_%s', sfConfig::get('app_voting_cookie_prefix', 'voting'), $token);
                 if (!is_null($this->getRequest()->getCookie($cookie_name))) {
                     $message = $this->messages['already_voted'];
                 } else {
                     if (!$object->allowsNeutralPosition() && $voting == 0) {
                         $msg = $this->messages['neutral_not_allowed'];
                         sfLogger::getInstance()->warning($msg);
                         $this->setError($msg);
                     } else {
                         $object->setVoting((int) $voting);
                         $cookie_ttl = sfConfig::get('app_voting_cookie_ttl', 86400 * 365 * 10);
                         $cookie_expires = date('Y-m-d H:m:i', time() + $cookie_ttl);
                         $this->getResponse()->setCookie($cookie_name, (int) $voting, $cookie_expires);
                         $message = $this->messages['thank_you'];
                     }
                 }
             }
         } else {
             $already_voted = $object->hasBeenVotedByUser($user_id);
             if ($already_voted) {
                 $message = $this->messages['already_voted'];
             } else {
                 if (!$object->allowsNeutralPosition() && $voting == 0) {
                     $msg = $this->messages['neutral_not_allowed'];
                     sfLogger::getInstance()->warning($msg);
                     $this->setError($msg);
                 } else {
                     sfLogger::getInstance()->info("{voting} object_id: " . $object->getId());
                     $object->setVoting((int) $voting, $user_id);
                     $message = $this->messages['thank_you'];
                 }
             }
         }
         $this->setFlash('depp_voting_message', $message);
         $this->redirect($this->getRequest()->getReferer());
     } catch (Exception $e) {
         $this->setError($e->getMessage());
     }
 }
 /**
  * Retrieve a votable object
  * 
  * @param  string  $object_model
  * @param  int     $object_id
  */
 public static function retrieveVotableObject($object_model, $object_id)
 {
     try {
         $peer = sprintf('%sPeer', $object_model);
         if (!class_exists($peer)) {
             throw new Exception(sprintf('Unable to load class %s', $peer));
         }
         $object = call_user_func(array($peer, 'retrieveByPk'), $object_id);
         if (is_null($object)) {
             throw new Exception(sprintf('Unable to retrieve %s with primary key %s', $object_model, $object_id));
         }
         if (!deppPropelActAsVotableBehaviorToolkit::isVotable($object)) {
             throw new Exception(sprintf('Class %s does not have the votable behavior', $object_model));
         }
         return $object;
     } catch (Exception $e) {
         return sfContext::getInstance()->getLogger()->log($e->getMessage());
     }
 }