Completed
Push — master ( a7ccef...ae69ba )
by Alexey
04:02
created

PageSize   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 64
rs 10
c 0
b 0
f 0

1 Method

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