JQGridRenderer   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 90
c 0
b 0
f 0
dl 0
loc 158
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setJqGridJs() 0 3 1
A setJqGridCss() 0 3 1
A setJqGridLocalJs() 0 3 1
A setJqGridLocalCss() 0 3 1
A afterBind() 0 29 6
A getData() 0 32 5
A render() 0 12 1
A getParams() 0 12 2
1
<?php
2
3
namespace Dtc\GridBundle\Grid\Renderer;
4
5
use Dtc\GridBundle\Grid\Column\AbstractGridColumn;
6
7
class JQGridRenderer extends AbstractJqueryRenderer
8
{
9
    public static $defaultOptions = [
10
            'datatype' => 'json',
11
            'jsonReader' => [
12
                    'root' => 'rows',
13
                    'total' => 'total',
14
                    'records' => 'records',
15
                    'page' => 'page',
16
                    'repeatitems' => false,
17
            ],
18
19
            'url' => null,
20
            'cell' => '',
21
            'width' => 840,
22
            'height' => 400,
23
            'loadui' => 'disable',
24
            'altRows' => true,
25
            'viewrecords' => true,
26
            'multiselect' => true,
27
            'styleUI' => 'Bootstrap4',
28
            'iconSet' => 'Octicons',
29
            // Paging params
30
            'prmNames' => [
31
                    'page' => 'page',
32
                    'rows' => 'limit',
33
                    'sort' => 'sort_column',
34
                    'order' => 'sort_order',
35
                    'nd' => null,
36
            ],
37
38
            'ajaxGridOptions' => [
39
                    'cache' => false,
40
                    'ifModified' => false,
41
            ],
42
43
            // Pager Config
44
            'pager' => 'grid-pager',
45
    ];
46
47
    protected $jqGridCss = [];
48
    protected $jqGridJs = [];
49
    private $jqGridLocalCss = [];
50
    private $jqGridLocalJs = [];
51
52
    protected function afterBind()
53
    {
54
        $id = $this->gridSource->getDivId();
55
        $this->options['pager'] = "{$id}-pager";
56
57
        $params = [
58
                'id' => $this->gridSource->getId(),
59
                'renderer' => 'jq_grid',
60
        ];
61
62
        $url = $this->router->generate('dtc_grid_data', $params);
63
        $this->options['url'] = $url;
64
65
        /** @var AbstractGridColumn $column */
66
        foreach ($this->gridSource->getColumns() as $column) {
67
            $info = [];
68
            $info['label'] = $column->getLabel();
69
            $info['name'] = $column->getField();
70
            $info['index'] = $column->getField();
71
            $info['sortable'] = $column->getOption('sortable') ? true : false;
72
            $info = array_merge($info, $column->getOptions());
73
74
            $this->options['colModel'][] = $info;
75
        }
76
77
        if ($sortInfo = $this->gridSource->getDefaultSort()) {
78
            if ($sortInfo['column']) {
79
                $this->options['sortname'] = $sortInfo['column'];
80
                $this->options['sortorder'] = strtolower($sortInfo['direction'] ?: 'ASC');
81
            }
82
        }
83
    }
84
85
    public function getData()
86
    {
87
        $columns = $this->gridSource->getColumns();
88
        $gridSource = $this->gridSource;
89
        $records = $gridSource->getRecords();
90
91
        $retVal = [
92
                'page' => $gridSource->getPager()
93
                    ->getCurrentPage(),
94
                'total' => $gridSource->getPager()
95
                    ->getTotalPages(),
96
                'records' => $gridSource->getCount(),
97
                'id' => $gridSource->getId(), // unique id
98
        ];
99
100
        foreach ($records as $record) {
101
            $info = [];
102
            /** @var AbstractGridColumn $column */
103
            foreach ($columns as $column) {
104
                if (method_exists($column, 'setRouter')) {
105
                    $column->setRouter($this->router);
106
                }
107
                if (method_exists($column, 'setGridSourceId')) {
108
                    $column->setGridSourceId($gridSource->getId());
109
                }
110
                $info[$column->getField()] = $column->format($record, $this->gridSource);
111
            }
112
113
            $retVal['rows'][] = $info;
114
        }
115
116
        return $retVal;
117
    }
118
119
    public function setJqGridCss(array $css)
120
    {
121
        $this->jqGridCss = $css;
122
    }
123
124
    public function setJqGridJs(array $js)
125
    {
126
        $this->jqGridJs = $js;
127
    }
128
129
    public function setJqGridLocalCss(array $css)
130
    {
131
        $this->jqGridLocalCss = $css;
132
    }
133
134
    public function setJqGridLocalJs(array $js)
135
    {
136
        $this->jqGridLocalJs = $js;
137
    }
138
139
    public function getParams(array &$params = null)
140
    {
141
        if (null === $params) {
142
            $params = [];
143
        }
144
        parent::getParams($params);
145
        $params['dtc_grid_jq_grid_css'] = $this->jqGridCss;
146
        $params['dtc_grid_jq_grid_js'] = $this->jqGridJs;
147
        $params['dtc_grid_local_css'] = $this->jqGridLocalCss;
148
        $params['dtc_grid_local_js'] = $this->jqGridLocalJs;
149
150
        return $params;
151
    }
152
153
    public function render()
154
    {
155
        $id = $this->gridSource->getDivId();
156
157
        $params = [
158
                'options' => $this->options,
159
                'id' => $id,
160
        ];
161
162
        $template = '@DtcGrid/Grid/jq_grid.html.twig';
163
164
        return $this->twig->render($template, $params);
165
    }
166
}
167