v1.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * The formBatch 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. require_once dirname(__DIR__) . DS . 'formbatchitem' . DS . 'v1.php';
  13. require_once dirname(__DIR__) . DS . 'formbase' . DS . 'v1.php';
  14. /**
  15. * 批量编辑表单(formBatch)部件类,支持 Ajax 提交。
  16. * The batch operate form widget class.
  17. *
  18. * @author Hao Sun
  19. */
  20. class formBatch extends formBase
  21. {
  22. /**
  23. * Define widget properties.
  24. *
  25. * @var array
  26. * @access protected
  27. */
  28. protected static $defineProps = array(
  29. 'items?: array[]', // 使用一个列定义对象数组来定义批量表单项。
  30. 'minRows?: int', // 最小显示的行数目。
  31. 'maxRows?: int', // 最多显示的行数目。
  32. 'data?: array[]', // 初始化行数据。
  33. 'mode?: string', // 批量操作模式,可以为 `'add'`(批量添加) 或 `'edit'`(批量编辑)。
  34. 'actionsText?: string', // 操作列头部文本,如果不指定则使用 `$lang->actions` 的值。
  35. 'idKey?: string', // 用于从行数据获取 ID 的属性名。
  36. 'addRowIcon?: string|false', // 添加行的图标,如果设置为 `false` 则不显示图标
  37. 'deleteRowIcon?: string|false', // 删除行的图标,如果设置为 `false` 则不显示图标
  38. 'sortRowIcon?: string|false', // 排序行的图标,如果设置为 `false` 则不显示图标
  39. 'sortable?: boo|array', // 排序配置,设置为 false 不启用排序,设置为 true 使用默认排序
  40. 'onRenderRow?: function', // 渲染行时的回调函数。
  41. 'hiddenFields?: array', // 被隐藏的字段。
  42. 'onRenderRowCol?: function', // 渲染列时的回调函数。
  43. 'batchFormOptions?: array' // 批量表单选项。
  44. );
  45. /**
  46. * Define default properties.
  47. *
  48. * @var array
  49. * @access protected
  50. */
  51. protected static $defaultProps = array(
  52. 'maxRows' => 100,
  53. 'mode' => 'add'
  54. );
  55. /**
  56. * @var mixed[]
  57. */
  58. protected static $defineBlocks = array(
  59. 'formBefore' => array()
  60. );
  61. /**
  62. * Handle building inner items.
  63. *
  64. * @param node|array $item
  65. * @access public
  66. * @return mixed
  67. */
  68. public function onBuildItem($item)
  69. {
  70. if($item instanceof formBatchItem) return $item;
  71. if(!($item instanceof item))
  72. {
  73. if(!is_array($item)) return $item;
  74. $item = item(set($item));
  75. }
  76. return new formBatchItem(inherit($item));
  77. }
  78. public function children()
  79. {
  80. $children = array();
  81. $children[] = $this->buildContent();
  82. $children[] = $this->buildActions();
  83. return $children;
  84. }
  85. /**
  86. * Build batch form content.
  87. *
  88. * @access protected
  89. * @return array|node
  90. */
  91. protected function buildContent()
  92. {
  93. $items = array_merge($this->prop('items', array()), $this->block('children'));
  94. $hiddenFields = $this->prop('hiddenFields', array());
  95. $templateItems = array();
  96. $headItems = array();
  97. $otherItems = array();
  98. foreach($items as $item)
  99. {
  100. if($item instanceof setting) $item = $item->toArray();
  101. if($item instanceof item || is_array($item)) $item = $this->onBuildItem($item);
  102. if($item instanceof formBatchItem)
  103. {
  104. if($item->hasProp('name') && is_null($item->prop('hidden'))) $item->setProp('hidden', in_array($item->prop('name'), $hiddenFields));
  105. list($headItem, $templateItem) = $item->build();
  106. $headItems[] = $headItem;
  107. $templateItems[] = $templateItem;
  108. }
  109. else
  110. {
  111. $otherItems[] = $item;
  112. }
  113. }
  114. if($this->prop('mode') === 'add')
  115. {
  116. $actionsText = $this->prop('actionsText');
  117. if($actionsText === null) $actionsText = data('lang.actions');
  118. $headItems[] = h::th
  119. (
  120. zui::width($this->prop('actionsWidth')),
  121. set('data-name', 'ACTIONS'),
  122. setClass('form-batch-head'),
  123. span(setClass('form-label form-batch-label'), $actionsText)
  124. );
  125. }
  126. return array(
  127. div
  128. (
  129. setClass('form-batch-container relative'),
  130. $this->block('formBefore'),
  131. h::table(setClass('table form-batch-table'), h::thead(setClass('sticky top-0 bg-canvas z-10'), h::tr($headItems)), h::tbody())
  132. ),
  133. template(setClass('form-batch-template'), h::tr($templateItems)),
  134. $otherItems
  135. );
  136. }
  137. /**
  138. * Build batch form props.
  139. *
  140. * @access protected
  141. * @return array
  142. */
  143. protected function buildProps()
  144. {
  145. $props = parent::buildProps();
  146. $props[] = setClass('form-batch');
  147. $batchFormOptions = $this->props->pick(array('minRows', 'maxRows', 'data', 'mode', 'idKey', 'onRenderRow', 'onRenderRowCol', 'addRowIcon', 'deleteRowIcon', 'sortRowIcon', 'sortable'));
  148. $batchFormOptions = array_merge($batchFormOptions, $this->prop('batchFormOptions', array()));
  149. $props = array_merge($props, zui::create('batchForm', $batchFormOptions));
  150. return $props;
  151. }
  152. }