Skip to content

Commit

Permalink
Added max_value configuration
Browse files Browse the repository at this point in the history
Added configuration to set the maximum value of the vote
  • Loading branch information
damianociarla committed Oct 18, 2014
1 parent a98a047 commit 3472f21
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
19 changes: 12 additions & 7 deletions Controller/RatingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function showRateAction($id)
return $this->render('DCSRatingBundle:Rating:star.html.twig', array(
'rating' => $rating,
'rate' => $rating->getRate(),
'maxValue' => $this->container->getParameter('dcs_rating.max_value'),
));
}

Expand All @@ -40,14 +41,15 @@ public function controlAction($id)
} else {
$isUnique = $this->container->getParameter('dcs_rating.unique_vote');
$viewName = (!$isUnique || ($isUnique && null === $voteManager->findOneByRatingAndVoter($rating, $this->getUser())))
? 'choice'
: 'star';
? 'choice'
: 'star';
}

return $this->render('DCSRatingBundle:Rating:'.$viewName.'.html.twig', array(
'rating' => $rating,
'rate' => $rating->getRate(),
'params' => $this->get('request')->get('params', array()),
'maxValue' => $this->container->getParameter('dcs_rating.max_value'),
));
}

Expand All @@ -61,8 +63,10 @@ public function addVoteAction($id, $value)
throw new AccessDeniedHttpException('You can not perform the evaluation');
}

if (!is_numeric($value) || $value < 0 || $value > 5) {
throw new BadRequestHttpException('You must specify a value between 0 and 5');
$maxValue = $this->container->getParameter('dcs_rating.max_value');

if (!is_numeric($value) || $value < 0 || $value > $maxValue) {
throw new BadRequestHttpException(sprintf('You must specify a value between 0 and %d', $maxValue));
}

$user = $this->getUser();
Expand All @@ -80,10 +84,11 @@ public function addVoteAction($id, $value)
$voteManager->saveVote($vote);

if (null === $redirect = $this->get('request')->headers->get('referer', $rating->getPermalink())) {
if ($this->get('router')->getRouteCollection()->get($this->container->getParameter('dcs_rating.base_path_to_redirect'))) {
$redirect = $this->generateUrl($this->container->getParameter('dcs_rating.base_path_to_redirect'));
$pathToRedirect = $this->container->getParameter('dcs_rating.base_path_to_redirect');
if ($this->get('router')->getRouteCollection()->get($pathToRedirect)) {
$redirect = $this->generateUrl($pathToRedirect);
} else {
$redirect = $this->container->getParameter('dcs_rating.base_path_to_redirect');
$redirect = $pathToRedirect;
}
}

Expand Down
8 changes: 0 additions & 8 deletions DCSRatingExtra.php

This file was deleted.

4 changes: 1 addition & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function getConfigTreeBuilder()
->scalarNode('base_security_role')->defaultValue('IS_AUTHENTICATED_FULLY')->end()
->scalarNode('base_path_to_redirect')->defaultValue('/')->end()
->booleanNode('unique_vote')->defaultTrue()->end()
->scalarNode('default_values')
->defaultValue(\DCS\RatingBundle\DCSRatingExtra::$defaultValues)
->end()
->integerNode('max_value')->cannotBeEmpty()->defaultValue(5)->end()
->end()
->append($this->buildModelConfiguration())
->append($this->buildServiceConfiguration())
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/DCSRatingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('dcs_rating.base_security_role', $config['base_security_role']);
$container->setParameter('dcs_rating.base_path_to_redirect', $config['base_path_to_redirect']);
$container->setParameter('dcs_rating.unique_vote', $config['unique_vote']);
$container->setParameter('dcs_rating.default_values', $config['default_values']);
$container->setParameter('dcs_rating.max_value', $config['max_value']);
$container->setParameter('dcs_rating.model.rating.class', $config['model']['rating']);
$container->setParameter('dcs_rating.model.vote.class', $config['model']['vote']);

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ You must extend the abstract entities provided by the bundle and creating the ap
dcs_rating:
db_driver: orm
base_path_to_redirect: / # when the permalink is not configured
max_value: 5 # maximum value for the vote (stars displayed)
model:
rating: MyProject\MyBundle\Entity\Rating
vote: MyProject\MyBundle\Entity\Vote
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Rating/choice.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% spaceless %}
<div class="rating">
{% for i in 5..1 %}
{% for i in maxValue..1 %}
{% set style = '' %}
{% if i <= rating.rate %}
{% set style = 'full' %}
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Rating/star.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% spaceless %}
<div class="rating">
{% for i in 5..1 %}
{% for i in maxValue..1 %}
{% set style = '' %}
{% if i <= rate %}
{% set style = 'full' %}
Expand Down

0 comments on commit 3472f21

Please sign in to comment.