Completed
Push — master ( c933cb...bae5b1 )
by Alexey
03:50
created

PageSize::run()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 0
1
<?php
2
3
namespace common\widgets;
4
5
use yii\helpers\Html;
6
7
/**
8
 * Class PageSize
9
 * @package common\widgets
10
 */
11
class PageSize extends \yii\base\Widget
12
{
13
    /**
14
     * @var string the label text.
15
     */
16
    public $label = 'items';
17
18
    /**
19
     * @var integer the defualt page size. This page size will be used when the $_GET['per-page'] is empty.
20
     */
21
    public $defaultPageSize = 20;
22
23
    /**
24
     * @var string the name of the GET request parameter used to specify the size of the page.
25
     * This will be used as the input name of the dropdown list with page size options.
26
     */
27
    public $pageSizeParam = 'per-page';
28
29
    /**
30
     * @var array the list of page sizes
31
     */
32
    public $sizes = [2 => 2, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 50 => 50, 100 => 100, 200 => 200];
33
34
    /**
35
     * @var string the template to be used for rendering the output.
36
     */
37
    public $template = '{list} {label}';
38
39
    /**
40
     * @var array the list of options for the drop down list.
41
     */
42
    public $options;
43
44
    /**
45
     * @var array the list of options for the label
46
     */
47
    public $labelOptions;
48
49
    /**
50
     * @var boolean whether to encode the label text.
51
     */
52
    public $encodeLabel = true;
53
54
    /**
55
     * Runs the widget and render the output
56
     */
57
    public function run()
58
    {
59
        if (empty($this->options['id'])) {
60
            $this->options['id'] = $this->id;
61
        }
62
63
        if ($this->encodeLabel) {
64
            $this->label = Html::encode($this->label);
65
        }
66
67
        $perPage = !empty($_GET[$this->pageSizeParam]) ? $_GET[$this->pageSizeParam] : $this->defaultPageSize;
68
69
        $listHtml = Html::dropDownList($this->pageSizeParam, $perPage, $this->sizes, $this->options);
70
        $labelHtml = Html::label($this->label, $this->options['id'], $this->labelOptions);
71
72
        $output = str_replace(['{list}', '{label}'], [$listHtml, $labelHtml], $this->template);
73
74
        return $output;
75
    }
76
}
77