PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/simbio2/simbio_GUI/form_maker/simbio_form_maker.inc.php

https://github.com/awriel/s3st15_matoa
PHP | 261 lines | 103 code | 28 blank | 130 comment | 2 complexity | 7c74987d9a4804319f251fb14cf97500 MD5 | raw file
  1. <?php
  2. /**
  3. * simbio_form_maker
  4. * Class for creating form with element based on simbio form elements
  5. *
  6. * Copyright (C) 2007,2008 Arie Nugraha (dicarve@yahoo.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. // be sure that this file not accessed directly
  24. if (!defined('INDEX_AUTH')) {
  25. die("can not access this file directly");
  26. } elseif (INDEX_AUTH != 1) {
  27. die("can not access this file directly");
  28. }
  29. require 'simbio_form_element.inc.php';
  30. /**
  31. * A Helper class for containing anything in form
  32. */
  33. class simbio_form_maker_anything extends abs_simbio_form_element
  34. {
  35. public $content = '';
  36. public function out()
  37. {
  38. return $this->content;
  39. }
  40. }
  41. class simbio_form_maker
  42. {
  43. public $submit_target = '_self';
  44. protected $elements = array();
  45. protected $hidden_elements = array();
  46. protected $form_name = '';
  47. protected $form_method = '';
  48. protected $form_action = '';
  49. protected $disable = '';
  50. protected $enable_upload = true;
  51. /**
  52. * Class Constructor
  53. *
  54. * @param string $str_form_name
  55. * @param string $str_form_action
  56. * @param string $str_form_method
  57. * @param boolean $bool_enable_upload
  58. */
  59. public function __construct($str_form_name = 'mainForm', $str_form_action = '', $str_form_method = 'post', $bool_enable_upload = true)
  60. {
  61. $this->form_name = $str_form_name;
  62. $this->form_action = $str_form_action;
  63. $this->form_method = $str_form_method;
  64. $this->enable_upload = $bool_enable_upload;
  65. }
  66. /**
  67. * Method to start form
  68. *
  69. * @return string
  70. */
  71. public function startForm()
  72. {
  73. return '<form name="'.$this->form_name.'" id="'.$this->form_name.'" '.($this->disable?'class="disabled"':'')
  74. .'method="'.$this->form_method.'" '
  75. .'action="'.$this->form_action.'" target="'.$this->submit_target.'"'.($this->enable_upload?' enctype="multipart/form-data"':'').'>';
  76. }
  77. /**
  78. * Method to end form
  79. *
  80. * @return string
  81. */
  82. public function endForm()
  83. {
  84. return '</form>';
  85. }
  86. /**
  87. * Method to printOut form object
  88. *
  89. */
  90. protected function printOut()
  91. {
  92. // please extends this method
  93. }
  94. /**
  95. * Method to add text field to form
  96. *
  97. * @param string $str_elmnt_type
  98. * @param string $str_elmnt_name
  99. * @param string $str_elmnt_label
  100. * @param string $str_elmnt_value
  101. * @param string $str_elmnt_attr
  102. * @param string $str_elmnt_info
  103. * @return void
  104. */
  105. public function addTextField($str_elmnt_type, $str_elmnt_name, $str_elmnt_label, $str_elmnt_value = '', $str_elmnt_attr = '', $str_elmnt_info = '')
  106. {
  107. // create instance
  108. $_form_element = new simbio_fe_text();
  109. // set form element object properties
  110. $_form_element->element_type = $str_elmnt_type;
  111. $_form_element->element_name = $str_elmnt_name;
  112. $_form_element->element_value = $str_elmnt_value;
  113. $_form_element->element_attr = $str_elmnt_attr;
  114. $this->elements[$str_elmnt_name] = array('label' => $str_elmnt_label, 'element' => $_form_element, 'info' => $str_elmnt_info);
  115. }
  116. /**
  117. * Method to add select list field to form
  118. *
  119. * @param string $str_elmnt_name
  120. * @param string $str_elmnt_label
  121. * @param array $array_option
  122. * @param string $str_default_selected
  123. * @param string $str_elmnt_attr
  124. * @param string $str_elmnt_info
  125. * @return void
  126. */
  127. public function addSelectList($str_elmnt_name, $str_elmnt_label, $array_option, $str_default_selected = '', $str_elmnt_attr = '', $str_elmnt_info = '')
  128. {
  129. // create instance
  130. $_form_element = new simbio_fe_select();
  131. // set form element object properties
  132. $_form_element->element_name = $str_elmnt_name;
  133. $_form_element->element_options = $array_option;
  134. $_form_element->element_value = $str_default_selected;
  135. $_form_element->element_attr = $str_elmnt_attr;
  136. $this->elements[$str_elmnt_name] = array('label' => $str_elmnt_label, 'element' => $_form_element, 'info' => $str_elmnt_info);
  137. }
  138. /**
  139. * Method to add checkbox field to form
  140. *
  141. * @param string $str_elmnt_name
  142. * @param string $str_elmnt_label
  143. * @param array $array_chbox
  144. * @param mixed $default_checked
  145. * @param string $str_elmnt_info
  146. * @return void
  147. */
  148. public function addCheckBox($str_elmnt_name, $str_elmnt_label, $array_chbox, $default_checked = '', $str_elmnt_info = '')
  149. {
  150. // create instance
  151. $_form_element = new simbio_fe_checkbox();
  152. // set form element object properties
  153. $_form_element->element_name = $str_elmnt_name;
  154. $_form_element->element_options = $array_chbox;
  155. $_form_element->element_value = $default_checked;
  156. $this->elements[$str_elmnt_name] = array('label' => $str_elmnt_label, 'element' => $_form_element, 'info' => $str_elmnt_info);
  157. }
  158. /**
  159. * Method to add radio button field to form
  160. *
  161. * @param string $str_elmnt_name
  162. * @param string $str_elmnt_label
  163. * @param array $array_option
  164. * @param mixed $default_checked
  165. * @param string $str_elmnt_info
  166. * @return void
  167. */
  168. public function addRadio($str_elmnt_name, $str_elmnt_label, $array_option, $default_checked = '', $str_elmnt_info = '')
  169. {
  170. // create instance
  171. $_form_element = new simbio_fe_radio();
  172. // set form element object properties
  173. $_form_element->element_name = $str_elmnt_name;
  174. $_form_element->element_options = $array_option;
  175. $_form_element->element_value = $default_checked;
  176. $this->elements[$str_elmnt_name] = array('label' => $str_elmnt_label, 'element' => $_form_element, 'info' => $str_elmnt_info);
  177. }
  178. /**
  179. * Method to add date selection field to form
  180. *
  181. * @param string $str_date_elmnt_name
  182. * @param string $str_month_elmnt_name
  183. * @param string $str_year_elmnt_name
  184. * @param string $str_elmnt_label
  185. * @param string $str_date
  186. * @param string $str_elmnt_info
  187. * @return void
  188. */
  189. public function addDateField($str_elmnt_name, $str_elmnt_label, $str_elmnt_value = '', $str_elmnt_attr = '', $str_elmnt_info = '')
  190. {
  191. $this->addTextField('date', $str_elmnt_name, $str_elmnt_label, $str_elmnt_value, $str_elmnt_attr, $str_elmnt_info);
  192. }
  193. /**
  194. * Method to add hidden fields
  195. *
  196. * @param string $str_elmnt_name
  197. * @param string $str_elmnt_value
  198. * @return void
  199. */
  200. public function addHidden($str_elmnt_name, $str_elmnt_value)
  201. {
  202. $_form_element = new simbio_fe_text();
  203. $_form_element->element_type = 'hidden';
  204. $_form_element->element_name = $str_elmnt_name;
  205. $_form_element->element_value = $str_elmnt_value;
  206. $this->hidden_elements[] = $_form_element;
  207. }
  208. /**
  209. * Method to add anything such as text or other HTML element to form
  210. *
  211. * @param string $str_elmnt_label
  212. * @param string $str_content
  213. * @return void
  214. */
  215. public function addAnything($str_elmnt_label, $str_content)
  216. {
  217. $_form_element = new simbio_form_maker_anything();
  218. $_form_element->content = $str_content;
  219. $this->elements[] = array('label' => $str_elmnt_label, 'element' => $_form_element, 'info' => null);
  220. }
  221. /**
  222. * Method to add simbio form elements object directly
  223. *
  224. * @param string $str_elmnt_label
  225. * @param object $obj_simbio_fe
  226. * @return void
  227. */
  228. public function addFormObject($str_elmnt_label, $obj_simbio_fe, $str_elmnt_info = '')
  229. {
  230. $this->elements[$obj_simbio_fe->element_name] = array('label' => $str_elmnt_label, 'element' => $obj_simbio_fe, 'info' => $str_elmnt_info);
  231. }
  232. }
  233. ?>