From 3472f21f130971e1c99cdcfb4001d1c58ec50109 Mon Sep 17 00:00:00 2001 From: Damiano Ciarla Date: Sat, 18 Oct 2014 08:25:13 +0200 Subject: [PATCH] Added max_value configuration Added configuration to set the maximum value of the vote --- Controller/RatingController.php | 19 ++++++++++++------- DCSRatingExtra.php | 8 -------- DependencyInjection/Configuration.php | 4 +--- DependencyInjection/DCSRatingExtension.php | 2 +- README.md | 1 + Resources/views/Rating/choice.html.twig | 2 +- Resources/views/Rating/star.html.twig | 2 +- 7 files changed, 17 insertions(+), 21 deletions(-) delete mode 100644 DCSRatingExtra.php diff --git a/Controller/RatingController.php b/Controller/RatingController.php index dc23144..a0026a1 100644 --- a/Controller/RatingController.php +++ b/Controller/RatingController.php @@ -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'), )); } @@ -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'), )); } @@ -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(); @@ -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; } } diff --git a/DCSRatingExtra.php b/DCSRatingExtra.php deleted file mode 100644 index c882699..0000000 --- a/DCSRatingExtra.php +++ /dev/null @@ -1,8 +0,0 @@ -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()) diff --git a/DependencyInjection/DCSRatingExtension.php b/DependencyInjection/DCSRatingExtension.php index b2339bf..5d5ff15 100644 --- a/DependencyInjection/DCSRatingExtension.php +++ b/DependencyInjection/DCSRatingExtension.php @@ -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']); diff --git a/README.md b/README.md index ebfa257..650c5e9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Resources/views/Rating/choice.html.twig b/Resources/views/Rating/choice.html.twig index abb9170..b2135aa 100644 --- a/Resources/views/Rating/choice.html.twig +++ b/Resources/views/Rating/choice.html.twig @@ -1,6 +1,6 @@ {% spaceless %}
- {% for i in 5..1 %} + {% for i in maxValue..1 %} {% set style = '' %} {% if i <= rating.rate %} {% set style = 'full' %} diff --git a/Resources/views/Rating/star.html.twig b/Resources/views/Rating/star.html.twig index 6231424..53fc8af 100644 --- a/Resources/views/Rating/star.html.twig +++ b/Resources/views/Rating/star.html.twig @@ -1,6 +1,6 @@ {% spaceless %}
- {% for i in 5..1 %} + {% for i in maxValue..1 %} {% set style = '' %} {% if i <= rate %} {% set style = 'full' %}