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