1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ilateral\SilverStripe\Reviews\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataExtension; |
6
|
|
|
use ilateral\SilverStripe\Reviews\Helpers\ReviewHelper; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
|
9
|
|
|
class CommentExtension extends DataExtension |
10
|
|
|
{ |
11
|
|
|
private static $db = [ |
|
|
|
|
12
|
|
|
'Rating' => 'Int' |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
private static $casting = [ |
|
|
|
|
16
|
|
|
'MaxRating' => 'Int', |
17
|
|
|
'RatingStars' => 'HTMLText', |
18
|
|
|
'ExcessStars' => 'HTMLText' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
private static $summary_fields = [ |
|
|
|
|
22
|
|
|
'Rating' |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function getMaxRating() |
26
|
|
|
{ |
27
|
|
|
return $this->getOwner()->Parent()->getCommentsOption('max_rating'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the rating as HTML Star characters |
32
|
|
|
* (one star per increment of rating). |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getRatingStars() |
37
|
|
|
{ |
38
|
|
|
return ReviewHelper::getStarsFromValues( |
39
|
|
|
$this->getOwner()->Parent()->getCommentsOption('min_rating'), |
40
|
|
|
round($this->getOwner()->Rating) |
|
|
|
|
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the excess rating as HTML Star characters |
46
|
|
|
* (one star per increment of rating). |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getExcessStars() |
51
|
|
|
{ |
52
|
|
|
$max = $this->getOwner()->Parent()->getCommentsOption("max_rating"); |
53
|
|
|
$rating = $this->getOwner()->Rating; |
54
|
|
|
$excess = $max - round($rating); |
55
|
|
|
|
56
|
|
|
return ReviewHelper::getStarsFromValues( |
57
|
|
|
$this->getOwner()->Parent()->getCommentsOption("min_rating"), |
58
|
|
|
$excess, |
|
|
|
|
59
|
|
|
$html = "☆" |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function updateCMSFields(FieldList $fields) |
64
|
|
|
{ |
65
|
|
|
/** @var \SilverStripe\Comments\Model\Comment */ |
66
|
|
|
$owner = $this->getOwner(); |
67
|
|
|
|
68
|
|
|
$fields->insertBefore( |
69
|
|
|
'Name', |
70
|
|
|
$owner->dbObject('Rating')->scaffoldFormField($owner->fieldLabel('Rating')) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |