/system/core/Template/TE/form/class.form.php

https://gitlab.com/vanthanhhoh/devlovebook · PHP · 282 lines · 267 code · 14 blank · 1 comment · 4 complexity · d1c9aec4a25b3e5c762496cd95f4fcd2 MD5 · raw file

  1. <?php
  2. class Form
  3. {
  4. private $FormName = '';
  5. private $FormAction = '';
  6. private $FormMethod = '';
  7. private $FormElements = array();
  8. static $CompiledPath = '';
  9. static $BasePath = '';
  10. public $button, $fieldset, $input, $isindex, $label, $legend, $select, $radio, $checkbox, $file, $image;
  11. public function __construct($FormName) {
  12. $this->FormName = $FormName;
  13. Form::$BasePath = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR;
  14. }
  15. public function button($Name) {
  16. return $this->button = new FormButtonElement($Name);
  17. }
  18. public function fieldset($Name) {
  19. return $this->fieldset = new FormFieldsetElement($Name);
  20. }
  21. public function input($Name) {
  22. return $this->input = new FormInputElement($Name);
  23. }
  24. public function isindex($Name) {
  25. return $this->isindex = new FormIsindexElement($Name);
  26. }
  27. public function label($Name) {
  28. return $this->label = new FormLabelElement($Name);
  29. }
  30. public function legend($Name) {
  31. return $this->legend = new FormLegendElement($Name);
  32. }
  33. public function select($Name) {
  34. return $this->select = new FormSelectElement($Name);
  35. }
  36. public function radio($Name) {
  37. return $this->radio = new FormRadioElement($Name);
  38. }
  39. public function checkbox($Name) {
  40. return $this->checkbox = new FormCheckboxElement($Name);
  41. }
  42. public function file($Name) {
  43. return $this->file = new FormFileElement($Name);
  44. }
  45. public function image($Name) {
  46. return $this->image = new FormImageElement($Name);
  47. }
  48. public function SetFormName($FormName = '') {
  49. $this->FormName = $FormName;
  50. return $this;
  51. }
  52. public function Action($FormAction = '') {
  53. $this->FormAction = $FormAction;
  54. return $this;
  55. }
  56. public function Method($FormMethod = 'GET') {
  57. $this->FormMethod = $FormMethod;
  58. return $this;
  59. }
  60. public function AddFormElement($FormElementObject) {
  61. $this->FormElements[] = $FormElementObject;
  62. }
  63. public function Render($Rebuild = false) {
  64. $Vars = array();
  65. $RenderedForm = '';
  66. $i = 0;
  67. foreach($this->FormElements as $ElementName => $Element) {
  68. $i++;
  69. $Vars['var' . $i] = $Element->Element;
  70. }
  71. if(file_exists(Form::$CompiledPath . $this->FormName . '.php') && !$Rebuild) {
  72. //include(Form::$CompiledPath . $this->FormName . '.php');
  73. }
  74. else {
  75. $i = 0;
  76. foreach($this->FormElements as $ElementName => $Element) {
  77. $i++;
  78. $RenderedForm .= '<?php $Field = $Vars[\'var' . $i . '\']; ?>' . PHP_EOL;
  79. $RenderedForm .= $Element->FieldContent();
  80. }
  81. file_put_contents(Form::$CompiledPath . $this->FormName . '.php', $RenderedForm);
  82. }
  83. include(Form::$CompiledPath . $this->FormName . '.php');
  84. }
  85. }
  86. class FormBaseElement {
  87. public $Element = array('ElementType' => '',
  88. 'Name' => '',
  89. 'Value' => '',
  90. 'Type' => '',
  91. 'Class' => '',
  92. 'Style' => '',
  93. 'Content' => '',
  94. 'Label' => '',
  95. 'Prompt' => '',
  96. 'Cols' => '',
  97. 'Rows' => '',
  98. 'Multiple' => false,
  99. 'Options' => array());
  100. public function __construct($Name) {
  101. $this->Element['Name'] = $Name;
  102. return $this;
  103. }
  104. public function ElementType($Name, $Type) {
  105. $this->Element['Name'] = $Name;
  106. $this->Element['ElementType'] = $Type;
  107. return $this;
  108. }
  109. public function Name($Name) {
  110. $this->Element['Name'] = $Name;
  111. return $this;
  112. }
  113. public function Value($Value = '') {
  114. $this->Element['Value'] = $Value;
  115. return $this;
  116. }
  117. protected function FieldType($Type, $ValidTypes, $DefaultType) {
  118. if(!in_array(strtolower($Type), $ValidTypes)) $Type = $DefaultType;
  119. $this->Element['Type'] = $Type;
  120. return $this;
  121. }
  122. public function Content($Value = '') {
  123. $this->Element['Content'] = $Value;
  124. return $this;
  125. }
  126. public function Label($Value = '') {
  127. $this->Element['Label'] = $Value;
  128. return $this;
  129. }
  130. public function Style($Value = '') {
  131. $this->Element['Style'] = $Value;
  132. return $this;
  133. }
  134. public function Prompt($Value = '') {
  135. $this->Element['Prompt'] = $Value;
  136. return $this;
  137. }
  138. public function Cols($Value = '') {
  139. $this->Element['Cols'] = $Value;
  140. return $this;
  141. }
  142. public function Rows($Value = '') {
  143. $this->Element['Rows'] = $Value;
  144. return $this;
  145. }
  146. public function Multiple($Value = true) {
  147. $this->Element['Multiple'] = $Value;
  148. return $this;
  149. }
  150. public function Options($Options = array()) {
  151. $this->Element['Options'] += $Options;
  152. return $this;
  153. }
  154. public function ResetOptions() {
  155. $this->Element['Options'] = array();
  156. return $this;
  157. }
  158. public function DeleteOptions($Name) {
  159. unset($this->Element['Options'][$Name]);
  160. return $this;
  161. }
  162. public function FieldOutput() {
  163. $FieldContent = file_get_contents(Form::$BasePath . 'field_template' . DIRECTORY_SEPARATOR . $this->Element['ElementType'] . '.php');
  164. $FieldContent = str_replace('[@@FieldName@@]', $this->Element['Name'], $FieldContent);
  165. $FieldContent = str_replace('[@@FieldLabel@@]', $this->Element['Label'], $FieldContent);
  166. $FieldContent = str_replace('[@@FieldClass@@]', $this->Element['Class'], $FieldContent);
  167. $FieldContent = str_replace('[@@FieldStyle@@]', $this->Element['Style'], $FieldContent);
  168. return $FieldContent;
  169. }
  170. public function FieldContent() {
  171. $FieldContent = $this->FieldOutput();
  172. return $FieldContent . PHP_EOL;
  173. }
  174. }
  175. class FormButtonElement extends FormBaseElement {
  176. public function __construct($Name) {$this->ElementType($Name, 'button');}
  177. public function Type($Type) {
  178. $ValidTypes = array('button', 'submit', 'reset');
  179. $this->FieldType($Type, $ValidTypes, 'button');
  180. return $this;
  181. }
  182. public function FieldContent() {
  183. $FieldContent = $this->FieldOutput();
  184. $FieldContent = str_replace('[@@FieldValue@@]', '<?php echo $Field[\'Value\'] ?>', $FieldContent);
  185. return $FieldContent . PHP_EOL;
  186. }
  187. }
  188. class FormInputElement extends FormBaseElement {
  189. public function __construct($Name) {$this->ElementType($Name, 'input');}
  190. public function Type($Type) {
  191. $ValidTypes = array('button','checkbox','file','hidden','image','password','radio','reset','submit','text ');
  192. $this->FieldType($Type, $ValidTypes, 'text');
  193. return $this;
  194. }
  195. public function FieldContent() {
  196. $FieldContent = $this->FieldOutput();
  197. $FieldContent = str_replace('[@@FieldValue@@]', '<?php echo $Field[\'Value\'] ?>', $FieldContent);
  198. $FieldContent = str_replace('[@@FieldType@@]', $this->Element['Type'], $FieldContent);
  199. return $FieldContent . PHP_EOL;
  200. }
  201. }
  202. class FormFieldsetElement extends FormBaseElement {
  203. public function __construct($Name) {$this->ElementType($Name, 'fieldset');return $this;}
  204. public function FieldContent() {
  205. $FieldContent = $this->FieldOutput();
  206. $FieldContent = str_replace('[@@FieldContent@@]', $this->Element['Content'], $FieldContent);
  207. return $FieldContent . PHP_EOL;
  208. }
  209. }
  210. class FormIsindexElement extends FormBaseElement {
  211. public function __construct($Name) {$this->ElementType($Name, 'insindex');return $this;}
  212. }
  213. class FormLabelElement extends FormBaseElement {
  214. public function __construct($Name) {$this->ElementType($Name, 'label');return $this;}
  215. }
  216. class FormLegendElement extends FormBaseElement {
  217. public function __construct($Name) {$this->ElementType($Name, 'legend');return $this;}
  218. }
  219. class FormSelectElement extends FormBaseElement {
  220. public function __construct($Name) {$this->ElementType($Name, 'select');return $this;}
  221. public function FieldContent() {
  222. $FieldContent = $this->FieldOutput();
  223. $options = '
  224. <?php foreach($Field[\'Options\'] as $Options):?>
  225. <option value="<?php echo $Options[\'value\'] ?>"<?php if($Options[\'value\'] == $Field[\'Value\']): ?> selected="selected"<?php endif ?>><?php echo $Options[\'text\'] ?></option>
  226. <?php endforeach ?>';
  227. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  228. $FieldContent = str_replace('[@@FieldMultiple@@]', $this->Element['Multiple'] ? 'multiple' : '', $FieldContent);
  229. return $FieldContent . PHP_EOL;
  230. }
  231. }
  232. class FormRadioElement extends FormBaseElement {
  233. public function __construct($Name) {$this->ElementType($Name, 'radio');return $this;}
  234. public function FieldContent() {
  235. $FieldContent = $this->FieldOutput();
  236. $options = '
  237. <?php foreach($Field[\'Options\'] as $Options):?>
  238. <label for="ID_<?php echo $Field[\'Name\'] ?>_<?php echo $Options[\'value\'] ?>">
  239. <input type="radio" name="<?php echo $Field[\'Name\'] ?>" id="ID_<?php echo $Field[\'Name\'] ?>_<?php echo $Options[\'value\'] ?>" class="<?php echo $Field[\'Class\'] ?>" value="<?php echo $Options[\'value\'] ?>"<?php if($Options[\'value\'] == $Field[\'Value\']): ?> checked="checked"<?php endif ?>/>
  240. <?php echo $Options[\'text\'] ?>
  241. </label>
  242. <?php endforeach ?>';
  243. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  244. return $FieldContent . PHP_EOL;
  245. }
  246. }
  247. class FormCheckboxElement extends FormBaseElement {
  248. public function __construct($Name) {$this->ElementType($Name, 'checkbox');return $this;}
  249. public function FieldContent() {
  250. $FieldContent = $this->FieldOutput();
  251. $options = '
  252. <?php foreach($Field[\'Options\'] as $Options):?>
  253. <label for="ID_<?php echo $Field[\'Name\'] ?>_<?php echo $Options[\'value\'] ?>">
  254. <input type="checkbox" name="<?php echo $Field[\'Name\'] ?>[]" id="ID_<?php echo $Field[\'Name\'] ?>_<?php echo $Options[\'value\'] ?>" class="<?php echo $Field[\'Class\'] ?>" value="<?php echo $Options[\'value\'] ?>"<?php if($Options[\'value\'] == $Field[\'Value\']): ?> checked="checked"<?php endif ?>/>
  255. <?php echo $Options[\'text\'] ?>
  256. </label>
  257. <?php endforeach ?>';
  258. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  259. return $FieldContent . PHP_EOL;
  260. }
  261. }
  262. class FormFileElement extends FormBaseElement {
  263. public function __construct($Name) {$this->ElementType($Name, 'file');return $this;}
  264. }
  265. class FormImageElement extends FormBaseElement {
  266. public function __construct($Name) {$this->ElementType($Name, 'image');return $this;}
  267. }
  268. ?>