Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
MultiResourceCRUDController.php
Go to the documentation of this file.
1<?php
2
3
5
6
7use Ajax\php\ubiquity\JsUtils;
8use Ajax\semantic\html\base\constants\Color;
9use Ajax\semantic\html\base\constants\icons\Animals;
10use Ajax\semantic\widgets\datatable\Pagination;
16
23
24 public string $resource = '';
25
26 private $displayedItems = [];
27
28 private $_hasDropdown = false;
29
30 public function initialize() {
31 parent::initialize();
32 $this->model = $this->getModelName();
33 }
34
35 public function home() {
36 $models = $this->getIndexModels();
37 $items = [];
38 $myModels = $this->getIndexModelsDetails();
39 list($mainType, $type) = $this->getIndexType();
40 foreach ($models as $model) {
41 $resource = \lcfirst(ClassUtils::getClassSimpleName($model));
42 $myModel = $myModels[$resource] ?? [];
43 $this->displayedItems[$resource] = $displayedItems = [
44 'title' => $myModel['title'] ?? $this->getIndexDefaultTitle($resource),
45 'desc' => $myModel['desc'] ?? $this->getIndexDefaultDesc($model),
46 'resource' => $resource,
47 'icon' => $myModel['icon'] ?? $this->getIndexDefaultIcon($resource),
48 'url' => $myModel['url'] ?? $this->getIndexDefaultUrl($resource),
49 'meta' => $myModel['meta'] ?? $this->getIndexDefaultMeta($model),
50 'actions' => $myModel['actions'] ?? null,
51 'type' => $type
52 ];
53 $items[$resource] = $this->loadView($this->_getFiles()->getViewItemHome(), $displayedItems, true);
54 }
55
56 $data = ['items' => $items, 'type' => $mainType];
57 if ($this->hasNavigation()) {
58 $data['nav'] = $this->nav($models);
59 }
60 $data['routeNamePrefix'] = $this->getRouteNamePrefix();
61 $this->onRenderView($data);
62 $this->addIndexBehavior();
63 $this->jquery->renderView($this->_getFiles()->getViewHome(), $data);
64 }
65
70 protected function onRenderView(array &$data): void {
71
72 }
73
79 protected function hasNavigation(): bool {
80 return true;
81 }
82
83 protected function getRouteNamePrefix(): string {
84 return '';
85 }
86
87 protected function nav(?array $models = null, string $btIcon = 'chevron right', string $btTitle = 'Navigate to...', bool $asString = true): ?string {
88 $this->_hasDropdown = true;
89 $models ??= $this->getIndexModels();
90 $myModels = $this->getIndexModelsDetails();
91 $items = [];
92 foreach ($models as $model) {
93 $resource = \lcfirst(ClassUtils::getClassSimpleName($model));
94 $items[] = $this->displayedItems[$resource] ?? ['title' => $myModels['title'] ?? $this->getIndexDefaultTitle($resource), 'icon' => $myModels['icon'] ?? $this->getIndexDefaultIcon($resource), 'url' => $myModels['url'] ?? $this->getIndexDefaultUrl($resource)];
95 }
96
97 return $this->loadView($this->_getFiles()->getViewNav(), compact('items', 'btIcon', 'btTitle'), $asString);
98 }
99
100 protected function getIndexModels(): array {
101 return DAO::getModels('default');
102 }
103
104 protected function getIndexModelsDetails(): array {
105 return [];
106 }
107
108 protected function getIndexDefaultIcon(string $resource): string {
109 return ' colored ' . Animals::getRandomValue(true) . ' ' . Color::getRandomValue(true);
110 }
111
112 protected function getIndexDefaultTitle(string $resource): string {
113 return \ucfirst($resource);
114 }
115
116 protected function getIndexDefaultDesc(string $modelClass): string {
117 return $modelClass;
118 }
119
120 protected function getIndexDefaultUrl(string $resource): string {
121 return Router::path($this->getRouteNamePrefix() . 'crud.index', [$resource]);
122 }
123
124 protected function getIndexDefaultMeta(string $modelClass): ?string {
125 return null;
126 }
127
128 protected function addIndexBehavior(): void {
129 $isAjax = URequest::isAjax();
130 if ($this->_hasDropdown) {
131 $this->jquery->execAtLast('$(".dropdown._nav").dropdown();');
132 if (!$isAjax) {
133 $this->jquery->getOnClick('.item[data-href]', '', '.crud', ['hasLoader' => false, 'preventDefault' => false, 'stopPropagation' => false, 'attr' => 'data-href', 'listenerOn' => 'body']);
134 }
135 }
136 if (!$isAjax) {
137 $this->jquery->getHref('a[href]._crud, a._home', "", ['historize' => false, 'hasLoader' => false, 'listenerOn' => 'body']);
138 }
139 }
140
141 protected function getIndexType(): array {
142 return ['four link cards', 'card'];
143 }
144
145 protected function getModelName(): string {
146 return Startup::getNS('models') . \ucfirst($this->resource);
147 }
148
149 public abstract function _getBaseRoute(): string;
150
151 public function showDetail($ids) {
152 $this->detailClick('showModelClick', '.crud', [
153 "attr" => "data-ajax",
154 "hasLoader" => false
155 ]);
156 parent::showDetail($ids);
157
158 }
159
160 public function showModelClick($modelAndId) {
161 $array = \explode("||", $modelAndId);
162 if (\is_array($array)) {
163 $m = $array[0];
164 $this->model = $model = \str_replace('.', '\\', $m);
165 $this->resource = \lcfirst(\substr($m, \strpos($m, ".") + 1));
166 $id = $array[1];
167 $totalCount = DAO::count($model, $this->_getAdminData()->_getInstancesFilter($model));
168 $recordsPerPage = $this->_getModelViewer()->recordsPerPage($model, $totalCount);
169 if (\is_numeric($recordsPerPage)) {
170 if (isset($id)) {
171 $rownum = DAO::getRownum($model, $id);
172 $this->activePage = Pagination::getPageOfRow($rownum, $recordsPerPage);
173 }
174 }
175 $this->jquery->execAtLast("$(\"tr[data-ajax='" . $id . "']\").click();");
176 $this->index();
177 }
178 }
179}
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
Starts the framework.
Definition Startup.php:19
Ubiquity\controllers\crud$CRUDController This class is part of Ubiquity.
loadView(string $viewName, $pData=null, bool $asString=false)
hasNavigation()
To override Return true for adding a navigation dropdown menu.
initialize()
Method called before each action Can be override in derived class.
nav(?array $models=null, string $btIcon='chevron right', string $btTitle='Navigate to...', bool $asString=true)
showDetail($ids)
Shows associated members with foreign keys.
Gateway class between database and object model.
Definition DAO.php:33
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18