Order::active()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Generate sorting order links
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Pagination
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\pagination;
17
18
/**
19
 * Generate sorting order links
20
 *
21
 * @category  Core
22
 * @package   Pagination
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Order
30
{
31
    /**
32
     * Plugin name
33
     **/
34
    private $_plugin;
35
36
    /**
37
     * Action name
38
     **/
39
    private $_action;
40
41
    /**
42
     * List of available columns
43
     **/
44
    private $_columns;
45
46
    /**
47
     * Current sorting order
48
     **/
49
    private $_active;
50
51
    /**
52
     * Reverse order if set to true
53
     **/
54
    private $_desc = false;
55
56
    /**
57
     * Additional parameters for url
58
     **/
59
    private $_params = [];
60
61
    /**
62
     * Arrow down content
63
     **/
64
    private $_arrowDown = '';
65
66
    /**
67
     * Arrow up content
68
     **/
69
    private $_arrowUp = '';
70
71
    /**
72
     * Start a new order generator
73
     *
74
     * @param string  $plugin  Plugin name
75
     * @param string  $action  Action name
76
     * @param array   $columns Allowed columns as an array
77
     * @param string  $default Column to order by default
78
     * @param boolean $desc    Use reverse order for default sorting
79
     *
80
     * @return \csphere\core\pagination\Order
81
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
82
83
    public function __construct(
84
        $plugin, $action, array $columns, $default, $desc = false
85
    ) {
86
        // Store settings
87
        $this->_plugin  = $plugin;
88
        $this->_action  = $action;
89
        $this->_columns = $columns;
90
        $this->_active  = $default;
91
        $this->_desc    = $desc;
92
93
        // Get active order and desc setting
94
        $get_active = \csphere\core\http\Input::get('get', 'order');
95
        $get_desc   = \csphere\core\http\Input::get('get', 'desc');
96
97
        if (in_array($get_active, $this->_columns)) {
98
99
            $this->_active = $get_active;
100
101
            if ($get_desc == 1) {
102
103
                $this->_desc = true;
104
105
            } else {
106
107
                $this->_desc = false;
108
            }
109
        }
110
    }
111
112
    /**
113
     * Generate array of order urls
114
     *
115
     * @return array
116
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
117
118
    public function urls()
119
    {
120
        $params = $this->_params;
121
122
        $urls = [];
123
124
        // Create list of usable urls
125
        foreach ($this->_columns AS $col) {
126
127
            $params['order'] = $col;
128
            $params['desc'] = '';
129
130
            if ($col == $this->_active && $this->_desc == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
131
132
                $params['desc'] = 1;
133
            }
134
135
            $urls[$col] = \csphere\core\url\Link::href(
136
                $this->_plugin, $this->_action, $params
137
            );
138
        }
139
140
        return $urls;
141
    }
142
143
    /**
144
     * Generate array of arrows for sort directions
145
     *
146
     * @return array
147
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
148
149
    public function arrows()
150
    {
151
        // Check if arrows are cached
152
        if ($this->_arrowUp == '') {
153
154
            $this->_getArrows();
155
        }
156
157
        $arrows = [];
158
159
        // Create list of arrows
160
        foreach ($this->_columns AS $col) {
161
162
            $var = '';
163
164
            // Only add arrow to active column
165
            if ($col == $this->_active) {
166
167
                $var = $this->_arrowUp;
168
169
                // Change direction for ASC
170
                if ($this->_desc == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
171
172
                    $var = $this->_arrowDown;
173
                }
174
            }
175
176
            $arrows[$col] = $var;
177
        }
178
179
        return $arrows;
180
    }
181
182
    /**
183
     * Get active column
184
     *
185
     * @return string
186
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
187
188
    public function active()
189
    {
190
        return $this->_active;
191
    }
192
193
    /**
194
     * Get desc setting for active column
195
     *
196
     * @return boolean
197
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
198
199
    public function desc()
200
    {
201
        return $this->_desc;
202
    }
203
204
    /**
205
     * Set additional parameters
206
     *
207
     * @param array $params Parameters as an array of key value pairs
208
     *
209
     * @return boolean
210
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
211
212
    public function params(array $params)
213
    {
214
        $this->_params = $params;
215
216
        return true;
217
    }
218
219
    /**
220
     * Get up and down arrow
221
     *
222
     * @return void
223
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
224
225
    private function _getArrows()
226
    {
227
        // Get arrows from cache
228
        $pre    = 'pagination_order_';
229
        $loader = \csphere\core\service\Locator::get();
230
        $cache  = $loader->load('cache');
231
232
        $this->_arrowUp   = $cache->load($pre . 'up');
233
        $this->_arrowDown = $cache->load($pre . 'down');
234
235
        // Create arrow cache entries otherwise
236
        if ($this->_arrowUp == false) {
237
238
            $view = $loader->load('view');
239
240
            // Send data to view and fetch box result
241
            $view->template('default', 'core_order', ['sort' => 'up'], true);
242
            $this->_arrowUp = $view->box();
243
            $view->template('default', 'core_order', ['sort' => 'down'], true);
244
            $this->_arrowDown = $view->box();
245
246
            // Save arrows to cache
247
            $cache->save($pre . 'up', $this->_arrowUp);
248
            $cache->save($pre . 'down', $this->_arrowDown);
249
        }
250
    }
251
}
252