i-lateral /
silverstripe-reviews
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace ilateral\SilverStripe\Reviews\Extensions; |
||||
| 4 | |||||
| 5 | use SilverStripe\ORM\DataExtension; |
||||
| 6 | use SilverStripe\View\Requirements; |
||||
| 7 | use SilverStripe\Control\Controller; |
||||
| 8 | use ilateral\SilverStripe\Reviews\Control\ReviewsController; |
||||
| 9 | use ilateral\SilverStripe\Reviews\Helpers\ReviewHelper; |
||||
| 10 | |||||
| 11 | class ReviewsExtension extends DataExtension |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * Extra configuration values (to be combined with those provided by comments) |
||||
| 15 | * |
||||
| 16 | * min_rating: The minimum value used for the ratings field |
||||
| 17 | * max_rating: The maximum value used for the ratings field |
||||
| 18 | * disable_url:Hide the "URL" field |
||||
| 19 | * |
||||
| 20 | * @var array |
||||
| 21 | * |
||||
| 22 | * @config |
||||
| 23 | */ |
||||
| 24 | private static $comments = [ |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 25 | 'show_ratings' => true, |
||||
| 26 | 'min_rating' => 1, |
||||
| 27 | 'max_rating' => 5, |
||||
| 28 | 'enable_url' => false |
||||
| 29 | ]; |
||||
| 30 | |||||
| 31 | private static $casting = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 32 | "AverageRating" => "Decimal", |
||||
| 33 | "AverageRatingStars" => "HTMLText", |
||||
| 34 | "ExcessRatingStars" => "HTMLText", |
||||
| 35 | "AverageRatingPercent" => "HTMLText" |
||||
| 36 | ]; |
||||
| 37 | |||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * Get the MEAN average rating for the objects |
||||
| 41 | * reviews |
||||
| 42 | * |
||||
| 43 | * @return float |
||||
| 44 | */ |
||||
| 45 | public function getAverageRating() |
||||
| 46 | { |
||||
| 47 | $comments = $this |
||||
| 48 | ->getOwner() |
||||
| 49 | ->Comments() |
||||
| 50 | ->filter("Rating:not", null); |
||||
| 51 | |||||
| 52 | $total_rating = 0; |
||||
| 53 | $total_comments = $comments->count(); |
||||
| 54 | |||||
| 55 | foreach ($comments as $comment) { |
||||
| 56 | $total_rating = $total_rating + $comment->Rating; |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | if ($total_rating > 0) { |
||||
| 60 | return $total_rating / $total_comments; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | return 0; |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * Get the average rating as HTML Star characters |
||||
| 68 | * (one star per increment of rating). |
||||
| 69 | * |
||||
| 70 | * @return string |
||||
| 71 | */ |
||||
| 72 | public function getAverageRatingStars() |
||||
| 73 | { |
||||
| 74 | return ReviewHelper::getStarsFromValues( |
||||
| 75 | $min = $this->getOwner()->getCommentsOption("min_rating"), |
||||
| 76 | round($this->getOwner()->AverageRating) |
||||
|
0 ignored issues
–
show
round($this->getOwner()->AverageRating) of type double is incompatible with the type integer expected by parameter $max of ilateral\SilverStripe\Re...r::getStarsFromValues().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 77 | ); |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Get the stars remaining (total minus the average) |
||||
| 82 | * |
||||
| 83 | * @return string |
||||
| 84 | */ |
||||
| 85 | public function getExcessRatingStars() |
||||
| 86 | { |
||||
| 87 | $max = $this->getOwner()->getCommentsOption("max_rating"); |
||||
| 88 | $rating = $this->getOwner()->AverageRating; |
||||
| 89 | $excess = $max - round($rating); |
||||
| 90 | |||||
| 91 | return ReviewHelper::getStarsFromValues( |
||||
| 92 | $this->getOwner()->getCommentsOption("min_rating"), |
||||
| 93 | $excess, |
||||
|
0 ignored issues
–
show
$excess of type double is incompatible with the type integer expected by parameter $max of ilateral\SilverStripe\Re...r::getStarsFromValues().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 94 | $html = "☆" |
||||
| 95 | ); |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Custom comments form that overwrites the default comments extension |
||||
| 100 | * |
||||
| 101 | * @return string |
||||
| 102 | */ |
||||
| 103 | public function CommentsForm() |
||||
| 104 | { |
||||
| 105 | $owner = $this->getOwner(); |
||||
| 106 | // Check if enabled |
||||
| 107 | $enabled = $owner->getCommentsEnabled(); |
||||
| 108 | if ($enabled && $owner->getCommentsOption('include_js')) { |
||||
| 109 | Requirements::javascript('silverstripe/comments:client/dist/js/jquery.min.js'); |
||||
| 110 | Requirements::javascript('silverstripe/comments:client/dist/js/jquery-validation/jquery.validate.min.js'); |
||||
| 111 | Requirements::javascript('silverstripe/admin:client/dist/js/i18n.js'); |
||||
| 112 | Requirements::add_i18n_javascript('silverstripe/comments:client/lang'); |
||||
|
0 ignored issues
–
show
The function
SilverStripe\View\Requir...::add_i18n_javascript() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 113 | Requirements::javascript('silverstripe/comments:client/dist/js/CommentsInterface.js'); |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | $controller = ReviewsController::create(); |
||||
| 117 | $controller->setOwnerRecord($owner); |
||||
| 118 | $controller->setParentClass($owner->getClassName()); |
||||
| 119 | $controller->setOwnerController(Controller::curr()); |
||||
| 120 | |||||
| 121 | $session = Controller::curr()->getRequest()->getSession(); |
||||
| 122 | $moderatedSubmitted = $session->get('CommentsModerated'); |
||||
| 123 | $session->clear('CommentsModerated'); |
||||
| 124 | |||||
| 125 | $form = ($enabled) ? $controller->CommentsForm() : false; |
||||
| 126 | |||||
| 127 | // a little bit all over the show but to ensure a slightly easier upgrade for users |
||||
| 128 | // return back the same variables as previously done in comments |
||||
| 129 | return $this |
||||
| 130 | ->owner |
||||
| 131 | ->customise(array( |
||||
| 132 | 'AddCommentForm' => $form, |
||||
| 133 | 'ModeratedSubmitted' => $moderatedSubmitted, |
||||
| 134 | )) |
||||
| 135 | ->renderWith('ReviewsInterface'); |
||||
| 136 | } |
||||
| 137 | } |