Completed
Push — master ( 701cf4...686575 )
by Jean-Christophe
03:12
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\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
		$this->_instanceViewer->setInstance($this->_modelInstance);
33
34
		$form=$this->content["form"];
35
		$this->_generateContent($form);
36
37
		if(isset($this->_toolbar)){
38
			$this->_setToolbarPosition($form);
39
		}
40
		$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"form",PositionInTable::AFTERTABLE]);
41
		return parent::compile($js,$view);
42
	}
43
44
	/**
45
	 * @param HtmlForm $form
46
	 */
47
	protected function _generateContent($form){
48
		$values= $this->_instanceViewer->getValues();
49
		$count=$this->_instanceViewer->count();
50
51
		$separators=$this->_instanceViewer->getSeparators();
52
		$size=\sizeof($separators);
53
		if($size===1){
54
			foreach ($values as $v){
55
				$form->addField($v);
56
			}
57
		}else{
58
			$separators[]=$count;
59
			for($i=0;$i<$size;$i++){
60
				$fields=\array_slice($values, $separators[$i]+1,$separators[$i+1]-$separators[$i]);
61
				//TODO check why $fields is empty
62
				if(\sizeof($fields)===1){
63
					$form->addField($fields[0]);
64
				}elseif(\sizeof($fields)>1){
65
					$form->addFields($fields);
66
					$i+=\sizeof($fields)-1;
67
				}
68
			}
69
		}
70
	}
71
72
	/**
73
	 * {@inheritDoc}
74
	 * @see \Ajax\common\Widget::getForm()
75
	 */
76
	public 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 fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
95
		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...
96
			$button=new HtmlButton($id,$value,$cssStyle);
97
			$button->setProperty("type", "reset");
98
			return $button;
99
		}, $index,$attributes);
100
	}
101
102
	/**
103
	 * {@inheritDoc}
104
	 * @see \Ajax\common\Widget::getHtmlComponent()
105
	 * @return HtmlForm
106
	 */
107
	public function getHtmlComponent() {
108
		return $this->content["form"];
109
	}
110
	/**
111
	 * {@inheritdoc}
112
	 * @see \Ajax\common\Widget::_setToolbarPosition()
113
	 */
114
	protected function _setToolbarPosition($table, $captions=NULL) {
115
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
116
	}
117
118
	public function run(JsUtils $js=NULL){
119
		return parent::run($js);
120
	}
121
}