Completed
Push — master ( 41f0f3...f58e0b )
by Jean-Christophe
03:38
created

DataForm::_generateFields()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 13
nc 24
nop 6
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
		$wrappers=$this->_instanceViewer->getWrappers();
56
		\sort($separators);
57
		$size=\sizeof($separators);
58
		if($size===1){
59
			foreach ($values as $v){
60
				$form->addField($v);
61
			}
62
		}else{
63
			$separators[]=$count;
64
			for($i=0;$i<$size;$i++){
65
				$this->_generateFields($form, $values, $headers, $separators[$i], $separators[$i+1], $wrappers);
66
			}
67
		}
68
	}
69
70
	protected function _generateFields($form,$values,$headers,$sepFirst,$sepLast,$wrappers){
71
		$wrapper=null;
72
		$fields=\array_slice($values, $sepFirst+1,$sepLast-$sepFirst);
73
		if(isset($headers[$sepFirst+1]))
74
			$form->addHeader($headers[$sepFirst+1],4,true);
75
			if(isset($wrappers[$sepFirst+1])){
76
				$wrapper=$wrappers[$sepFirst+1];
77
			}
78
			//TODO check why $fields is empty
79
			if(\sizeof($fields)===1){
80
				$added=$form->addField($fields[0]);
81
			}elseif(\sizeof($fields)>1){
82
				$added=$form->addFields($fields);
83
			}
84
			if(isset($wrapper))
85
				$added->wrap($wrapper[0],$wrapper[1]);
0 ignored issues
show
Bug introduced by
The variable $added does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
86
	}
87
88
	/**
89
	 * {@inheritDoc}
90
	 * @see \Ajax\common\Widget::getForm()
91
	 */
92
	public function getForm(){
93
		return $this->content["form"];
94
	}
95
96
	public function addSeparatorAfter($fieldNum){
97
		$fieldNum=$this->_getIndex($fieldNum);
98
		$this->_instanceViewer->addSeparatorAfter($fieldNum);
99
		return $this;
100
	}
101
102
	public function getSeparators() {
103
		return $this->_instanceViewer->getSeparators();
104
	}
105
106
	public function setSeparators($separators) {
107
		$this->_instanceViewer->setSeparators($separators);
108
		return $this;
109
	}
110
111
	public function fieldAsReset($index,$cssStyle=NULL,$attributes=NULL){
112
		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...
113
			$button=new HtmlButton($id,$value,$cssStyle);
114
			$button->setProperty("type", "reset");
115
			return $button;
116
		}, $index,$attributes);
117
	}
118
119
	/**
120
	 * {@inheritDoc}
121
	 * @see \Ajax\common\Widget::getHtmlComponent()
122
	 * @return HtmlForm
123
	 */
124
	public function getHtmlComponent() {
125
		return $this->content["form"];
126
	}
127
	/**
128
	 * {@inheritdoc}
129
	 * @see \Ajax\common\Widget::_setToolbarPosition()
130
	 */
131
	protected function _setToolbarPosition($table, $captions=NULL) {
132
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
133
	}
134
135
	public function addDividerBefore($index,$title){
136
		$index=$this->_getIndex($index);
137
		$this->_instanceViewer->addHeaderDividerBefore($index, $title);
138
		return $this;
139
	}
140
141
	public function addWrapper($index,$contentBefore,$contentAfter=null){
142
		$index=$this->_getIndex($index);
143
		$this->_instanceViewer->addWrapper($index, $contentBefore,$contentAfter);
144
		return $this;
145
	}
146
147
	public function run(JsUtils $js){
148
		return parent::run($js);
149
	}
150
}