Completed
Push — master ( ceccd5...145e32 )
by Jean-Christophe
01:37
created

_renderDataTableForRefresh()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
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
12
trait CRUDControllerUtilitiesTrait {
13
	
14
	protected function getInstances(&$totalCount,$page=1,$id=null){
15
		$this->activePage=$page;
0 ignored issues
show
Bug introduced by
The property activePage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
		$model=$this->model;
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
		$condition=$this->_getAdminData()->_getInstancesFilter($model);
18
		$totalCount=DAO::count($model,$condition);
19
		$recordsPerPage=$this->_getModelViewer()->recordsPerPage($model,$totalCount);
20
		if(is_numeric($recordsPerPage)){
21
			if(isset($id)){
22
				$rownum=DAO::getRownum($model, $id);
23
				$this->activePage=Pagination::getPageOfRow($rownum,$recordsPerPage);
24
			}
25
			return DAO::paginate($model,$this->activePage,$recordsPerPage,$condition);
26
		}
27
		return DAO::getAll($model,$condition);
28
	}
29
	
30
	protected function search($model,$search){
31
		$fields=$this->_getAdminData()->getSearchFieldNames($model);
32
		$condition=$this->_getAdminData()->_getInstancesFilter($model);
33
		return CRUDHelper::search($model, $search, $fields,$condition);
34
	}
35
	
36
	private function getModelInstance($ids,$included=true) {
37
		$ids=\explode("_", $ids);
38
		if(!is_bool($included)){
39
			if(!is_array($included)){
40
				$included=[$included];
41
			}
42
		}
43
		$instance=DAO::getOne($this->model, $ids,$included);
44
		if(isset($instance)){
45
			return $instance;
46
		}
47
		$message=new CRUDMessage("This object does not exist!","Get object","warning","warning circle");
48
		$message=$this->_getEvents()->onNotFoundMessage($message,$ids);
49
		echo $this->_showSimpleMessage($message);
0 ignored issues
show
Bug introduced by
It seems like _showSimpleMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
		echo $this->jquery->compile($this->view);
0 ignored issues
show
Bug introduced by
The property jquery does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
		exit(1);
52
	}
53
	
54
	protected function updateMemberDataElement($member,$instance){
55
		$dt=$this->_getModelViewer()->getModelDataElement($instance, $this->model, false);
56
		$dt->compile();
57
		echo new HtmlContentOnly($dt->getFieldValue($member));
58
	}
59
	
60
	private function _renderDataTableForRefresh($instances,$model,$totalCount){
61
		$this->formModal=($this->_getModelViewer()->isModal($instances,$model))? "modal" : "no";
0 ignored issues
show
Bug introduced by
The property formModal does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
		$compo= $this->_getModelViewer()->getModelDataTable($instances, $model,$totalCount)->refresh(["tbody"]);
63
		$this->_getEvents()->onDisplayElements($compo,$instances,true);
64
		$compo->setLibraryId("_compo_");
65
		$this->jquery->renderView("@framework/main/component.html");
66
	}
67
68
	protected function _edit($instance, $modal="no") {
69
		$_SESSION["instance"]=$instance;
70
		$modal=($modal == "modal");
71
		$form=$this->_getModelViewer()->getForm("frmEdit", $instance);
72
		$this->jquery->click("#action-modal-frmEdit-0", "$('#frmEdit').form('submit');", false);
73
		if (!$modal) {
74
			$this->jquery->click("#bt-cancel", "$('#form-container').transition('drop');");
75
			$this->jquery->compile($this->view);
76
			$this->loadView($this->_getFiles()->getViewForm(), [ "modal" => $modal,"instance"=>$instance,"isNew"=>$instance->_new ]);
0 ignored issues
show
Bug introduced by
The method loadView() does not exist on Ubiquity\controllers\cru...ontrollerUtilitiesTrait. Did you maybe mean crudLoadView()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
		} else {
78
			$this->jquery->exec("$('#modal-frmEdit').modal('show');", true);
79
			$form=$form->asModal(\get_class($instance));
80
			$form->setActions([ "Okay","Cancel" ]);
81
			$btOkay=$form->getAction(0);
82
			$btOkay->addClass("green")->setValue("Validate modifications");
83
			$form->onHidden("$('#modal-frmEdit').remove();");
84
			echo $form->compile($this->jquery, $this->view);
85
			echo $this->jquery->compile($this->view);
86
		}
87
	}
88
	
89
	protected function _showModel($id=null) {
90
		$model=$this->model;
91
		$datas=$this->getInstances($totalCount,1,$id);
92
		$this->formModal=($this->_getModelViewer()->isModal($datas,$model))? "modal" : "no";
93
		return $this->_getModelViewer()->getModelDataTable($datas, $model,$totalCount,$this->activePage);
94
	}
95
96
	/**
97
	 * Helper to delete multiple objects
98
	 * @param mixed $data
99
	 * @param string $action
100
	 * @param string $target the css selector for refreshing
101
	 * @param callable|string $condition the callback for generating the SQL where (for deletion) with the parameter data, or a simple string
102
	 */
103
	protected function _deleteMultiple($data,$action,$target,$condition){
104
		if(URequest::isPost()){
105
			if(is_callable($condition)){
106
				$condition=$condition($data);
107
			}
108
			$rep=DAO::deleteAll($this->model, $condition);
109
			if($rep){
110
				$message=new CRUDMessage("Deleting {count} objects","Deletion","info","info circle",4000);
111
				$message=$this->_getEvents()->onSuccessDeleteMultipleMessage($message,$rep);
112
				$message->parseContent(["count"=>$rep]);
113
			}
114
			$this->_showSimpleMessage($message,"delete-all");
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
It seems like _showSimpleMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
115
			$this->index();
0 ignored issues
show
Bug introduced by
It seems like index() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
116
		}else{
117
			$message=new CRUDMessage("Do you confirm the deletion of this objects?", "Remove confirmation","error");
118
			$this->_getEvents()->onConfDeleteMultipleMessage($message,$data);
119
			$message=$this->_showConfMessage($message, $this->_getBaseRoute() . "/{$action}/{$data}",$target, $data,["jqueryDone"=>"replaceWith"]);
0 ignored issues
show
Bug introduced by
It seems like _getBaseRoute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like _showConfMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
120
			echo $message;
121
			echo $this->jquery->compile($this->view);
122
		}
123
	}
124
	
125
	
126
	protected function refreshInstance($instance,$isNew){
127
		if($this->_getAdminData()->refreshPartialInstance() && !$isNew){
128
			$this->jquery->setJsonToElement(OrmUtils::objectAsJSON($instance));
129
		}else{
130
			$pk=OrmUtils::getFirstKeyValue($instance);
131
			$this->jquery->get($this->_getBaseRoute() . "/refreshTable/".$pk, "#lv", [ "jqueryDone" => "replaceWith" ]);
0 ignored issues
show
Bug introduced by
It seems like _getBaseRoute() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
132
		}
133
	}
134
	
135
	/**
136
	 * To override for defining a new adminData
137
	 * @return CRUDDatas
138
	 */
139
	protected function getAdminData ():CRUDDatas{
140
		return new CRUDDatas();
141
	}
142
	
143
	public function _getAdminData ():CRUDDatas{
144
		return $this->getSingleton($this->modelViewer,"getAdminData");
0 ignored issues
show
Bug introduced by
The property modelViewer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
145
	}
146
	
147
	/**
148
	 * To override for defining a new ModelViewer
149
	 * @return ModelViewer
150
	 */
151
	protected function getModelViewer ():ModelViewer{
152
		return new ModelViewer($this);
153
	}
154
	
155
	private function _getModelViewer():ModelViewer{
156
		return $this->getSingleton($this->modelViewer,"getModelViewer");
157
	}
158
	
159
	/**
160
	 * To override for changing view files
161
	 * @return CRUDFiles
162
	 */
163
	protected function getFiles ():CRUDFiles{
164
		return new CRUDFiles();
165
	}
166
	
167
	/**
168
	 * @return CRUDFiles
169
	 */
170
	public function _getFiles(){
171
		return $this->getSingleton($this->crudFiles,"getFiles");
0 ignored issues
show
Bug introduced by
The property crudFiles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
172
	}
173
	
174
	/**
175
	 * To override for changing events
176
	 * @return CRUDEvents
177
	 */
178
	protected function getEvents ():CRUDEvents{
179
		return new CRUDEvents($this);
180
	}
181
	
182
	private function _getEvents():CRUDEvents{
183
		return $this->getSingleton($this->events,"getEvents");
0 ignored issues
show
Bug introduced by
The property events does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
184
	}
185
	
186
	private function getSingleton($value, $method) {
187
		if (! isset ( $value )) {
188
			$value = $this->$method ();
189
		}
190
		return $value;
191
	}
192
	
193
	private function crudLoadView($viewName,$vars=[]){
194
		$this->_getEvents()->beforeLoadView($viewName,$vars);
195
		if(!URequest::isAjax()){
196
			$files=$this->_getFiles();
197
			$mainTemplate=$files->getBaseTemplate();
198
			if(isset($mainTemplate)){
199
				$vars["_viewname"]=$viewName;
200
				$vars["_base"]=$mainTemplate;
201
				$this->jquery->renderView($files->getViewBaseTemplate(),$vars);
202
			}else{
203
				$vars["hasScript"]=true;
204
				$this->jquery->renderView($viewName,$vars);
205
			}
206
		}else{
207
			$vars["hasScript"]=true;
208
			$this->jquery->renderView($viewName,$vars);
209
		}
210
	}
211
}
212
213