Completed
Push — master ( 8545cb...05e72b )
by Jean-Christophe
01:22
created

UbiquityMyAdminViewer::isModal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace micro\controllers\admin;
4
5
use Ajax\JsUtils;
6
use Ajax\semantic\widgets\dataform\DataForm;
7
use micro\orm\OrmUtils;
8
use Ajax\service\JArray;
9
use micro\orm\DAO;
10
use micro\orm\parser\Reflexion;
11
12
/**
13
 * @author jc
14
 *
15
 */
16
class UbiquityMyAdminViewer {
17
	/**
18
	 * @var JsUtils
19
	 */
20
	private $jquery;
21
22
	/**
23
	 * @var UbiquityMyAdminBaseController
24
	 */
25
	private $controller;
26
	public function __construct(UbiquityMyAdminBaseController $controller){
27
		$this->jquery=$controller->jquery;
28
		$this->controller=$controller;
29
	}
30
31
	/**
32
	 * @param string $identifier
33
	 * @param object $instance
34
	 * @return DataForm
35
	 */
36
	public function getForm($identifier,$instance,$modal=false){
0 ignored issues
show
Unused Code introduced by
The parameter $modal is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
		$form=$this->jquery->semantic()->dataForm($identifier, $instance);
38
		$className=\get_class($instance);
39
		$form->setFields($this->controller->getAdminData()->getFormFieldNames($className));
40
41
		$fieldTypes=OrmUtils::getFieldTypes($className);
42
		foreach ($fieldTypes as $property=>$type){
43
			switch ($type){
44
				case "boolean":
45
					$form->fieldAsCheckbox($property);
46
					break;
47
				case "int":
48
					$form->fieldAsInput($property,["inputType"=>"number"]);
49
					break;
50
				case "password":
51
					$form->fieldAs($property, ["inputType"=>"password"]);
52
					break;
53
				case "email":
54
					$form->fieldAsInput($property,["inputType"=>"email","rules"=>["email"]]);
55
					break;
56
			}
57
		}
58
		$relations = OrmUtils::getFieldsInRelations($className);
59
		foreach ($relations as $member){
60
			if($this->controller->getAdminData()->getUpdateManyToOneInForm() && OrmUtils::getAnnotationInfoMember($className, "#manyToOne",$member)!==false){
61
				$this->manyToOneFormField($form, $member, $className, $instance);
62
			}elseif($this->controller->getAdminData()->getUpdateOneToManyInForm() && ($annot=OrmUtils::getAnnotationInfoMember($className, "#oneToMany",$member))!==false){
63
				$this->oneToManyFormField($form, $member, $className, $instance,$annot);
64
			}elseif($this->controller->getAdminData()->getUpdateManyToManyInForm() && ($annot=OrmUtils::getAnnotationInfoMember($className, "#manyToMany",$member))!==false){
65
				$this->manyToManyFormField($form, $member, $className, $instance,$annot);
66
			}
67
		}
68
		$form->setSubmitParams("Admin/update", "#table-details");
69
		return $form;
70
	}
71
72
	public function isModal($objects,$model){
1 ignored issue
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
		return \count($objects)>20;
74
	}
75
76
	protected function manyToOneFormField(DataForm $form,$member,$className,$instance){
77
		$joinColumn=OrmUtils::getAnnotationInfoMember($className, "#joinColumn", $member);
78
		if($joinColumn){
79
			$fkObject=Reflexion::getMemberValue($instance, $member);
80
			$fkClass=$joinColumn["className"];
81
			if($fkObject==null){
82
				$fkObject=new $fkClass();
83
			}
84
			$fkId=OrmUtils::getFirstKey($fkClass);
85
			$fkIdGetter="get".\ucfirst($fkId);
86
			if(\method_exists($fkObject, "__toString") && \method_exists($fkObject, $fkIdGetter)){
87
				$fkField=$joinColumn["name"];
88
				$fkValue=OrmUtils::getFirstKeyValue($fkObject);
89
				if(!Reflexion::setMemberValue($instance, $fkField, $fkValue)){
90
					$instance->{$fkField}=OrmUtils::getFirstKeyValue($fkObject);
91
					$form->addField($fkField);
92
				}
93
				$form->fieldAsDropDown($fkField,JArray::modelArray(DAO::getAll($fkClass),$fkIdGetter,"__toString"));
94
				$form->setCaption($fkField, \ucfirst($member));
95
			}
96
		}
97
	}
98
	protected function oneToManyFormField(DataForm $form,$member,$className,$instance,$annot){
0 ignored issues
show
Unused Code introduced by
The parameter $className is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
		$newField=$member."Ids";
100
		$fkClass=$annot["className"];
101
		$fkId=OrmUtils::getFirstKey($fkClass);
102
		$fkIdGetter="get".\ucfirst($fkId);
103
		$fkInstances=DAO::getOneToMany($instance, $member);
104
		$form->addField($newField);
105
		$ids=\array_map(function($elm) use($fkIdGetter){return $elm->{$fkIdGetter}();},$fkInstances);
106
		$instance->{$newField}=\implode(",", $ids);
107
		$form->fieldAsDropDown($newField,JArray::modelArray(DAO::getAll($fkClass),$fkIdGetter,"__toString"),true);
108
		$form->setCaption($newField, \ucfirst($member));
109
	}
110
111
	protected function manyToManyFormField(DataForm $form,$member,$className,$instance,$annot){
0 ignored issues
show
Unused Code introduced by
The parameter $className is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
		$newField=$member."Ids";
113
		$fkClass=$annot["targetEntity"];
114
		$fkId=OrmUtils::getFirstKey($fkClass);
115
		$fkIdGetter="get".\ucfirst($fkId);
116
		$fkInstances=DAO::getManyToMany($instance, $member);
117
		$form->addField($newField);
118
		$ids=\array_map(function($elm) use($fkIdGetter){return $elm->{$fkIdGetter}();},$fkInstances);
119
		$instance->{$newField}=\implode(",", $ids);
120
		$form->fieldAsDropDown($newField,JArray::modelArray($this->controller->getAdminData()->getManyToManyDatas($fkClass, $instance, $member),$fkIdGetter,"__toString"),true,["jsCallback"=>function($elm){$elm->getField()->asSearch();}]);
121
		$form->setCaption($newField, \ucfirst($member));
122
	}
123
}
124