1 | <?php |
||
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) |
||
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) |
||
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.