v1.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * The flowSubTable widget class file of zin module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2024 禅道软件(青岛)有限公司(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 Gang Liu <liugang@easycorp.ltd>
  8. * @package zin
  9. * @link http://www.zentao.net
  10. */
  11. namespace zin;
  12. class flowSubTable extends wg
  13. {
  14. /**
  15. * @var mixed[]
  16. */
  17. protected static $defineProps = [
  18. 'items?: array',
  19. 'value?: array'
  20. ];
  21. protected function build()
  22. {
  23. global $app;
  24. $common = $app->loadCommon();
  25. $common->loadModel('flow');
  26. list($fields, $dataList) = $this->prop(['items', 'value']);
  27. if(!$fields) return div('No fields found');
  28. $module = current($fields)['module'];
  29. $dittoControl = ['select', 'multi-select', 'radio', 'checkbox', 'date', 'datetime'];
  30. $notEmptyRule = $common->loadModel('workflowrule')->getByTypeAndRule('system', 'notempty');
  31. $items = [];
  32. foreach($fields as $field)
  33. {
  34. $field = (object)$field;
  35. if(!$field->show) continue;
  36. $required = ($notEmptyRule && (strpos(",{$field->layoutRules},", ",{$notEmptyRule->id},") !== false) || strpos(",{$field->rules},", ",{$notEmptyRule->id},") !== false) || !empty($field->required);
  37. $items[] = [
  38. 'name' => "children[sub_{$module}][{$field->field}]", // 子表的字段可以重名,所以需要加上 module
  39. 'label' => $field->name,
  40. 'control' => $common->flow->buildFormControl($field, 'batch'),
  41. 'items' => array_filter($field->options),
  42. 'width' => $field->width == 'auto' ? '160px' : $field->width,
  43. 'required' => $required,
  44. 'ditto' => in_array($field->control, $dittoControl),
  45. 'defaultDitto' => $dataList ? 'off' : 'on',
  46. 'placeholder' => isset($field->placeholder) ? $field->placeholder : '',
  47. 'readonly' => isset($field->readonly) ? (bool)$field->readonly : false
  48. ];
  49. }
  50. /**
  51. * 子表的 id 字段在 $fields 中不存在。添加 id 字段用来区分是新增还是编辑。
  52. * The id field of the sub-table does not exist in $fields. Add the id field to distinguish between new and edit.
  53. */
  54. $items[] = [
  55. 'name' => "children[sub_{$module}][id]",
  56. 'label' => 'id',
  57. 'hidden' => true
  58. ];
  59. /**
  60. * formBatchItem 要求 item 的 name 属性的值和数据的属性必须一致,所以需要将数据的属性名转换为 name 属性
  61. * The name attribute of the item must be consistent with the data attribute, so the data attribute name needs to be converted to the name attribute
  62. */
  63. $rows = [];
  64. foreach($dataList as $key => $data)
  65. {
  66. foreach($fields as $field)
  67. {
  68. $field = (object)$field;
  69. $newKey = "children[sub_{$module}][{$field->field}]";
  70. $rows[$key][$newKey] = $data->{$field->field};
  71. }
  72. $newKey = "children[sub_{$module}][id]";
  73. $rows[$key][$newKey] = $data->id;
  74. }
  75. return formBatch
  76. (
  77. set::tagName('div'),
  78. set::mode('add'),
  79. set::actions([]),
  80. set::maxRows($rows ? count($rows) : 1),
  81. set::items($items),
  82. set::data(array_values($rows))
  83. );
  84. }
  85. }