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

/add-ons/ext_formbuilder/fragment/forms/functions.php

https://github.com/iconifyit/SkyBlue-1.1
PHP | 428 lines | 344 code | 29 blank | 55 comment | 59 complexity | 4ac1ab88e1c2c7b2c7110475fcae5351 MD5 | raw file
  1. <?php
  2. defined('SKYBLUE') or die(basename(__FILE__));
  3. /*
  4. * Processes the submitted form
  5. * @param object The form object
  6. * @param array The form field objects
  7. * @return mixed
  8. */
  9. function handle_formbuilder_action(&$form, &$fields, $Request) {
  10. global $Core;
  11. $action = Filter::get($form, 'action');
  12. if ($Core->isRegistered($form->callback_event)) {
  13. $Core->trigger(
  14. $form->callback_event,
  15. array(
  16. 'request' => $Request,
  17. 'form' => $form,
  18. 'fields' => $fields
  19. )
  20. );
  21. }
  22. return true;
  23. }
  24. /*
  25. * Gets the fields for the form indicated by $formid
  26. * @param int The unique ID of the form
  27. * @param array All of the fiedls from storage
  28. * @return array The filtered list of fields for the current form
  29. */
  30. function formbuilder_get_fields($formId, $fields) {
  31. $filtered = array();
  32. if (empty($fields)) return $filtered;
  33. foreach ($fields as $field) {
  34. if ($field->formid == $formId) {
  35. array_push($filtered, $field);
  36. }
  37. }
  38. return $filtered;
  39. }
  40. /*
  41. * Determine the action triggered by the user.
  42. * @param object The Request object
  43. * @return string The name of the action
  44. */
  45. function formbuilder_action($Request) {
  46. return strToLower(Filter::get($Request, 'action', ''));
  47. }
  48. /*
  49. * Validates the posted form field values
  50. * @param object The current form data object
  51. * @param array The fields associated with the current object
  52. * @param object The HTTP Request object
  53. * @return array An array of validation errors
  54. */
  55. function formbuilder_validate($fields, $Request) {
  56. global $Core;
  57. $errors = array();
  58. $errorFields = array();
  59. foreach ($fields as $field) {
  60. if (!empty($field->validation)) {
  61. $validations = explode(',', $field->validation);
  62. for ($i=0; $i<count($validations); $i++) {
  63. $validations[$i] = trim($validations[$i]);
  64. if (empty($validations[$i])) continue;
  65. $option = "";
  66. if ($validations[$i] == "minlength") {
  67. $option = Filter::get($field, 'minlength');
  68. }
  69. if ($validations[$i] == "maxlength") {
  70. $option = Filter::get($field, 'maxlength');
  71. }
  72. if ($validations[$i] == "regex") {
  73. $option = Filter::get($field, 'regex');
  74. if (!empty($option)) {
  75. $option = base64_decode($option);
  76. }
  77. }
  78. if (!formbuilder_do_validation($Request->get($field->title), $validations[$i], $option)) {
  79. $fieldName = $field->title;
  80. if (in_array($fieldName, $errorFields)) continue;
  81. array_push($errorFields, $fieldName);
  82. $Request->$fieldName = "";
  83. $label = urldecode(base64_decode($field->label));
  84. $errorMessage = "Please enter valid data for '$label'";
  85. $fieldErrorMessage = Filter::get($field, 'validation_error_message');
  86. if (!empty($fieldErrorMessage)) {
  87. $errorMessage = base64_decode($fieldErrorMessage);
  88. }
  89. array_push($errors, array('errStr'=>$errorMessage, 'fieldName'=>$fieldName));
  90. }
  91. }
  92. }
  93. }
  94. return $errors;
  95. }
  96. /*
  97. * Performs the field validation
  98. * @param string The field value
  99. * @param string The validation type
  100. * @return boolean Whether or not the field value matches the validation type
  101. */
  102. function formbuilder_do_validation($value, $validation, $option="") {
  103. switch ($validation) {
  104. case 'notempty':
  105. case 'notnull':
  106. return trim($value) == "" ? false : true ;
  107. break;
  108. case 'number':
  109. return ereg(SB_REGEX_NUM, $value);
  110. break;
  111. case 'email':
  112. return eregi(SB_REGEX_EMAIL, $value);
  113. break;
  114. case 'url':
  115. return preg_match(SB_REGEX_URL, $value);
  116. break;
  117. case 'minlength':
  118. return strlen($value) >= $option ;
  119. break;
  120. case 'maxlength':
  121. return strlen($value) <= $option ;
  122. break;
  123. case 'regex':
  124. return preg_match($option, $value);
  125. break;
  126. default:
  127. return true;
  128. break;
  129. }
  130. }
  131. /*
  132. * Builds the label element for a field
  133. * @param object The field object
  134. * @return string The HTML label element
  135. */
  136. function formbuilder_label(&$field, $errors=array()) {
  137. global $Core;
  138. $attrs = array();
  139. $errorMarker = "";
  140. for ($i=0; $i<count($errors); $i++) {
  141. if ($errors[$i]['fieldName'] == Filter::get($field, 'title')) {
  142. $errorMarker = " <span class=\"field_error_marker\"><span>Error!</span></span>";
  143. $attrs['class'] = 'field_error_label';
  144. }
  145. }
  146. if (Filter::get($field, 'fieldtype') == "button") return null;
  147. $marker = Filter::get($field, 'validation') != '' ? '<span class="required">*</span>' : '' ;
  148. $label = Filter::get($field, 'label');
  149. $field_label = urldecode(base64_decode($label)) . " $marker";
  150. if (!empty($errorMarker)) {
  151. $field_label = "<span class=\"field_error_text\">"
  152. . urldecode(base64_decode($label)) . " $marker $errorMarker"
  153. . "</span>";
  154. }
  155. if (!empty($label)) {
  156. echo $Core->HTML->makeElement(
  157. 'label',
  158. $attrs,
  159. $field_label
  160. );
  161. }
  162. echo '';
  163. }
  164. /*
  165. * Determines if any of the fields are of type 'file' so the form type can be properly set
  166. * @param array The form fields array
  167. * @return boolean Whether or not any of the fields are of type 'file'
  168. */
  169. function formbuilder_has_file_field($fields) {
  170. foreach ($fields as $field) {
  171. if ($field->fieldtype == "file") {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. /*
  178. * Prints the Form blurb the web page
  179. * @param object The form object
  180. * @return void
  181. */
  182. function formbuilder_blurb(&$form) {
  183. $blurb = Filter::get($form, 'blurb');
  184. if (!empty($blurb)) {
  185. echo urldecode(base64_decode($blurb));
  186. }
  187. }
  188. /*
  189. * Builds the input element for a field
  190. * @param object The field object
  191. * @return string The HTML input element
  192. */
  193. function formbuilder_field($field, $Request) {
  194. global $Core;
  195. $label = Filter::get($field, 'label');
  196. $name = Filter::get($field, 'title');
  197. $fieldtype = Filter::get($field, 'fieldtype');
  198. $options = Filter::get($field, 'options');
  199. $cols = Filter::get($field, 'cols', 53);
  200. $rows = Filter::get($field, 'rows', 6);
  201. $validate = Filter::get($field, 'validation');
  202. $class = Filter::get($field, 'class');
  203. $html = "";
  204. $list = array();
  205. if (trim($options) != "") {
  206. $tmp = explode(';', $options);
  207. for ($i=0; $i<count($tmp); $i++) {
  208. if (trim($tmp[$i]) == "") continue;
  209. array_push($list, formbuilder_split_option($tmp[$i]));
  210. }
  211. }
  212. switch ($fieldtype) {
  213. case 'textarea':
  214. $html .= $Core->HTML->makeElement(
  215. 'textarea',
  216. array(
  217. 'rows' => $rows,
  218. 'cols' => $cols,
  219. 'name' => $name,
  220. 'class' => $class
  221. ),
  222. $Request->get($name)
  223. ) . "\n" ;
  224. break;
  225. case 'radio':
  226. $options = "";
  227. for ($i=0; $i<count($list); $i++) {
  228. $inputValue = Filter::get($list[$i], 'value');
  229. $inputText = Filter::get($list[$i], 'text');
  230. $attrs = array(
  231. 'type' => 'radio',
  232. 'name' => $name,
  233. 'value' => $inputValue,
  234. 'class' => $class
  235. );
  236. if ($Request->get($name) == $inputValue) {
  237. $attrs['checked'] = 'checked';
  238. }
  239. $options .= $Core->HTML->MakeElement(
  240. 'li',
  241. array(),
  242. $Core->HTML->MakeElement(
  243. 'input',
  244. $attrs,
  245. "",
  246. 0
  247. ) . "&nbsp;" . ucwords($inputText) . "\n"
  248. ) . "\n" ;
  249. }
  250. $html .= $Core->HTML->MakeElement(
  251. 'ul',
  252. array('class'=> $class),
  253. $options
  254. ) . "\n" ;
  255. break;
  256. case 'select':
  257. $options = "";
  258. for ($i=0; $i<count($list); $i++) {
  259. $inputValue = Filter::get($list[$i], 'value');
  260. $inputText = Filter::get($list[$i], 'text');
  261. $attrs = array('value' => $inputValue);
  262. if ($Request->get($name) == $inputValue) {
  263. $attrs['selected'] = 'selected';
  264. }
  265. $options .= $Core->HTML->makeElement(
  266. 'option',
  267. $attrs,
  268. ucwords($inputText)
  269. ) . "\n" ;
  270. }
  271. $attrs = array(
  272. 'type' => 'select',
  273. 'name' => $name,
  274. 'class' => $class
  275. );
  276. if (Filter::get($field, 'multi_select') == "1") {
  277. $attrs['multiple'] = 'multiple';
  278. }
  279. $size = Filter::get($field, 'size', "1");
  280. if (!is_numeric($size)) $size = "1";
  281. if ($size == "0") $size = "1";
  282. if (!empty($size)) {
  283. $attrs['size'] = $size;
  284. }
  285. $html .= $Core->HTML->makeElement(
  286. 'select',
  287. $attrs,
  288. $options
  289. ) . "\n" ;
  290. break;
  291. case 'checkbox':
  292. $options = "";
  293. for ($i=0; $i<count($list); $i++) {
  294. $inputValue = Filter::get($list[$i], 'value');
  295. $inputText = Filter::get($list[$i], 'text');
  296. $attrs = array(
  297. 'type' => 'checkbox',
  298. 'name' => "{$name}[]",
  299. 'value' => $inputValue,
  300. 'class' => $class
  301. );
  302. $vals = $Request->get($name);
  303. if (is_array($vals)) {
  304. if (in_array($inputValue, $vals)) {
  305. $attrs['checked'] = 'checked';
  306. }
  307. }
  308. $options .= $Core->HTML->MakeElement(
  309. 'li',
  310. array(),
  311. $Core->HTML->MakeElement(
  312. 'input',
  313. $attrs,
  314. "",
  315. 0
  316. ) . "&nbsp;" . ucwords($inputText) . "\n"
  317. ) . "\n" ;
  318. }
  319. $html .= $Core->HTML->MakeElement(
  320. 'ul',
  321. array('class'=>'checkbox_list'),
  322. $options
  323. ) . "\n" ;
  324. break;
  325. case 'file':
  326. $html .= $Core->HTML->makeElement(
  327. 'input',
  328. array(
  329. 'type' => 'file',
  330. 'name' => $name,
  331. 'value' => '',
  332. 'clase' => $class
  333. ),
  334. '',
  335. 0
  336. ) . "\n" ;
  337. break;
  338. case 'button':
  339. if (trim($label) != "") {
  340. $label = urldecode(base64_decode($label));
  341. }
  342. $attrs = array('name' => $name);
  343. if (!empty($class)) {
  344. $attrs['class'] = $class;
  345. }
  346. $html .= $Core->HTML->makeElement(
  347. 'button',
  348. $attrs,
  349. $label,
  350. 1
  351. ) . "\n" ;
  352. break;
  353. case 'password':
  354. $html .= $Core->HTML->MakeElement(
  355. 'input',
  356. array(
  357. 'type' => 'password',
  358. 'name' => $name,
  359. 'value' => "",
  360. 'class' => $class
  361. ),
  362. "",
  363. 0
  364. ) . "\n" ;
  365. break;
  366. case 'text':
  367. default:
  368. $html .= $Core->HTML->makeElement(
  369. 'input',
  370. array(
  371. 'type' => 'text',
  372. 'name' => $name,
  373. 'value' => $Request->get($name),
  374. 'class' => $class
  375. ),
  376. "",
  377. 0
  378. ) . "\n";
  379. break;
  380. }
  381. echo $html;
  382. }
  383. /*
  384. * Splits the select, radio or checkbox options into value -> text
  385. * @param string The option text of format foo:Foo Bar Baz;
  386. * @return array An array of format array('value' => 'foo', 'text' => 'Foo Bar Baz')
  387. */
  388. function formbuilder_split_option($option) {
  389. if (is_array($option)) Core::Dump($option);
  390. $bits = explode(":", $option);
  391. if (count($bits) == 2) {
  392. $split = array('value' => $bits[0], 'text' => $bits[1]);
  393. }
  394. else {
  395. $split = array('value' => $option, 'text' => $option);
  396. }
  397. return $split;
  398. }
  399. ?>