HtmlForm::compile()   C
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 43
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
rs 5.3846
cc 8
eloc 38
nc 4
nop 2
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
6
use Ajax\JsUtils;
7
8
use Ajax\common\html\html5\HtmlSelect;
9
10
class HtmlForm extends HtmlBsDoubleElement {
11
	protected $formElementsPrefix;
12
	protected $futureElements;
13
	protected $formGroups;
14
15
	public function __construct($identifier) {
16
		parent::__construct($identifier, "form");
17
		$this->_template='<form id="%identifier%" name="%identifier%" %properties%>%content%</form>';
18
		$this->futureElements=array ();
19
		$this->formGroups=array ();
20
	}
21
22
	/*
23
	 * (non-PHPdoc)
24
	 * @see \Ajax\bootstrap\html\base\BaseHtml::compile()
25
	 */
26
	public function compile(JsUtils $js=NULL, &$view=NULL) {
27
		if (isset($js)) {
28
			$this->formElementsPrefix=$js->config()->getVar("formElementsPrefix");
29
			foreach ( $this->futureElements as $futureElement ) {
30
				$futureElementValue=$this->getPrefix($futureElement);
31
				$futureElementValues=explode("_", $futureElementValue);
32
				switch($futureElementValues [0]) {
33
					case "input":
34
						$control=new HtmlInput($futureElement);
35
						$control->setClass("form-control");
36
						$control->setLabel($this->getPart($futureElement));
37
						break;
38
					case "checkbox":
39
						$control=new HtmlInputCheckbox($futureElement);
40
						$control->setLabel($this->getPart($futureElement), false);
41
						break;
42
					case "radio":
43
						$name=$this->getPart($futureElement);
44
						$label=$this->getPart($futureElement, 2);
45
						$control=new HtmlInputRadio($futureElement);
46
						$control->setProperty("name", strtolower($name));
47
						$control->setLabel($label, false);
48
						break;
49
					case "select":
50
						$control=new HtmlSelect($futureElement);
0 ignored issues
show
Bug introduced by
The call to HtmlSelect::__construct() misses a required argument $caption.

This check looks for function calls that miss required arguments.

Loading history...
51
						$control->setProperty("size", $futureElementValues [1]);
52
						$control->setClass("form-control");
53
						$control->setLabel($this->getPart($futureElement));
0 ignored issues
show
Bug introduced by
The method setLabel() does not seem to exist on object<Ajax\common\html\html5\HtmlSelect>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
						break;
55
					default:
56
						$control=new HtmlInput($futureElement);
57
						$control->setClass("form-control");
58
						$control->setLabel($this->getPart($futureElement));
59
						break;
60
				}
61
				$this->addElement($control);
62
			}
63
		}
64
		foreach ( $this->formGroups as $group ) {
65
			$this->addContent($group);
66
		}
67
		return parent::compile($js, $view);
68
	}
69
70 View Code Duplication
	private function getPart($str, $part=1) {
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...
71
		$result=preg_split('/(?=[A-Z])/', $str);
72
		if (sizeof($result)>$part) {
73
			$result=$result [$part];
74
		} else {
75
			$result=$str;
76
		}
77
		return $result;
78
	}
79
80 View Code Duplication
	private function getId($str) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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...
81
		$result=preg_split('/(?=[A-Z])/', $str);
82
		if (sizeof($result)>2) {
83
			$result=$result [2];
84
		} else {
85
			$result=$str;
86
		}
87
		return $result;
88
	}
89
90
	private function getPrefix($element) {
91
		$result="input_text";
92
		foreach ( $this->formElementsPrefix as $k => $v ) {
93
			if (Text::startsWith($element, $k)) {
94
				$result=$v;
95
				break;
96
			}
97
		}
98
		return $result;
99
	}
100
101
	public function addGroup($identifier="") {
102
		if ($identifier==="")
103
			$identifier="form-".$this->identifier;
104
		$group=new HtmlBsDoubleElement($identifier);
105
		$group->setTagName("div");
106
		$group->setClass("form-group");
107
		$this->formGroups []=$group;
108
		return $group;
109
	}
110
111
	public function addElement($element) {
112
		if (sizeof($this->formGroups)===0) {
113
			$this->addGroup();
114
		}
115
		$group=$this->formGroups [sizeof($this->formGroups)-1];
116
		$group->addContent($element);
117
		return $group;
118
	}
119
120
	public function getElement($name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name 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...
121
		$element=null;
122
		foreach ( $this->formGroups as $group ) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
123
		}
124
		return $element;
125
	}
126
127
	/*
128
	 * (non-PHPdoc)
129
	 * @see \Ajax\bootstrap\html\base\HtmlSingleElement::fromArray()
130
	 */
131
	public function fromArray($array) {
132
		foreach ( $array as $value ) {
133
			if (is_string($value)) {
134
				$this->futureElements []=$value;
135
			}
136
		}
137
	}
138
}