Passed
Push — master ( ef609f...421c61 )
by Jean-Christophe
02:48
created

DataForm::__construct()   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 2
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
	}
92
93
	/**
94
	 * @return HtmlForm
95
	 */
96
	public function getForm(){
97
		return $this->content["form"];
98
	}
99
100
	public function addSeparatorAfter($fieldNum){
101
		$fieldNum=$this->_getIndex($fieldNum);
102
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
103
		return $this;
104
	}
105
106
	public function getSeparators() {
107
		return $this->_instanceViewer->getSeparators();
108
	}
109
110
	public function setSeparators($separators) {
111
		$this->_instanceViewer->setSeparators($separators);
112
		return $this;
113
	}
114
115
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
116
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){
0 ignored issues
show
Unused Code introduced by
The parameter $caption is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
		return $this->_fieldAs(function($id,$name,$value,/** @scrutinizer ignore-unused */ $caption) use ($cssStyle){

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

Loading history...
117
			$button=new HtmlButton($id,$value,$cssStyle);
118
			$button->setProperty("type", "reset");
119
			return $button;
120
		}, $index,$attributes);
121
	}
122
123
	/**
124
	 * {@inheritDoc}
125
	 * @see \Ajax\common\Widget::getHtmlComponent()
126
	 * @return HtmlForm
127
	 */
128
	public function getHtmlComponent() {
129
		return $this->content["form"];
130
	}
131
	/**
132
	 * {@inheritdoc}
133
	 * @see \Ajax\common\Widget::_setToolbarPosition()
134
	 */
135
	protected function _setToolbarPosition($table, $captions=NULL) {
136
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
137
	}
138
139
	public function addDividerBefore($index,$title){
140
		$index=$this->_getIndex($index);
141
		$this->_instanceViewer->addHeaderDividerBefore($index, $title);
142
		return $this;
143
	}
144
145
	public function addWrapper($index,$contentBefore,$contentAfter=null){
146
		$index=$this->_getIndex($index);
147
		$this->_instanceViewer->addWrapper($index, $contentBefore,$contentAfter);
148
		return $this;
149
	}
150
151
	public function run(JsUtils $js){
152
		return parent::run($js);
153
	}
154
}
155