Completed
Push — master ( f5f495...b1701b )
by Jean-Christophe
03:57
created

DataForm::_generateContent()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 18
nc 4
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
				//$form->addField($v);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
				$this->_generateFields($form, [$v], $headers, $i, $i+1, $wrappers);
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], $separators[$i+1], $wrappers);
72
			}
73
		}
74
	}
75
76
	protected function _generateFields($form,$values,$headers,$sepFirst,$sepLast,$wrappers){
0 ignored issues
show
Unused Code introduced by
The parameter $sepLast 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...
77
		$wrapper=null;
78
		//$fields=\array_slice($values, $sepFirst+1,$sepLast-$sepFirst);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
		if(isset($headers[$sepFirst+1]))
80
			$form->addHeader($headers[$sepFirst+1],4,true);
81
		if(isset($wrappers[$sepFirst+1])){
82
			$wrapper=$wrappers[$sepFirst+1];
83
		}
84
		if(\sizeof($values)===1){
85
			$added=$form->addField($values[0]);
86
		}elseif(\sizeof($values)>1){
87
			$added=$form->addFields($values);
88
		}else
89
			return;
90
		if(isset($wrapper)){
91
			$added->wrap($wrapper[0],$wrapper[1]);
92
		}
93
	}
94
95
	/**
96
	 * @return HtmlForm
97
	 */
98
	public function getForm(){
99
		return $this->content["form"];
100
	}
101
102
	public function addSeparatorAfter($fieldNum){
103
		$fieldNum=$this->_getIndex($fieldNum);
104
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
105
		return $this;
106
	}
107
108
	public function getSeparators() {
109
		return $this->_instanceViewer->getSeparators();
110
	}
111
112
	public function setSeparators($separators) {
113
		$this->_instanceViewer->setSeparators($separators);
114
		return $this;
115
	}
116
117
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
118
		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.

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