PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Form/Form_bu.php

https://gitlab.com/vanthanhhoh/devlovebook
PHP | 382 lines | 363 code | 17 blank | 2 comment | 17 complexity | 55494bb32d2423e18eeda1a544e408b2 MD5 | raw file
  1. <?php
  2. if( !defined('VNP_SYSTEM') && !defined('VNP_APPLICATION') ) die('Access denied!');
  3. class Form
  4. {
  5. private $FormName = '';
  6. private $FormAction = '';
  7. private $FormMethod = '';
  8. private $FormElements = array();
  9. static $CompiledPath = '';
  10. static $BasePath = '';
  11. private $Rebuild = false;
  12. static $TemplateDir = '';
  13. public $button, $fieldset, $input, $textarea, $isindex, $label, $legend, $select, $multi_select, $radio, $checkbox, $file, $image;
  14. public function __construct($FormName, $Rebuild = false) {
  15. self::$TemplateDir = Form::$BasePath . 'field_template' . DIRECTORY_SEPARATOR;
  16. $this->Rebuild = $Rebuild;
  17. $this->FormName = $FormName;
  18. Form::$BasePath = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR;
  19. }
  20. public function TemplateDir($Path) {
  21. self::$TemplateDir = $Path;
  22. }
  23. public function button($Name) {
  24. return $this->button = new FormButtonElement($Name);
  25. }
  26. public function fieldset($Name) {
  27. return $this->fieldset = new FormFieldsetElement($Name);
  28. }
  29. public function input($Name) {
  30. return $this->input = new FormInputElement($Name);
  31. }
  32. public function textarea($Name) {
  33. return $this->textarea = new FormTextareaElement($Name);
  34. }
  35. public function isindex($Name) {
  36. return $this->isindex = new FormIsindexElement($Name);
  37. }
  38. public function label($Name) {
  39. return $this->label = new FormLabelElement($Name);
  40. }
  41. public function legend($Name) {
  42. return $this->legend = new FormLegendElement($Name);
  43. }
  44. public function select($Name) {
  45. return $this->select = new FormSelectElement($Name);
  46. }
  47. public function multi_select($Name) {
  48. return $this->multi_select = new FormMultiSelectElement($Name);
  49. }
  50. public function radio($Name) {
  51. return $this->radio = new FormRadioElement($Name);
  52. }
  53. public function checkbox($Name) {
  54. return $this->checkbox = new FormCheckboxElement($Name);
  55. }
  56. public function file($Name) {
  57. return $this->file = new FormFileElement($Name);
  58. }
  59. public function image($Name) {
  60. return $this->image = new FormImageElement($Name);
  61. }
  62. public function SetFormName($FormName = '') {
  63. $this->FormName = $FormName;
  64. return $this;
  65. }
  66. public function Action($FormAction = '') {
  67. $this->FormAction = $FormAction;
  68. return $this;
  69. }
  70. public function Method($FormMethod = 'GET') {
  71. $this->FormMethod = $FormMethod;
  72. return $this;
  73. }
  74. public function AddFormElement($FormElementObject) {
  75. $this->FormElements[] = $FormElementObject;
  76. }
  77. public function Render($Return = true) {
  78. $Vars = array();
  79. $RenderedForm = '';
  80. $i = 0;
  81. foreach($this->FormElements as $ElementName => $Element) {
  82. $i++;
  83. $Vars['var' . $i] = $Element->Element;
  84. }
  85. if(file_exists(Form::$CompiledPath . $this->FormName . '.php') && !$this->Rebuild) {
  86. //include(Form::$CompiledPath . $this->FormName . '.php');
  87. }
  88. else {
  89. $i = 0;
  90. foreach($this->FormElements as $ElementName => $Element) {
  91. $i++;
  92. $RenderedForm .= '<?php $Field = $Vars[\'var' . $i . '\']; ?>' . PHP_EOL;
  93. $RenderedForm .= $Element->FieldContent();
  94. }
  95. file_put_contents(Form::$CompiledPath . $this->FormName . '.php', $RenderedForm);
  96. }
  97. if($Return) {
  98. $FormStr = '';
  99. ob_start();
  100. include(Form::$CompiledPath . $this->FormName . '.php');
  101. $FormStr = ob_get_clean();
  102. return $FormStr;
  103. }
  104. else include(Form::$CompiledPath . $this->FormName . '.php');
  105. }
  106. }
  107. class FormBaseElement {
  108. public $Element = array('ElementType' => '',
  109. 'Name' => '',
  110. 'Value' => '',
  111. 'Type' => '',
  112. 'Class' => '',
  113. 'Style' => '',
  114. 'Content' => '',
  115. 'Label' => '',
  116. 'Prompt' => '',
  117. 'Cols' => '',
  118. 'Rows' => '',
  119. 'Required' => false,
  120. 'Multiple' => false,
  121. 'Options' => array(),
  122. 'StaticOptions' => false);
  123. public function __construct($Name) {
  124. $this->Element['Name'] = $Name;
  125. return $this;
  126. }
  127. public function ElementType($Name, $Type) {
  128. $this->Element['Name'] = $Name;
  129. $this->Element['ElementType'] = $Type;
  130. return $this;
  131. }
  132. public function Name($Name) {
  133. $this->Element['Name'] = $Name;
  134. return $this;
  135. }
  136. public function Value($Value = '') {
  137. $this->Element['Value'] = $Value;
  138. return $this;
  139. }
  140. protected function FieldType($Type, $ValidTypes, $DefaultType) {
  141. if(!in_array(strtolower($Type), $ValidTypes)) $Type = $DefaultType;
  142. $this->Element['Type'] = $Type;
  143. return $this;
  144. }
  145. public function Content($Value = '') {
  146. $this->Element['Content'] = $Value;
  147. return $this;
  148. }
  149. public function Label($Value = '') {
  150. $this->Element['Label'] = $Value;
  151. return $this;
  152. }
  153. public function Required($Value = true) {
  154. $this->Element['Required'] = $Value;
  155. return $this;
  156. }
  157. public function FieldClass($Value = '') {
  158. $this->Element['Class'] = $Value;
  159. return $this;
  160. }
  161. public function Style($Value = '') {
  162. $this->Element['Style'] = $Value;
  163. return $this;
  164. }
  165. public function Prompt($Value = '') {
  166. $this->Element['Prompt'] = $Value;
  167. return $this;
  168. }
  169. public function Cols($Value = '') {
  170. $this->Element['Cols'] = $Value;
  171. return $this;
  172. }
  173. public function Rows($Value = '') {
  174. $this->Element['Rows'] = $Value;
  175. return $this;
  176. }
  177. public function Multiple($Value = true) {
  178. $this->Element['Multiple'] = $Value;
  179. return $this;
  180. }
  181. public function Options($Options = array()) {
  182. $this->Element['Options'] += $Options;
  183. return $this;
  184. }
  185. public function StaticOptions($S = false) {
  186. $this->Element['StaticOptions'] += $S;
  187. return $this;
  188. }
  189. public function ResetOptions() {
  190. $this->Element['Options'] = array();
  191. return $this;
  192. }
  193. public function DeleteOptions($Name) {
  194. unset($this->Element['Options'][$Name]);
  195. return $this;
  196. }
  197. public function FieldOutput() {
  198. $FieldContent = file_get_contents(Form::$TemplateDir . $this->Element['ElementType'] . '.php');
  199. if($this->Element['Required']) {
  200. $this->Element['Class'] .= ' RequiredField';
  201. $this->Element['Label'] .= '<span class="RequireField">*</span>';
  202. }
  203. $FieldContent = str_replace('[@@FieldName@@]', $this->Element['Name'], $FieldContent);
  204. $FieldContent = str_replace('[@@FieldLabel@@]', $this->Element['Label'], $FieldContent);
  205. $FieldContent = str_replace('[@@FieldClass@@]', $this->Element['Class'], $FieldContent);
  206. $FieldContent = str_replace('[@@FieldStyle@@]', $this->Element['Style'], $FieldContent);
  207. return $FieldContent;
  208. }
  209. public function FieldContent() {
  210. $FieldContent = $this->FieldOutput();
  211. return $FieldContent . PHP_EOL;
  212. }
  213. }
  214. class FormButtonElement extends FormBaseElement {
  215. public function __construct($Name) {$this->ElementType($Name, 'button');}
  216. public function Type($Type) {
  217. $ValidTypes = array('button', 'submit', 'reset');
  218. $this->FieldType($Type, $ValidTypes, 'button');
  219. return $this;
  220. }
  221. public function FieldContent() {
  222. $FieldContent = $this->FieldOutput();
  223. $FieldContent = str_replace('[@@FieldValue@@]', '<?php echo $Field[\'Value\'] ?>', $FieldContent);
  224. return $FieldContent . PHP_EOL;
  225. }
  226. }
  227. class FormInputElement extends FormBaseElement {
  228. public function __construct($Name) {$this->ElementType($Name, 'input');}
  229. public function Type($Type) {
  230. $ValidTypes = array('button','checkbox','file','hidden','image','password','radio','reset','submit','text','number');
  231. $this->FieldType($Type, $ValidTypes, 'text');
  232. return $this;
  233. }
  234. public function FieldContent() {
  235. $FieldContent = $this->FieldOutput();
  236. $FieldContent = str_replace('[@@FieldValue@@]', '<?php echo $Field[\'Value\'] ?>', $FieldContent);
  237. $FieldContent = str_replace('[@@FieldType@@]', $this->Element['Type'], $FieldContent);
  238. return $FieldContent . PHP_EOL;
  239. }
  240. }
  241. class FormTextareaElement extends FormBaseElement {
  242. public function __construct($Name) {$this->ElementType($Name, 'textarea');}
  243. public function FieldContent() {
  244. $FieldContent = $this->FieldOutput();
  245. $FieldContent = str_replace('[@@FieldValue@@]', '<?php echo $Field[\'Value\'] ?>', $FieldContent);
  246. //$FieldContent = str_replace('[@@FieldType@@]', $this->Element['Type'], $FieldContent);
  247. return $FieldContent . PHP_EOL;
  248. }
  249. }
  250. class FormFieldsetElement extends FormBaseElement {
  251. public function __construct($Name) {$this->ElementType($Name, 'fieldset');return $this;}
  252. public function FieldContent() {
  253. $FieldContent = $this->FieldOutput();
  254. $FieldContent = str_replace('[@@FieldContent@@]', $this->Element['Content'], $FieldContent);
  255. return $FieldContent . PHP_EOL;
  256. }
  257. }
  258. class FormIsindexElement extends FormBaseElement {
  259. public function __construct($Name) {$this->ElementType($Name, 'insindex');return $this;}
  260. }
  261. class FormLabelElement extends FormBaseElement {
  262. public function __construct($Name) {$this->ElementType($Name, 'label');return $this;}
  263. }
  264. class FormLegendElement extends FormBaseElement {
  265. public function __construct($Name) {$this->ElementType($Name, 'legend');return $this;}
  266. }
  267. class FormSelectElement extends FormBaseElement {
  268. public function __construct($Name) {$this->ElementType($Name, 'select');return $this;}
  269. public function FieldContent() {
  270. $FieldContent = $this->FieldOutput();
  271. if(!$this->Element['StaticOptions']) {
  272. $options = '<?php foreach($Field[\'Options\'] as $Options):?>
  273. <option value="<?php echo $Options[\'value\'] ?>"<?php if($Options[\'value\'] == $Field[\'Value\']): ?> selected="selected"<?php endif ?>><?php echo $Options[\'text\'] ?></option>
  274. <?php endforeach ?>';
  275. }
  276. else {
  277. $options = array();
  278. foreach($this->Element['Options'] as $Opt) {
  279. $options[] = '<option value="' . $Opt['value'] . '"<?php if(\'' . $Opt['value'] . '\' == $Field[\'Value\']): ?> selected="selected"<?php endif ?>>' . $Opt['text'] . '</option>';
  280. }
  281. $options = implode(PHP_EOL, $options);
  282. }
  283. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  284. $FieldContent = str_replace('[@@FieldMultiple@@]', $this->Element['Multiple'] ? 'multiple' : '', $FieldContent);
  285. return $FieldContent . PHP_EOL;
  286. }
  287. }
  288. class FormMultiSelectElement extends FormBaseElement {
  289. public function __construct($Name) {$this->ElementType($Name, 'multi_select');return $this;}
  290. public function FieldContent() {
  291. $FieldContent = $this->FieldOutput();
  292. if(!$this->Element['StaticOptions']) {
  293. $options = '<?php $ArrValue = explode(\',\',$Field[\'Value\']); foreach($Field[\'Options\'] as $Options):?>
  294. <option value="<?php echo $Options[\'value\'] ?>"<?php if(in_array($Options[\'value\'],$ArrValue)): ?> selected="selected"<?php endif ?>><?php echo $Options[\'text\'] ?></option>
  295. <?php endforeach ?>';
  296. }
  297. else {
  298. $options = array();
  299. foreach($this->Element['Options'] as $Opt) {
  300. $options[] = '<option value="' . $Opt['value'] . '"<?php if(in_array(\'' . $Opt['value'] . '\',$ArrValue)): ?> selected="selected"<?php endif ?>>' . $Opt['text'] . '</option>';
  301. }
  302. $options = '<?php $ArrValue = explode(\',\',$Field[\'Value\']); ?>' . implode(PHP_EOL, $options);
  303. }
  304. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  305. $FieldContent = str_replace('[@@FieldMultiple@@]', $this->Element['Multiple'] ? 'multiple' : '', $FieldContent);
  306. return $FieldContent . PHP_EOL;
  307. }
  308. }
  309. class FormRadioElement extends FormBaseElement {
  310. public function __construct($Name) {$this->ElementType($Name, 'radio');return $this;}
  311. public function FieldContent() {
  312. $FieldContent = $this->FieldOutput();
  313. if(!$this->Element['StaticOptions']) {
  314. $options = '
  315. <?php foreach($Field[\'Options\'] as $Options):?>
  316. <label for="ID_<?php echo $Field[\'Name\'] ?>_<?php echo $Options[\'value\'] ?>">
  317. <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 ?>/>
  318. <?php echo $Options[\'text\'] ?>
  319. </label>
  320. <?php endforeach ?>';
  321. }
  322. else {
  323. $options = array();
  324. foreach($this->Element['Options'] as $Opt) {
  325. $options[] = '<label for="ID_' . $this->Element['Name'] . '_' . $Opt['value'] . '">
  326. <input type="radio" name="' . $this->Element['Name'] . '" id="ID_' . $this->Element['Name'] . '_' . $Opt['value'] . '" class="' . $this->Element['Class'] . '" value="' . $Opt['value'] . '"<?php if(\'' . $Opt['value'] . '\' == $Field[\'Value\']): ?> checked="checked"<?php endif ?>/>' . $Opt['text'] . '</label>';
  327. }
  328. $options = implode(PHP_EOL, $options);
  329. }
  330. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  331. return $FieldContent . PHP_EOL;
  332. }
  333. }
  334. class FormCheckboxElement extends FormBaseElement {
  335. public function __construct($Name) {$this->ElementType($Name, 'checkbox');return $this;}
  336. public function FieldContent() {
  337. $FieldContent = $this->FieldOutput();
  338. if(!$this->Element['StaticOptions']) {
  339. $options = '
  340. <?php $ArrValue = explode(\',\',$Field[\'Value\']); foreach($Field[\'Options\'] as $Options):?>
  341. <label for="ID_<?php echo $Field[\'Name\'] ?>_<?php echo $Options[\'value\'] ?>">
  342. <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(in_array($Options[\'value\'],$ArrValue)): ?> checked="checked"<?php endif ?>/>
  343. <?php echo $Options[\'text\'] ?>
  344. </label>
  345. <?php endforeach ?>';
  346. }
  347. else {
  348. $options = array();
  349. foreach($this->Element['Options'] as $Opt) {
  350. $options[] = '<label for="ID_' . $this->Element['Name'] . '_' . $Opt['value'] . '">
  351. <input type="checkbox" name="' . $this->Element['Name'] . '[]" id="ID_' . $this->Element['Name'] . '_' . $Opt['value'] . '" class="' . $this->Element['Class'] . '" value="' . $Opt['value'] . '"<?php if(in_array(\'' . $Opt['value'] . '\', $ArrValue)): ?> checked="checked"<?php endif ?>/>' . $Opt['text'] . '</label>';
  352. }
  353. $options = '<?php $ArrValue = explode(\',\',$Field[\'Value\']); ?>' . implode(PHP_EOL, $options);
  354. }
  355. $FieldContent = str_replace('[@@FieldOptions@@]', $options, $FieldContent);
  356. return $FieldContent . PHP_EOL;
  357. }
  358. }
  359. class FormFileElement extends FormBaseElement {
  360. public function __construct($Name) {$this->ElementType($Name, 'file');return $this;}
  361. }
  362. class FormImageElement extends FormBaseElement {
  363. public function __construct($Name) {$this->ElementType($Name, 'image');return $this;}
  364. }
  365. ?>