v1.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * The select widget class file of zin module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author sunhao<sunhao@easycorp.ltd>
  8. * @package zin
  9. * @link http://www.zentao.net
  10. */
  11. namespace zin;
  12. /**
  13. * 选择框(select)部件类,支持 Ajax 提交。
  14. * The select control widget class.
  15. *
  16. * @author Hao Sun
  17. */
  18. class select extends wg
  19. {
  20. /**
  21. * Define widget properties.
  22. *
  23. * @var array
  24. * @access protected
  25. */
  26. protected static $defineProps = array
  27. (
  28. 'name: string',
  29. 'id?: string',
  30. 'class?: string="form-control"',
  31. 'value?: string=""',
  32. 'required?: bool',
  33. 'disabled?: bool',
  34. 'multiple?: bool',
  35. 'items?: array',
  36. 'size?: int'
  37. );
  38. /**
  39. * The lifecycle method of created.
  40. *
  41. * Set default id with name.
  42. * @access protected
  43. * @return void
  44. */
  45. protected function created()
  46. {
  47. if($this->prop('id') === null && $this->prop('name') !== null)
  48. {
  49. $name = $this->prop('name');
  50. $id = substr($name, -2) == '[]' ? substr($name, 0, - 2) : $name;
  51. $this->setProp('id', $id);
  52. }
  53. }
  54. /**
  55. * Handle building inner options.
  56. *
  57. * @param node|array $item
  58. * @access public
  59. * @return node
  60. */
  61. public function onBuildItem($item)
  62. {
  63. if($item instanceof item) $item = $item->props->toJSON();
  64. $text = isset($item['text']) ? $item['text'] : '';
  65. unset($item['text']);
  66. if(!isset($item['selected']))
  67. {
  68. $value = isset($item['value']) ? $item['value'] : '';
  69. $valueList = $this->getValueList();
  70. $item['selected'] = in_array($value, $valueList);
  71. }
  72. return h::option(set($item), $text);
  73. }
  74. /**
  75. * Get value list.
  76. *
  77. * @access public
  78. * @return array
  79. */
  80. public function getValueList()
  81. {
  82. list($value, $multiple) = $this->prop(array('value', 'multiple'));
  83. if($multiple) return is_array($value) ? $value : explode(',', (string)$value);
  84. return array($value);
  85. }
  86. /**
  87. * The lifecycle method of building.
  88. *
  89. * @access protected
  90. * @return mixed
  91. */
  92. protected function build()
  93. {
  94. list($items, $multiple, $required) = $this->prop(array('items', 'multiple', 'required'));
  95. $hasEmptyItem = false;
  96. $valueList = $this->getValueList();
  97. if(!empty($items))
  98. {
  99. foreach($items as $key => $item)
  100. {
  101. if(!is_array($item)) $item = array('text' => $item, 'value' => $key);
  102. if(!is_string($item['value'])) $item['value'] = strval($item['value']);
  103. if(!isset($item['selected'])) $item['selected'] = in_array($item['value'], $valueList);
  104. $items[$key] = $this->onBuildItem($item);
  105. if($item['value'] === '') $hasEmptyItem = true;
  106. }
  107. }
  108. else
  109. {
  110. $items = array();
  111. }
  112. /* Prepend empty option when current select is not required and whitout any empty item. */
  113. if(!$required && !$hasEmptyItem) array_unshift($items, $this->onBuildItem(array('text' => '', 'value' => '', 'selected' => in_array('', $valueList))));
  114. $props = $this->props->skip(['items', 'value', 'multiple', 'required']);
  115. return h::select
  116. (
  117. setClass('form-control', $required ? 'is-required' : ''),
  118. set::multiple($multiple),
  119. set($props),
  120. $items,
  121. $this->children()
  122. );
  123. }
  124. }