Completed
Push — master ( d5e46b...77750d )
by Matthew
16:31
created

JQGridRenderer::afterBind()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
cc 6
nc 9
nop 0
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 = array(
10
            'datatype' => 'json',
11
            'jsonReader' => array(
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' => array(
31
                    'page' => 'page',
32
                    'rows' => 'limit',
33
                    'sort' => 'sort_column',
34
                    'order' => 'sort_order',
35
                    'nd' => null,
36
            ),
37
38
            'ajaxGridOptions' => array(
39
                    'cache' => false,
40
                    'ifModified' => false,
41
            ),
42
43
            // Pager Config
44
            'pager' => 'grid-pager',
45
    );
46
47
    protected $jqGridCss = [];
48
    protected $jqGridJs = [];
49
50
    protected function afterBind()
51
    {
52
        $id = $this->gridSource->getDivId();
53
        $this->options['pager'] = "{$id}-pager";
54
55
        $params = array(
56
                'id' => $this->gridSource->getId(),
57
                'renderer' => 'jq_grid',
58
        );
59
60
        $url = $this->router->generate('dtc_grid_data', $params);
61
        $this->options['url'] = $url;
62
63
        /** @var AbstractGridColumn $column */
64
        foreach ($this->gridSource->getColumns() as $column) {
65
            $info = array();
66
            $info['label'] = $column->getLabel();
67
            $info['name'] = $column->getField();
68
            $info['index'] = $column->getField();
69
            $info['sortable'] = $column->getOption('sortable') ? true : false;
70
            $info = array_merge($info, $column->getOptions());
71
72
            $this->options['colModel'][] = $info;
73
        }
74
75
        if ($sortInfo = $this->gridSource->getDefaultSort()) {
76
            if ($sortInfo['column']) {
77
                $this->options['sortname'] = $sortInfo['column'];
78
                $this->options['sortorder'] = strtolower($sortInfo['direction'] ?: 'ASC');
79
            }
80
        }
81
    }
82
83
    public function getData()
84
    {
85
        $columns = $this->gridSource->getColumns();
86
        $gridSource = $this->gridSource;
87
        $records = $gridSource->getRecords();
88
89
        $retVal = array(
90
                'page' => $gridSource->getPager()
91
                    ->getCurrentPage(),
92
                'total' => $gridSource->getPager()
93
                    ->getTotalPages(),
94
                'records' => $gridSource->getCount(),
95
                'id' => $gridSource->getId(), // unique id
96
        );
97
98 View Code Duplication
        foreach ($records as $record) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
            $info = array();
100
            /** @var AbstractGridColumn $column */
101
            foreach ($columns as $column) {
102
                if (method_exists($column, 'setRouter')) {
103
                    $column->setRouter($this->router);
104
                }
105
                if (method_exists($column, 'setGridSourceId')) {
106
                    $column->setGridSourceId($gridSource->getId());
107
                }
108
                $info[$column->getField()] = $column->format($record, $this->gridSource);
109
            }
110
111
            $retVal['rows'][] = $info;
112
        }
113
114
        return $retVal;
115
    }
116
117
    public function setJqGridCss(array $css)
118
    {
119
        $this->jqGridCss = $css;
120
    }
121
122
    public function setJqGridJs(array $js)
123
    {
124
        $this->jqGridJs = $js;
125
    }
126
127
    /**
128
     * @param array|null $params
129
     */
130
    public function getParams(array &$params = null)
131
    {
132
        if (null === $params) {
133
            $params = [];
134
        }
135
        parent::getParams($params);
136
        $params['dtc_grid_jq_grid_css'] = $this->jqGridCss;
137
        $params['dtc_grid_jq_grid_js'] = $this->jqGridJs;
138
139
        return $params;
140
    }
141
142
    public function render()
143
    {
144
        $id = $this->gridSource->getDivId();
145
146
        $params = array(
147
                'options' => $this->options,
148
                'id' => $id,
149
        );
150
151
        $template = '@DtcGrid/Grid/jq_grid.html.twig';
152
153
        return $this->twig->render($template, $params);
154
    }
155
}
156