1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** Core view helpers */ |
11
|
|
|
namespace Core\View\Helper; |
12
|
|
|
|
13
|
|
|
use Laminas\View\Helper\AbstractHelper; |
14
|
|
|
use Core\Entity\RatingInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Renders a visual representation of a rating value. |
18
|
|
|
* |
19
|
|
|
* <code> |
20
|
|
|
* |
21
|
|
|
* // Renders a compact rating bar representation: |
22
|
|
|
* echo $this->rating(3); |
23
|
|
|
* |
24
|
|
|
* // Renders a wide rating bar representation: |
25
|
|
|
* echo $this->rating(4, 'wider'); |
26
|
|
|
* |
27
|
|
|
* // Pass an rating interface |
28
|
|
|
* $rating = $entity->getRating(); |
29
|
|
|
* echo $this->rating($rating); |
30
|
|
|
* </code> |
31
|
|
|
* |
32
|
|
|
* @see \Core\Entity\Rating |
33
|
|
|
* @author Mathias Gelhausen <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class Rating extends AbstractHelper |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Maps rating values to text. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected static $ratingValueMap = array( |
44
|
|
|
RatingInterface::RATING_NONE => 'Not Rated', |
45
|
|
|
RatingInterface::RATING_POOR => 'Poor', |
46
|
|
|
RatingInterface::RATING_BAD => 'Bad', |
47
|
|
|
RatingInterface::RATING_AVERAGE => 'Average', |
48
|
|
|
RatingInterface::RATING_GOOD => 'Good', |
49
|
|
|
RatingInterface::RATING_EXCELLENT => 'Excellent', |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* generates a rating bar from a rating value |
55
|
|
|
* |
56
|
|
|
* @param int|RatingInterface $rating |
57
|
|
|
* @param string $mode Rendering mode: |
58
|
|
|
* - "compact": renders a densed rating presentation. |
59
|
|
|
* - ANY STRING: renders a wide rating presentation. |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function __invoke($rating, $mode = 'compact') |
63
|
|
|
{ |
64
|
|
|
if ($rating instanceof RatingInterface) { |
65
|
|
|
$rating = $rating->getAverage(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$output = '<div class="br-widget br-readonly ' |
69
|
|
|
. ('compact' == $mode ? ' br-compact' : '') |
70
|
|
|
. '" title="' . $this->getView()->translate(self::$ratingValueMap[$rating]) . '">'; |
|
|
|
|
71
|
|
|
for ($i=1; $i<6; $i++) { |
72
|
|
|
$class = $i <= $rating ? 'br-selected' : ''; |
73
|
|
|
$class .= $i == $rating ? ' br-current' : ''; |
74
|
|
|
|
75
|
|
|
$output .= '<a class="' . $class . '"></a>'; |
76
|
|
|
} |
77
|
|
|
$output .= '</div>'; |
78
|
|
|
|
79
|
|
|
return $output; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|