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

/classes/bors/forms/combobox.php

https://bitbucket.org/Balancer/bors-core
PHP | 182 lines | 108 code | 40 blank | 34 comment | 29 complexity | 2587e68680f94f5465ad36e03e0ef401 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. // Отработка на:
  3. // http://admin2.aviaport.wrk.ru/directory/airlines/1/airports/
  4. // — старый вариант, с bors_form
  5. // http://admin2.aviaport.wrk.ru/digest/new/
  6. // — новый вариант, с bors_admin_newstep
  7. class bors_forms_combobox extends bors_forms_element
  8. {
  9. function html()
  10. {
  11. include_once('inc/bors/lists.php');
  12. $params = $this->params();
  13. if(!empty($params['property']))
  14. $params['name'] = $params['property'];
  15. $form = $this->form();
  16. extract($params);
  17. $object = $form->object();
  18. $html = "";
  19. $css_classes = array();
  20. $css_style = array();
  21. if(in_array($name, explode(',', session_var('error_fields'))))
  22. $css_classes[] = "error";
  23. // Если нужно, добавляем заголовок поля
  24. $html .= $this->label_html();
  25. // Если отдельный блок, то на всю ширину.
  26. if($this->label())
  27. $css_style[] = "width: 99%";
  28. if(!empty($fixed))
  29. $html .= "<label><input type=\"radio\" name=\"_{$name}\" style=\"float: left\" value=\"\" />&nbsp;</label>";
  30. $html .= "<div id=\"{$name}\" style=\"display: inline;\"";
  31. $class = join(' ', $css_classes);
  32. $style = join(';', $css_style);
  33. foreach(explode(' ', 'id size style multiple class onchange') as $p)
  34. if(!empty($$p))
  35. $html .= " $p=\"{$$p}\"";
  36. // if(empty($multiple))
  37. // $html .= " name=\"{$name}\"";
  38. // else
  39. // $html .= " name=\"{$name}[]\"";
  40. $html .= ">\n";
  41. if(!is_array($list))
  42. {
  43. if(preg_match("!^(\w+)\->(\w+)$!", $list, $m))
  44. {
  45. if($m[1] == 'this')
  46. $list = call_user_func([$object, $m[2]]);
  47. else
  48. $list = call_user_func([bors_foo($m[1]), $m[2]]);
  49. }
  50. elseif(preg_match("!^(\w+)\->(\w+)\('(.+)'\)!", $list, $m))
  51. {
  52. if($m[1] == 'this')
  53. $list = call_user_func([$object, $m[2]], $m[3]);
  54. else
  55. $list = call_user_func([bors_foo($m[1]), $m[2]], $m[3]);
  56. }
  57. elseif(preg_match("!^\w+$!", $list))
  58. {
  59. $list = new $list(@$args);
  60. $list = $list->named_list();
  61. }
  62. elseif($list)
  63. eval('$list='.$list);
  64. else
  65. $list = array();
  66. }
  67. $have_null = in_array(NULL, $list);
  68. $strict = defval($params, 'strict', $have_null);
  69. $is_int = defval($params, 'is_int');
  70. if(is_null($is_int) && !$strict)
  71. $is_int = true;
  72. // $value = NULL; // $this->value();
  73. $value = $this->value();
  74. /*
  75. if(empty($get))
  76. {
  77. if(preg_match('!^\w+$!', $name))
  78. $current = isset($value) ? $value : ($object ? $object->$name() : NULL);
  79. else
  80. $current = isset($value) ? $value : 0;
  81. }
  82. else
  83. $current = $object->$get();
  84. */
  85. if(!$current && !empty($list['default']))
  86. $current = $list['default'];
  87. if(empty($current))
  88. $current = session_var("form_value_{$name}");
  89. set_session_var("form_value_{$name}", NULL);
  90. if(!is_array($current))
  91. $current = array($current);
  92. if($is_int)
  93. for($i=0; $i<count($current); $i++)
  94. $current[$i] = ($have_null && is_null($current[$i])) ? NULL : intval($current[$i]);
  95. // foreach($list as $id => $iname)
  96. // if($id !== 'default')
  97. // $html .= "\t\t\t<option value=\"$id\"".(in_array($id, $current, $strict) ? " selected=\"selected\"" : "").">$iname</option>\n";
  98. $html .= "\t\t</div>";
  99. $attrs = array();
  100. if(!empty($fixed))
  101. {
  102. $html .= "\n<div class=\"clear\">&nbsp;</div>\n";
  103. foreach($fixed as $t => $v)
  104. {
  105. if($v == $value)
  106. $checked = " checked=\"checked\"";
  107. else
  108. $checked = "";
  109. $html .= "<label><input type=\"radio\" name=\"_{$name}\" value=\"{$v}\"{$checked} />&nbsp;{$t}</label>\n";
  110. }
  111. $form->append_attr('override_fields', "!_{$name}");
  112. // $attrs['onSelect'] = 'function() { alert("Sel="+this.value+", hid="+this.getAttribute("hiddenValue")) }';
  113. // $attrs['onSelect'] = 'function() { alert(123) }';
  114. // Костыль для Firefox
  115. // http://fairwaytech.com/flexbox
  116. $attrs['onSelect'] = "function() { \$('input:radio[name=\"_{$name}\"]').first().attr('checked', 'checked'); }";
  117. }
  118. else
  119. $html .= "\n";
  120. if($form->get('has_form_table'))
  121. $html .= "</td></tr>\n";
  122. if(!empty($per_page))
  123. $attrs['paging'] = array('pageSize' => $per_page);
  124. if(!empty($width))
  125. $attrs['width'] = $width;
  126. if(empty($json))
  127. {
  128. // var_dump($params);
  129. $json = "/_bors/data/lists/{$main_class}.json";
  130. }
  131. // http://admin2.aviaport.wrk.ru/directory/aviation/arp/5/
  132. // http://admin2.aviaport.wrk.ru/digest/new/
  133. if($value && is_numeric($value))
  134. {
  135. $v = bors_load($this->list_class(), $value);
  136. // var_dump($this->list_class(), $value, $v);
  137. $attrs['initialValue'] = $v->title();
  138. }
  139. jquery_flexbox::appear("'#{$name}'", $json, $attrs);
  140. return $html;
  141. }
  142. }