Total Complexity | 102 |
Total Lines | 1051 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 0 | Features | 1 |
Complex classes like app_CtrlRecord often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use app_CtrlRecord, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class app_CtrlRecord extends app_Controller |
||
29 | { |
||
30 | /** |
||
31 | * This methods returns the Record classname managed by this controller. |
||
32 | * The default method guess the record classname based on the controller name. |
||
33 | * It can be overriden by inherited controllers. |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | protected function getRecordClassName() |
||
38 | { |
||
39 | list(, $recordClassname) = explode('_Ctrl', $this->getClass()); |
||
40 | return $recordClassname; |
||
41 | } |
||
42 | |||
43 | |||
44 | /** |
||
45 | * This methods returns the Record set managed by this controller. |
||
46 | * The default method guess the record set based on the controller name. |
||
47 | * It can be overriden by inherited controllers. |
||
48 | * |
||
49 | * @return app_RecordSet |
||
50 | */ |
||
51 | protected function getRecordSet() |
||
52 | { |
||
53 | $App = $this->App(); |
||
54 | $recordClassname = $this->getRecordClassName(); |
||
55 | $recordSetClassname = $recordClassname . 'Set'; |
||
56 | |||
57 | $recordSet = $App->$recordSetClassname(); |
||
58 | return $recordSet; |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * @return app_RecordSet |
||
64 | */ |
||
65 | protected function getEditRecordSet() |
||
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * This methods returns the Record set used to save records |
||
73 | * |
||
74 | * @return app_RecordSet |
||
75 | */ |
||
76 | protected function getSaveRecordSet() |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * This methods returns the Record set used to delete records |
||
83 | * |
||
84 | * @return app_RecordSet |
||
85 | */ |
||
86 | protected function getDeleteRecordSet() |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * |
||
94 | * @param Widget_Action|Widget_Link|Widget_Action[]|Widget_Link[]|array $actions |
||
95 | * @param Widget_Item $box |
||
96 | * @return string |
||
97 | */ |
||
98 | protected function createMenu($actions, $icon = true, $showLabel = false) |
||
132 | } |
||
133 | |||
134 | |||
135 | /** |
||
136 | * |
||
137 | * @return string[] |
||
138 | */ |
||
139 | protected function getAvailableModelViewTypes() |
||
140 | { |
||
141 | $App = $this->App(); |
||
142 | $Ui = $App->Ui(); |
||
143 | |||
144 | $types = array(); |
||
145 | |||
146 | $recordClassname = $this->getRecordClassName(); |
||
147 | |||
148 | $viewClassname = $recordClassname . 'TableView'; |
||
149 | if (method_exists($Ui, $viewClassname)) { |
||
150 | $types['table'] = array( |
||
151 | 'classname' => $viewClassname, |
||
152 | 'icon' => Func_Icons::ACTIONS_VIEW_LIST_TEXT, |
||
153 | 'label' => $App->translate('Detailed list') |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | $viewClassname = $recordClassname . 'CardsView'; |
||
158 | if (method_exists($Ui, $viewClassname)) { |
||
159 | $types['cards'] = array( |
||
160 | 'classname' => $viewClassname, |
||
161 | 'icon' => 'actions-view-list-cards', //Func_Icons::ACTIONS_VIEW_LIST_CARDS |
||
162 | 'label' => $App->translate('Cards') |
||
163 | ); |
||
164 | } |
||
165 | |||
166 | $viewClassname = $recordClassname . 'MapView'; |
||
167 | if (method_exists($Ui, $viewClassname)) { |
||
168 | $types['map'] = array( |
||
169 | 'classname' => $viewClassname, |
||
170 | 'icon' => Func_Icons::APPS_PREFERENCES_SITE, |
||
171 | 'label' => $App->translate('Map') |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | $viewClassname = $recordClassname . 'CalendarView'; |
||
176 | if (method_exists($Ui, $viewClassname)) { |
||
177 | $types['calendar'] = array( |
||
178 | 'classname' => $viewClassname, |
||
179 | 'icon' => Func_Icons::APPS_CALENDAR, |
||
180 | 'label' => $App->translate('Calendar') |
||
181 | ); |
||
182 | } |
||
183 | |||
184 | return $types; |
||
185 | } |
||
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * Returns an array of field names and descriptions that can be used in Full/CardFrames. |
||
191 | * |
||
192 | * @return string[] |
||
193 | */ |
||
194 | public function getAvailableDisplayFields() |
||
217 | } |
||
218 | |||
219 | |||
220 | |||
221 | /** |
||
222 | * Displays an editor of the record values corresponding to the fields of a custom section. |
||
223 | * |
||
224 | * @param int $id The id of the record to edit |
||
225 | * @param int $customSectionId The id of the section used to create the editor |
||
226 | * @param string $itemId |
||
227 | * |
||
228 | * @throws app_AccessException |
||
229 | * @return app_Page |
||
230 | */ |
||
231 | public function editSection($id, $customSectionId, $itemId = null) |
||
232 | { |
||
233 | $W = bab_Widgets(); |
||
234 | $App = $this->App(); |
||
235 | $Ui = $App->Ui(); |
||
236 | |||
237 | $page = $App->Ui()->Page(); |
||
238 | |||
239 | $customSectionSet = $App->CustomSectionSet(); |
||
240 | $customSection = $customSectionSet->get( |
||
241 | $customSectionSet->id->is($customSectionId) |
||
242 | ); |
||
243 | |||
244 | $page->setTitle($customSection->name); |
||
245 | |||
246 | $recordSet = $this->getEditRecordSet(); |
||
247 | |||
248 | $recordClassname = $this->getRecordClassName(); |
||
249 | $editorClassname = $recordClassname . 'SectionEditor'; |
||
250 | /* @var $editor app_RecordEditor */ |
||
251 | $editor = $Ui->$editorClassname(); |
||
252 | if (!isset($itemId)) { |
||
253 | $itemId = $this->getClass() . '_' . __FUNCTION__; |
||
254 | } |
||
255 | $editor->setId($itemId); |
||
256 | $editor->setHiddenValue('tg', $App->controllerTg); |
||
257 | $editor->setSaveAction($this->proxy()->save()); |
||
258 | $editor->setName('data'); |
||
259 | $editor->addItem($W->Hidden()->setName('id')); |
||
260 | |||
261 | $editor->recordSet = $recordSet; |
||
262 | $section = $editor->sectionContent($customSectionId); |
||
263 | $section->setSizePolicy('widget-60em'); |
||
264 | |||
265 | $editor->addItem($section); |
||
266 | |||
267 | if (isset($id)) { |
||
268 | $record = $recordSet->request($id); |
||
269 | if (!$record->isUpdatable()) { |
||
270 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
271 | } |
||
272 | $editor->setRecord($record); |
||
273 | } else { |
||
274 | if (!$recordSet->isCreatable()) { |
||
275 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
276 | } |
||
277 | $record = $recordSet->newRecord(); |
||
278 | $editor->setRecord($record); |
||
279 | } |
||
280 | |||
281 | $editor->isAjax = bab_isAjaxRequest(); |
||
282 | |||
283 | $page->addItem($editor); |
||
284 | |||
285 | return $page; |
||
286 | } |
||
287 | |||
288 | |||
289 | /** |
||
290 | * @param string $itemId |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getModelViewDefaultId($itemId = null) |
||
294 | { |
||
295 | if (!isset($itemId)) { |
||
296 | $itemId = $this->getClass() . '_modelView'; |
||
297 | } |
||
298 | |||
299 | return $itemId; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Returns the xxxModelView associated to the RecordSet. |
||
304 | * |
||
305 | * @param array|null $filter |
||
306 | * @param string $type |
||
307 | * @param array|null $columns Optional list of columns. array($columnPath] => '1' | '0'). |
||
308 | * @param string|null $itemId Widget item id |
||
309 | * @return app_TableModelView |
||
310 | */ |
||
311 | protected function modelView($filter = null, $type = null, $columns = null, $itemId = null) |
||
312 | { |
||
313 | $App = $this->App(); |
||
314 | $Ui = $App->Ui(); |
||
315 | |||
316 | $recordSet = $this->getRecordSet(); |
||
317 | |||
318 | $recordClassname = $this->getRecordClassName(); |
||
319 | |||
320 | $itemId = $this->getModelViewDefaultId($itemId); |
||
321 | |||
322 | if (!isset($type)) { |
||
323 | $type = $this->getFilteredViewType($itemId); |
||
324 | } |
||
325 | |||
326 | //$types = $this->getAvailableModelViewTypes(); |
||
327 | |||
328 | switch ($type) { |
||
329 | case 'cards': |
||
330 | $tableviewClassname = $recordClassname . 'CardsView'; |
||
331 | break; |
||
332 | |||
333 | case 'map': |
||
334 | $tableviewClassname = $recordClassname . 'MapView'; |
||
335 | break; |
||
336 | |||
337 | case 'calendar': |
||
338 | $tableviewClassname = $recordClassname . 'CalendarView'; |
||
339 | break; |
||
340 | |||
341 | case 'table': |
||
342 | default: |
||
343 | $tableviewClassname = $recordClassname . 'TableView'; |
||
344 | break; |
||
345 | } |
||
346 | |||
347 | |||
348 | /* @var $tableview widget_TableModelView */ |
||
349 | $tableview = $Ui->$tableviewClassname(); |
||
350 | |||
351 | $tableview->setRecordController($this); |
||
352 | |||
353 | $tableview->setId($itemId); |
||
354 | |||
355 | $tableview->setRecordSet($recordSet); |
||
356 | $tableview->addDefaultColumns($recordSet); |
||
357 | if (isset($filter)) { |
||
358 | $tableview->setFilterValues($filter); |
||
359 | } |
||
360 | $filter = $tableview->getFilterValues(); |
||
361 | |||
362 | if (isset($filter['showTotal']) && $filter['showTotal']) { |
||
363 | $tableview->displaySubTotalRow(true); |
||
364 | } |
||
365 | |||
366 | $conditions = $tableview->getFilterCriteria($filter); |
||
367 | |||
368 | $conditions = $conditions->_AND_( |
||
369 | $recordSet->isReadable() |
||
370 | ); |
||
371 | |||
372 | $records = $recordSet->select($conditions); |
||
373 | |||
374 | |||
375 | $tableview->setDataSource($records); |
||
376 | |||
377 | |||
378 | |||
379 | if (isset($columns)) { |
||
380 | $availableColumns = $tableview->getVisibleColumns(); |
||
381 | |||
382 | $remainingColumns = array(); |
||
383 | foreach ($availableColumns as $availableColumn) { |
||
384 | $colPath = $availableColumn->getFieldPath(); |
||
385 | if (isset($columns[$colPath]) && $columns[$colPath] == '1') { |
||
386 | $remainingColumns[] = $availableColumn; |
||
387 | } |
||
388 | } |
||
389 | $tableview->setColumns($remainingColumns); |
||
390 | } |
||
391 | |||
392 | $tableview->allowColumnSelection(); |
||
393 | |||
394 | return $tableview; |
||
395 | } |
||
396 | |||
397 | |||
398 | |||
399 | |||
400 | /** |
||
401 | * @return app_Editor |
||
402 | */ |
||
403 | protected function recordEditor($itemId = null) |
||
404 | { |
||
405 | $App = $this->App(); |
||
406 | $Ui = $App->Ui(); |
||
407 | |||
408 | $recordClassname = $this->getRecordClassName(); |
||
409 | $editorClassname = $recordClassname . 'Editor'; |
||
410 | $editor = $Ui->$editorClassname(); |
||
411 | if (!isset($itemId)) { |
||
412 | $itemId = $this->getClass() . '_' . __FUNCTION__; |
||
413 | } |
||
414 | $editor->setId($itemId); |
||
415 | $editor->setHiddenValue('tg', $App->controllerTg); |
||
416 | $editor->setSaveAction($this->proxy()->save()); |
||
417 | $editor->setName('data'); |
||
418 | |||
419 | $editor->isAjax = bab_isAjaxRequest(); |
||
420 | |||
421 | return $editor; |
||
422 | } |
||
423 | |||
424 | |||
425 | |||
426 | /** |
||
427 | * @param widget_TableModelView $tableView |
||
428 | * @return app_Toolbar |
||
429 | */ |
||
430 | protected function toolbar($tableView) |
||
431 | { |
||
432 | $W = bab_Widgets(); |
||
433 | $App = $this->App(); |
||
434 | |||
435 | $proxy = $this->proxy(); |
||
436 | |||
437 | $filter = $tableView->getFilterValues(); |
||
438 | |||
439 | $toolbar = $W->FlowItems(); |
||
440 | $toolbar->addClass('widget-toolbar', Func_Icons::ICON_LEFT_16); |
||
441 | |||
442 | $viewTypes = $this->getAvailableModelViewTypes(); |
||
443 | |||
444 | if (count($viewTypes) > 1) { |
||
445 | $viewsBox = $W->Items(); |
||
446 | $viewsBox->setSizePolicy('pull-right'); |
||
447 | // $viewsBox->addClass('btn-group', Func_Icons::ICON_LEFT_16); |
||
448 | $toolbar->addItem($viewsBox); |
||
449 | |||
450 | $filteredViewType = $this->getFilteredViewType($tableView->getId()); |
||
451 | |||
452 | foreach ($viewTypes as $viewTypeId => $viewType) { |
||
453 | $viewsBox->addItem( |
||
454 | $W->Link( |
||
455 | '', |
||
456 | $proxy->setFilteredViewType($tableView->getId(), $viewTypeId) |
||
457 | )->addClass('icon', $viewType['icon'] . ($filteredViewType=== $viewTypeId ? ' active' : '')) |
||
458 | // ->setSizePolicy('btn btn-xs btn-default ' . ($filteredViewType === $viewTypeId ? 'active' : '')) |
||
459 | ->setTitle($viewType['label']) |
||
460 | ->setAjaxAction() |
||
461 | ); |
||
462 | } |
||
463 | } |
||
464 | |||
465 | if (method_exists($proxy, 'exportSelect')) { |
||
466 | $toolbar->addItem( |
||
467 | $W->Link( |
||
468 | $App->translate('Export'), |
||
469 | $proxy->exportSelect($filter) |
||
470 | )->addClass('icon', Func_Icons::ACTIONS_DOCUMENT_DOWNLOAD) |
||
471 | ->setOpenMode(Widget_Link::OPEN_DIALOG) |
||
472 | ); |
||
473 | } |
||
474 | |||
475 | if (method_exists($proxy, 'printList')) { |
||
476 | $toolbar->addItem( |
||
477 | $W->Link( |
||
478 | $App->translate('Print'), |
||
479 | $proxy->printList($filter) |
||
480 | )->addClass('icon', Func_Icons::ACTIONS_DOCUMENT_PRINT) |
||
481 | ->setOpenMode(Widget_Link::OPEN_POPUP) |
||
482 | ); |
||
483 | } |
||
484 | |||
485 | return $toolbar; |
||
486 | } |
||
487 | |||
488 | |||
489 | |||
490 | |||
491 | /** |
||
492 | * @param string $itemId |
||
493 | * @return boolean |
||
494 | */ |
||
495 | public function toggleFilterVisibility($itemId) |
||
496 | { |
||
497 | $W = bab_Widgets(); |
||
498 | |||
499 | $filterVisibility = $this->getFilterVisibility($itemId); |
||
500 | $filterVisibility = !$filterVisibility; |
||
501 | $W->setUserConfiguration($itemId . '/filterVisibility', $filterVisibility); |
||
502 | |||
503 | return true; |
||
504 | } |
||
505 | |||
506 | |||
507 | /** |
||
508 | * @param string $itemId |
||
509 | * @return boolean |
||
510 | */ |
||
511 | protected function getFilterVisibility($itemId) |
||
512 | { |
||
513 | $W = bab_Widgets(); |
||
514 | $filterVisibility = $W->getUserConfiguration($itemId . '/filterVisibility'); |
||
515 | if (!isset($filterVisibility)) { |
||
516 | $filterVisibility = false; |
||
517 | $W->setUserConfiguration($itemId . '/filterVisibility', $filterVisibility); |
||
518 | } |
||
519 | return $filterVisibility; |
||
520 | } |
||
521 | |||
522 | |||
523 | /** |
||
524 | * Returns the current filtered view type (table, cards...) |
||
525 | * @param string $itemId The model view widget id |
||
526 | * @return string |
||
527 | */ |
||
528 | protected function getFilteredViewType($itemId) |
||
529 | { |
||
530 | $W = bab_Widgets(); |
||
531 | $type = $W->getUserConfiguration($itemId . '/viewType'); |
||
532 | if (!isset($type)) { |
||
533 | $type = 'table'; |
||
534 | } |
||
535 | return $type; |
||
536 | } |
||
537 | |||
538 | |||
539 | /** |
||
540 | * Sets the current filtered view type (table, cards...) |
||
541 | * @param string $itemId The model view widget id |
||
542 | * @param string $type 'table, 'cards'... |
||
543 | */ |
||
544 | public function setFilteredViewType($itemId, $type = null) |
||
545 | { |
||
546 | $W = bab_Widgets(); |
||
547 | $W->setUserConfiguration($itemId . '/viewType', $type); |
||
548 | |||
549 | return true; |
||
550 | } |
||
551 | |||
552 | |||
553 | /** |
||
554 | * @param array $filter |
||
555 | * @param string $type |
||
556 | * @param string $itemId |
||
557 | */ |
||
558 | public function filteredView($filter = null, $type = null, $itemId = null) |
||
559 | { |
||
560 | $W = bab_Widgets(); |
||
561 | |||
562 | $view = $this->modelView($filter, $type, null, $itemId); |
||
563 | $view->setAjaxAction(); |
||
564 | |||
565 | $filter = $view->getFilterValues(); |
||
566 | |||
567 | $filterPanel = $view->advancedFilterPanel($filter); |
||
568 | |||
569 | if ($this->getFilterVisibility($itemId)) { |
||
570 | $filterPanel->addClass('show-filter'); |
||
571 | } else { |
||
572 | $filterPanel->addClass('hide-filter'); |
||
573 | } |
||
574 | $toolbar = $this->toolbar($view); |
||
575 | |||
576 | $box = $W->VBoxItems( |
||
577 | $toolbar, |
||
578 | $filterPanel |
||
579 | ); |
||
580 | $box->setReloadAction($this->proxy()->filteredView(null, null, $view->getId())); |
||
581 | |||
582 | return $box; |
||
583 | } |
||
584 | |||
585 | |||
586 | |||
587 | /** |
||
588 | * @param array $filter |
||
589 | * @param string $type |
||
590 | * @return array |
||
591 | */ |
||
592 | protected function getListItemMenus($filter = null, $type = null) |
||
612 | } |
||
613 | |||
614 | |||
615 | |||
616 | /** |
||
617 | * @param array $filter |
||
618 | * @param string $type |
||
619 | * @return Widget_Page |
||
620 | */ |
||
621 | public function displayList($filter = null, $type = null) |
||
622 | { |
||
623 | $W = bab_Widgets(); |
||
624 | |||
625 | $page = $W->BabPage(); |
||
626 | $page->addClass('app-page-list'); |
||
627 | |||
628 | $itemMenus = $this->getListItemMenus(); |
||
629 | |||
630 | foreach ($itemMenus as $itemMenuId => $itemMenu) { |
||
631 | $page->addItemMenu($itemMenuId, $itemMenu['label'], $itemMenu['action']); |
||
632 | } |
||
633 | |||
634 | $filteredView = $this->filteredView($filter, $type); |
||
635 | |||
636 | $page->addItem($filteredView); |
||
637 | |||
638 | return $page; |
||
639 | } |
||
640 | |||
641 | |||
642 | /** |
||
643 | * |
||
644 | * @param array $filter |
||
645 | * @return Widget_Page |
||
646 | */ |
||
647 | public function exportSelect($filter = null) |
||
648 | { |
||
649 | $App = $this->App(); |
||
650 | $Ui = $App->Ui(); |
||
651 | |||
652 | $page = $App->Ui()->Page(); |
||
653 | $page->addClass('app-page-editor'); |
||
654 | |||
655 | $page->setTitle($App->translate('Export list')); |
||
656 | |||
657 | $tableview = $this->modelView($filter, 'table'); |
||
658 | |||
659 | $editor = $Ui->ExportSelectEditor(__METHOD__, $tableview, $filter); |
||
660 | |||
661 | $editor->setSaveAction( |
||
662 | $this->proxy()->exportList(), |
||
663 | $App->translate('Export') |
||
664 | ); |
||
665 | $page->addItem($editor); |
||
666 | |||
667 | return $page; |
||
668 | } |
||
669 | |||
670 | |||
671 | |||
672 | |||
673 | |||
674 | /** |
||
675 | * @param array $filter |
||
676 | */ |
||
677 | public function exportList($filter = null, $format = 'csv', $columns = null, $inline = true, $filename = 'export') |
||
678 | { |
||
679 | $App = $this->App(); |
||
680 | |||
681 | $page = $App->Ui()->Page(); |
||
682 | $page->addClass('app-page-list'); |
||
683 | |||
684 | $tableview = $this->modelView($filter, 'table', $columns); |
||
685 | $tableview->allowColumnSelection(false); |
||
686 | |||
687 | switch ($format) { |
||
688 | case 'xlsx': |
||
689 | $tableview->downloadXlsx($filename . '.xlsx'); |
||
690 | break; |
||
691 | case 'xls': |
||
692 | $tableview->downloadExcel($filename . '.xls'); |
||
693 | break; |
||
694 | case 'ssv': |
||
695 | $tableview->downloadCsv($filename . '.csv', ';', $inline, 'Windows-1252'); |
||
696 | break; |
||
697 | case 'csv': |
||
698 | default: |
||
699 | $tableview->downloadCsv($filename . '.csv', ',', $inline, bab_charset::getIso()); |
||
700 | break; |
||
701 | } |
||
702 | } |
||
703 | |||
704 | |||
705 | /** |
||
706 | * Returns a page with the record information. |
||
707 | * |
||
708 | * @param int $id |
||
709 | * @return app_Page |
||
710 | */ |
||
711 | public function display($id, $view = '') |
||
712 | { |
||
713 | $App = $this->App(); |
||
714 | $page = $App->Ui()->Page(); |
||
715 | |||
716 | $recordSet = $this->getRecordSet(); |
||
717 | |||
718 | $record = $recordSet->request($id); |
||
719 | |||
720 | if (!$record->isReadable()) { |
||
721 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
722 | } |
||
723 | |||
724 | $W = bab_Widgets(); |
||
725 | $Ui = $App->Ui(); |
||
726 | |||
727 | $recordClassname = $this->getRecordClassName(); |
||
728 | $fullFrameClassname = $recordClassname . 'FullFrame'; |
||
729 | $fullFrame = $Ui->$fullFrameClassname($record, $view); |
||
730 | |||
731 | |||
732 | $maxHistory = 8; |
||
733 | $historyFrame = $W->VBoxItems( |
||
734 | $App->Controller()->Search(false)->history($record->getRef(), $maxHistory, 0, true) |
||
735 | ); |
||
736 | $fullFrame->addItem( |
||
737 | $W->Section( |
||
738 | $App->translate('History'), |
||
739 | $historyFrame, |
||
740 | 2 |
||
741 | )->setFoldable(true) |
||
742 | ->addClass('compact') |
||
743 | ); |
||
744 | |||
745 | $page->addItem($fullFrame); |
||
746 | |||
747 | |||
748 | // Actions |
||
749 | $page->addContextItem($this->getDisplayActionFrame($page, $record)); |
||
750 | |||
751 | return $page; |
||
752 | } |
||
753 | |||
754 | |||
755 | |||
756 | protected function getDisplayActionFrame(Widget_Page $page, app_Record $record) |
||
757 | { |
||
758 | $actionsFrame = $page->ActionsFrame(); |
||
759 | return $actionsFrame; |
||
760 | } |
||
761 | |||
762 | |||
763 | |||
764 | /** |
||
765 | * Returns a page containing an editor for the record. |
||
766 | * |
||
767 | * @param string|null id A record id or null for a new record editor. |
||
768 | * @return Widget_Page |
||
769 | */ |
||
770 | public function edit($id = null, $view = '') |
||
771 | { |
||
772 | $W = bab_Widgets(); |
||
773 | $App = $this->App(); |
||
774 | |||
775 | $recordSet = $this->getEditRecordSet(); |
||
776 | |||
777 | $page = $W->BabPage(); |
||
778 | $page->addClass('app-page-editor'); |
||
779 | |||
780 | $page->setTitle($App->translate($recordSet->getDescription())); |
||
781 | |||
782 | $editor = $this->recordEditor(); |
||
783 | |||
784 | if (isset($id)) { |
||
785 | $record = $recordSet->request($id); |
||
786 | if (!$record->isUpdatable()) { |
||
787 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
788 | } |
||
789 | } else { |
||
790 | if (!$recordSet->isCreatable()) { |
||
791 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
792 | } |
||
793 | $record = $recordSet->newRecord(); |
||
794 | } |
||
795 | $editor->setRecord($record); |
||
796 | |||
797 | $page->addItem($editor); |
||
798 | |||
799 | return $page; |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * Returns a page containing an editor for the tags record. |
||
804 | * |
||
805 | * @param string|null id A record id or null for a new record editor. |
||
806 | * @return Widget_Page |
||
807 | */ |
||
808 | public function editTags($id = null, $view = '') |
||
809 | { |
||
810 | $W = bab_Widgets(); |
||
811 | $App = $this->App(); |
||
812 | $Ui = $App->Ui(); |
||
813 | |||
814 | $recordSet = $this->getEditRecordSet(); |
||
815 | |||
816 | $page = $W->BabPage(); |
||
817 | $page->addClass('app-page-editor'); |
||
818 | |||
819 | $page->setTitle($App->translate($recordSet->getDescription())); |
||
820 | |||
821 | $editor = $Ui->TagsEditor(); |
||
822 | |||
823 | if (isset($id)) { |
||
824 | $record = $recordSet->request($id); |
||
825 | if (!$record->isUpdatable()) { |
||
826 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
827 | } |
||
828 | $editor->setRecord($record); |
||
829 | } else { |
||
830 | if (!$recordSet->isCreatable()) { |
||
831 | throw new app_AccessException($App->translate('You do not have access to this page.')); |
||
832 | } |
||
833 | $record = $recordSet->newRecord(); |
||
834 | $editor->setRecord($record); |
||
835 | } |
||
836 | |||
837 | $page->addItem($editor); |
||
838 | |||
839 | return $page; |
||
840 | } |
||
841 | |||
842 | |||
843 | public function ok() |
||
846 | } |
||
847 | |||
848 | |||
849 | |||
850 | /** |
||
851 | * |
||
852 | * @param $data |
||
853 | */ |
||
854 | public function create($data) |
||
855 | { |
||
856 | |||
857 | } |
||
858 | |||
859 | |||
860 | /** |
||
861 | * |
||
862 | * @param $data |
||
863 | */ |
||
864 | public function update($data) |
||
865 | { |
||
866 | |||
867 | } |
||
868 | |||
869 | |||
870 | |||
871 | /** |
||
872 | * |
||
873 | */ |
||
874 | public function read($id) |
||
875 | { |
||
876 | |||
877 | } |
||
878 | |||
879 | |||
880 | /** |
||
881 | * Get the message to display on delete next page |
||
882 | * @return string | true |
||
883 | */ |
||
884 | protected function getDeletedMessage(ORM_Record $record) |
||
885 | { |
||
886 | $App = $this->App(); |
||
887 | $recordSet = $this->getRecordSet(); |
||
888 | |||
889 | $recordTitle = $record->getRecordTitle(); |
||
890 | |||
891 | $message = sprintf($App->translate('%s has been deleted'), $App->translate($recordSet->getDescription()) . ' "' . $recordTitle . '"'); |
||
892 | return $message; |
||
893 | } |
||
894 | |||
895 | /** |
||
896 | * Get the message to display on create next page |
||
897 | * @return string | true |
||
898 | */ |
||
899 | protected function getCreatedMessage() |
||
900 | { |
||
901 | return true; |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * Get the message to display on modification next page |
||
906 | * @param app_Record $record The record before the modification |
||
907 | * @return string | true |
||
908 | */ |
||
909 | protected function getModifedMessage(ORM_Record $record) |
||
910 | { |
||
911 | return true; |
||
912 | } |
||
913 | |||
914 | |||
915 | /** |
||
916 | * Method to check reord before saving |
||
917 | * @param app_Record $record |
||
918 | */ |
||
919 | protected function preSave(ORM_Record $record, $data) |
||
921 | |||
922 | } |
||
923 | |||
924 | |||
925 | /** |
||
926 | * Method for post save actions on record |
||
927 | * @param app_Record $record |
||
928 | */ |
||
929 | protected function postSave(ORM_Record $record, $data) |
||
930 | { |
||
931 | |||
932 | } |
||
933 | |||
934 | |||
935 | /** |
||
936 | * Save a record |
||
937 | * |
||
938 | * @requireSaveMethod |
||
939 | * |
||
940 | * @param array $data |
||
941 | */ |
||
942 | public function save($data = null) |
||
995 | } |
||
996 | |||
997 | |||
998 | /** |
||
999 | * Deletes the specified record. |
||
1000 | * |
||
1001 | * @requireDeleteMethod |
||
1002 | * |
||
1003 | * @param string $id The record id |
||
1004 | * @return boolean |
||
1005 | */ |
||
1006 | public function delete($id) |
||
1007 | { |
||
1008 | $this->requireDeleteMethod(); |
||
1009 | $recordSet = $this->getDeleteRecordSet(); |
||
1010 | |||
1011 | $record = $recordSet->request($id); |
||
1012 | |||
1013 | if (!$record->isDeletable()) { |
||
1014 | throw new app_AccessException('Sorry, You are not allowed to perform this operation'); |
||
1015 | } |
||
1016 | $deletedMessage = $this->getDeletedMessage($record); |
||
1017 | |||
1018 | if ($record->delete()) { |
||
1019 | $this->addMessage($deletedMessage); |
||
1020 | } |
||
1021 | |||
1022 | $this->addReloadSelector('.depends-' . strtolower($record->getClassName())); |
||
1023 | |||
1024 | return true; |
||
1025 | } |
||
1026 | |||
1027 | |||
1028 | /** |
||
1029 | * Displays a page asking to confirm the deletion of a record. |
||
1030 | * |
||
1031 | * @param int $id |
||
1032 | */ |
||
1033 | public function confirmDelete($id) |
||
1074 | } |
||
1075 | |||
1076 | protected function getClass() |
||
1081 |