Passed
Push — master ( 213516...f477f5 )
by Jean-Christophe
04:14
created

CRUDControllerUtilitiesTrait::getAdminData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\controllers\crud;
4
5
use Ubiquity\utils\http\URequest;
6
use Ubiquity\orm\DAO;
7
use Ubiquity\controllers\admin\viewers\ModelViewer;
8
use Ajax\semantic\widgets\datatable\Pagination;
9
use Ajax\common\html\HtmlContentOnly;
10
use Ubiquity\orm\OrmUtils;
11
use Ajax\semantic\html\collections\HtmlMessage;
12
13
/**
14
 * @author jc
15
 * @property int $activePage
16
 * @property string $model
17
 * @property \Ajax\php\ubiquity\JsUtils $jquery
18
 * @property \Ubiquity\views\View $view
19
 */
20
trait CRUDControllerUtilitiesTrait {
21
	abstract protected function _showSimpleMessage(CRUDMessage $message,$staticName=null):HtmlMessage;
22
	abstract public function loadView($viewName, $pData = NULL, $asString = false);
23
	abstract public function index();
24
	abstract public function _getBaseRoute();
25
	abstract protected function _showConfMessage(CRUDMessage $message,$url, $responseElement, $data, $attributes = NULL):HtmlMessage;
26
27
	protected $modelViewer;
28
	protected $adminDatas;
29
	protected $events;
30
	protected $crudFiles;
31
	
32 1
	protected function getInstances(&$totalCount,$page=1,$id=null){
33 1
		$this->activePage=$page;
34 1
		$model=$this->model;
35 1
		$condition=$this->_getAdminData()->_getInstancesFilter($model);
36 1
		$totalCount=DAO::count($model,$condition);
37 1
		if($totalCount){
38 1
			$recordsPerPage=$this->_getModelViewer()->recordsPerPage($model,$totalCount);
39 1
			if(is_numeric($recordsPerPage)){
40
				if(isset($id)){
41
					$rownum=DAO::getRownum($model, $id);
42
					$this->activePage=Pagination::getPageOfRow($rownum,$recordsPerPage);
43
				}
44
				return DAO::paginate($model,$this->activePage,$recordsPerPage,$condition);
45
			}
46
		}
47 1
		return DAO::getAll($model,$condition);
48
	}
49
	
50
	protected function search($model,$search){
51
		$fields=$this->_getAdminData()->getSearchFieldNames($model);
52
		$condition=$this->_getAdminData()->_getInstancesFilter($model);
53
		return CRUDHelper::search($model, $search, $fields,$condition);
54
	}
55
	
56
	/**
57
	 * @param mixed $ids
58
	 * @param boolean $included
59
	 * @return object
60
	 */
61 1
	private function getModelInstance($ids,$included=true) {
62 1
		$ids=\explode("_", $ids);
63 1
		if(!is_bool($included)){
64
			if(!is_array($included)){
65
				$included=[$included];
66
			}
67
		}
68 1
		$instance=DAO::getOne($this->model, $ids,$included);
69 1
		if(isset($instance)){
70 1
			return $instance;
71
		}
72
		$message=new CRUDMessage("This object does not exist!","Get object","warning","warning circle");
73
		$message=$this->_getEvents()->onNotFoundMessage($message,$ids);
74
		echo $this->_showSimpleMessage($message);
75
		echo $this->jquery->compile($this->view);
76
		exit(1);
77
	}
78
	
79
	protected function updateMemberDataElement($member,$instance){
80
		$dt=$this->_getModelViewer()->getModelDataElement($instance, $this->model, false);
81
		$dt->compile();
82
		echo new HtmlContentOnly($dt->getFieldValue($member));
83
	}
84
	
85
	private function _renderDataTableForRefresh($instances,$model,$totalCount){
86
		$compo= $this->_getModelViewer()->getModelDataTable($instances, $model,$totalCount)->refresh(["tbody"]);
87
		$this->_getEvents()->onDisplayElements($compo,$instances,true);
88
		$compo->setLibraryId("_compo_");
89
		$this->jquery->renderView("@framework/main/component.html");
90
	}
91
92 1
	protected function _edit($instance, $modal="no") {
93 1
		$_SESSION["instance"]=$instance;
94 1
		$modal=($modal == "modal");
95 1
		$form=$this->_getModelViewer()->getForm("frmEdit", $instance);
96 1
		$this->jquery->click("#action-modal-frmEdit-0", "$('#frmEdit').form('submit');", false);
97 1
		if (!$modal) {
98 1
			$this->jquery->click("#bt-cancel", "$('#form-container').transition('drop');");
99 1
			$this->jquery->compile($this->view);
100 1
			$this->loadView($this->_getFiles()->getViewForm(), [ "modal" => $modal,"instance"=>$instance,"isNew"=>$instance->_new ]);
101
		} else {
102
			$this->jquery->exec("$('#modal-frmEdit').modal('show');", true);
103
			$form=$form->asModal(\get_class($instance));
104
			$form->setActions([ "Okay","Cancel" ]);
105
			$btOkay=$form->getAction(0);
106
			$btOkay->addClass("green")->setValue("Validate modifications");
107
			$form->onHidden("$('#modal-frmEdit').remove();");
108
			echo $form->compile($this->jquery, $this->view);
109
			echo $this->jquery->compile($this->view);
110
		}
111 1
	}
112
	
113
	protected function _showModel($id=null) {
114
		$model=$this->model;
115
		$datas=$this->getInstances($totalCount,1,$id);
116
		return $this->_getModelViewer()->getModelDataTable($datas, $model,$totalCount,$this->activePage);
117
	}
118
119
	/**
120
	 * Helper to delete multiple objects
121
	 * @param mixed $data
122
	 * @param string $action
123
	 * @param string $target the css selector for refreshing
124
	 * @param callable|string $condition the callback for generating the SQL where (for deletion) with the parameter data, or a simple string
125
	 */
126
	protected function _deleteMultiple($data,$action,$target,$condition){
127
		if(URequest::isPost()){
128
			if(is_callable($condition)){
129
				$condition=$condition($data);
130
			}
131
			$rep=DAO::deleteAll($this->model, $condition);
132
			if($rep){
133
				$message=new CRUDMessage("Deleting {count} objects","Deletion","info","info circle",4000);
134
				$message=$this->_getEvents()->onSuccessDeleteMultipleMessage($message,$rep);
135
				$message->parseContent(["count"=>$rep]);
136
			}
137
			if(isset($message)){
138
				$this->_showSimpleMessage($message,"delete-all");
139
			}
140
			$this->index();
141
		}else{
142
			$message=new CRUDMessage("Do you confirm the deletion of this objects?", "Remove confirmation","error");
143
			$this->_getEvents()->onConfDeleteMultipleMessage($message,$data);
144
			$message=$this->_showConfMessage($message, $this->_getBaseRoute() . "/{$action}/{$data}",$target, $data,["jqueryDone"=>"replaceWith"]);
145
			echo $message;
146
			echo $this->jquery->compile($this->view);
147
		}
148
	}
149
	
150
	
151 1
	protected function refreshInstance($instance,$isNew){
152 1
		if($this->_getAdminData()->refreshPartialInstance() && !$isNew){
153 1
			$this->jquery->setJsonToElement(OrmUtils::objectAsJSON($instance));
154
		}else{
155
			$pk=OrmUtils::getFirstKeyValue($instance);
156
			$this->jquery->get($this->_getBaseRoute() . "/refreshTable/".$pk, "#lv", [ "jqueryDone" => "replaceWith" ]);
157
		}
158 1
	}
159
	
160
	/**
161
	 * To override for defining a new adminData
162
	 * @return CRUDDatas
163
	 */
164
	protected function getAdminData ():CRUDDatas{
165
		return new CRUDDatas();
166
	}
167
	
168 1
	public function _getAdminData ():CRUDDatas{
169 1
		return $this->getSingleton($this->adminDatas,"getAdminData");
170
	}
171
	
172
	/**
173
	 * To override for defining a new ModelViewer
174
	 * @return ModelViewer
175
	 */
176
	protected function getModelViewer ():ModelViewer{
177
		return new ModelViewer($this);
178
	}
179
	
180 1
	private function _getModelViewer():ModelViewer{
181 1
		return $this->getSingleton($this->modelViewer,"getModelViewer");
182
	}
183
	
184
	/**
185
	 * To override for changing view files
186
	 * @return CRUDFiles
187
	 */
188
	protected function getFiles ():CRUDFiles{
189
		return new CRUDFiles();
190
	}
191
	
192
	/**
193
	 * @return CRUDFiles
194
	 */
195 1
	public function _getFiles(){
196 1
		return $this->getSingleton($this->crudFiles,"getFiles");
197
	}
198
	
199
	/**
200
	 * To override for changing events
201
	 * @return CRUDEvents
202
	 */
203
	protected function getEvents ():CRUDEvents{
204
		return new CRUDEvents($this);
205
	}
206
	
207 1
	private function _getEvents():CRUDEvents{
208 1
		return $this->getSingleton($this->events,"getEvents");
209
	}
210
	
211 1
	private function getSingleton($value, $method) {
212 1
		if (! isset ( $value )) {
213 1
			$value = $this->$method ();
214
		}
215 1
		return $value;
216
	}
217
	
218 1
	private function crudLoadView($viewName,$vars=[]){
219 1
		$this->_getEvents()->beforeLoadView($viewName,$vars);
220 1
		if(!URequest::isAjax()){
221 1
			$files=$this->_getFiles();
222 1
			$mainTemplate=$files->getBaseTemplate();
223 1
			if(isset($mainTemplate)){
224
				$vars["_viewname"]=$viewName;
225
				$vars["_base"]=$mainTemplate;
226
				$this->jquery->renderView($files->getViewBaseTemplate(),$vars);
227
			}else{
228 1
				$vars["hasScript"]=true;
229 1
				$this->jquery->renderView($viewName,$vars);
230
			}
231
		}else{
232
			$vars["hasScript"]=true;
233
			$this->jquery->renderView($viewName,$vars);
234
		}
235 1
	}
236
	
237
	/**
238
	 * @param object $instance
239
	 * @return string
240
	 */
241
	protected function getInstanceToString($instance){
242
		if (method_exists($instance, "__toString")){
243
			return $instance . "";
244
		}
245
		else{
246
			return get_class($instance);
247
		}
248
	}
249
}
250
251