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

DataForm::getForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
		$separators[]=$count;
50
		for($i=0;$i<\sizeof($separators)-1;$i++){
51
			$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
52
			if(\sizeof($fields)===1){
53
				$form->addField($fields[0]);
54
			}else
55
				$form->addFields($fields);
56
		}
57
	}
58
59
	/**
60
	 * @return HtmlForm
61
	 */
62
	protected function getForm(){
63
		return $this->content["form"];
64
	}
65
66
	public function addSeparatorAfter($fieldNum){
67
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
68
		return $this;
69
	}
70
71
	public function getSeparators() {
72
		return $this->_instanceViewer->getSeparators();
73
	}
74
75
	public function setSeparators($separators) {
76
		$this->_instanceViewer->setSeparators($separators);
77
		return $this;
78
	}
79
80
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
81
		$button=new HtmlButton($identifier,$value,$cssStyle);
82
		$this->_buttonAsSubmit($button,"click",$url,$responseElement);
83
		return $this->addInToolbar($button);
84
	}
85
86
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
87
		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...
88
			$button=new HtmlButton($id,$value,$cssStyle);
89
			$this->_buttonAsSubmit($button,"click",$url,$responseElement);
90
			return $button;
91
		}, $index,$attributes);
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 * @see \Ajax\common\Widget::getHtmlComponent()
97
	 * @return HtmlForm
98
	 */
99
	public function getHtmlComponent() {
100
		return $this->content["form"];
101
	}
102
	/**
103
	 * {@inheritdoc}
104
	 * @see \Ajax\common\Widget::_setToolbarPosition()
105
	 */
106
	protected function _setToolbarPosition($table, $captions=NULL) {
107
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
108
	}
109
110
	public function setValidationParams(array $_validationParams) {
111
		$this->getForm()->setValidationParams($_validationParams);
112
		return $this;
113
	}
114
}