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); |
|
|
|
|
51
|
|
|
$control->setProperty("size", $futureElementValues [1]); |
52
|
|
|
$control->setClass("form-control"); |
53
|
|
|
$control->setLabel($this->getPart($futureElement)); |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
121
|
|
|
$element=null; |
122
|
|
|
foreach ( $this->formGroups as $group ) { |
|
|
|
|
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
|
|
|
} |
This check looks for function calls that miss required arguments.