Completed
Push — master ( 030ef0...22ba15 )
by Jean-Christophe
03:09
created

DataForm::fieldAsReset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
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
 */
19
class DataForm extends Widget {
20
	use FormFieldAsTrait,FormTrait;
21
22 View Code Duplication
	public function __construct($identifier, $modelInstance=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...
23
		parent::__construct($identifier, null,$modelInstance);
24
		$this->_instanceViewer=new FormInstanceViewer();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Ajax\semantic\widge...rm\FormInstanceViewer() of type object<Ajax\semantic\wid...orm\FormInstanceViewer> is incompatible with the declared type object<Ajax\common\InstanceViewer> of property $_instanceViewer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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