Passed
Push — 1.0 ( 983892...3e6880 )
by Morven
02:16
created

ComplexCategoryController::getPaginationLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverCommerce\ComplexCategory;
4
5
use CategoryController;
0 ignored issues
show
Bug introduced by
The type CategoryController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Forms\DropdownField;
11
12
class ComplexCategoryController extends CategoryController
13
{
14
    const DEFAULT_SORT = "Default";
15
16
    const DEFAULT_LIMIT = 15;
17
18
    /**
19
     * Specify sort options and their titles
20
     * (these can be expanded upon using global config)
21
     * 
22
     * @var array
23
     */
24
    private static $sort_options = [
25
        self::DEFAULT_SORT => self::DEFAULT_SORT,
26
        'Title ASC' => 'Name (A-Z)',
27
        'Title DESC' => 'Name (Z-A)',
28
        'BasePrice ASC' => 'Price (Low - High)',
29
        'BasePrice DESC' => 'Price (High - Low)'
30
    ];
31
32
    /**
33
     * List of default limit options (can be amended via config)
34
     * 
35
     * @var array
36
     */
37
    private static $show_options = [
38
        self::DEFAULT_LIMIT => self::DEFAULT_LIMIT,
39
        '30' => '30',
40
        '60' => '60',
41
        '90' => '90'
42
    ];
43
44
    /**
45
     * Get an i18n friendly version of the sort name
46
     * 
47
     * @return string
48
     */
49
    public function getTranslatedSort($option)
50
    {
51
        return _t(self::class . "." . $option, $option);
52
    }
53
54
    /**
55
     * Generate a sort/limit form
56
     * 
57
     * @return Form 
58
     */
59
    public function SortLimitForm()
60
    {
61
        $sort_options = $this->config()->sort_options;
62
        $show_options = $this->config()->show_options;
63
64
        // Get the current query
65
        $query = $this->getQuery();
66
67
        // Create a new array based on available sort/limit options
68
        $sort = [];
69
        $limit = [];
70
71
        $i = 0;
72
        foreach ($sort_options as $key => $value) {
73
            $sort[$i] = $this->getTranslatedSort($value);
74
            $i++;
75
        }
76
77
        $i = 0;
78
        foreach ($show_options as $item) {
79
            $limit[$i] = $item;
80
            $i++;
81
        }
82
83
        $form = Form::create(
84
            $this,
85
            __FUNCTION__,
86
            FieldList::create(
87
                $sort_field = DropdownField::create(
88
                    "s[sort]",
89
                    _t(self::class . '.Sort', 'Sort'),
90
                    $sort
91
                ),
92
                $limit_field = DropdownField::create(
93
                    "s[show]",
94
                    _t(self::class . '.Show', 'Show'),
95
                    $limit
96
                ),
97
                FormAction::create(
98
                    "go",
99
                    _t(self::class . '.Go', 'Go')
100
                )
101
            ),
102
            FieldList::create()
103
        );
104
105
        // Add extra bootstrap class (if required)
106
        $form
107
            ->addExtraClass("form-inline")
108
            ->setTemplate('SilverCommerce\\ComplexCategory\\' . __FUNCTION__)
109
            ->setFormMethod("GET")
110
            ->setFormAction($this->Link())
111
            ->disableSecurityToken();
112
113
        if (!empty($query["sort"])) {
114
            $sort_field->setValue($query["sort"]);
115
        }
116
117
        if (!empty($query["show"])) {
118
            $limit_field->setValue($query["show"]);
119
        }
120
121
        $this->extend("updateSortLimitForm", $form);
122
123
        return $form;
124
    }
125
126
    /**
127
     * Get the pagination limit selected (either via the URL or by default)
128
     * 
129
     * @return int
130
     */
131
    protected function getPaginationLimit()
132
    {
133
        $show_options = Config::inst()->get(self::class, "show_options");
134
        $new_limit = $this->getCurrentOption($show_options, "show");
135
136
        if (!empty($new_limit)) {
137
            $limit = $new_limit;
138
        } else {
139
            $limit = self::DEFAULT_LIMIT;
140
        }
141
142
        return (int)$limit;
143
    }
144
145
    /**
146
     * Get a paginated list of all products at this level and below
147
     * 
148
     * This is expanded to support the length dropdown
149
     *
150
     * @return PaginatedList
0 ignored issues
show
Bug introduced by
The type SilverCommerce\ComplexCategory\PaginatedList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
151
     */
152
    public function PaginatedProducts($limit = 10)
153
    {
154
        $limit = $this->getPaginationLimit();
155
156
        return parent::PaginatedAllProducts($limit);
157
    }
158
159
    /**
160
     * Get a paginated list of all products at this level and below
161
     * 
162
     * This is expanded to support the length dropdown
163
     *
164
     * @return PaginatedList
165
     */
166
    public function PaginatedAllProducts($limit = 10)
167
    {
168
        $limit = $this->getPaginationLimit();
169
170
        return parent::PaginatedAllProducts($limit);
171
    }
172
}