HasTree.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Support\Collection;
  6. trait HasTree
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $parentIdQueryName = '__parent_id__';
  12. /**
  13. * @var string
  14. */
  15. protected $levelQueryName = '__level__';
  16. /**
  17. * @var bool
  18. */
  19. protected $showAllChildrenNodes = false;
  20. /**
  21. * @var bool
  22. */
  23. protected $allowedTreeQuery = true;
  24. /**
  25. * @var array
  26. */
  27. protected $treeIgnoreQueryNames = [];
  28. /**
  29. * 开启树形表格功能
  30. *
  31. * @param bool $showAll
  32. * @param bool $sortable
  33. *
  34. * @return void
  35. */
  36. public function enableTree(bool $showAll, bool $sortable)
  37. {
  38. $this->showAllChildrenNodes = $showAll;
  39. $this->grid->fetching(function () use ($sortable) {
  40. $this->sortTree($sortable);
  41. $this->bindChildrenNodesQuery();
  42. if ($this->getParentIdFromRequest()) {
  43. $this->setPageName(
  44. $this->getChildrenPageName($this->getParentIdFromRequest())
  45. );
  46. }
  47. });
  48. $this->collection(function (Collection $collection) {
  49. if (! $this->getParentIdFromRequest()) {
  50. return $collection;
  51. }
  52. if ($collection->isEmpty()) {
  53. abort(404);
  54. }
  55. $this->buildChildrenNodesPagination();
  56. return $collection;
  57. });
  58. }
  59. /**
  60. * 禁止树形表格查询
  61. *
  62. * @return $this
  63. */
  64. public function disableBindTreeQuery()
  65. {
  66. $this->allowedTreeQuery = false;
  67. return $this->filterQueryBy(function ($query) {
  68. if (
  69. $query['method'] === 'where'
  70. && $query['arguments']
  71. && $query['arguments'][0] === $this->repository->getParentColumn()
  72. ) {
  73. return false;
  74. }
  75. return true;
  76. });
  77. }
  78. /**
  79. * 设置子节点查询链接需要忽略的字段
  80. *
  81. * @param string|array $keys
  82. *
  83. * @return $this
  84. */
  85. public function treeUrlWithoutQuery($keys)
  86. {
  87. $this->treeIgnoreQueryNames = array_merge(
  88. $this->treeIgnoreQueryNames,
  89. (array) $keys
  90. );
  91. return $this;
  92. }
  93. public function generateTreeUrl()
  94. {
  95. return Helper::urlWithoutQuery(
  96. $this->grid->filter()->urlWithoutFilters(),
  97. $this->treeIgnoreQueryNames
  98. );
  99. }
  100. protected function buildChildrenNodesPagination()
  101. {
  102. if ($this->grid()->allowPagination()) {
  103. $nextPage = $this->getCurrentChildrenPage() + 1;
  104. Admin::html(
  105. <<<HTML
  106. <next-page class="hidden">{$nextPage}</next-page>
  107. <last-page class="hidden">{$this->paginator()->lastPage()}</last-page>
  108. HTML
  109. );
  110. }
  111. }
  112. protected function sortTree(bool $sortable)
  113. {
  114. if (
  115. $sortable
  116. && ! $this->findQueryByMethod('orderBy')
  117. && ! $this->findQueryByMethod('orderByDesc')
  118. && ($orderColumn = $this->repository->getOrderColumn())
  119. ) {
  120. $this->orderBy($orderColumn)
  121. ->orderBy($this->repository->getKeyName());
  122. }
  123. }
  124. protected function bindChildrenNodesQuery()
  125. {
  126. if (! $this->allowedTreeQuery) {
  127. return;
  128. }
  129. $this->where($this->repository->getParentColumn(), $this->getParentIdFromRequest());
  130. }
  131. /**
  132. * @return mixed
  133. */
  134. public function getChildrenQueryNamePrefix()
  135. {
  136. return $this->grid->getName();
  137. }
  138. /**
  139. * @param mixed $parentId
  140. *
  141. * @return string
  142. */
  143. public function getChildrenPageName($parentId)
  144. {
  145. return $this->getChildrenQueryNamePrefix().'children_page_'.$parentId;
  146. }
  147. /**
  148. * @return int
  149. */
  150. public function getCurrentChildrenPage()
  151. {
  152. return $this->request->get(
  153. $this->getChildrenPageName(
  154. $this->getParentIdFromRequest()
  155. )
  156. ) ?: 1;
  157. }
  158. /**
  159. * @return string
  160. */
  161. public function getParentIdQueryName()
  162. {
  163. return $this->getChildrenQueryNamePrefix().$this->parentIdQueryName;
  164. }
  165. /**
  166. * @return int
  167. */
  168. public function getParentIdFromRequest()
  169. {
  170. return $this->request->get(
  171. $this->getParentIdQueryName()
  172. ) ?: 0;
  173. }
  174. /**
  175. * @return string
  176. */
  177. public function getLevelQueryName()
  178. {
  179. return $this->getChildrenQueryNamePrefix().$this->levelQueryName;
  180. }
  181. /**
  182. * @return int
  183. */
  184. public function getLevelFromRequest()
  185. {
  186. return $this->request->get(
  187. $this->getLevelQueryName()
  188. ) ?: 0;
  189. }
  190. /**
  191. * @return bool
  192. */
  193. public function showAllChildrenNodes()
  194. {
  195. return $this->showAllChildrenNodes;
  196. }
  197. }