PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/simbio2/simbio_GUI/form_maker/simbio_form_element.inc.php

https://gitlab.com/mucill/sman7
PHP | 399 lines | 260 code | 37 blank | 102 comment | 50 complexity | 0ebed90f52719feba77673fc4bceb754 MD5 | raw file
  1. <?php
  2. /**
  3. * simbio_form_element
  4. * Collection of Form Element Class
  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. /* abstract form element class to be inherited by form element classes */
  30. abstract class abs_simbio_form_element
  31. {
  32. public $element_type = 'text';
  33. public $element_name = '';
  34. public $element_value;
  35. public $element_options;
  36. public $element_attr = '';
  37. public $element_css_class = '';
  38. public $element_disabled = false;
  39. public $element_helptext = '';
  40. /**
  41. * Below method must be inherited
  42. *
  43. * @return string
  44. */
  45. abstract protected function out();
  46. }
  47. /* Text field object */
  48. class simbio_fe_text extends abs_simbio_form_element
  49. {
  50. public function out()
  51. {
  52. $_buffer = '';
  53. if (!in_array($this->element_type, array('textarea', 'text', 'password', 'button', 'file', 'hidden', 'submit', 'button', 'reset', 'date'))) {
  54. return 'Unrecognized element type!';
  55. }
  56. // check if disabled
  57. if ($this->element_disabled) {
  58. $_disabled = ' disabled="disabled"';
  59. } else { $_disabled = ''; }
  60. if ($this->element_helptext) {
  61. $this->element_attr .= ' title="'.$this->element_helptext.'"';
  62. }
  63. // maxlength attribute
  64. if (!stripos($this->element_attr, 'maxlength')) {
  65. if ($this->element_type == 'text') {
  66. $this->element_attr .= ' maxlength="256"';
  67. } else if ($this->element_type == 'textarea') {
  68. $this->element_attr .= ' maxlength="'.(30*1024).'"';
  69. }
  70. }
  71. // sanitize name for ID
  72. $_elID = str_replace(array('[', ']', ' '), '', $this->element_name);
  73. // checking element type
  74. if ($this->element_type == 'textarea') {
  75. $_buffer .= '<textarea name="'.$this->element_name.'" id="'.$_elID.'" '.$this->element_attr.''.$_disabled.'>';
  76. $_buffer .= $this->element_value;
  77. $_buffer .= '</textarea>'."\n";
  78. } else if (stripos($this->element_type, 'date', 0) !== false) {
  79. $_buffer .= '<div class="dateField"><input class="dateInput" type="'.$this->element_type.'" name="'.$this->element_name.'" id="'.$_elID.'" ';
  80. $_buffer .= 'value="'.$this->element_value.'" '.$this->element_attr.''.$_disabled.' /><a class="calendarLink notAJAX" style="cursor: pointer;" onclick="javascript: dateType = \''.$this->element_type.'\'; openCalendar(\''.$_elID.'\');" title="Open Calendar"></a></div>'."\n";
  81. } else {
  82. $_buffer .= '<input type="'.$this->element_type.'" name="'.$this->element_name.'" id="'.$_elID.'" ';
  83. $_buffer .= 'value="'.$this->element_value.'" '.$this->element_attr.''.$_disabled.' />'."\n";
  84. }
  85. return $_buffer;
  86. }
  87. }
  88. /* Drop Down Select List object */
  89. class simbio_fe_select extends abs_simbio_form_element
  90. {
  91. public function out()
  92. {
  93. // check for $array_option param
  94. if (!is_array($this->element_options)) {
  95. return '<select name="'.$this->element_name.'" '.$this->element_attr.'></select>';
  96. }
  97. // check if disabled
  98. if ($this->element_disabled) {
  99. $_disabled = ' disabled="disabled"';
  100. } else { $_disabled = ''; }
  101. if ($this->element_helptext) {
  102. $this->element_attr .= ' title="'.$this->element_helptext.'"';
  103. }
  104. $_buffer = '<select name="'.$this->element_name.'" id="'.$this->element_name.'" '.$this->element_attr.''.$_disabled.'>'."\n";
  105. foreach ($this->element_options as $option) {
  106. if (is_string($option)) {
  107. // if the selected element is an array then
  108. // the selected option is also multiple to
  109. if (is_array($this->element_value)) {
  110. $_buffer .= '<option value="'.$option.'" '.(in_array($option, $this->element_value)?'selected':'').'>';
  111. $_buffer .= $option.'</option>'."\n";
  112. } else {
  113. $_buffer .= '<option value="'.$option.'" '.(($option == $this->element_value)?'selected':'').'>';
  114. $_buffer .= $option.'</option>'."\n";
  115. }
  116. } else {
  117. if (is_array($this->element_value)) {
  118. $_buffer .= '<option value="'.$option[0].'" '.(in_array($option[0], $this->element_value)?'selected':'').'>';
  119. $_buffer .= $option[1].'</option>'."\n";
  120. } else {
  121. $_buffer .= '<option value="'.$option[0].'" '.(($option[0] == $this->element_value)?'selected':'').'>';
  122. $_buffer .= $option[1].'</option>'."\n";
  123. }
  124. }
  125. }
  126. $_buffer .= '</select>'."\n";
  127. return $_buffer;
  128. }
  129. }
  130. /* AJAX drop down select list object */
  131. class simbio_fe_AJAX_select extends abs_simbio_form_element
  132. {
  133. /**
  134. * AJAX drop down special properties
  135. */
  136. public $handler_URL = 'about:blank';
  137. public $element_dd_list_class = 'ajaxDDlist';
  138. public $element_dd_list_default_text = 'SEARCHING...';
  139. public $additional_params = '';
  140. public function out()
  141. {
  142. $_buffer = '<input type="text" autocomplete="off" id="'.$this->element_name.'" name="'.$this->element_name.'" class="'.$this->element_css_class.' notAJAX" onkeyup="showDropDown(\''.$this->handler_URL.'\', \''.$this->element_name.'\', \''.$this->additional_params.'\')" value="'.$this->element_value.'" />';
  143. $_buffer .= '<ul class="'.$this->element_dd_list_class.'" id="'.$this->element_name.'List"><li style="padding: 2px; font-weight: bold;">'.$this->element_dd_list_default_text.'</li></ul>';
  144. return $_buffer;
  145. }
  146. }
  147. /* Checkbox button groups object */
  148. class simbio_fe_checkbox extends abs_simbio_form_element
  149. {
  150. public function out()
  151. {
  152. // check for $this->element_options param
  153. if (!is_array($this->element_options)) {
  154. return 'The radio button options list must be an array';
  155. } else {
  156. foreach ($this->element_options as $cbox) {
  157. // if the $cbox is not an array
  158. if (!is_array($cbox)) {
  159. return 'The radio button options list must be a 2 multi-dimensional array';
  160. }
  161. }
  162. }
  163. $_elmnt_num = count($this->element_options);
  164. $_row_column = 5;
  165. $_helptext = '';
  166. // check if disabled
  167. if ($this->element_disabled) {
  168. $_disabled = ' disabled="disabled"';
  169. } else { $_disabled = ''; }
  170. if ($this->element_helptext) {
  171. $_helptext .= ' title="'.$this->element_helptext.'"';
  172. }
  173. $_buffer = '';
  174. if ($_elmnt_num <= $_row_column) {
  175. foreach ($this->element_options as $_cbox) {
  176. if (is_array($this->element_value)) {
  177. $_buffer .= '<div '.$_helptext.'><input type="checkbox" name="'.$this->element_name.'[]"'
  178. .' value="'.$_cbox[0].'" style="border: 0;" '.(in_array($_cbox[0], $this->element_value)?'checked':'').$_disabled.' />'
  179. .' '.$_cbox[1]."</div>\n";
  180. } else {
  181. $_buffer .= '<div '.$_helptext.'><input type="checkbox" name="'.$this->element_name.'[]"'
  182. .' value="'.$_cbox[0].'" style="border: 0;" '.(($_cbox[0] == $this->element_value)?'checked':'').$_disabled.' />'
  183. .' '.$_cbox[1]."</div>\n";
  184. }
  185. }
  186. } else {
  187. $_column_array = array_chunk($this->element_options, $_row_column);
  188. $_buffer = '<table '.$_helptext.'>'."\n";
  189. $_buffer .= '<tr>'."\n";
  190. foreach ($_column_array as $_chunked_options) {
  191. $_buffer .= '<td valign="top">'."\n";
  192. foreach ($_chunked_options as $_cbox) {
  193. if (is_array($this->element_value)) {
  194. $_buffer .= '<div><input type="checkbox" name="'.$this->element_name.'[]"'
  195. .' value="'.$_cbox[0].'" style="border: 0;" '.(in_array($_cbox[0], $this->element_value)?'checked':'').$_disabled.' />'
  196. .' '.$_cbox[1]."</div>\n";
  197. } else {
  198. $_buffer .= '<div><input type="checkbox" name="'.$this->element_name.'[]"'
  199. .' value="'.$_cbox[0].'" style="border: 0;" '.(($_cbox[0] == $this->element_value)?'checked':'').$_disabled.' />'
  200. .' '.$_cbox[1]."</div>\n";
  201. }
  202. }
  203. $_buffer .= '</td>'."\n";
  204. }
  205. $_buffer .= '</tr>'."\n";
  206. $_buffer .= '</table>'."\n";
  207. }
  208. return $_buffer;
  209. }
  210. }
  211. /* Radio button groups object */
  212. class simbio_fe_radio extends abs_simbio_form_element
  213. {
  214. public function out()
  215. {
  216. // check for $this->element_options param
  217. if (!is_array($this->element_options)) {
  218. return 'The third argument must be an array';
  219. }
  220. $_buffer = '';
  221. // number of element in each column
  222. if (count($this->element_options) > 10) {
  223. $_elmnt_each_column = 4;
  224. } else {
  225. $_elmnt_each_column = 2;
  226. }
  227. $_helptext = '';
  228. if ($this->element_helptext) {
  229. $_helptext .= ' title="'.$this->element_helptext.'"';
  230. }
  231. // chunk the array into pieces of array
  232. $_chunked_array = array_chunk($this->element_options, $_elmnt_each_column, true);
  233. $_buffer .= '<table '.$_helptext.'>'."\n";
  234. $_buffer .= '<tr>'."\n";
  235. foreach ($_chunked_array as $_chunk) {
  236. $_buffer .= '<td valign="top">';
  237. foreach ($_chunk as $_radio) {
  238. if ($_radio[0] == $this->element_value) {
  239. $_buffer .= '<div><input type="radio" name="'.$this->element_name.'" id="'.$this->element_name.'"'
  240. .' value="'.$_radio[0].'" style="border: 0;" checked />'
  241. .' '.$_radio[1]."</div>\n";
  242. } else {
  243. $_buffer .= '<div><input type="radio" name="'.$this->element_name.'" id="'.$this->element_name.'"'
  244. .' value="'.$_radio[0].'" style="border: 0;" />'
  245. .' '.$_radio[1]."</div>\n";
  246. }
  247. }
  248. $_buffer .= '</td>';
  249. }
  250. $_buffer .= '</tr>'."\n";
  251. $_buffer .= '</table>'."\n";
  252. return $_buffer;
  253. }
  254. }
  255. /* Date field */
  256. /* Global vars containing date and month array */
  257. $simbio_fe_date_array = array( array('01', strtoupper(__('Date')), '01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
  258. '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
  259. '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'));
  260. $simbio_fe_date_month_array = array( array('01', strtoupper(__('Month')), array('01', 'January'), array('02', 'February'), array('03', 'March'),
  261. array('04', 'April'), array('05', 'May'), array('06', 'June'),
  262. array('07', 'July'), array('08', 'August'), array('09', 'September'),
  263. array('10', 'October'), array('11', 'November'), array('12', 'December')));
  264. /* Depecrated class for compability with older code */
  265. class simbio_form_element
  266. {
  267. /**
  268. * Static Method to create input field form element
  269. *
  270. * @param string $str_elmnt_type
  271. * @param string $str_elmnt_name
  272. * @param string $str_elmnt_value
  273. * @param string $str_elmnt_attr
  274. */
  275. public static function textField($str_elmnt_type, $str_elmnt_name, $str_elmnt_value = '', $str_elmnt_attr = '')
  276. {
  277. $_textField = new simbio_fe_text();
  278. $_textField->element_type = $str_elmnt_type;
  279. $_textField->element_name = $str_elmnt_name;
  280. $_textField->element_value = $str_elmnt_value;
  281. $_textField->element_attr = $str_elmnt_attr;
  282. return $_textField->out();
  283. }
  284. /**
  285. * Static Method to create form element
  286. *
  287. * @param string $str_elmnt_name
  288. * @param array $array_option
  289. * @param string $str_default_selected
  290. * @param string $str_elmnt_attr
  291. * @return string
  292. */
  293. public static function selectList($str_elmnt_name, $array_option, $str_default_selected = '', $str_elmnt_attr = '')
  294. {
  295. $_selectList = new simbio_fe_select();
  296. $_selectList->element_name = $str_elmnt_name;
  297. $_selectList->element_value = $str_default_selected;
  298. $_selectList->element_attr = $str_elmnt_attr;
  299. $_selectList->element_options = $array_option;
  300. return $_selectList->out();
  301. }
  302. /**
  303. * Static Method to create form element
  304. *
  305. * @param string $str_elmnt_name
  306. * @param string $str_elmnt_label
  307. * @param array $array_chbox
  308. * @return string
  309. */
  310. public static function checkBox($str_elmnt_name, $array_chbox, $default_checked = '')
  311. {
  312. $_checkBox = new simbio_fe_checkbox();
  313. $_checkBox->element_name = $str_elmnt_name;
  314. $_checkBox->element_value = $default_checked;
  315. $_checkBox->element_options = $array_chbox;
  316. return $_checkBox->out();
  317. }
  318. /**
  319. * Static Method to create form element
  320. *
  321. * @param string $str_elmnt_name
  322. * @param array $array_option
  323. * @param string $str_default_selected
  324. * @return string
  325. */
  326. public static function radioButton($str_elmnt_name, $array_option, $default_checked = '')
  327. {
  328. $_radio = new simbio_fe_select();
  329. $_radio->element_name = $str_elmnt_name;
  330. $_radio->element_value = $default_checked;
  331. $_radio->element_options = $array_option;
  332. return $_radio->out();
  333. }
  334. /**
  335. * Static Method to create date input field form element
  336. *
  337. * @param string $str_date_elmnt_name
  338. * @param string $str_month_elmnt_name
  339. * @param string $str_year_elmnt_name
  340. * @param string $str_date
  341. * @return string
  342. */
  343. public static function dateField($str_elmnt_name, $str_elmnt_value = '', $str_elmnt_attr = '')
  344. {
  345. return self::textField('date', $str_elmnt_name, $str_elmnt_value, $str_elmnt_attr);
  346. }
  347. /**
  348. * Static Method to create form element
  349. *
  350. * @param string $str_date_elmnt_name
  351. * @param string $str_elmnt_value
  352. * @return string
  353. */
  354. public static function hiddenField($str_elmnt_name, $str_elmnt_value)
  355. {
  356. $_textField = new simbio_fe_text();
  357. $_textField->element_type = 'hidden';
  358. $_textField->element_name = $str_elmnt_name;
  359. $_textField->element_value = $str_elmnt_value;
  360. return $_textField->out();
  361. }
  362. }