PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/bors/forms/multiselect.php

https://bitbucket.org/Balancer/bors-core
PHP | 82 lines | 65 code | 17 blank | 0 comment | 16 complexity | cbb07cbe62148440705ec8113621b969 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. class bors_forms_multiselect extends bors_forms_element
  3. {
  4. function html()
  5. {
  6. $params = $this->params();
  7. if(!empty($params['property']))
  8. $params['name'] = $params['property'];
  9. $form = $this->form();
  10. extract($params);
  11. $object = $form->object();
  12. $params = "";
  13. foreach(explode(' ', 'size style') as $p)
  14. if(!empty($$p))
  15. $params .= " $p=\"{$$p}\"";
  16. $html = "<select multiple=\"multiple\" name=\"".addslashes($name)."[]\"$params>\n";
  17. if(!is_array($list))
  18. {
  19. if(preg_match("!^(\w+)\->(\w+)$!", $list, $m))
  20. {
  21. if($m[1] == 'this')
  22. $list = call_user_func([$object, $m[2]]);
  23. else
  24. $list = call_user_func([bors_foo($m[1]), $m[2]]);
  25. }
  26. elseif(preg_match("!^(\w+)\->(\w+)\('(.+)'\)!", $list, $m))
  27. {
  28. if($m[1] == 'this')
  29. $list = call_user_func([$object, $m[2]], $m[3]);
  30. else
  31. $list = call_user_func([bors_foo($m[1]), $m[2]], $m[3]);
  32. }
  33. elseif(preg_match("!^\w+$!", $list))
  34. {
  35. $list = new $list(@$args);
  36. $list = $list->named_list();
  37. }
  38. else
  39. {
  40. eval('$list='.$list);
  41. }
  42. }
  43. $have_null = in_array(NULL, $list);
  44. $strict = defval($params, 'strict', $have_null);
  45. $is_int = defval($params, 'is_int');
  46. if(is_null($is_int) && !$strict)
  47. $is_int = true;
  48. $current = $object ? $object->$name() : array();
  49. if($is_int)
  50. for($i=0; $i<count($current); $i++)
  51. $current[$i] = ($have_null && is_null($current[$i])) ? NULL : intval($current[$i]);
  52. foreach($list as $id => $iname)
  53. {
  54. if(!$id && !empty($params['have_null']))
  55. $id = NULL;
  56. if($id !== 'default')
  57. $html .= "\t\t\t<option value=\"$id\"".(in_array($id, $current, $strict) ? " selected=\"selected\"" : "").">$iname</option>\n";
  58. }
  59. $html .= "</select>";
  60. $vcbs = bors_object::template_data('form_checkboxes_list');
  61. $vcbs[] = $name;
  62. bors_object::add_template_data('form_checkboxes_list', $vcbs);
  63. return $html;
  64. }
  65. }