PageRenderTime 643ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/bors/forms/dropdown.php

https://bitbucket.org/Balancer/bors-core
PHP | 214 lines | 169 code | 40 blank | 5 comment | 42 complexity | 27616341221d0d837242822d6649171b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. class bors_forms_dropdown extends bors_forms_element
  3. {
  4. function html()
  5. {
  6. include_once('inc/bors/lists.php');
  7. $this->make_data();
  8. $params = $this->params();
  9. if(!empty($params['property']))
  10. $params['name'] = $params['property'];
  11. $form = $this->form();
  12. extract($params);
  13. $dom_id = defval($params, 'dom_id', $id);
  14. $object = object_property($form, 'object');
  15. $html = "";
  16. $class = explode(' ', $this->css());
  17. if(in_array($name, explode(',', session_var('error_fields'))))
  18. $class[] = $this->css_error();
  19. $class = join(' ', $class);
  20. // Если указано, то это заголовок строки таблицы: <tr><th>{$label}</th><td>...code...</td></tr>
  21. if($label = $this->label())
  22. {
  23. $html .= $this->label_html();
  24. if(empty($style))
  25. $style = "width: 99%";
  26. }
  27. elseif(!empty($params['width']))
  28. $style = "width: ".$params['width'];
  29. if(!empty($json) && empty($dom_id))
  30. {
  31. $dom_id = md5(rand());
  32. $tag = "<input type=\"hidden\" style=\"width: 100%\"";
  33. }
  34. else
  35. $tag = "<select";
  36. $html .= $tag;
  37. foreach(array('dom_id' => 'id') as $var => $hn)
  38. if(!empty($$var))
  39. $html .= " $hn=\"{$$var}\"";
  40. foreach(explode(' ', 'size style multiple class onchange') as $p)
  41. if(!empty($$p))
  42. $html .= " $p=\"{$$p}\"";
  43. if(empty($multiple))
  44. $html .= " name=\"{$name}\"";
  45. else
  46. $html .= " name=\"{$name}[]\"";
  47. if(!empty($json))
  48. $html .= " value=\"".htmlspecialchars($value)."\"";
  49. $html .= ">\n";
  50. if(empty($list) && !empty($params['class_name']))
  51. {
  52. $list = base_list::make($params['class_name'], array(), array());
  53. }
  54. elseif(!is_array($list))
  55. {
  56. if(preg_match("!^(\w+)\->(\w+)$!", $list, $m))
  57. {
  58. if($m[1] == 'this')
  59. $list = call_user_func([$object, $m[2]]);
  60. else
  61. $list = call_user_func([bors_foo($m[1]), $m[2]]);
  62. }
  63. elseif(preg_match("!^(\w+)\->(\w+)\('(.+)'\)!", $list, $m))
  64. {
  65. if($m[1] == 'this')
  66. $list = call_user_func([$object, $m[2]], $m[3]);
  67. else
  68. $list = call_user_func([bors_foo($m[1]), $m[2]], $m[3]);
  69. }
  70. elseif(preg_match("!^[a-z]\w+$!", $list))
  71. {
  72. $list = new $list(@$args);
  73. $list = $list->named_list();
  74. }
  75. elseif($list)
  76. eval('$list='.$list);
  77. else
  78. $list = array();
  79. }
  80. $have_null = in_array(NULL, $list);
  81. $strict = defval($params, 'strict', $have_null);
  82. $is_int = defval($params, 'is_int');
  83. if(is_null($is_int) && !$strict)
  84. {
  85. $is_int = true;
  86. foreach($list as $k => $v)
  87. if($k && !is_int($k))
  88. {
  89. $is_int = false;
  90. break;
  91. }
  92. }
  93. $value = $this->value();
  94. if(empty($get))
  95. {
  96. if(preg_match('!^\w+$!', $name))
  97. $current = isset($value) ? $value : ($object ? $object->$name() : NULL);
  98. else
  99. $current = isset($value) ? $value : 0;
  100. }
  101. else
  102. $current = $object->$get();
  103. if(!$current && !empty($list['default']))
  104. $current = $list['default'];
  105. unset($list['default']);
  106. if(empty($current))
  107. $current = session_var("form_value_{$name}");
  108. set_session_var("form_value_{$name}", NULL);
  109. if(!is_array($current))
  110. $current = array($current);
  111. if($is_int)
  112. for($i=0; $i<count($current); $i++)
  113. $current[$i] = ($have_null && is_null($current[$i])) ? NULL : intval($current[$i]);
  114. foreach($list as $id => $iname)
  115. {
  116. if(!$id && $have_null)
  117. $id = 'NULL';
  118. if($id !== 'default')
  119. $html .= "\t\t\t<option value=\"$id\"".(in_array($id, $current, $strict) ? " selected=\"selected\"" : "").">$iname</option>\n";
  120. }
  121. if(empty($json))
  122. $html .= "\t\t</select>\n";
  123. if($label)
  124. $html .= "</td></tr>\n";
  125. if(!empty($json))
  126. {
  127. jquery_select2::appear_ajax("'#{$dom_id}'", $json, array_merge(defval($params, 'edit_params', array()), array(
  128. 'order' => defval($params, 'order', 'title'),
  129. 'title_field' => defval($params, 'title_field', 'title'),
  130. // 'placeholder' => 'Введите часть названия источника',
  131. )));
  132. $value_title = object_property(bors_load($json, $value), 'title');
  133. jquery::on_ready("$('#{$name}').select2(\"data\", { id: '{$value}', text: \"$value_title\" })");
  134. }
  135. return $html;
  136. }
  137. function make_data()
  138. {
  139. $data = $this->params();
  140. $form = $this->form();
  141. if(array_key_exists('list', $data))
  142. {
  143. // Ничего не делаем, массив уже в данных.
  144. }
  145. elseif(array_key_exists('named_list', $data))
  146. {
  147. if(preg_match('/^(\w+):(\w+)$/', $data['named_list'], $m))
  148. {
  149. $list_class_name = $m[1];
  150. $id = $m[2];
  151. }
  152. else
  153. {
  154. $list_class_name = $data['named_list'];
  155. $id = NULL;
  156. }
  157. $list = new $list_class_name($id); //TODO: статический вызов тут не прокатит, пока не появится повсеместный PHP-5.3.3.
  158. $data['list'] = $list->named_list();
  159. }
  160. else
  161. {
  162. $list_filter = popval($data, 'where', popval($data, 'list_filter', array()));
  163. if(is_string($list_filter))
  164. eval("\$list_filter = $list_filter;");
  165. // $data['main_class'] — http://admin.aviaport.wrk.ru/job/cabinets/236/
  166. $data['list'] = base_list::make(defval($data, 'main_class', $class), $list_filter, $data);
  167. }
  168. // Смешанная проверка для тестирования на http://ucrm.wrk.ru/admin/persons/9/
  169. if(is_array($data['list']) && ($data['is_int'] = defval($data, 'is_int', true)))
  170. foreach($data['list'] as $k => $v)
  171. $data['is_int'] &= !$k || is_numeric($k);
  172. $this->set_params($data);
  173. }
  174. }