1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @company MTE Telecom, Ltd. |
4
|
|
|
* @author Roman Malashin <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Nnx\DataGrid\View\Helper\JqGrid; |
8
|
|
|
|
9
|
|
|
use Nnx\DataGrid\Mutator\HighlightMutatorInterface; |
10
|
|
|
use Nnx\DataGrid\PaginatorGridInterface; |
11
|
|
|
use Zend\View\Helper\AbstractHelper; |
12
|
|
|
use Nnx\DataGrid\GridInterface; |
13
|
|
|
use Zend\View\Helper\EscapeHtml; |
14
|
|
|
use Zend\View\Renderer\PhpRenderer; |
15
|
|
|
use Nnx\DataGrid\View\Helper\Exception; |
16
|
|
|
use Nnx\DataGrid\NavigationBar\NavigationBarInterface; |
17
|
|
|
use Nnx\DataGrid\Button\ButtonInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Grid |
21
|
|
|
* @package Nnx\DataGrid\View\Helper |
22
|
|
|
*/ |
23
|
|
|
class Grid extends AbstractHelper |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param GridInterface $grid |
27
|
|
|
* @return string |
28
|
|
|
* @throws Exception\RuntimeException |
29
|
|
|
*/ |
30
|
|
|
public function __invoke(GridInterface $grid) |
31
|
|
|
{ |
32
|
|
|
$columns = $grid->getColumns(); |
33
|
|
|
if (count($columns) === 0) { |
34
|
|
|
throw new Exception\RuntimeException('В гриде нет колонок!'); |
35
|
|
|
} |
36
|
|
|
/** @var PhpRenderer $view */ |
37
|
|
|
$view = $this->getView(); |
38
|
|
|
/** @var EscapeHtml $escape */ |
39
|
|
|
$escape = $view->plugin('escapeHtml'); |
|
|
|
|
40
|
|
|
$bottomNavigationBar = $this->renderNavigationBar($grid->getBottomNavigationBar()); |
41
|
|
|
$topNavigationBar = $this->renderNavigationBar($grid->getTopNavigationBar()); |
|
|
|
|
42
|
|
|
$res = $topNavigationBar['html'] . '<table id="grid-' . $escape($grid->getName()) . '"></table>' . $bottomNavigationBar['html']; |
|
|
|
|
43
|
|
|
$buttonsJs = $topNavigationBar['js'] . $bottomNavigationBar['js']; |
|
|
|
|
44
|
|
|
/** @var PhpRenderer $view */ |
45
|
|
|
$view = $this->getView(); |
|
|
|
|
46
|
|
|
$config = $this->getGridConfig($grid); |
47
|
|
|
foreach ($columns as $column) { |
48
|
|
|
$columnClass = get_class($column); |
49
|
|
|
$columnPath = explode('\\', $columnClass); |
|
|
|
|
50
|
|
|
$colName = array_pop($columnPath); |
|
|
|
|
51
|
|
|
$helperName = 'nnxGridJqGrid' . $colName; |
|
|
|
|
52
|
|
|
/** @var string $columnsJqOptions */ |
53
|
|
|
$config['colModel'][] = $view->$helperName($column); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$rowAttr = null; |
57
|
|
|
if ($mutators = $grid->getMutators()) { |
58
|
|
|
foreach ($mutators as $mutator) { |
59
|
|
|
if ($mutator instanceof HighlightMutatorInterface) { |
60
|
|
|
$config['rowattr'] = '%rowAttrFunction%'; |
61
|
|
|
$rowAttr = 'function(rd) {' . |
|
|
|
|
62
|
|
|
'if(rd.' . $mutator->getDataName() . ') {' |
63
|
|
|
. 'return {"class": rd.' . $mutator->getDataName() . '};' |
64
|
|
|
. '}' |
65
|
|
|
. '}'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$gridName = $grid->getName(); |
71
|
|
|
if ($grid instanceof PaginatorGridInterface) { |
72
|
|
|
$config['rowNum'] = $grid->getPaginator()->getItemCountPerPage(); |
|
|
|
|
73
|
|
|
$config['rowList'] = $grid->getPaginator()->getPossibleItemCountPerPage(); |
74
|
|
|
$config['pager'] = 'pager-grid-' . $gridName; |
|
|
|
|
75
|
|
|
$res .= "<div id='pager-grid-{$gridName}'>"; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$options = \Zend\Json\Json::encode( |
79
|
|
|
$config, |
80
|
|
|
false, |
81
|
|
|
['enableJsonExprFinder' => true] |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$view->headScript()->appendScript('$(function(){' |
85
|
|
|
. 'var grid = $("#grid-' . $grid->getName() . '").jqGrid(' |
86
|
|
|
. str_replace('"%rowAttrFunction%"', $rowAttr, $options) . ');' |
87
|
|
|
. str_replace('%gridName%', $grid->getName(), $buttonsJs) . '});'); |
88
|
|
|
return $res; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param NavigationBarInterface $navigationBar |
|
|
|
|
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function renderNavigationBar(NavigationBarInterface $navigationBar = null) |
96
|
|
|
{ |
97
|
|
|
$html = ''; |
98
|
|
|
$js = ''; |
|
|
|
|
99
|
|
|
if ($navigationBar) { |
100
|
|
|
/** @var PhpRenderer $view */ |
101
|
|
|
$view = $this->getView(); |
|
|
|
|
102
|
|
|
$navigationBarOptions = $navigationBar->getOptions(); |
103
|
|
|
$urlVariables = []; |
|
|
|
|
104
|
|
|
if (isset($navigationBarOptions['urlVariables'])) { |
105
|
|
|
$urlVariables = $navigationBarOptions['urlVariables']; |
106
|
|
|
} |
107
|
|
|
/** @var ButtonInterface $button */ |
108
|
|
|
foreach ($navigationBar->getButtons() as $button) { |
109
|
|
|
$buttonResult = $view->nnxGridJqGridButton($button, $urlVariables); |
110
|
|
|
$html .= $buttonResult['html']; |
|
|
|
|
111
|
|
|
$js .= $buttonResult['js']; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
$html = "<div class='buttons-panel'>$html</div><br>"; |
114
|
|
|
} |
115
|
|
|
return ['html' => $html, 'js' => $js]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param GridInterface $grid |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function getGridConfig(GridInterface $grid) |
123
|
|
|
{ |
124
|
|
|
$attributes = $grid->getAttributes(); |
125
|
|
|
$config = [ |
|
|
|
|
126
|
|
|
'shrinkToFit' => false, |
127
|
|
|
'width' => $this->getConfigVal('width', $attributes, '100%'), |
128
|
|
|
'datatype' => $this->getConfigVal('datatype', $attributes, 'local') |
129
|
|
|
]; |
130
|
|
|
if (!array_key_exists('width', $attributes) && !array_key_exists('autowidth', $attributes)) { |
131
|
|
|
$config['autowidth'] = true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$config = array_merge($config, $attributes); |
135
|
|
|
return $config; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $key |
140
|
|
|
* @param array $options |
141
|
|
|
* @param mixed $default |
142
|
|
|
* @return null|string|array |
143
|
|
|
*/ |
144
|
|
|
protected function getConfigVal($key, array $options, $default = null) |
145
|
|
|
{ |
146
|
|
|
return array_key_exists($key, $options) ? $options[$key] : $default; |
147
|
|
|
} |
148
|
|
|
} |
|
|
|
|
149
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.