Test Failed
Branch master (5aadec)
by Agel_Nash
04:00
created

DLpaginate::changeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Class DLpaginate
5
 */
6
class DLpaginate
7
{
8
    /**
9
     * Script Name: *Digg Style Paginator Class
10
     * Script URI: http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/
11
     * Description: Class in PHP that allows to use a pagination like a digg or sabrosus style.
12
     * Script Version: 0.5
13
     * Author: Victor De la Rocha
14
     * Author: Agel Nash
15
     * Author URI: http://www.mis-algoritmos.com
16
     */
17
18
    /**Default values*/
19
    public $total_pages = -1; //items
20
    public $limit = null;
21
    public $target = "";
22
    public $page = 1;
23
    public $adjacents = 2;
24
    public $showCounter = false;
25
    public $className = "pagination";
26
    public $parameterName = "page";
27
    public $urlF = null; //urlFriendly
28
29
    /**Buttons next and previous*/
30
    public $nextT = ' <a href="[+link+]">Next</a> ';
31
    public $nextI = "&#187;"; //&#9658;
32
    public $prevT = ' <a href="[+link+]">Previous</a> ';
33
    public $prevI = "&#171;"; //&#9668;
34
35
    /**Buttons last and first*/
36
    public $lastT = ' <a href="[+link+]">Last</a> ';
37
    public $lastI = "&#187;&#187;"; //&#9658;
38
    public $firstT = ' <a href="[+link+]">First</a> ';
39
    public $firstI = "&#171;&#171;"; //&#9668;
40
41
    public $numberT = ' <a href="[+link+]">[+num+]</a> ';
42
    public $currentT = ' <b>[+num+]</b> ';
43
44
    public $mainTpl = '<div class="[+classname+]">[+wrap+]</div>';
45
46
    public $dotsT = ' ... ';
47
48
    /*****/
49
    protected $mode = null;
50
    protected $modeConfig = array();
51
52
    private $calculate = false;
53
    private $pagination;
54
55
    /**
56
     * @param $mode
57
     * @param array $config
58
     * @return $this
59
     */
60
    public function setMode($mode, array $config = array())
61
    {
62
        $this->mode = $mode;
63
        $this->modeConfig = $config;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Total items
70
     *
71
     * @param $value
72
     * @return $this
73
     */
74
    public function items($value)
75
    {
76
        $this->total_pages = (int)$value;
77
78
        return $this;
79
    }
80
81
    /**
82
     * how many items to show per page
83
     *
84
     * @param $value
85
     * @return $this
86
     */
87
    public function limit($value)
88
    {
89
        $this->limit = (int)$value;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Page to sent the page value
96
     *
97
     * @param $value
98
     * @return $this
99
     */
100
    public function target($value)
101
    {
102
        $this->target = $value;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Current page
109
     *
110
     * @param $value
111
     * @return $this
112
     */
113
    public function currentPage($value)
114
    {
115
        $this->page = (int)$value;
116
117
        return $this;
118
    }
119
120
    /**
121
     * How many adjacent pages should be shown on each side of the current page?
122
     *
123
     * @param $value
124
     * @return $this
125
     */
126
    public function adjacents($value)
127
    {
128
        $this->adjacents = (int)$value;
129
130
        return $this;
131
    }
132
133
    /**
134
     * show counter?
135
     *
136
     * @param string $value
137
     * @return $this
138
     */
139
    public function showCounter($value = "")
140
    {
141
        $this->showCounter = ($value === true) ? true : false;
0 ignored issues
show
introduced by
The condition $value === true can never be true.
Loading history...
142
143
        return $this;
144
    }
145
146
    /**
147
     * to change the class name of the pagination div
148
     *
149
     * @param string $value
150
     * @return $this
151
     */
152
    public function changeClass($value = "")
153
    {
154
        $this->className = $value;
155
156
        return $this;
157
    }
158
159
    /**
160
     * @param $value
161
     * @return $this
162
     */
163
    public function mainTpl($value)
164
    {
165
        $this->mainTpl = $value;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @param $value
172
     * @return $this
173
     */
174
    public function nextLabel($value)
175
    {
176
        $this->nextT = $value;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param $value
183
     * @return $this
184
     */
185
    public function nextIcon($value)
186
    {
187
        $this->nextI = $value;
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param $value
194
     * @return $this
195
     */
196
    public function prevLabel($value)
197
    {
198
        $this->prevT = $value;
199
200
        return $this;
201
    }
202
203
    /**
204
     * @param $value
205
     * @return $this
206
     */
207
    public function prevIcon($value)
208
    {
209
        $this->prevI = $value;
210
211
        return $this;
212
    }
213
214
    /**
215
     * to change the class name of the pagination div
216
     *
217
     * @param string $value
218
     * @return $this
219
     */
220
    public function parameterName($value = "")
221
    {
222
        $this->parameterName = $value;
223
224
        return $this;
225
    }
226
227
    /**
228
     * to change urlFriendly
229
     *
230
     * @param string $value
231
     * @return $this
232
     */
233
    public function urlFriendly($value = "%")
234
    {
235
        if (preg_match('/^\s/', $value)) {
236
            $this->urlF = false;
237
        }
238
        $this->urlF = $value;
239
240
        return $this;
241
    }
242
243
    public function show()
244
    {
245
        echo $this->getOutput();
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function getOutput()
252
    {
253
        $out = '';
254
        if (!$this->calculate && $this->calculate() && !empty($this->pagination)) {
255
            $out = str_replace(array("[+class+]", "[+wrap+]"), array($this->className, $this->pagination),
256
                    $this->mainTpl) . "\n";
257
        }
258
259
        return $out;
260
    }
261
262
    /**
263
     * @param $page
264
     * @return int|mixed
265
     */
266
    protected function getPageQuery($page)
267
    {
268
        switch ($this->mode) {
269
            case 'offset':
270
                $display = isset($this->modeConfig['display']) ? $this->modeConfig['display'] : 0;
271
                $out = $display * ($page - 1);
272
                break;
273
            case 'back':
274
            case 'pages':
275
            default:
276
                $out = $page;
277
                break;
278
        }
279
280
        return $out;
281
    }
282
283
    /**
284
     * @param $id
285
     * @return mixed|string
286
     */
287
    public function get_pagenum_link($id)
288
    {
289
        $flag = (strpos($this->target, '?') === false);
290
        $value = $this->getPageQuery($id);
291
        if ($flag && !empty($this->urlF)) {
292
            $out = str_replace($this->urlF, $value, $this->target);
293
        } else {
294
            $out = $this->target;
295
            if ($id > 1) {
296
                $out .= ($flag ? "?" : "&") . $this->parameterName . "=" . $value;
297
            }
298
        }
299
300
        return $out;
301
    }
302
303
    /**
304
     * @return bool
305
     */
306
    public function calculate()
307
    {
308
        $this->pagination = "";
309
        $this->calculate = true;
310
        $error = false;
311
312
        if (!empty($this->urlF) && is_scalar($this->urlF) && $this->urlF != '%' && strpos($this->target,
313
                $this->urlF) === false
314
        ) {
315
            //Es necesario especificar el comodin para sustituir
316
            $error = true;
317
        } elseif (!empty($this->urlF) && is_scalar($this->urlF) && $this->urlF == '%' && strpos($this->target,
318
                $this->urlF) === false
319
        ) {
320
            $error = true;
321
        }
322
323
        if ($this->total_pages < 0) {
324
            $error = true;
325
        }
326
        if (!is_int($this->limit)) {
327
            $error = true;
328
        }
329
        if ($error) {
330
            return false;
331
        }
332
333
        $counter = 0;
334
335
        /* Setup page vars for display. */
336
        $prev = ($this->page <= 1) ? 0 : $this->page - 1; //previous page is page - 1
337
        $next = (($this->page == $this->total_pages) ? 0 : ($this->page + 1)); //next page is page + 1
338
        $lastpage = $this->total_pages;
339
        if ($this->limit > 1 && $lastpage > $this->limit) {
340
            $lastpage = $this->limit;
341
        }
342
        $lpm1 = $lastpage - 1; //last page minus 1
343
344
        /*
345
                Now we apply our rules and draw the pagination object.
346
                We're actually saving the code to a variable in case we want to draw it more than once.
347
        */
348
        if ($lastpage > 1) {
349
            if ($this->page) {
350
                if ($this->page > 1) {
351
                    $this->pagination .= $this->firstT ? $this->renderItemTPL($this->firstT, 0) : '';
352
                    $this->pagination .= $this->prevT ? $this->renderItemTPL($this->prevT, $prev) : '';
353
                } else {
354
                    $this->pagination .= $this->firstI ? $this->renderItemTPL($this->firstI, 0) : '';
355
                    $this->pagination .= $this->prevI ? $this->renderItemTPL($this->prevI, $prev) : '';
356
                }
357
            }
358
            //pages
359
            if ($lastpage < 7 + ($this->adjacents * 2)) { //not enough pages to bother breaking it up
360
                for ($counter = 1; $counter <= $lastpage; $counter++) {
361
                    $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
362
                    $this->pagination .= $this->renderItemTPL($tpl, $counter);
363
                }
364
            } elseif ($lastpage > 5 + ($this->adjacents * 2)) { //enough pages to hide some
365
                //close to beginning; only hide later pages
366
                if ($this->page <= 2 + ($this->adjacents * 2)) {
367
                    for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++) {
368
                        $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
369
                        $this->pagination .= $this->renderItemTPL($tpl, $counter);
370
                    }
371
                    $this->pagination .= $this->renderItemTPL($this->dotsT, $counter);
372
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lpm1);
373
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lastpage);
374
                } //in middle; hide some front and some back
375
                elseif ($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)) {
376
                    $this->pagination .= $this->renderItemTPL($this->numberT, 1);
377
                    $this->pagination .= $this->renderItemTPL($this->numberT, 2);
378
                    $this->pagination .= $this->renderItemTPL($this->dotsT, 3);
379
380
                    for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++) {
381
                        $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
382
                        $this->pagination .= $this->renderItemTPL($tpl, $counter);
383
                    }
384
                    $this->pagination .= $this->renderItemTPL($this->dotsT, $counter);
385
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lpm1);
386
                    $this->pagination .= $this->renderItemTPL($this->numberT, $lastpage);
387
                } //close to end; only hide early pages
388
                else {
389
                    $this->pagination .= $this->renderItemTPL($this->numberT, 1);
390
                    $this->pagination .= $this->renderItemTPL($this->numberT, 2);
391
                    $this->pagination .= $this->renderItemTPL($this->dotsT, 3);
392
393
                    for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++) {
394
                        $tpl = ($counter == $this->page) ? $this->currentT : $this->numberT;
395
                        $this->pagination .= $this->renderItemTPL($tpl, $counter);
396
                    }
397
                }
398
            }
399
            if ($this->page) {
400
                if ($this->page < $counter - 1) {
401
                    $this->pagination .= $this->nextT ? $this->renderItemTPL($this->nextT, $next) : '';
402
                    $this->pagination .= $this->lastT ? $this->renderItemTPL($this->lastT, $lastpage) : '';
403
                } else {
404
                    $this->pagination .= $this->nextI ? $this->renderItemTPL($this->nextI, $next) : '';
405
                    $this->pagination .= $this->lastI ? $this->renderItemTPL($this->lastI, $lastpage) : '';
406
                }
407
408
                if ($this->showCounter) {
409
                    $this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
410
                }
411
            }
412
        }
413
414
        return true;
415
    }
416
417
    /**
418
     * @param $tpl
419
     * @param $num
420
     * @return mixed
421
     */
422
    protected function renderItemTPL($tpl, $num)
423
    {
424
        return str_replace(array('[+num+]', '[+link+]'), array($num, $this->get_pagenum_link($num)), $tpl);
425
    }
426
}
427