HasTree.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 $tierQueryName = '_tier_';
  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. $this->addIgnoreQueries();
  48. });
  49. $this->collection(function (Collection $collection) {
  50. if (! $this->getParentIdFromRequest()) {
  51. return $collection;
  52. }
  53. if ($collection->isEmpty()) {
  54. abort(404);
  55. }
  56. $this->buildChildrenNodesPagination();
  57. return $collection;
  58. });
  59. }
  60. /**
  61. * 设置保存为"前一个页面地址"时需要忽略的参数.
  62. */
  63. protected function addIgnoreQueries()
  64. {
  65. Admin::addIgnoreQueryName([
  66. $this->getParentIdQueryName(),
  67. $this->getTierQueryName(),
  68. $this->getChildrenPageName($this->getParentIdFromRequest()),
  69. ]);
  70. }
  71. /**
  72. * 禁止树形表格查询.
  73. *
  74. * @return $this
  75. */
  76. public function disableBindTreeQuery()
  77. {
  78. $this->allowedTreeQuery = false;
  79. return $this->filterQueryBy(function ($query) {
  80. if (
  81. $query['method'] === 'where'
  82. && $query['arguments']
  83. && $query['arguments'][0] === optional($this->repository)->getParentColumn()
  84. ) {
  85. return false;
  86. }
  87. return true;
  88. });
  89. }
  90. /**
  91. * 设置子节点查询链接需要忽略的字段.
  92. *
  93. * @param string|array $keys
  94. *
  95. * @return $this
  96. */
  97. public function treeUrlWithoutQuery($keys)
  98. {
  99. $this->treeIgnoreQueryNames = array_merge(
  100. $this->treeIgnoreQueryNames,
  101. (array) $keys
  102. );
  103. return $this;
  104. }
  105. public function generateTreeUrl()
  106. {
  107. return Helper::urlWithoutQuery(
  108. $this->grid->filter()->urlWithoutFilters(),
  109. $this->treeIgnoreQueryNames
  110. );
  111. }
  112. protected function buildChildrenNodesPagination()
  113. {
  114. if ($this->grid()->allowPagination()) {
  115. $nextPage = $this->getCurrentChildrenPage() + 1;
  116. Admin::html(
  117. <<<HTML
  118. <next-page class="hidden">{$nextPage}</next-page>
  119. <last-page class="hidden">{$this->paginator()->lastPage()}</last-page>
  120. HTML
  121. );
  122. }
  123. }
  124. protected function sortTree(bool $sortable)
  125. {
  126. if (
  127. $sortable
  128. && ! $this->findQueryByMethod('orderBy')
  129. && ! $this->findQueryByMethod('orderByDesc')
  130. && ($orderColumn = $this->repository->getOrderColumn())
  131. ) {
  132. $this->orderBy($orderColumn)
  133. ->orderBy($this->repository->getKeyName());
  134. }
  135. }
  136. protected function bindChildrenNodesQuery()
  137. {
  138. if (! $this->allowedTreeQuery) {
  139. return;
  140. }
  141. $this->where($this->repository->getParentColumn(), $this->getParentIdFromRequest());
  142. }
  143. /**
  144. * @return mixed
  145. */
  146. public function getChildrenQueryNamePrefix()
  147. {
  148. return $this->grid->getName();
  149. }
  150. /**
  151. * @param mixed $parentId
  152. *
  153. * @return string
  154. */
  155. public function getChildrenPageName($parentId)
  156. {
  157. return $this->getChildrenQueryNamePrefix().'_children_page_'.$parentId;
  158. }
  159. /**
  160. * @return int
  161. */
  162. public function getCurrentChildrenPage()
  163. {
  164. return $this->request->get(
  165. $this->getChildrenPageName(
  166. $this->getParentIdFromRequest()
  167. )
  168. ) ?: 1;
  169. }
  170. /**
  171. * @return string
  172. */
  173. public function getParentIdQueryName()
  174. {
  175. return $this->getChildrenQueryNamePrefix().$this->parentIdQueryName;
  176. }
  177. /**
  178. * @return int
  179. */
  180. public function getParentIdFromRequest()
  181. {
  182. return $this->request->get(
  183. $this->getParentIdQueryName()
  184. ) ?: 0;
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function getTierQueryName()
  190. {
  191. return $this->getChildrenQueryNamePrefix().$this->tierQueryName;
  192. }
  193. /**
  194. * @return int
  195. */
  196. public function getTierFromRequest()
  197. {
  198. return $this->request->get(
  199. $this->getTierQueryName()
  200. ) ?: 0;
  201. }
  202. /**
  203. * @return bool
  204. */
  205. public function showAllChildrenNodes()
  206. {
  207. return $this->showAllChildrenNodes;
  208. }
  209. }