Passed
Push — master ( ac12b8...337a26 )
by Jean-Christophe
02:24
created

DataForm::setSeparators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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