WebPortfolioPage::getCMSFields()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 0
1
<?php
2
3
/**
4
 * @author Nicolaas [at] sunnysideup.co.nz
5
 * @package webportfolio
6
 * @sub-packages webportfolio
7
 *
8
 *
9
 *
10
 */
11
12
class WebPortfolioPage 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...
13
{
14
    private static $icon = "webportfolio/images/treeicons/WebPortfolioPage";
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...
15
16
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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...
17
        'HighlightsOnly' => 'Boolean'
18
    );
19
20
    private static $has_one = array();
0 ignored issues
show
Unused Code introduced by
The property $has_one 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...
21
22
    private static $many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $many_many 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...
23
        "WebPortfolioItems" => "WebPortfolioItem"
24
    );
25
26
    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...
27
    {
28
        $fields = parent::getCMSFields();
29
        $itemOptionSet = WebPortfolioItem::get();
30
        $itemOptionSetMap = ($itemOptionSet->count()) ? $itemOptionSet->map('ID', 'Title')->toArray() : array();
31
        $fields->addFieldsToTab(
32
            "Root.Portfolio",
33
            array(
34
                CheckboxField::create(
35
                    'HighlightsOnly',
36
                    'Highlights Only'
37
                ),
38
                LiteralField::create("UpdatePortfolio", "<h3>Update Portfolio</h3>"),
39
                LiteralField::create("EditPortfolio", "<p><a href=\"/admin/webportfolio\" target=\"_blank\">edit portfolio</a></p>"),
40
                LiteralField::create("RefreshPortfolio", "<p><a href=\"".$this->Link("json/?flush=json")."\" target=\"_blank\">clear portfolio cache</a> (portfolio data is cached to increase loading speed)</p>"),
41
                CheckboxSetField::create(
42
                    $name = "WebPortfolioItems",
43
                    $title = "Items shown",
44
                    $source = $itemOptionSetMap
45
                )
46
            )
47
        );
48
        return $fields;
49
    }
50
}
51
52
class WebPortfolioPage_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...
53
{
54
    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...
55
        "show"
56
    );
57
58 View Code Duplication
    public function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        parent::init();
61
        Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
62
        if (class_exists("PrettyPhoto")) {
63
            PrettyPhoto::include_code();
64
        } else {
65
            user_error("It is recommended that you include the PrettyPhoto Module", E_USER_NOTICE);
66
        }
67
        Requirements::javascript("webportfolio/javascript/webportfolio.js");
68
        Requirements::themedCSS("WebPortfolioPage", "webportfolio");
69
    }
70
71
    protected $IDArray = array();
72
    protected $hasFilter = false;
73
    protected $currentCode = "";
74
    protected $currentDescription = "";
75
76
    public function index()
77
    {
78
        if (!$this->HighlightsOnly) {
79
            $this->Title .= " - Favourites";
80
        }
81
        return array();
82
    }
83
84
    public function show()
85
    {
86
        $this->hasFilter = true;
87
        $code = Convert::raw2sql($this->request->param("ID"));
88
        if (is_numeric($code) && intval($code) > 0) {
89
            $this->currentCode = $code;
0 ignored issues
show
Documentation Bug introduced by
It seems like $code can also be of type integer or double. However, the property $currentCode is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
90
            $item = WebPortfolioItem::get()->byID(intval($code));
91
            if ($item) {
92
                $this->IDArray = array($item->ID => $item->ID);
93
                $this->Title .= " - ".$item->getHeadLine();
94
                $this->currentDescription = $item->Notes;
95
            }
96
        } elseif ($code) {
97
            $this->currentCode = $code;
98
            $obj = WebPortfolioWhatWeDidDescriptor::get()->filter(array("Code" => $code))->first();
99
            $this->Title .= " - ".$obj->Name;
100
            if ($obj) {
101
                $this->currentDescription = $obj->Description;
102
                $components = $obj->getManyManyComponents('WebPortfolioItem');
103
                if ($components && $components->count()) {
104
                    $this->IDArray = $components->column("ID");
105
                }
106
            }
107
        }
108
        return array();
109
    }
110
111
    public function SelectedWebPortfolioItems()
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...
112
    {
113
        if ($this->HighlightsOnly) {
114
            return $this->WebPortfolioItems()
115
                ->sort(array("Favourites" => "DESC", "RAND()" => "ASC"));
116
        }
117
        if ($this->hasFilter) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
118
        } else {
119
            $components = $this->getManyManyComponents('WebPortfolioItems');
120
            if ($components && $components->count()) {
121
                $this->IDArray = $components->column("ID");
122
            }
123
        }
124
        $reset = false;
125
        if (!$this->IDArray) {
126
            $reset = true;
127
        } elseif (!is_array($this->IDArray)) {
128
            $reset = true;
129
        } elseif (!count($this->IDArray)) {
130
            $reset = true;
131
        }
132
        if ($reset) {
133
            $this->IDArray = array(0 => 0);
134
        }
135
        $extraWhere = "";
136
        if (!$this->hasFilter) {
137
            $extraWhere = " AND \"Favourites\" = 1";
138
        }
139
        return WebPortfolioItem::get()
140
            ->where("\"WebPortfolioItem\".\"ID\" IN (".implode(",", $this->IDArray).") AND \"WebPortfolioPage_WebPortfolioItems\".\"WebPortfolioPageID\" = ".$this->ID.$extraWhere)
141
            ->sort(array("Favourites" => "DESC", "RAND()" => "ASC"))
142
            ->innerJoin("WebPortfolioPage_WebPortfolioItems", "\"WebPortfolioPage_WebPortfolioItems\".\"WebPortfolioItemID\" = \"WebPortfolioItem\".\"ID\"");
143
    }
144
145
    public function HasFilter()
146
    {
147
        return $this->hasFilter;
148
    }
149
150
    public function CurrentDescription()
151
    {
152
        return $this->currentDescription;
153
    }
154
155
    public function FilterList()
156
    {
157
        if ($this->HighlightsOnly) {
158
            return null;
159
        }
160
        $items = WebPortfolioWhatWeDidDescriptor::get()
161
            ->innerJoin("WebPortfolioItem_WhatWeDid", " \"WebPortfolioItem_WhatWeDid\".\"WebPortfolioWhatWeDidDescriptorID\" = \"WebPortfolioWhatWeDidDescriptor\".\"ID\"");
162
        foreach ($items as $item) {
163
            if ($item->Code == $this->currentCode) {
164
                $item->LinkingMode = "current";
165
            } else {
166
                $item->LinkingMode = "link";
167
            }
168
        }
169
        return $items;
170
    }
171
}
172