CustomerGalleryPage_Controller   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 11
dl 0
loc 186
rs 8.3999
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A PhotoUploadPage() 0 4 1
A HasFilter() 0 4 2
A Location() 0 5 1
A Product() 0 19 4
A Years() 0 5 1
C makeDos() 0 23 11
B Items() 0 17 5
A Form() 0 10 1
B search() 0 27 6
B show() 0 25 5
1
<?php
2
3
class CustomerGalleryPage extends Page
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...
4
{
5
    private static $icon = 'mysite/images/treeicons/CustomerGalleryPage';
0 ignored issues
show
Unused Code introduced by
The property $icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
7
    private static $description = 'Gallery of Customer Feedback and Images';
0 ignored issues
show
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
8
9
    private static $can_be_root = false;
0 ignored issues
show
Unused Code introduced by
The property $can_be_root is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11
    private static $allow_children = 'none';
0 ignored issues
show
Unused Code introduced by
The property $allow_children is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
13
    public function canCreate($member = null)
14
    {
15
        return CustomerGalleryPage::get()->count() ? true : false;
16
    }
17
18
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
19
    {
20
        $fields = parent::getCMSFields();
21
        $fields->removeByName('FolderID');
22
        $fields->removeByName('Extensions');
23
        return $fields;
24
    }
25
}
26
27
class CustomerGalleryPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
28
{
29
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
        "show",
31
        "Form"
32
    );
33
34
    protected $filter = '', $filterType = '', $filterValue = '', $join = '';
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...
35
36
    public function init()
37
    {
38
        parent::init();
39
        PrettyPhoto::include_code();
40
        Requirements::javascript("mysite/javascript/MediaArticles.js");
41
    }
42
43
    public function PhotoUploadPage()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45
        return PhotoUploadPage::get()->First();
46
    }
47
48
49
    public function HasFilter()
50
    {
51
        return $this->filter ? true : false;
52
    }
53
54
    public function Location()
55
    {
56
        $countries = DB::query("SELECT DISTINCT(\"PictureLocation\") FROM \"CustomerImage\" WHERE \"CustomerImage\".\"Status\" = 'Approved' ORDER BY \"PictureLocation\" ")->keyedColumn();
57
        return $this->makeDos("location", $countries);
58
    }
59
60
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
    function MyPictures() {
62
        $mypictures = array();
63
        $member = Member::currentUser();
64
        if($member) {
65
            $dos = CustomerImage::get()->filter(array("Status" => 'Approved', "OwnerID" => $member->ID));
66
            if($dos->count()) {
67
                foreach($dos as $do) {
68
                    $mypictures[$member->ID] = "My pictures (".$dos->count().")";
69
                }
70
                return $this->makeDos("mypictures", $mypictures);
71
            }
72
        }
73
    }
74
    */
75
76
    public function Product()
77
    {
78
        $stage = Versioned::current_stage();
79
        $productTable = "SiteTree";
80
        if ($stage) {
81
            $productTable .= "_".$stage;
82
        }
83
        $row = DB::query("SELECT DISTINCT(\"ProductPageID\") AS ProductPageID FROM \"CustomerImage\" INNER JOIN \"$productTable\" ON \"$productTable\".\"ID\" = \"CustomerImage\".\"ProductPageID\" WHERE \"CustomerImage\".\"Status\" = 'Approved' ORDER BY \"$productTable\".\"Title\"");
84
        $newArray = array();
85
        foreach ($row as $dataArray) {
86
            $page = SiteTree::get()->byID($dataArray["ProductPageID"]);
0 ignored issues
show
Bug introduced by
The call to get() misses a required argument $callerClass.

This check looks for function calls that miss required arguments.

Loading history...
87
            if ($page) {
88
                $key = $page->ID;
89
                $value = $page->Title;
90
            }
91
            $newArray[$key] = $value;
0 ignored issues
show
Bug introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
92
        }
93
        return $this->makeDos("product", $newArray);
94
    }
95
96
    public function Years()
97
    {
98
        $years = DB::query("SELECT DISTINCT(YEAR(\"Created\")) FROM \"CustomerImage\" INNER JOIN \"File\" ON \"File\".\"ID\" = \"CustomerImage\".\"ID\" WHERE \"CustomerImage\".\"Status\" = 'Approved' ORDER BY \"Created\" DESC")->keyedColumn();
99
        return $this->makeDos("year", $years);
100
    }
101
102
    protected function makeDos($title, $data)
103
    {
104
        $dos = new ArrayList();
105
        //sort($data);
106
        if ($data && count($data)) {
107
            foreach ($data as $key => $value) {
108
                if ($key && $value) {
109
                    $key = urlencode(preg_replace("/[^a-zA-Z0-9\s]/", "", $key));
110
                    $do = new DataObject();
111
                    $do->Code = $key;
112
                    $do->Name = $value;
113
                    $do->LinkingMode = (($this->filterType == $title) && ($this->filterValue == $key)) ? "current" : "link";
114
                    $do->filter = (($this->filterType == $title) && ($this->filterValue == $key)) ? "alwaysfilter" : "filter";
115
                    $do->Link = $this->Link("show/$title/".$key."/");
116
                    $dos->push($do);
117
                }
118
            }
119
        }
120
        if ($dos->count()) {
121
            return $dos;
122
        }
123
        return null;
124
    }
125
126
127
    public function Items($limit = 0)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
128
    {
129
        if ($this->SortBy == 'Title') {
130
            $sort = array("File.Title", "ASC");
131
        } elseif ($this->SortBy == 'UploadDate ASC') {
132
            $sort = array("File.Created", "ASC");
133
        } elseif ($this->SortBy == 'UploadDate DESC') {
134
            $sort = array("File.Created", "DESC");
135
        } else {
136
            $sort = array();
137
        }
138
        if ($this->filter) {
139
            $this->filter .= " AND ";
140
        }
141
        $this->filter .= "\"CustomerImage\".\"Status\" = 'Approved' ";
142
        return CustomerImage::get()->where($this->filter)->sort($sort)->limit($limit);
0 ignored issues
show
Bug introduced by
The call to get() misses a required argument $callerClass.

This check looks for function calls that miss required arguments.

Loading history...
143
    }
144
145
146
147
    public function Form()
148
    {
149
        $fields = new FieldList(
150
            new TextField('Keyword', 'Keyword(s)', $this->value)
151
            //$date = new DateField('Date')
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
152
        );
153
        //$date->setConfig('showcalendar', true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
154
        $actions = new FieldList(new FormAction('search', 'Search'));
0 ignored issues
show
Documentation introduced by
'search' is of type string, but the function expects a object<The>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
        return new Form($this, 'Form', $fields, $actions);
156
    }
157
158
159
    public function search($data, $form)
160
    {
161
        user_error("Not yet updated to 3.0");
162
        $this->value = $data['Keyword'];
163
        $data = Convert::raw2sql($data);
164
        if ($data['Keyword']) {
165
            $stage = Versioned::current_stage();
166
            $productTable = "SiteTree";
167
            if ($stage) {
168
                $productTable .= "_".$stage;
169
            }
170
            $kwords = trim($data['Keyword']);
171
            $kwordsKWArray=split(" ", $kwords);//Breaking the string to array of words
172
            // Now let us generate the sql
173
            $kwordsFilter = array();
174
            while (list($key, $val) = each($kwordsKWArray)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
175
                $val = trim($val);
176
                if ($val<>" " and strlen($val) > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
177
                    $kwordsFilter[] = " \"Member\".\"ScreenName\" LIKE '%$val%' OR \"$productTable\".\"Title\" LIKE '%$val%' OR \"PictureLocation\" LIKE '%$val%' ";
178
                }
179
            }
180
            $this->filter = " (". implode(" AND ", $kwordsFilter).") ";
181
            $this->join .= " LEFT JOIN \"$productTable\" ON \"ProductPageID\" = \"$productTable\".\"ID\" " ;
182
            $this->join .= " INNER JOIN \"Member\" ON \"OwnerID\" = \"Member\".\"ID\" " ;
183
        }
184
        return array();
185
    }
186
187
    public function show($request)
188
    {
189
        $action = $request->param("ID");
190
        $value = Convert::raw2sql($request->param("OtherID"));
191
        switch ($action) {
192
            case "year":
193
                $where = "Year(\"File\".\"Created\") = '$value'";
194
                break;
195
            case "location":
196
                $where = "\"PictureLocation\" = '$value'";
197
                break;
198
            case "product":
199
                $where = "\"ProductPageID\" = '$value'";
200
                break;
201
            case "mypictures":
202
                $where = "\"OwnerID\" = '$value'";
203
                break;
204
            default:
205
                $where = "";
206
        }
207
        $this->filter = $where;
208
        $this->filterType = $action;
209
        $this->filterValue = $value;
210
        return array();
211
    }
212
}
213