Completed
Push — master ( 2c7b4a...2bc7fe )
by Jean-Christophe
03:22
created

DataForm::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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\collections\form\traits\FormTrait;
11
use Ajax\semantic\html\elements\HtmlButton;
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 FormTrait;
22
23
	public function __construct($identifier, $modelInstance=NULL) {
24
		parent::__construct($identifier, null,$modelInstance);
25
		$this->_init(new FormInstanceViewer($identifier), "form", new HtmlForm($identifier), true);
26
	}
27
28
	protected function _getFieldIdentifier($prefix,$name=""){
29
		return $this->identifier."-{$name}-".$this->_instanceViewer->getIdentifier();
30
	}
31
32 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...
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
		return parent::compile($js,$view);
43
	}
44
45
	/**
46
	 * @param HtmlForm $form
47
	 */
48
	protected function _generateContent($form){
49
		$values= $this->_instanceViewer->getValues();
50
		$count=$this->_instanceViewer->count();
51
52
		$separators=$this->_instanceViewer->getSeparators();
53
		$size=\sizeof($separators);
54
		if($size===1){
55
			foreach ($values as $v){
56
				$form->addField($v);
57
			}
58
		}else{
59
			$separators[]=$count;
60
			for($i=0;$i<$size;$i++){
61
				$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
62
				//TODO check why $fields is empty
63
				if(\sizeof($fields)===1){
64
					$form->addField($fields[0]);
65
				}elseif(\sizeof($fields)>1){
66
					$form->addFields($fields);
67
					$i+=\sizeof($fields)-1;
68
				}
69
			}
70
		}
71
	}
72
73
	/**
74
	 * @return HtmlForm
75
	 */
76
	protected function getForm(){
77
		return $this->content["form"];
78
	}
79
80
	public function addSeparatorAfter($fieldNum){
81
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
82
		return $this;
83
	}
84
85
	public function getSeparators() {
86
		return $this->_instanceViewer->getSeparators();
87
	}
88
89
	public function setSeparators($separators) {
90
		$this->_instanceViewer->setSeparators($separators);
91
		return $this;
92
	}
93
94
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
95
		$button=new HtmlButton($identifier,$value,$cssStyle);
96
		$this->_buttonAsSubmit($button,"click",$url,$responseElement);
97
		return $this->addInToolbar($button);
98
	}
99
100
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
101
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$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
			$this->_buttonAsSubmit($button,"click",$url,$responseElement);
104
			return $button;
105
		}, $index,$attributes);
106
	}
107
108
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
109
		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...
110
			$button=new HtmlButton($id,$value,$cssStyle);
111
			$button->setProperty("type", "reset");
112
			return $button;
113
		}, $index,$attributes);
114
	}
115
116
	/**
117
	 * {@inheritDoc}
118
	 * @see \Ajax\common\Widget::getHtmlComponent()
119
	 * @return HtmlForm
120
	 */
121
	public function getHtmlComponent() {
122
		return $this->content["form"];
123
	}
124
	/**
125
	 * {@inheritdoc}
126
	 * @see \Ajax\common\Widget::_setToolbarPosition()
127
	 */
128
	protected function _setToolbarPosition($table, $captions=NULL) {
129
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
130
	}
131
132
	public function setValidationParams(array $_validationParams) {
133
		$this->getForm()->setValidationParams($_validationParams);
134
		return $this;
135
	}
136
}