v1.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'panel' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'sqlbuilderpicker' . DS . 'v1.php';
  5. class sqlBuilderGroupBy extends wg
  6. {
  7. /**
  8. * @var mixed[]
  9. */
  10. protected static $defineProps = array(
  11. 'groups?: array',
  12. 'aggs?: array',
  13. 'onChangeAgg?: function',
  14. 'onChangeType?: function',
  15. 'onSort?: function'
  16. );
  17. public static function getPageCSS()
  18. {
  19. return file_get_contents(__DIR__ . DS . 'css' . DS . 'v1.css');
  20. }
  21. /**
  22. * @param int $index
  23. * @param mixed[] $group
  24. */
  25. protected function buildFieldItem($index, $group)
  26. {
  27. global $lang;
  28. list($name, $type) = array($group['name'], $group['type']);
  29. list($onChangeType) = $this->prop(array('onChangeType'));
  30. return div
  31. (
  32. setClass('flex row gap-x-4 justify-between'),
  33. span
  34. (
  35. setClass('ellipsis'),
  36. set::title($name),
  37. $name
  38. ),
  39. btnGroup
  40. (
  41. btn
  42. (
  43. setClass('switch-group-by'),
  44. set::size('sm'),
  45. set::type($type == 'group' ? 'secondary' : 'default'),
  46. set('data-index', $index),
  47. set('data-type', 'group'),
  48. $lang->bi->groupField,
  49. on::click()->do($onChangeType)
  50. ),
  51. btn
  52. (
  53. setClass('switch-group-by'),
  54. set::size('sm'),
  55. set::type($type == 'agg' ? 'secondary' : 'default'),
  56. set('data-index', $index),
  57. set('data-type', 'agg'),
  58. $lang->bi->aggField,
  59. on::click()->do($onChangeType)
  60. )
  61. )
  62. );
  63. }
  64. protected function buildAllField()
  65. {
  66. list($groups) = $this->prop(array('groups'));
  67. $items = array();
  68. foreach($groups as $index => $group) $items[] = $this->buildFieldItem($index, $group);
  69. return $items;
  70. }
  71. protected function buildAggFieldRow($agg)
  72. {
  73. global $lang;
  74. list($table, $field, $function, $alias, $name) = array($agg['table'], $agg['field'], $agg['function'], $agg['alias'], $agg['name']);
  75. list($onChangeAgg) = $this->prop(array('onChangeAgg'));
  76. return formRow
  77. (
  78. sqlBuilderPicker
  79. (
  80. set::name("{$table}_{$field}_{$alias}"),
  81. set::label(sprintf($lang->bi->aggTipA, $name)),
  82. set::labelWidth('176px'),
  83. set::labelAlign('left'),
  84. set::width('64'),
  85. set::required(true),
  86. set::items($lang->bi->aggList),
  87. set::placeholder($lang->bi->selectFuncTip),
  88. set::value($function),
  89. set::onChange($onChangeAgg)
  90. ),
  91. sqlBuilderControl
  92. (
  93. set::type(''),
  94. set::label(sprintf($lang->bi->aggTipB, $alias)),
  95. set::labelWidth('320px'),
  96. set::width('96'),
  97. set::labelAlign('left')
  98. )
  99. );
  100. }
  101. protected function buildAggField()
  102. {
  103. list($aggs) = $this->prop(array('aggs'));
  104. $formRows = array();
  105. foreach($aggs as $agg) $formRows[] = $this->buildAggFieldRow($agg);
  106. return $formRows;
  107. }
  108. protected function buildGroupField()
  109. {
  110. list($groups, $onSort) = $this->prop(array('groups', 'onSort'));
  111. usort($groups, function($a, $b) {return $a['order'] <= $b['order'] ? -1 : 1;});
  112. $items = array();
  113. foreach($groups as $index => $group) if($group['type'] == 'group') $items[] = array('text' => $group['name'], 'data-index' => $index);
  114. return zui::SortableList
  115. (
  116. setClass('group-by-sort'),
  117. set::items($items),
  118. set::itemProps(array('icon' => 'move muted')),
  119. set::onSort(jsCallback()->do($onSort))
  120. );
  121. }
  122. protected function build()
  123. {
  124. global $lang;
  125. return div
  126. (
  127. setClass('flex row w-full gap-x-4'),
  128. panel
  129. (
  130. setID('allFieldPanel'),
  131. setClass('basis-72 h-72'),
  132. set::headingClass('bg-gray-100'),
  133. set::bodyClass('h-70 overflow-y-auto flex col gap-y-2'),
  134. set::title($lang->bi->allFields),
  135. to::heading(sqlBuilderHelpIcon(set::text($lang->bi->allFieldsTip))),
  136. $this->buildAllField()
  137. ),
  138. panel
  139. (
  140. setID('groupFieldPanel'),
  141. setClass('basis-36 h-72'),
  142. set::headingClass('bg-gray-100'),
  143. set::bodyClass('h-70 overflow-y-auto flex col gap-y-2'),
  144. set::title($lang->bi->groupField),
  145. to::heading(sqlBuilderHelpIcon(set::text($lang->bi->groupFieldTip))),
  146. $this->buildGroupField()
  147. ),
  148. panel
  149. (
  150. setID('aggFieldPanel'),
  151. setClass('flex-1 h-72'),
  152. set::headingClass('bg-gray-100'),
  153. set::bodyClass('h-64 overflow-y-auto flex col gap-y-2'),
  154. set::title($lang->bi->aggField),
  155. to::heading(sqlBuilderHelpIcon(set::text($lang->bi->aggFieldTip))),
  156. $this->buildAggField()
  157. )
  158. );
  159. }
  160. }