ModelTree.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace Dcat\Admin\Traits;
  3. use Dcat\Admin\Support\Helper;
  4. use Dcat\Admin\Tree;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Request;
  10. use Spatie\EloquentSortable\SortableTrait;
  11. /**
  12. * @property string $parentColumn
  13. * @property string $titleColumn
  14. * @property string $orderColumn
  15. * @property array $sortable
  16. */
  17. trait ModelTree
  18. {
  19. use SortableTrait;
  20. /**
  21. * @var array
  22. */
  23. protected static $branchOrder = [];
  24. /**
  25. * @var \Closure[]
  26. */
  27. protected $queryCallbacks = [];
  28. /**
  29. * Get children of current node.
  30. *
  31. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  32. */
  33. public function children()
  34. {
  35. return $this->hasMany(static::class, $this->getParentColumn());
  36. }
  37. /**
  38. * Get parent of current node.
  39. *
  40. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  41. */
  42. public function parent()
  43. {
  44. return $this->belongsTo(static::class, $this->getParentColumn());
  45. }
  46. /**
  47. * @return string
  48. */
  49. public function getParentColumn()
  50. {
  51. return empty($this->parentColumn) ? 'parent_id' : $this->parentColumn;
  52. }
  53. /**
  54. * Set parent column.
  55. *
  56. * @param string $column
  57. */
  58. public function setParentColumn(string $column)
  59. {
  60. $this->parentColumn = $column;
  61. }
  62. /**
  63. * Get title column.
  64. *
  65. * @return string
  66. */
  67. public function getTitleColumn()
  68. {
  69. return empty($this->titleColumn) ? 'title' : $this->titleColumn;
  70. }
  71. /**
  72. * Set title column.
  73. *
  74. * @param string $column
  75. */
  76. public function setTitleColumn(string $column)
  77. {
  78. $this->titleColumn = $column;
  79. }
  80. /**
  81. * Get order column name.
  82. *
  83. * @return string
  84. */
  85. public function getOrderColumn()
  86. {
  87. return empty($this->orderColumn) ? 'order' : $this->orderColumn;
  88. }
  89. /**
  90. * Set order column.
  91. *
  92. * @param string $column
  93. */
  94. public function setOrderColumn(string $column)
  95. {
  96. $this->orderColumn = $column;
  97. }
  98. /**
  99. * Set query callback to model.
  100. *
  101. * @param \Closure|null $query
  102. *
  103. * @return $this
  104. */
  105. public function withQuery(\Closure $query = null)
  106. {
  107. $this->queryCallbacks[] = $query;
  108. return $this;
  109. }
  110. /**
  111. * Format data to tree like array.
  112. *
  113. * @return array
  114. */
  115. public function toTree(array $nodes = null)
  116. {
  117. if ($nodes === null) {
  118. $nodes = $this->allNodes();
  119. }
  120. return Helper::buildNestedArray(
  121. $nodes,
  122. 0,
  123. $this->getKeyName(),
  124. $this->getParentColumn()
  125. );
  126. }
  127. /**
  128. * Get all elements.
  129. *
  130. * @return mixed
  131. */
  132. public function allNodes()
  133. {
  134. $orderColumn = DB::getQueryGrammar()->wrap($this->getOrderColumn());
  135. $byOrder = 'ROOT ASC, '.$orderColumn;
  136. return $this->callQueryCallbacks(new static())
  137. ->selectRaw('*, '.$orderColumn.' ROOT')
  138. ->orderByRaw($byOrder)
  139. ->get()
  140. ->toArray();
  141. }
  142. /**
  143. * @param $this $model
  144. *
  145. * @return $this|Builder
  146. */
  147. protected function callQueryCallbacks($model)
  148. {
  149. foreach ($this->queryCallbacks as $callback) {
  150. if ($callback) {
  151. $model = $callback($model);
  152. }
  153. }
  154. return $model;
  155. }
  156. /**
  157. * Set the order of branches in the tree.
  158. *
  159. * @param array $order
  160. *
  161. * @return void
  162. */
  163. protected static function setBranchOrder(array $order)
  164. {
  165. static::$branchOrder = array_flip(Arr::flatten($order));
  166. static::$branchOrder = array_map(function ($item) {
  167. return ++$item;
  168. }, static::$branchOrder);
  169. }
  170. /**
  171. * Save tree order from a tree like array.
  172. *
  173. * @param array $tree
  174. * @param int $parentId
  175. */
  176. public static function saveOrder($tree = [], $parentId = 0)
  177. {
  178. if (empty(static::$branchOrder)) {
  179. static::setBranchOrder($tree);
  180. }
  181. foreach ($tree as $branch) {
  182. $node = static::find($branch['id']);
  183. $node->{$node->getParentColumn()} = $parentId;
  184. $node->{$node->getOrderColumn()} = static::$branchOrder[$branch['id']];
  185. $node->save();
  186. if (isset($branch['children'])) {
  187. static::saveOrder($branch['children'], $branch['id']);
  188. }
  189. }
  190. }
  191. protected function determineOrderColumnName()
  192. {
  193. return $this->getOrderColumn();
  194. }
  195. public function moveOrderDown()
  196. {
  197. $orderColumnName = $this->determineOrderColumnName();
  198. $parentColumnName = $this->getParentColumn();
  199. $swapWithModel = $this->buildSortQuery()->limit(1)
  200. ->ordered()
  201. ->where($orderColumnName, '>', $this->$orderColumnName)
  202. ->where($parentColumnName, $this->$parentColumnName)
  203. ->first();
  204. if (! $swapWithModel) {
  205. return $this;
  206. }
  207. return $this->swapOrderWithModel($swapWithModel);
  208. }
  209. public function moveOrderUp()
  210. {
  211. $orderColumnName = $this->determineOrderColumnName();
  212. $parentColumnName = $this->getParentColumn();
  213. $swapWithModel = $this->buildSortQuery()->limit(1)
  214. ->ordered('desc')
  215. ->where($orderColumnName, '<', $this->$orderColumnName)
  216. ->where($parentColumnName, $this->$parentColumnName)
  217. ->first();
  218. if (! $swapWithModel) {
  219. return $this;
  220. }
  221. return $this->swapOrderWithModel($swapWithModel);
  222. }
  223. public function moveToStart()
  224. {
  225. $parentColumnName = $this->getParentColumn();
  226. $firstModel = $this->buildSortQuery()->limit(1)
  227. ->ordered()
  228. ->where($parentColumnName, $this->$parentColumnName)
  229. ->first();
  230. if ($firstModel->id === $this->id) {
  231. return $this;
  232. }
  233. $orderColumnName = $this->determineOrderColumnName();
  234. $this->$orderColumnName = $firstModel->$orderColumnName;
  235. $this->save();
  236. $this->buildSortQuery()->where($this->getKeyName(), '!=', $this->id)->increment($orderColumnName);
  237. return $this;
  238. }
  239. public function getHighestOrderNumber(): int
  240. {
  241. $parentColumnName = $this->getParentColumn();
  242. return (int) $this->buildSortQuery()
  243. ->where($parentColumnName, $this->$parentColumnName)
  244. ->max($this->determineOrderColumnName());
  245. }
  246. /**
  247. * Get options for Select field in form.
  248. *
  249. * @param \Closure|null $closure
  250. * @param string $rootText
  251. *
  252. * @return array
  253. */
  254. public static function selectOptions(\Closure $closure = null, $rootText = null)
  255. {
  256. $rootText = $rootText ?: admin_trans_label('root');
  257. $options = (new static())->withQuery($closure)->buildSelectOptions();
  258. return collect($options)->prepend($rootText, 0)->all();
  259. }
  260. /**
  261. * Build options of select field in form.
  262. *
  263. * @param array $nodes
  264. * @param int $parentId
  265. * @param string $prefix
  266. *
  267. * @return array
  268. */
  269. protected function buildSelectOptions(array $nodes = [], $parentId = 0, $prefix = '')
  270. {
  271. $prefix = $prefix ?: str_repeat('&nbsp;', 6);
  272. $options = [];
  273. if (empty($nodes)) {
  274. $nodes = $this->allNodes();
  275. }
  276. $titleColumn = $this->getTitleColumn();
  277. $parentColumn = $this->getParentColumn();
  278. foreach ($nodes as $node) {
  279. $node[$titleColumn] = $prefix.'&nbsp;'.$node[$titleColumn];
  280. if ($node[$parentColumn] == $parentId) {
  281. $children = $this->buildSelectOptions($nodes, $node[$this->getKeyName()], $prefix.$prefix);
  282. $options[$node[$this->getKeyName()]] = $node[$titleColumn];
  283. if ($children) {
  284. $options += $children;
  285. }
  286. }
  287. }
  288. return $options;
  289. }
  290. /**
  291. * {@inheritdoc}
  292. */
  293. public function delete()
  294. {
  295. $this->where($this->getParentColumn(), $this->getKey())->delete();
  296. return parent::delete();
  297. }
  298. /**
  299. * {@inheritdoc}
  300. */
  301. protected static function boot()
  302. {
  303. parent::boot();
  304. static::saving(function (Model $branch) {
  305. $parentColumn = $branch->getParentColumn();
  306. if (
  307. $branch->getKey()
  308. && Request::has($parentColumn)
  309. && Request::input($parentColumn) == $branch->getKey()
  310. ) {
  311. throw new \Exception(trans('admin.parent_select_error'));
  312. }
  313. if (Request::has('_order')) {
  314. $order = Request::input('_order');
  315. Request::offsetUnset('_order');
  316. Tree::make(new static())->saveOrder($order);
  317. return false;
  318. }
  319. return $branch;
  320. });
  321. }
  322. }