Completed
Push — master ( 96882f...1c82c2 )
by Nicolaas
01:24
created

PageRaterExtension_Controller::PageHasBeenRatedByUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
4
5
class PageRaterExtension_Controller extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
8
9
    /**
10
     * add the default rating to each page ...
11
     * @var boolean
12
     */
13
    private static $items_per_page = 8;
14
15
    /**
16
     * @var string
17
     */
18
    private static $field_title = "Click on any star to rate:";
19
20
    /**
21
     * @var string
22
     */
23
    private static $field_right_title = "On a scale from 1 to 5, with 5 being the best";
24
25
    /**
26
     * @var boolean
27
     */
28
    private static $show_average_rating_in_rating_field = false;
29
30
    /**
31
     * @var boolean
32
     */
33
    private static $only_show_approved = false;
34
35
    private static $allowed_actions = array(
36
        "PageRatingForm",
37
        "rateagain",
38
        "dopagerating",
39
        "removedefaultpageratings",
40
        "removeallpageratings"
41
    );
42
43
    /**
44
     * action to allow use to rate again...
45
     */
46
    public function rateagain($request)
47
    {
48
        $id = intval(Session::get('PageRated'.$this->owner->dataRecord->ID))-0;
49
        $pageRating = PageRating::get()->byID($id);
50
        if ($pageRating) {
51
            $pageRating->delete();
52
        }
53
        Session::set('PageRated'.$this->owner->dataRecord->ID, false);
54
        Session::clear('PageRated'.$this->owner->dataRecord->ID);
55
        return $this->owner->redirect($this->owner->Link());
56
    }
57
58
    /**
59
     * @return Form
60
     */
61
    public function PageRatingForm()
62
    {
63
        Requirements::themedCSS('PageRater', "pagerater");
64
        if ($this->owner->PageHasBeenRatedByUser()) {
65
            $ratingField = LiteralField::create("RatingFor".$this->owner->dataRecord->ID, $this->owner->renderWith("PageRaterAjaxReturn"));
66
            $actions = FieldList::create();
67
            $requiredFields = null;
68
        } else {
69
            if (Config::inst()->get("PageRaterExtension_Controller", "show_average_rating_in_rating_field")) {
70
                $defaultStart = $this->owner->getStarRating();
71
            } else {
72
                $defaultStart = 0;
73
            }
74
            $ratingField = PageRaterStarField::create(
75
                'RatingFor'.$this->owner->dataRecord->ID,
76
                Config::inst()->get("PageRaterExtension_Controller", "field_title"),
77
                $defaultStart,
78
                PageRating::get_number_of_stars()
79
            );
80
            $ratingField->setRightTitle(Config::inst()->get("PageRaterExtension_Controller", "field_right_title"));
81
            $requiredFields = RequiredFields::create($ratingField->getRequiredFields());
82
            $actions = FieldList::create(FormAction::create('dopagerating', 'Submit'));
83
        }
84
        $fields = FieldList::create(
85
            $ratingField,
86
            HiddenField::create('ParentID', "ParentID", $this->owner->dataRecord->ID)
87
        );
88
89
        return Form::create($this->owner, 'PageRatingForm', $fields, $actions, $requiredFields);
90
    }
91
92
    /**
93
     * action Page Rating Form
94
     */
95
    public function dopagerating($data, $form)
96
    {
97
        $id = $this->owner->dataRecord->ID;
98
        $fieldName = "RatingFor".$id;
99
        $data = Convert::raw2sql($data);
100
        $pageRating = PageRating::create();
101
        $form->saveInto($pageRating);
102
        $pageRating->ParentID = $this->owner->dataRecord->ID;
103
        if (isset($data[$fieldName])) {
104
            $pageRating->Rating = floatval($data[$fieldName]);
105
        }
106
        if (isset($data[$fieldName."_Comment"])) {
107
            $pageRating->Comment = Convert::raw2sql($data[$fieldName."_Comment"]);
108
        }
109
        if (isset($data[$fieldName."_Name"])) {
110
            $pageRating->Name = Convert::raw2sql($data[$fieldName."_Name"]);
111
        }
112
        if (isset($data[$fieldName."_Title"])) {
113
            $pageRating->Title = Convert::raw2sql($data[$fieldName."_Title"]);
114
        }
115
        $pageRating->write();
116
        Session::set('PageRated'.$this->owner->dataRecord->ID, $pageRating->ID);
117
        if (Director::is_ajax()) {
118
            return $this->owner->renderWith("PageRaterAjaxReturn");
119
        } else {
120
            $this->owner->redirectBack();
121
        }
122
    }
123
124
125
    public function removedefaultpageratings()
126
    {
127
        if (Permission::check("ADMIN")) {
128
            DB::query("DELETE FROM PageRating WHERE IsDefault = 1;");
129
            debug::show("removed all default ratings for all pages");
130
        } else {
131
            Security::permissionFailure($this->owner, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.'));
132
        }
133
    }
134
135
    public function removeallpageratings()
136
    {
137
        if (Permission::check("ADMIN")) {
138
            DB::query("DELETE FROM PageRating;");
139
            debug::show("removed all ratings for all pages");
140
        } else {
141
            Security::permissionFailure($this->owner, _t('Security.PERMFAILURE', ' This page is secured and you need administrator rights to access it. Enter your credentials below and we will send you right along.'));
142
        }
143
    }
144
145
146
147
148
149
    /**
150
     * list of all rated pages ...
151
     * @return ArrayList
152
     */
153
    public function PageRaterListOfAllForPage($paginated = false)
154
    {
155
        if ($this->owner->onlyShowApprovedPageRatings()) {
156
            $list = $this->owner->turnPageRaterSQLIntoArrayList(
157
                $this->owner->PageRatings()->filter(array("IsApproved" => 1)),
158
                "PageRaterListOfAllForPage"
159
            );
160
        } else {
161
            $list = $this->owner->turnPageRaterSQLIntoArrayList(
162
                $this->owner->PageRatings(),
163
                "PageRaterListOfAllForPage"
164
            );
165
        }
166
        if ($paginated) {
167
            $limit = Config::inst()->get('PageRaterExtension_Controller', 'items_per_page');
168
            if ($limit) {
169
                $list = PaginatedList::create($list, $this->owner->getRequest());
170
                $list->setPageLength($limit);
171
            }
172
        }
173
        return $list;
174
    }
175
176
    {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '{', expecting T_FUNCTION or T_CONST
Loading history...
177
        $sqlQuery = new SQLQuery();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sqlQuery.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
178
        $sqlQuery->setSelect("\"PageRating\".\"Rating\" AS RatingAverage, \"PageRating\".\"ParentID\"");
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sqlQuery.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
179
        if ($this->onlyShowApprovedPageRatings()) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
180
            $sqlQuery->setWhere("\"PageRating\".\"IsApproved\" = 1");
181
        }
182
        $sqlQuery->setFrom(" \"PageRating\"");
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sqlQuery.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
183
        $sqlQuery->addInnerJoin("SiteTree", " \"PageRating\".\"ParentID\" = \"SiteTree\".\"ID\"");
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sqlQuery.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
184
        $sqlQuery->setOrderBy("RatingAverage DESC");
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sqlQuery.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
185
        $sqlQuery->setGroupby("\"SiteTree\".\"ParentID\"");
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sqlQuery.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
186
        return $this->owner->turnPageRaterSQLIntoArrayList($sqlQuery, "PageRaterList");
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
187
    }
188
189
    /**
190
     * @return boolean
191
     */
192
    public function PageHasBeenRatedByUser()
193
    {
194
        return Session::get('PageRated'.$this->owner->ID) ? true : false;
195
    }
196
197
198
199
}
200