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