Completed
Push — master ( 138cdf...e53430 )
by Jean-Christophe
03:16
created

DataForm::_generateContent()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 6.7272
cc 7
eloc 20
nc 9
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
12
/**
13
 * DataForm widget for editing model objects
14
 * @version 1.0
15
 * @author jc
16
 * @since 2.2
17
 * @property FormInstanceViewer $_instanceViewer
18
 */
19
class DataForm extends Widget {
20
21
	public function __construct($identifier, $modelInstance=NULL) {
22
		parent::__construct($identifier, null,$modelInstance);
23
		$this->_form=new HtmlForm($identifier);
24
		$this->_init(new FormInstanceViewer($identifier), "form", $this->_form, true);
25
	}
26
27
	protected function _getFieldIdentifier($prefix,$name=""){
28
		return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier();
29
	}
30
31 View Code Duplication
	public function compile(JsUtils $js=NULL,&$view=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
		if(!$this->_generated){
33
			$this->_instanceViewer->setInstance($this->_modelInstance);
34
35
			$form=$this->content["form"];
36
			$this->_generateContent($form);
37
38
			if(isset($this->_toolbar)){
39
				$this->_setToolbarPosition($form);
40
			}
41
			$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]);
42
			$this->_generated=true;
43
		}
44
		return parent::compile($js,$view);
45
	}
46
47
	/**
48
	 * @param HtmlForm $form
49
	 */
50
	protected function _generateContent($form){
51
		$values= $this->_instanceViewer->getValues();
52
		$count=$this->_instanceViewer->count();
53
		$separators=$this->_instanceViewer->getSeparators();
54
		$headers=$this->_instanceViewer->getHeaders();
55
		\sort($separators);
56
		$size=\sizeof($separators);
57
		if($size===1){
58
			foreach ($values as $v){
59
				$form->addField($v);
60
			}
61
		}else{
62
			$separators[]=$count;
63
			for($i=0;$i<$size;$i++){
64
				$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
65
				if(isset($headers[$separators[$i]+1]))
66
					$form->addHeader($headers[$separators[$i]+1],4,true);
67
				//TODO check why $fields is empty
68
				if(\sizeof($fields)===1){
69
					$form->addField($fields[0]);
70
				}elseif(\sizeof($fields)>1){
71
					$form->addFields($fields);
72
				}
73
			}
74
		}
75
	}
76
77
	/**
78
	 * {@inheritDoc}
79
	 * @see \Ajax\common\Widget::getForm()
80
	 */
81
	public function getForm(){
82
		return $this->content["form"];
83
	}
84
85
	public function addSeparatorAfter($fieldNum){
86
		$fieldNum=$this->_getIndex($fieldNum);
87
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
88
		return $this;
89
	}
90
91
	public function getSeparators() {
92
		return $this->_instanceViewer->getSeparators();
93
	}
94
95
	public function setSeparators($separators) {
96
		$this->_instanceViewer->setSeparators($separators);
97
		return $this;
98
	}
99
100
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
101
		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...
102
			$button=new HtmlButton($id,$value,$cssStyle);
103
			$button->setProperty("type", "reset");
104
			return $button;
105
		}, $index,$attributes);
106
	}
107
108
	/**
109
	 * {@inheritDoc}
110
	 * @see \Ajax\common\Widget::getHtmlComponent()
111
	 * @return HtmlForm
112
	 */
113
	public function getHtmlComponent() {
114
		return $this->content["form"];
115
	}
116
	/**
117
	 * {@inheritdoc}
118
	 * @see \Ajax\common\Widget::_setToolbarPosition()
119
	 */
120
	protected function _setToolbarPosition($table, $captions=NULL) {
121
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
122
	}
123
124
	public function addDividerBefore($index,$title){
125
		$index=$this->_getIndex($index);
126
		$this->_instanceViewer->addHeaderDividerBefore($index, $title);
127
		return $this;
128
	}
129
130
	public function run(JsUtils $js){
131
		return parent::run($js);
132
	}
133
}