/** * <p>Rate a propel object. This action is typically executed from an AJAX * request.</p> * * <p>You should override this method in your own exteends actions class if * you need to associate current rating with a user.</p> * * @see sfPropelActAsRatableBehavior API * @link http://trac.symfony-project.com/trac/wiki/sfPropelActAsRatableBehaviorPlugin */ public function executeRate() { try { if ($this->getRequest()->getMethod() !== sfRequest::POST) { return $this->renderText($this->messages['post_only']); } // Retrieve parameters from request $token = $this->getRequestParameter('token'); $rating = $this->getRequestParameter('rating'); $star_width = $this->getRequestParameter('star_width', sfConfig::get('app_rating_star_width', 25)); // Retrieve ratable propel object if (is_null($token) or is_null($rating)) { return $this->renderFatalError($this->messages['missing_params']); } $object = sfPropelActAsRatableBehaviorToolkit::retrieveFromToken($token); if (is_null($object)) { return $this->renderFatalError($this->message['ratable_error']); } // User retrieval $user_id = sfPropelActAsRatableBehaviorToolkit::getUserId(); if (is_null($user_id)) { // Votes are cookie based $cookie_name = sprintf('%s_%s', sfConfig::get('app_rating_cookie_prefix', 'rating'), $token); if (!is_null($this->getRequest()->getCookie($cookie_name))) { $message = $this->messages['already_voted']; } else { $object->setRating((int) $rating); $cookie_ttl = sfConfig::get('app_rating_cookie_ttl', 86400 * 365 * 10); $cookie_expires = date('Y-m-d H:m:i', time() + $cookie_ttl); $this->getResponse()->setCookie($cookie_name, (int) $rating, $cookie_expires); $message = $this->messages['thank_you']; } } else { $already_rated = $object->hasBeenRatedByUser($user_id); $object->setRating((int) $rating, $user_id); $message = $already_rated === true ? $this->messages['thank_you_update'] : $this->messages['thank_you']; } $this->token = $token; $this->rating = $object->getRating(); $this->star_width = $star_width; $this->message = $message; } catch (Exception $e) { return $this->renderFatalError($e->getMessage()); } }
/** * Return the HTML code for a unordered list showing rating stars * * @param BaseObject $object Propel object instance * @param array $options Array of HTML options to apply on the HTML list * @throws sfPropelActAsRatableException * @return string **/ function sf_rater($object, $options = array()) { if (is_null($object)) { sfLogger::getInstance()->debug('A NULL object cannot be rated'); } if (!isset($options['star-width'])) { $star_width = sfConfig::get('app_rating_star_width', 25); } else { $star_width = $options['star-width']; unset($options['star-width']); } try { $max_rating = $object->getMaxRating(); $actual_rating = $object->getRating(); $bar_width = $actual_rating * $star_width; $options = _parse_attributes($options); if (!isset($options['class'])) { $options = array_merge($options, array('class' => 'star-rating')); } if (!isset($options['style']) or !preg_match('/width:/i', $options['style'])) { $full_bar_width = $max_rating * $star_width; $options = array_merge($options, array('style' => 'width:' . $full_bar_width . 'px')); } if ($object instanceof sfOutputEscaperObjectDecorator) { $object_class = get_class($object->getRawValue()); } else { $object_class = get_class($object); } $object_id = $object->getReferenceKey(); $token = sfPropelActAsRatableBehaviorToolkit::addTokenToSession($object_class, $object_id); $msg_domid = sprintf('rating_message_%s', $token); $bar_domid = sprintf('current_rating_%s', $token); $list_content = ' <li class="current-rating" id="' . $bar_domid . '" style="width:' . $bar_width . 'px;">'; $list_content .= sprintf(__('Currently rated %d star(s) on %d'), $object->getRating(), $max_rating); $list_content .= ' </li>'; for ($i = 1; $i <= $max_rating; $i++) { $label = sprintf(__('Rate it %d stars'), $i); $list_content .= ' <li>' . link_to_remote($label, array('url' => sprintf('sfRating/rate?token=%s&rating=%d&star_width=%d', $token, $i, $star_width), 'update' => $msg_domid, 'script' => true, 'complete' => visual_effect('appear', $msg_domid) . visual_effect('highlight', $msg_domid)), array('class' => 'r' . $i . 'stars', 'title' => $label)) . '</li>'; } return content_tag('ul', $list_content, $options) . content_tag('div', null, array('id' => $msg_domid)); } catch (Exception $e) { sfLogger::getInstance()->err('Exception catched from sf_rater helper: ' . $e->getMessage()); } }
/** * Retrieve a ratable object * * @param string $object_model * @param int $object_id */ public static function retrieveRatableObject($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 (!sfPropelActAsRatableBehaviorToolkit::isRatable($object)) { throw new Exception(sprintf('Class %s does not have the ratable behavior', $object_model)); } return $object; } catch (Exception $e) { return sfContext::getInstance()->getLogger()->log($e->getMessage()); } }