Passed
Push — master ( 8b3dbf...d47765 )
by Jean-Christophe
01:40
created

DataForm::addSeparatorAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
		if($size===1){
61
			$i=-1;
62
			foreach ($values as $v){
63
				$this->_generateFields($form, [$v], $headers, $i, $wrappers);
64
				$i++;
65
			}
66
		}else{
67
			$separators[]=$count;
68
			for($i=0;$i<$size;$i++){
69
				$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
70
				$this->_generateFields($form, $fields, $headers, $separators[$i], $wrappers);
71
			}
72
		}
73
	}
74
75
	protected function _generateFields($form,$values,$headers,$sepFirst,$wrappers){
76
		$wrapper=null;
77
		if(isset($headers[$sepFirst+1]))
78
			$form->addHeader($headers[$sepFirst+1],4,true);
79
		if(isset($wrappers[$sepFirst+1])){
80
			$wrapper=$wrappers[$sepFirst+1];
81
		}
82
		if(\sizeof($values)===1){
83
			$added=$form->addField($values[0]);
84
		}elseif(\sizeof($values)>1){
85
			$added=$form->addFields($values);
86
		}else
87
			return;
88
		if(isset($wrapper)){
89
			$added->wrap($wrapper[0],$wrapper[1]);
90
		}
91
		$this->execHook("onGenerateFields",$added);
92
	}
93
	
94
	/**
95
	 * Function called when a field is generated
96
	 * the generated field is the first parameter
97
	 * @param callable $callback the fonction to call when a field is generated
98
	 */
99
	public function onGenerateField($callback){
100
		$this->addHook("onGenerateFields",$callback);
101
	}
102
103
	/**
104
	 * @return HtmlForm
105
	 */
106
	public function getForm(){
107
		return $this->content["form"];
108
	}
109
110
	public function addSeparatorAfter($fieldNum){
111
		$fieldNum=$this->_getIndex($fieldNum);
112
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
113
		return $this;
114
	}
115
116
	public function getSeparators() {
117
		return $this->_instanceViewer->getSeparators();
118
	}
119
120
	public function setSeparators($separators) {
121
		$this->_instanceViewer->setSeparators($separators);
122
		return $this;
123
	}
124
125
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
126
		return $this->_fieldAs(function($id,$name,$value) use ($cssStyle){
127
			$button=new HtmlButton($id,$value,$cssStyle);
128
			$button->setProperty("type", "reset");
129
			return $button;
130
		}, $index,$attributes);
131
	}
132
133
	/**
134
	 * {@inheritDoc}
135
	 * @see \Ajax\common\Widget::getHtmlComponent()
136
	 * @return HtmlForm
137
	 */
138
	public function getHtmlComponent() {
139
		return $this->content["form"];
140
	}
141
	/**
142
	 * {@inheritdoc}
143
	 * @see \Ajax\common\Widget::_setToolbarPosition()
144
	 */
145
	protected function _setToolbarPosition($table, $captions=NULL) {
146
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
147
	}
148
149
	public function addDividerBefore($index,$title){
150
		$index=$this->_getIndex($index);
151
		$this->_instanceViewer->addHeaderDividerBefore($index, $title);
152
		return $this;
153
	}
154
155
	public function addWrapper($index,$contentBefore,$contentAfter=null){
156
		$index=$this->_getIndex($index);
157
		$this->_instanceViewer->addWrapper($index, $contentBefore,$contentAfter);
158
		return $this;
159
	}
160
161
	public function run(JsUtils $js){
162
		parent::run($js);
163
	}
164
}
165