These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Admingenerator\GeneratorBundle\Builder\Admin; |
||
4 | |||
5 | use Admingenerator\GeneratorBundle\Generator\Column; |
||
6 | use Admingenerator\GeneratorBundle\Generator\Action; |
||
7 | use Admingenerator\GeneratorBundle\Generator\Action\Generic\ExcelAction; |
||
8 | |||
9 | /** |
||
10 | * This builder generates php for list actions |
||
11 | * |
||
12 | * @author cedric Lombardot |
||
13 | * @author Piotr Gołębiewski <[email protected]> |
||
14 | * @author Stéphane Escandell <[email protected]> |
||
15 | */ |
||
16 | class ListBuilder extends BaseBuilder |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $excelActions = null; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $batchActions = null; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $scopeColumns = null; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $filterColumns = null; |
||
37 | |||
38 | /** |
||
39 | * (non-PHPdoc) |
||
40 | * @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey() |
||
41 | */ |
||
42 | public function getYamlKey() |
||
43 | { |
||
44 | return 'list'; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Retrieve the FQCN formType used by this builder |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function getFormType() |
||
53 | { |
||
54 | return sprintf( |
||
55 | '%s%s\\Form\Type\\%s\\FiltersType', |
||
56 | $this->getVariable('namespace_prefix') ? $this->getVariable('namespace_prefix') . '\\' : '', |
||
57 | $this->getVariable('bundle_name'), |
||
58 | $this->getBaseGeneratorName() |
||
59 | ); |
||
60 | } |
||
61 | |||
62 | public function getFilterColumns() |
||
63 | { |
||
64 | if (null === $this->filterColumns) { |
||
65 | $this->filterColumns = array(); |
||
66 | $this->findFilterColumns(); |
||
67 | } |
||
68 | |||
69 | return $this->filterColumns; |
||
70 | } |
||
71 | |||
72 | protected function findFilterColumns() |
||
73 | { |
||
74 | $columnsName = $this->getVariable('filters'); |
||
75 | $fromFilterConfiguration = true; |
||
76 | if (null === $columnsName) { |
||
77 | $fromFilterConfiguration = false; |
||
78 | $columnsName = $this->getAllFields(); |
||
79 | } |
||
80 | |||
81 | foreach ($columnsName as $columnName) { |
||
82 | $column = $this->createColumn($columnName, true); |
||
83 | |||
84 | if ($fromFilterConfiguration || $column->isFilterable()) { |
||
85 | $this->addFilterColumn($column); |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | protected function addFilterColumn(Column $column) |
||
91 | { |
||
92 | $this->filterColumns[$column->getName()] = $column; |
||
93 | } |
||
94 | |||
95 | public function getFilterColumnsCredentials() |
||
96 | { |
||
97 | $credentials = array(); |
||
98 | |||
99 | foreach($this->getFilterColumns() as $column) { |
||
100 | if (! $filterCredentials = $column->getFiltersCredentials()) { |
||
101 | // If one column has no Credentials constraint, we always |
||
102 | // have to display the filter panel |
||
103 | return array(); |
||
104 | } |
||
105 | |||
106 | $credentials[] = $filterCredentials; |
||
107 | } |
||
108 | |||
109 | return $credentials; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Find scopes parameters |
||
114 | */ |
||
115 | public function getScopes() |
||
116 | { |
||
117 | return $this->getGenerator()->getFromYaml('builders.list.params.scopes'); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getScopeColumns() |
||
124 | { |
||
125 | if (null === $this->scopeColumns) { |
||
126 | $this->scopeColumns = array(); |
||
127 | $this->findScopeColumns(); |
||
128 | } |
||
129 | |||
130 | return $this->scopeColumns; |
||
131 | } |
||
132 | |||
133 | protected function findScopeColumns() |
||
134 | { |
||
135 | foreach ($this->getScopesDisplayColumns() as $columnName) { |
||
136 | $column = $this->createColumn($columnName, true); |
||
137 | |||
138 | // Set the user parameters |
||
139 | $this->addScopeColumn($column); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return array Scopes display column names |
||
145 | */ |
||
146 | protected function getScopesDisplayColumns() |
||
147 | { |
||
148 | $scopeGroups = $this->getGenerator()->getFromYaml('builders.list.params.scopes', array()); |
||
149 | $scopeColumns = array(); |
||
150 | |||
151 | foreach ($scopeGroups as $scopeGroup) { |
||
152 | foreach ($scopeGroup as $scopeFilter) { |
||
153 | if (array_key_exists('filters', $scopeFilter) && is_array($scopeFilter['filters'])) { |
||
154 | foreach ($scopeFilter['filters'] as $field => $value) { |
||
155 | $scopeColumns[] = $field; |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $scopeColumns; |
||
162 | } |
||
163 | |||
164 | protected function addScopeColumn(Column $column) |
||
165 | { |
||
166 | $this->scopeColumns[$column->getName()] = $column; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return a list of batch action from list.batch_actions |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getBatchActions() |
||
174 | { |
||
175 | if (null === $this->batchActions) { |
||
176 | $this->batchActions = array(); |
||
177 | $this->findBatchActions(); |
||
178 | } |
||
179 | |||
180 | return $this->batchActions; |
||
181 | } |
||
182 | |||
183 | View Code Duplication | protected function setUserBatchActionConfiguration(Action $action) |
|
0 ignored issues
–
show
|
|||
184 | { |
||
185 | $batchActions = $this->getVariable('batch_actions', array()); |
||
186 | $builderOptions = is_array($batchActions) && array_key_exists($action->getName(), $batchActions) |
||
187 | ? $batchActions[$action->getName()] |
||
188 | : array(); |
||
189 | |||
190 | $globalOptions = $this->getGenerator()->getFromYaml( |
||
191 | 'params.batch_actions.'.$action->getName(), |
||
192 | array() |
||
193 | ); |
||
194 | |||
195 | if (null !== $builderOptions) { |
||
196 | foreach ($builderOptions as $option => $value) { |
||
197 | $action->setProperty($option, $value); |
||
198 | } |
||
199 | } elseif (null !== $globalOptions) { |
||
200 | foreach ($globalOptions as $option => $value) { |
||
201 | $action->setProperty($option, $value); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | protected function addBatchAction(Action $action) |
||
207 | { |
||
208 | $this->batchActions[$action->getName()] = $action; |
||
209 | } |
||
210 | |||
211 | View Code Duplication | protected function findBatchActions() |
|
212 | { |
||
213 | $batchActions = $this->getVariable('batch_actions', array()); |
||
214 | |||
215 | foreach ($batchActions as $actionName => $actionParams) { |
||
216 | $action = $this->findBatchAction($actionName); |
||
217 | |||
218 | if (!$action) { |
||
219 | $action = new Action($actionName); |
||
220 | } |
||
221 | |||
222 | if ($globalCredentials = $this->getGenerator()->getFromYaml('params.credentials')) { |
||
223 | // If generator is globally protected by credentials |
||
224 | // batch actions are also protected |
||
225 | $action->setCredentials($globalCredentials); |
||
226 | } |
||
227 | |||
228 | $this->setUserBatchActionConfiguration($action); |
||
229 | $this->addBatchAction($action); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Return a list of actions from excel.export |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function getExcelActions() |
||
239 | { |
||
240 | if (null === $this->excelActions) { |
||
241 | $this->excelActions = array(); |
||
242 | $this->fillExportActions(); |
||
243 | } |
||
244 | |||
245 | return $this->excelActions; |
||
246 | } |
||
247 | |||
248 | protected function fillExportActions() |
||
249 | { |
||
250 | $export = $this->getGenerator()->getFromYaml('builders.excel.params.export', []); |
||
251 | if (!count($export)) return; |
||
252 | |||
253 | foreach ($export as $keyName => $params ) { |
||
254 | if (!isset($params['show_button']) || (isset($params['show_button']) && filter_var($params['show_button'], FILTER_VALIDATE_BOOLEAN))) { |
||
255 | $action = new ExcelAction($keyName, $this); |
||
256 | $action->setCredentials($this->getExportParamsForKey($keyName, 'credentials', 'AdmingenAllowed')); |
||
257 | $action->setClass($this->getExportParamsForKey($keyName, 'class', 'btn-info')); |
||
258 | $action->setIcon($this->getExportParamsForKey($keyName, 'icon', 'fa-file-excel-o')); |
||
259 | $action->setLabel($this->getExportParamsForKey($keyName, 'label', 'action.generic.excel')); |
||
260 | $this->excelActions[$keyName] = $action; |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | |||
265 | public function getExportParamsForKey($key, $name, $default) |
||
266 | { |
||
267 | if (!$key) return $default; |
||
268 | |||
269 | $export = $this->getGenerator()->getFromYaml('builders.excel.params.export', []); |
||
270 | if (!count($export) || !isset($export[$key]) || !count($export[$key]) || !isset($export[$key][$name])) return $default; |
||
271 | |||
272 | return $export[$key][$name]; |
||
273 | } |
||
274 | } |
||
275 |
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.