Passed
Push — master ( b97787...c0f601 )
by Jean-Christophe
16:10
created

MultiResourceCRUDController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 97.67%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 24
eloc 69
c 2
b 0
f 2
dl 0
loc 144
ccs 84
cts 86
cp 0.9767
rs 10

17 Methods

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