Completed
Push — master ( 9b0ab1...980c15 )
by Jean-Christophe
01:27
created

UbiquityMyAdminViewer::getFkList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
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
use Ajax\semantic\html\elements\HtmlHeader;
12
use Ajax\common\html\HtmlCollection;
13
use Ajax\common\html\BaseHtml;
14
15
/**
16
 * @author jc
17
 *
18
 */
19
class UbiquityMyAdminViewer {
20
	/**
21
	 * @var JsUtils
22
	 */
23
	private $jquery;
24
25
	/**
26
	 * @var UbiquityMyAdminBaseController
27
	 */
28
	private $controller;
29
30
	public function __construct(UbiquityMyAdminBaseController $controller){
31
		$this->jquery=$controller->jquery;
32
		$this->controller=$controller;
33
	}
34
35
	/**
36
	 * Returns the form for adding or modifying an object
37
	 * @param string $identifier
38
	 * @param object $instance the object to add or modify
39
	 * @return DataForm
40
	 */
41
	public function getForm($identifier,$instance){
42
		$form=$this->jquery->semantic()->dataForm($identifier, $instance);
43
		$className=\get_class($instance);
44
		$fields=$this->controller->getAdminData()->getFormFieldNames($className);
45
		$form->setFields($fields);
46
47
		$fieldTypes=OrmUtils::getFieldTypes($className);
48
		foreach ($fieldTypes as $property=>$type){
49
			switch ($type){
50
				case "boolean": case "bool":
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
51
					$form->fieldAsCheckbox($property);
52
					break;
53
				case "int": case "integer":
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
54
					$form->fieldAsInput($property,["inputType"=>"number"]);
55
					break;
56
			}
57
		}
58
		$this->relationMembersInForm($form, $instance, $className);
59
		$form->setCaptions($this->getFormCaptions($form->getInstanceViewer()->getVisibleProperties(),$className,$instance));
60
		$form->setSubmitParams($this->controller->getAdminFiles()->getAdminBaseRoute()."/update", "#table-details");
61
		return $form;
62
	}
63
64
	/**
65
	 * Condition to determine if the edit or add form is modal for $model objects
66
	 * @param array $objects
67
	 * @param string $model
68
	 * @return boolean
69
	 */
70
	public function isModal($objects,$model){
71
		return \count($objects)>20;
72
	}
73
74
	/**
75
	 * Returns the captions for list fields in showTable action
76
	 * @param array $captions
77
	 * @param string $className
78
	 */
79
	public function getCaptions($captions,$className){
1 ignored issue
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...
80
		return array_map("ucfirst", $captions);
81
	}
82
83
	/**
84
	 * Returns the captions for form fields
85
	 * @param array $captions
86
	 * @param string $className
87
	 */
88
	public function getFormCaptions($captions,$className,$instance){
1 ignored issue
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...
Unused Code introduced by
The parameter $instance 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...
89
		return array_map("ucfirst", $captions);
90
	}
91
92
	public function getFkHeaderElement($member,$className,$object){
2 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...
Unused Code introduced by
The parameter $object 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...
93
		return new HtmlHeader("",4,$member,"content");
94
	}
95
96
	public function getFkHeaderList($member,$className,$list){
1 ignored issue
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...
97
		return new HtmlHeader("",4,$member." (".\count($list).")","content");
98
	}
99
100
	/**
101
	 * @param string $member
102
	 * @param string $className
103
	 * @param object $object
104
	 * @return BaseHtml
105
	 */
106
	public function getFkElement($member,$className,$object){
107
		return $this->jquery->semantic()->htmlLabel("element-".$className.".".$member,$object."");
108
	}
109
110
	/**
111
	 * @param string $member
112
	 * @param string $className
113
	 * @param array|\Traversable $list
114
	 * @return HtmlCollection
115
	 */
116
	public function getFkList($member,$className,$list){
1 ignored issue
show
Unused Code introduced by
The parameter $list 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...
117
		$element=$this->jquery->semantic()->htmlList("list-".$className.".".$member);
118
		return $element->addClass("animated divided celled");
119
	}
120
121
	public function displayFkElementList($element,$member,$className,$object){
0 ignored issues
show
Unused Code introduced by
The parameter $element 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...
Unused Code introduced by
The parameter $member 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...
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...
Unused Code introduced by
The parameter $object 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...
122
123
	}
124
125
	protected function relationMembersInForm($form,$instance,$className){
126
		$relations = OrmUtils::getFieldsInRelations($className);
127
		foreach ($relations as $member){
128
			if($this->controller->getAdminData()->getUpdateManyToOneInForm() && OrmUtils::getAnnotationInfoMember($className, "#manyToOne",$member)!==false){
129
				$this->manyToOneFormField($form, $member, $className, $instance);
130
			}elseif($this->controller->getAdminData()->getUpdateOneToManyInForm() && ($annot=OrmUtils::getAnnotationInfoMember($className, "#oneToMany",$member))!==false){
131
				$this->oneToManyFormField($form, $member, $instance,$annot);
132
			}elseif($this->controller->getAdminData()->getUpdateManyToManyInForm() && ($annot=OrmUtils::getAnnotationInfoMember($className, "#manyToMany",$member))!==false){
133
				$this->manyToManyFormField($form, $member, $instance,$annot);
134
			}
135
		}
136
	}
137
138
	protected function manyToOneFormField(DataForm $form,$member,$className,$instance){
139
		$joinColumn=OrmUtils::getAnnotationInfoMember($className, "#joinColumn", $member);
140
		if($joinColumn){
141
			$fkObject=Reflexion::getMemberValue($instance, $member);
142
			$fkClass=$joinColumn["className"];
143
			if($fkObject===null){
144
				$fkObject=new $fkClass();
145
			}
146
			$fkId=OrmUtils::getFirstKey($fkClass);
147
			$fkIdGetter="get".\ucfirst($fkId);
148
			if(\method_exists($fkObject, "__toString") && \method_exists($fkObject, $fkIdGetter)){
149
				$fkField=$joinColumn["name"];
150
				$fkValue=OrmUtils::getFirstKeyValue($fkObject);
151
				if(!Reflexion::setMemberValue($instance, $fkField, $fkValue)){
152
					$instance->{$fkField}=OrmUtils::getFirstKeyValue($fkObject);
153
					$form->addField($fkField);
154
				}
155
				$form->fieldAsDropDown($fkField,JArray::modelArray(DAO::getAll($fkClass),$fkIdGetter,"__toString"));
156
				$form->setCaption($fkField, \ucfirst($member));
157
			}
158
		}
159
	}
160
	protected function oneToManyFormField(DataForm $form,$member,$instance,$annot){
161
		$newField=$member."Ids";
162
		$fkClass=$annot["className"];
163
		$fkId=OrmUtils::getFirstKey($fkClass);
164
		$fkIdGetter="get".\ucfirst($fkId);
165
		$fkInstances=DAO::getOneToMany($instance, $member);
166
		$form->addField($newField);
167
		$ids=\array_map(function($elm) use($fkIdGetter){return $elm->{$fkIdGetter}();},$fkInstances);
168
		$instance->{$newField}=\implode(",", $ids);
169
		$form->fieldAsDropDown($newField,JArray::modelArray(DAO::getAll($fkClass),$fkIdGetter,"__toString"),true);
170
		$form->setCaption($newField, \ucfirst($member));
171
	}
172
173
	protected function manyToManyFormField(DataForm $form,$member,$instance,$annot){
174
		$newField=$member."Ids";
175
		$fkClass=$annot["targetEntity"];
176
		$fkId=OrmUtils::getFirstKey($fkClass);
177
		$fkIdGetter="get".\ucfirst($fkId);
178
		$fkInstances=DAO::getManyToMany($instance, $member);
179
		$form->addField($newField);
180
		$ids=\array_map(function($elm) use($fkIdGetter){return $elm->{$fkIdGetter}();},$fkInstances);
181
		$instance->{$newField}=\implode(",", $ids);
182
		$form->fieldAsDropDown($newField,JArray::modelArray($this->controller->getAdminData()->getManyToManyDatas($fkClass, $instance, $member),$fkIdGetter,"__toString"),true,["jsCallback"=>function($elm){$elm->getField()->asSearch();}]);
183
		$form->setCaption($newField, \ucfirst($member));
184
	}
185
}
186