Completed
Push — master ( 84905b...948707 )
by Jean-Christophe
01:52
created

CRUDController::newModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Ubiquity\controllers\crud;
4
5
use Ubiquity\orm\DAO;
6
use Ubiquity\controllers\ControllerBase;
7
use Ubiquity\controllers\admin\interfaces\HasModelViewer;
8
use Ubiquity\controllers\admin\UbiquityMyAdminData;
9
use Ubiquity\controllers\admin\viewers\ModelViewer;
10
use Ubiquity\controllers\semantic\MessagesTrait;
11
12
abstract class CRUDController extends ControllerBase implements HasModelViewer{
13
	use MessagesTrait;
14
	protected $model;
15
	protected $modelViewer;
16
	protected $crudFiles;
17
	
18
	/**
19
	 * Default page : list all objects
20
	 */
21
	public function index() {
22
		$objects=DAO::getAll($this->model);
23
		$modal=($this->_getModelViewer()->isModal($objects,$this->model))?"modal":"no";
24
		$this->_getModelViewer()->getModelDataTable($objects, $this->model);
25
		$this->jquery->getOnClick ( "#btAddNew", $this->_getBaseRoute() . "/newModel/" . $modal, "#frm-add-update",["hasLoader"=>"internal"] );
26
		$this->jquery->compile($this->view);
27
		$this->loadView($this->_getFiles()->getViewShowTable(), [ "classname" => $this->model ]);
28
	}
29
	
30
	/**
31
	 * Edits an instance
32
	 * @param string $modal Accept "no" or "modal" for a modal dialog
33
	 * @param string $ids the primary value(s)
34
	 */
35
	public function edit($modal="no", $ids="") {
36
		$instance=$this->getModelInstance($ids);
37
		$instance->_new=false;
38
		$this->_edit($instance, $modal);
39
	}
40
	/**
41
	 * Adds a new instance and edits it
42
	 * @param string $modal Accept "no" or "modal" for a modal dialog
43
	 */
44
	public function newModel($modal="no") {
45
		$model=$this->model;
46
		$instance=new $model();
47
		$instance->_new=true;
48
		$this->_edit($instance, $modal);
49
	}
50
	
51
	protected function _edit($instance, $modal="no") {
52
		$_SESSION["instance"]=$instance;
53
		$modal=($modal == "modal");
54
		$form=$this->_getModelViewer()->getForm("frmEdit", $instance);
55
		$this->jquery->click("#action-modal-frmEdit-0", "$('#frmEdit').form('submit');", false);
56
		if (!$modal) {
57
			$this->jquery->click("#bt-cancel", "$('#form-container').transition('drop');");
58
			$this->jquery->compile($this->view);
59
			$this->loadView($this->_getFiles()->getViewEditTable(), [ "modal" => $modal ]);
60
		} else {
61
			$this->jquery->exec("$('#modal-frmEdit').modal('show');", true);
62
			$form=$form->asModal(\get_class($instance));
63
			$form->setActions([ "Okay","Cancel" ]);
64
			$btOkay=$form->getAction(0);
65
			$btOkay->addClass("green")->setValue("Validate modifications");
66
			$form->onHidden("$('#modal-frmEdit').remove();");
67
			echo $form->compile($this->jquery, $this->view);
68
			echo $this->jquery->compile($this->view);
69
		}
70
	}
71
	
72
	protected function _showModel() {
73
		$model=$this->model;
74
		$datas=DAO::getAll($model);
75
		return $this->_getModelViewer()->getModelDataTable($datas, $model);
76
	}
77
	
78
	/**
79
	 * Deletes an instance
80
	 * @param mixed $ids
81
	 */
82
	public function delete($ids) {
83
		$instance=$this->getModelInstance($ids);
84
		if (method_exists($instance, "__toString"))
85
			$instanceString=$instance . "";
86
		else
87
			$instanceString=get_class($instance);
88
		if (sizeof($_POST) > 0) {
89
			if (DAO::remove($instance)) {
0 ignored issues
show
Bug introduced by
It seems like $instance defined by $this->getModelInstance($ids) on line 83 can also be of type null; however, Ubiquity\orm\traits\DAOUpdatesTrait::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
				$message=$this->showSimpleMessage("Deletion of `<b>" . $instanceString . "</b>`", "info", "info", 4000);
91
				$this->jquery->exec("$('tr[data-ajax={$ids}]').remove();", true);
92
			} else {
93
				$message=$this->showSimpleMessage("Can not delete `" . $instanceString . "`", "warning", "warning");
94
			}
95
		} else {
96
			$message=$this->showConfMessage("Do you confirm the deletion of `<b>" . $instanceString . "</b>`?", "error", $this->_getBaseRoute() . "/delete/{$ids}", "#table-messages", $ids);
97
		}
98
		echo $message;
99
		echo $this->jquery->compile($this->view);
100
	}
101
	
102
	public function refreshTable() {
103
		echo $this->_showModel();
104
		echo $this->jquery->compile($this->view);
105
	}
106
	
107
	/**
108
	 * Updates an instance from the data posted in a form
109
	 */
110
	public function update() {
111
		$message=$this->jquery->semantic()->htmlMessage("msgUpdate", "Modifications were successfully saved", "info");
112
		$instance=@$_SESSION["instance"];
113
		$updated=CRUDHelper::update($instance, $_POST,$this->_getAdminData()->getUpdateManyToOneInForm(),$this->_getAdminData()->getUpdateManyToManyInForm());
114
		if($updated){
115
			$message->setStyle("success")->setIcon("checkmark");
116
			$this->jquery->get($this->_getBaseRoute() . "/refreshTable", "#lv", [ "jqueryDone" => "replaceWith" ]);
117
		} else {
118
			$message->setContent("An error has occurred. Can not save changes.")->setStyle("error")->setIcon("warning circle");
119
		}
120
		echo $message;
121
		echo $this->jquery->compile($this->view);
122
	}
123
	
124
	/**
125
	 * Shows associated members with foreign keys
126
	 * @param mixed $ids
127
	 */
128
	public function showDetail($ids) {
129
		$instance=$this->getModelInstance($ids);
130
		$viewer=$this->_getModelViewer();
131
		$hasElements=false;
132
		$model=$this->model;
133
		$fkInstances=CRUDHelper::getFKIntances($instance, $model);
134
		$semantic=$this->jquery->semantic();
135
		$grid=$semantic->htmlGrid("detail");
136
		if (sizeof($fkInstances) > 0) {
137
			$wide=intval(16 / sizeof($fkInstances));
138
			if ($wide < 4)
139
				$wide=4;
140
				foreach ( $fkInstances as $member=>$fkInstanceArray ) {
141
					$element=$viewer->getFkMemberElement($member,$fkInstanceArray["objectFK"],$fkInstanceArray["fkClass"],$fkInstanceArray["fkTable"]);
142
					if (isset($element)) {
143
						$grid->addCol($wide)->setContent($element);
144
						$hasElements=true;
145
					}
146
				}
147
				if ($hasElements)
148
					echo $grid;
149
				$this->jquery->getOnClick(".showTable", $this->_getBaseRoute() . "/showTableClick", "#divTable", [ "attr" => "data-ajax","ajaxTransition" => "random" ]);
150
				echo $this->jquery->compile($this->view);
151
		}
152
153
	}
154
	
155
	private function getModelInstance($ids) {
156
		$ids=\explode("_", $ids);
157
		$instance=DAO::getOne($this->model, $ids);
158
		if(isset($instance)){
159
			return $instance;
160
		}
161
		echo $this->showSimpleMessage("This object does not exist!", "warning","warning circle");
162
		echo $this->jquery->compile($this->view);
163
		exit(1);
164
	}
165
	
166
	public function _getAdminData ():UbiquityMyAdminData{
167
		return new UbiquityMyAdminData();
168
	}
169
	
170
	/**
171
	 * To override for defining a new ModelViewer
172
	 * @return ModelViewer
173
	 */
174
	protected function getModelViewer ():ModelViewer{
175
		return new ModelViewer($this);
176
	}
177
	
178
	private function _getModelViewer():modelViewer{
179
		return $this->getSingleton($this->modelViewer,"getModelViewer");
180
	}
181
	
182
	/**
183
	 * To override for changing view files
184
	 * @return CRUDFiles
185
	 */
186
	protected function getFiles ():CRUDFiles{
187
		return new CRUDFiles();
188
	}
189
	
190
	private function _getFiles():CRUDFiles{
191
		return $this->getSingleton($this->crudFiles,"getFiles");
192
	}
193
	
194
	private function getSingleton($value, $method) {
195
		if (! isset ( $value )) {
196
			$value = $this->$method ();
197
		}
198
		return $value;
199
	}
200
}
201
202