Passed
Push — master ( 9a9b82...74dc3c )
by Jean-Christophe
02:28
created

DataForm::_setToolbarPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\widgets\dataform;
4
5
use Ajax\common\Widget;
6
use Ajax\semantic\html\collections\form\HtmlForm;
7
use Ajax\semantic\widgets\datatable\PositionInTable;
8
use Ajax\service\JArray;
9
use Ajax\JsUtils;
10
use Ajax\semantic\html\elements\HtmlButton;
11
use Ajax\semantic\html\base\traits\BaseTrait;
12
13
/**
14
 * DataForm widget for editing model objects
15
 * @version 1.0
16
 * @author jc
17
 * @since 2.2
18
 * @property FormInstanceViewer $_instanceViewer
19
 */
20
class DataForm extends Widget {
21
	use BaseTrait;
22
23
	public function __construct($identifier, $modelInstance=NULL) {
24
		parent::__construct($identifier, null,$modelInstance);
25
		$this->_form=new HtmlForm($identifier);
26
		$this->_init(new FormInstanceViewer($identifier), "form", $this->_form, true);
27
	}
28
29
	protected function _getFieldIdentifier($prefix,$name=""){
30
		return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier();
31
	}
32
33
	public function compile(JsUtils $js=NULL,&$view=NULL){
34
		if(!$this->_generated){
35
			$this->_instanceViewer->setInstance($this->_modelInstance);
36
37
			$form=$this->content["form"];
38
			$this->_generateContent($form);
39
40
			if(isset($this->_toolbar)){
41
				$this->_setToolbarPosition($form);
42
			}
43
			$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]);
44
			$this->_generated=true;
45
		}
46
		return parent::compile($js,$view);
47
	}
48
49
	/**
50
	 * @param HtmlForm $form
51
	 */
52
	protected function _generateContent($form){
53
		$values= $this->_instanceViewer->getValues();
54
		$count=$this->_instanceViewer->count();
55
		$separators=$this->_instanceViewer->getSeparators();
56
		$headers=$this->_instanceViewer->getHeaders();
57
		$wrappers=$this->_instanceViewer->getWrappers();
58
		\sort($separators);
59
		$size=\sizeof($separators);
60
		$nb=0;
61
		if($size===1){
62
			$i=-1;
63
			foreach ($values as $v){
64
				$this->_generateFields($form, [$v], $headers, $i, $wrappers,$nb++);
65
				$i++;
66
			}
67
		}else{
68
			$separators[]=$count;
69
			for($i=0;$i<$size;$i++){
70
				$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
71
				$this->_generateFields($form, $fields, $headers, $separators[$i], $wrappers,$nb++);
72
			}
73
		}
74
		if($this->_hasRules && !$this->getForm()->hasValidationParams()){
75
				$this->setValidationParams(["inline"=>true]);
76
		}
77
	}
78
79
	protected function _generateFields($form,$values,$headers,$sepFirst,$wrappers,$nb){
80
		$wrapper=null;
81
		if(isset($headers[$sepFirst+1]))
82
			$form->addHeader($headers[$sepFirst+1],4,true);
83
		if(isset($wrappers[$sepFirst+1])){
84
			$wrapper=$wrappers[$sepFirst+1];
85
		}
86
		if(\sizeof($values)===1){
87
			$added=$form->addField($values[0]);
88
		}elseif(\sizeof($values)>1){
89
			$added=$form->addFields($values);
90
		}else
91
			return;
92
		if(isset($wrapper)){
93
			$added->wrap($wrapper[0],$wrapper[1]);
94
		}
95
		$this->execHook("onGenerateFields",$added,$nb);
96
	}
97
	
98
	/**
99
	 * Function called when a field is generated
100
	 * the generated field is the first parameter
101
	 * @param callable $callback the fonction to call when a field is generated
102
	 */
103
	public function onGenerateField($callback){
104
		$this->addHook("onGenerateFields",$callback);
105
	}
106
107
	/**
108
	 * @return HtmlForm
109
	 */
110
	public function getForm(){
111
		return $this->content["form"];
112
	}
113
114
	public function addSeparatorAfter($fieldNum){
115
		$fieldNum=$this->_getIndex($fieldNum);
116
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
117
		return $this;
118
	}
119
120
	public function getSeparators() {
121
		return $this->_instanceViewer->getSeparators();
122
	}
123
124
	public function setSeparators($separators) {
125
		$this->_instanceViewer->setSeparators($separators);
126
		return $this;
127
	}
128
129
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
130
		return $this->_fieldAs(function($id,$name,$value) use ($cssStyle){
131
			$button=new HtmlButton($id,$value,$cssStyle);
132
			$button->setProperty("type", "reset");
133
			return $button;
134
		}, $index,$attributes);
135
	}
136
137
	/**
138
	 * {@inheritDoc}
139
	 * @see \Ajax\common\Widget::getHtmlComponent()
140
	 * @return HtmlForm
141
	 */
142
	public function getHtmlComponent() {
143
		return $this->content["form"];
144
	}
145
	/**
146
	 * {@inheritdoc}
147
	 * @see \Ajax\common\Widget::_setToolbarPosition()
148
	 */
149
	protected function _setToolbarPosition($table, $captions=NULL) {
150
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
151
	}
152
153
	public function addDividerBefore($index,$title){
154
		$index=$this->_getIndex($index);
155
		$this->_instanceViewer->addHeaderDividerBefore($index, $title);
156
		return $this;
157
	}
158
159
	public function addWrapper($index,$contentBefore,$contentAfter=null){
160
		$index=$this->_getIndex($index);
161
		$this->_instanceViewer->addWrapper($index, $contentBefore,$contentAfter);
162
		return $this;
163
	}
164
165
	public function run(JsUtils $js){
166
		parent::run($js);
167
	}
168
}
169