Passed
Push — master ( 055b0b...f9591e )
by Jean-Christophe
08:37
created

CRUDControllerUtilitiesTrait::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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