Issues (11)

src/Control/ReviewsController.php (1 issue)

1
<?php
2
3
namespace ilateral\SilverStripe\Reviews\Control;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Forms\OptionsetField;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Core\Config\Configurable;
11
use SilverStripe\Comments\Forms\CommentForm;
12
use SilverStripe\Comments\Controllers\CommentingController;
13
14
class ReviewsController extends CommentingController
15
{
16
17
    private static $allowed_actions = [
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
18
        'delete',
19
        'spam',
20
        'ham',
21
        'approve',
22
        'rss',
23
        'CommentsForm',
24
        'reply',
25
        'doPostComment',
26
        'doPreviewComment'
27
    ];
28
29
    /**
30
     * Workaround for generating the link to this controller
31
     *
32
     * @param  string $action
33
     * @param  int    $id
34
     * @param  string $other
35
     * @return string
36
     */
37
    public function Link($action = '', $id = '', $other = '')
38
    {
39
        return Controller::join_links(
40
            Director::baseURL(),
41
            'ssreviews',
42
            $action,
43
            $id,
44
            $other
45
        );
46
    }
47
48
    public function CommentsForm()
49
    {
50
        $form = Injector::inst()->create(CommentForm::class, __FUNCTION__, $this);
51
        $fields = $form->Fields();
52
53
        $enable_url = $this->getOption('enable_url');
54
        $min = $this->getOption('min_rating');
55
        $max = $this->getOption('max_rating');
56
        $id = $this->getRequest()->postVar('ParentID');
57
        $class = $this->getRequest()->postVar('ParentClassName');
58
        
59
        // If we dont have exact values, look to see if we are using a post
60
        if ((empty($min) || empty($max)) && $id && $class) {
61
            if ($object = $class::get()->byID($id)) {
62
                $min = $object->getCommentsOption('min_rating');
63
                $max = $object->getCommentsOption('max_rating');
64
            }
65
        }
66
67
        // Add reviews field
68
        $required_text = _t(
69
            self::class . '.Rating_Required',
70
            'Please enter a Rating'
71
        );
72
73
        // Setup possible ratings
74
        $ratings = [];
75
76
        for ($i = $min; $i <= $max; $i++) {
77
            $ratings[$i] = $i;
78
        }
79
80
        // Add rating field to comment form
81
        $fields->insertBefore(
82
            'Comment',
83
            OptionsetField::create(
84
                'Rating',
85
                _t(
86
                    self::class . '.Rating',
87
                    'Rating'
88
                ),
89
                $ratings
90
            )->setCustomValidationMessage($required_text)
91
            ->setAttribute('data-msg-required', $required_text)
92
        );
93
94
        // Website URL is possibly overkill for a review, disable unless we overwrite this
95
        if ($enable_url !== true) {
96
            $fields->removeByName("URL");
97
        }
98
99
        $fields->setForm($form);
100
        $form->setFields($fields);
101
102
        // hook to allow further extensions to alter the comments form
103
        $this->extend('alterCommentForm', $form);
104
105
        return $form;
106
    }
107
}