1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Ubiquity\controllers\crud; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Ajax\php\ubiquity\JsUtils; |
8
|
|
|
use Ajax\semantic\html\base\constants\Color; |
9
|
|
|
use Ajax\semantic\html\base\constants\icons\Animals; |
10
|
|
|
use Ajax\semantic\widgets\datatable\Pagination; |
11
|
|
|
use Ubiquity\cache\ClassUtils; |
12
|
|
|
use Ubiquity\controllers\Router; |
13
|
|
|
use Ubiquity\controllers\Startup; |
14
|
|
|
use Ubiquity\orm\DAO; |
15
|
|
|
use Ubiquity\utils\http\URequest; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class MultiResourceCRUDController |
19
|
|
|
* @package controllers |
20
|
|
|
* @property JsUtils $jquery |
21
|
|
|
*/ |
22
|
|
|
abstract class MultiResourceCRUDController extends \Ubiquity\controllers\crud\CRUDController { |
23
|
|
|
|
24
|
|
|
public string $resource=''; |
25
|
|
|
|
26
|
|
|
private $displayedItems=[]; |
27
|
|
|
|
28
|
|
|
private $_hasDropdown=false; |
29
|
|
|
|
30
|
2 |
|
public function initialize() { |
31
|
2 |
|
parent::initialize(); |
32
|
2 |
|
$this->model = $this->getModelName(); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function home(){ |
36
|
2 |
|
$models=$this->getIndexModels(); |
37
|
2 |
|
$items=[]; |
38
|
2 |
|
$myModels=$this->getIndexModelsDetails(); |
39
|
2 |
|
list($mainType,$type)=$this->getIndexType(); |
40
|
2 |
|
foreach ($models as $model){ |
41
|
2 |
|
$resource=\lcfirst(ClassUtils::getClassSimpleName($model)); |
42
|
2 |
|
$myModel=$myModels[$resource]??[]; |
43
|
2 |
|
$this->displayedItems[$resource]=$displayedItems=[ |
44
|
2 |
|
'title'=>$myModel['title']??$this->getIndexDefaultTitle($resource), |
45
|
2 |
|
'desc'=>$myModel['desc']??$this->getIndexDefaultDesc($model), |
46
|
|
|
'resource'=>$resource, |
47
|
2 |
|
'icon'=>$myModel['icon']??$this->getIndexDefaultIcon($resource), |
48
|
2 |
|
'url'=>$myModel['url']??$this->getIndexDefaultUrl($resource), |
49
|
2 |
|
'meta'=>$myModel['meta']??$this->getIndexDefaultMeta($model), |
50
|
2 |
|
'actions'=>$myModel['actions']??null, |
51
|
|
|
'type'=>$type |
52
|
|
|
]; |
53
|
2 |
|
$items[$resource]=$this->loadView($this->_getFiles()->getViewItemHome(),$displayedItems,true); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$data=['items'=>$items,'type'=>$mainType]; |
57
|
2 |
|
if($this->hasNavigation()){ |
58
|
2 |
|
$data['nav']=$this->nav($models); |
59
|
|
|
} |
60
|
2 |
|
$data['routeNamePrefix']=$this->getRouteNamePrefix(); |
61
|
2 |
|
$this->onRenderView($data); |
62
|
2 |
|
$this->addIndexBehavior(); |
63
|
2 |
|
$this->jquery->renderView($this->_getFiles()->getViewHome(),$data); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* To override |
68
|
|
|
* @param array $data |
69
|
|
|
*/ |
70
|
|
|
protected function onRenderView(array &$data):void{ |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* To override |
76
|
|
|
* Return true for adding a navigation dropdown menu. |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
2 |
|
protected function hasNavigation():bool{ |
80
|
2 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
protected function getRouteNamePrefix():string { |
84
|
2 |
|
return ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
protected function nav(?array $models=null,string $btIcon='chevron right',string $btTitle='Navigate to...',bool $asString=true):?string{ |
88
|
2 |
|
$this->_hasDropdown=true; |
89
|
2 |
|
$models??=$this->getIndexModels(); |
90
|
2 |
|
$myModels=$this->getIndexModelsDetails(); |
91
|
2 |
|
$items=[]; |
92
|
2 |
|
foreach ($models as $model){ |
93
|
2 |
|
$resource=\lcfirst(ClassUtils::getClassSimpleName($model)); |
94
|
2 |
|
$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
|
2 |
|
return $this->loadView($this->_getFiles()->getViewNav(),compact('items','btIcon','btTitle'),$asString); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
protected function getIndexModels():array{ |
101
|
2 |
|
return DAO::getModels('default'); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
protected function getIndexModelsDetails():array{ |
105
|
2 |
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
protected function getIndexDefaultIcon(string $resource): string { |
109
|
2 |
|
return ' colored '.Animals::getRandomValue(true).' '.Color::getRandomValue(true); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
protected function getIndexDefaultTitle(string $resource):string{ |
113
|
2 |
|
return \ucfirst($resource); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
protected function getIndexDefaultDesc(string $modelClass):string{ |
117
|
2 |
|
return $modelClass; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
protected function getIndexDefaultUrl(string $resource):string{ |
121
|
2 |
|
return Router::path($this->getRouteNamePrefix().'crud.index',[$resource]); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
protected function getIndexDefaultMeta(string $modelClass):?string{ |
125
|
2 |
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
protected function addIndexBehavior():void{ |
129
|
2 |
|
$isAjax=URequest::isAjax(); |
130
|
2 |
|
if($this->_hasDropdown){ |
131
|
2 |
|
$this->jquery->execAtLast('$(".dropdown._crud").dropdown();'); |
132
|
2 |
|
if(!$isAjax) { |
133
|
2 |
|
$this->jquery->getOnClick('.item[data-href]', '', '.crud', ['hasLoader' => false, 'preventDefault' => false, 'stopPropagation' => false, 'attr' => 'data-href','listenerOn'=>'body']); |
134
|
|
|
} |
135
|
|
|
} |
136
|
2 |
|
if(!$isAjax) { |
137
|
2 |
|
$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
|
2 |
|
protected function getModelName(): string { |
146
|
2 |
|
return Startup::getNS('models') . \ucfirst($this->resource); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public abstract function _getBaseRoute():string; |
150
|
|
|
|
151
|
1 |
|
public function showDetail($ids) { |
152
|
1 |
|
$this->detailClick('showModelClick','.crud',[ |
153
|
|
|
"attr" => "data-ajax", |
154
|
|
|
"hasLoader" => false |
155
|
|
|
]); |
156
|
1 |
|
parent::showDetail($ids); |
157
|
|
|
|
158
|
|
|
} |
159
|
1 |
|
public function showModelClick($modelAndId){ |
160
|
1 |
|
$array = \explode("||", $modelAndId); |
161
|
1 |
|
if (\is_array($array)) { |
162
|
1 |
|
$m=$array[0]; |
163
|
1 |
|
$this->model = $model = \str_replace('.', '\\',$m); |
164
|
1 |
|
$this->resource=\lcfirst(\substr($m, \strpos($m, ".") + 1)); |
165
|
1 |
|
$id = $array[1]; |
166
|
1 |
|
$totalCount = DAO::count($model, $this->_getAdminData()->_getInstancesFilter($model)); |
167
|
1 |
|
$recordsPerPage = $this->_getModelViewer()->recordsPerPage($model, $totalCount); |
168
|
1 |
|
if (\is_numeric($recordsPerPage)) { |
169
|
1 |
|
if (isset($id)) { |
170
|
1 |
|
$rownum = DAO::getRownum($model, $id); |
171
|
1 |
|
$this->activePage = Pagination::getPageOfRow($rownum, $recordsPerPage); |
172
|
|
|
} |
173
|
|
|
} |
174
|
1 |
|
$this->jquery->execAtLast("$(\"tr[data-ajax='" . $id . "']\").click();"); |
175
|
1 |
|
$this->index(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|