1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\Components;
13
14 use Grido\Components\Actions\Action;
15 use Grido\Components\Columns\Column;
16 use Grido\Components\Columns\Editable;
17 use Grido\Components\Filters\Filter;
18 use Grido\Helpers;
19
20 21 22 23 24 25 26 27
28 abstract class Container extends \Nette\Application\UI\Control
29 {
30
31 protected $hasColumns;
32
33
34 protected $hasFilters;
35
36
37 protected $hasActions;
38
39
40 protected $hasOperation;
41
42
43 protected $hasExport;
44
45 46 47 48 49 50
51 public function getColumn($name, $need = TRUE)
52 {
53 return $this->hasColumns()
54 ? $this->getComponent(Column::ID)->getComponent(Helpers::formatColumnName($name), $need)
55 : NULL;
56 }
57
58 59 60 61 62 63
64 public function getFilter($name, $need = TRUE)
65 {
66 return $this->hasFilters()
67 ? $this->getComponent(Filter::ID)->getComponent(Helpers::formatColumnName($name), $need)
68 : NULL;
69 }
70
71 72 73 74 75 76
77 public function getAction($name, $need = TRUE)
78 {
79 return $this->hasActions()
80 ? $this->getComponent(Action::ID)->getComponent($name, $need)
81 : NULL;
82 }
83
84 85 86 87 88
89 public function getOperation($need = TRUE)
90 {
91 return $this->getComponent(Operation::ID, $need);
92 }
93
94 95 96 97 98
99 public function getExport($need = TRUE)
100 {
101 return $this->getComponent(Export::ID, $need);
102 }
103
104
105
106 107 108 109 110
111 public function hasColumns($useCache = TRUE)
112 {
113 $hasColumns = $this->hasColumns;
114
115 if ($hasColumns === NULL || $useCache === FALSE) {
116 $container = $this->getComponent(Column::ID, FALSE);
117 $hasColumns = $container && count($container->getComponents()) > 0;
118 $this->hasColumns = $useCache ? $hasColumns : NULL;
119 }
120
121 return $hasColumns;
122 }
123
124 125 126 127 128
129 public function hasFilters($useCache = TRUE)
130 {
131 $hasFilters = $this->hasFilters;
132
133 if ($hasFilters === NULL || $useCache === FALSE) {
134 $container = $this->getComponent(Filter::ID, FALSE);
135 $hasFilters = $container && count($container->getComponents()) > 0;
136 $this->hasFilters = $useCache ? $hasFilters : NULL;
137 }
138
139 return $hasFilters;
140 }
141
142 143 144 145 146
147 public function hasActions($useCache = TRUE)
148 {
149 $hasActions = $this->hasActions;
150
151 if ($hasActions === NULL || $useCache === FALSE) {
152 $container = $this->getComponent(Action::ID, FALSE);
153 $hasActions = $container && count($container->getComponents()) > 0;
154 $this->hasActions = $useCache ? $hasActions : NULL;
155 }
156
157 return $hasActions;
158 }
159
160 161 162 163 164
165 public function hasOperation($useCache = TRUE)
166 {
167 $hasOperation = $this->hasOperation;
168
169 if ($hasOperation === NULL || $useCache === FALSE) {
170 $hasOperation = (bool) $this->getComponent(Operation::ID, FALSE);
171 $this->hasOperation = $useCache ? $hasOperation : NULL;
172 }
173
174 return $hasOperation;
175 }
176
177 178 179 180 181
182 public function hasExport($useCache = TRUE)
183 {
184 $hasExport = $this->hasExport;
185
186 if ($hasExport === NULL || $useCache === FALSE) {
187 $hasExport = (bool) $this->getComponent(Export::ID, FALSE);
188 $this->hasExport = $useCache ? $hasExport : NULL;
189 }
190
191 return $hasExport;
192 }
193
194
195
196 197 198 199 200
201 public function addColumnText($name, $label)
202 {
203 return new Columns\Text($this, $name, $label);
204 }
205
206 207 208
209 public function addColumnMail($name, $label)
210 {
211 trigger_error(__METHOD__ . '() is deprecated; use addColumnEmail() instead.', E_USER_DEPRECATED);
212
213 return $this->addColumnEmail($name, $label);
214 }
215
216 217 218 219 220
221 public function addColumnEmail($name, $label)
222 {
223 return new Columns\Email($this, $name, $label);
224 }
225
226 227 228
229 public function addColumnHref($name, $label)
230 {
231 trigger_error(__METHOD__ . '() is deprecated; use addColumnLink() instead.', E_USER_DEPRECATED);
232
233 return new Columns\Link($this, $name, $label);
234 }
235
236 237 238 239 240
241 public function addColumnLink($name, $label)
242 {
243 return new Columns\Link($this, $name, $label);
244 }
245
246 247 248 249 250 251
252 public function addColumnDate($name, $label, $dateFormat = NULL)
253 {
254 return new Columns\Date($this, $name, $label, $dateFormat);
255 }
256
257 258 259 260 261 262 263 264
265 public function addColumnNumber($name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL)
266 {
267 return new Columns\Number($this, $name, $label, $decimals, $decPoint, $thousandsSep);
268 }
269
270
271
272 273 274 275 276
277 public function addFilterText($name, $label)
278 {
279 return new Filters\Text($this, $name, $label);
280 }
281
282 283 284 285 286
287 public function addFilterDate($name, $label)
288 {
289 return new Filters\Date($this, $name, $label);
290 }
291
292 293 294 295 296
297 public function addFilterDateRange($name, $label)
298 {
299 return new Filters\DateRange($this, $name, $label);
300 }
301
302 303 304 305 306
307 public function addFilterCheck($name, $label)
308 {
309 return new Filters\Check($this, $name, $label);
310 }
311
312 313 314 315 316 317
318 public function addFilterSelect($name, $label, array $items = NULL)
319 {
320 return new Filters\Select($this, $name, $label, $items);
321 }
322
323 324 325 326 327
328 public function addFilterNumber($name, $label)
329 {
330 return new Filters\Number($this, $name, $label);
331 }
332
333 334 335 336 337
338 public function addFilterCustom($name, \Nette\Forms\IControl $formControl)
339 {
340 return new Filters\Custom($this, $name, NULL, $formControl);
341 }
342
343
344
345 346 347 348 349 350 351
352 public function addActionHref($name, $label, $destination = NULL, array $args = NULL)
353 {
354 return new Actions\Href($this, $name, $label, $destination, $args);
355 }
356
357 358 359 360 361 362
363 public function addActionEvent($name, $label, $onClick = NULL)
364 {
365 return new Actions\Event($this, $name, $label, $onClick);
366 }
367
368
369
370 371 372 373 374
375 public function setOperation(array $operations, $onSubmit)
376 {
377 return new Operation($this, $operations, $onSubmit);
378 }
379
380 381 382 383
384 public function setExport($label = NULL)
385 {
386 return new Export($this, $label);
387 }
388
389 390 391 392 393 394
395 public function setEditableColumns($callback = NULL)
396 {
397 $this->onRegistered[] = function(\Grido\Grid $grid) use ($callback) {
398 if (!$grid->hasColumns()) {
399 return;
400 }
401
402 foreach ($grid->getComponent(Column::ID)->getComponents() as $column) {
403 if ($column instanceof Editable && !$column->isEditableDisabled() && !$column->editableCallback) {
404 $column->setEditable($callback);
405 }
406 }
407 };
408
409 return $this;
410 }
411 }
412