SiteAlbumFolder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\SiteAlbumFolder as Model;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class SiteAlbumFolder extends EloquentRepository
  6. {
  7. /**
  8. * Model.
  9. *
  10. * @var string
  11. */
  12. protected $eloquentClass = Model::class;
  13. /*
  14. * 改写列表显示返回的数据
  15. */
  16. public function get(Grid\Model|\Dcat\Admin\Grid\Model $model)
  17. {
  18. // 获取模型对象
  19. $obj = $this->model();
  20. // 获取数据
  21. $tree = $obj->orderBy('order', 'desc')->orderBy('id', 'asc')->get();
  22. $treeData = $tree->toArray();
  23. // 构建树形结构
  24. $data=buildTree($treeData);
  25. // 转换成一维数组
  26. $data=flattenTree($data);
  27. return $model->makePaginator(
  28. count($data), // 传入总记录数
  29. $data // 传入数据二维数组
  30. );
  31. }
  32. public static function selectMainOptions(\Closure $closure = null)
  33. {
  34. if (!$closure) {
  35. $closure = function ($query) {
  36. $query = $query->where("enabled", 1)->orderBy('order', 'desc')->orderBy('id', 'asc');
  37. return $query;
  38. };
  39. }
  40. $selectOptions = Model::class::selectOptions($closure);
  41. //只显示2层
  42. foreach ($selectOptions as $key => $value) {
  43. if (substr_count($value,'&nbsp;') > 8) {
  44. unset($selectOptions[$key]);
  45. }
  46. }
  47. return $selectOptions;
  48. }
  49. // 调用模型方法
  50. public static function selectOptions(\Closure $closure = null)
  51. {
  52. if (!$closure) {
  53. $closure = function ($query) {
  54. $query = $query->where('enabled', 1);
  55. $query = $query->orderBy('order', 'desc')->orderBy('id', 'asc');
  56. return $query;
  57. };
  58. }
  59. $selectOptions = Model::class::selectOptions($closure);
  60. return $selectOptions;
  61. }
  62. public static function siteAlbumFolderSelectOptions($inIds) {
  63. $ids = self::findIdsWithParents($inIds);
  64. $result = self::selectOptions(function ($query) use ($ids) {
  65. $query = $query->wherein('id', $ids);
  66. $query = $query->where('enabled', 1);
  67. $query = $query->orderBy('order', 'desc')->orderBy('id', 'asc');
  68. return $query;
  69. });
  70. unset($result[0]);
  71. return $result;
  72. }
  73. public static function siteAlbumFolderSelectOptionsAll() {
  74. $result = self::selectOptions(function ($query) {
  75. $query = $query->where('enabled', 1);
  76. $query = $query->orderBy('order', 'desc')->orderBy('id', 'asc');
  77. return $query;
  78. });
  79. unset($result[0]);
  80. return $result;
  81. }
  82. /**
  83. * 递归查找包括 $inIds 在内的所有记录及其父 ID 的记录
  84. *
  85. * @param array $inIds 初始 ID 数组
  86. * @param array $resultIds 结果 ID 数组(递归时使用)
  87. * @return array
  88. */
  89. public static function findIdsWithParents(array $inIds, array &$resultIds = []): array
  90. {
  91. $model = new Model();
  92. // 查询当前 ID 数组对应的记录
  93. $folders = $model->whereIn('id', $inIds)->get();
  94. foreach ($folders as $folder) {
  95. // 如果当前记录的 ID 尚未加入结果数组,则加入
  96. if (!in_array($folder->id, $resultIds)) {
  97. $resultIds[] = $folder->id;
  98. }
  99. // 如果当前记录有父 ID,则递归查找父记录
  100. if ($folder->parent_id) {
  101. self::findIdsWithParents([$folder->parent_id], $resultIds);
  102. }
  103. }
  104. return $resultIds;
  105. }
  106. public static function getAllFolderTabs()
  107. {
  108. $model = new Model();
  109. $folders = $model->where('enabled', 1)->orderBy('order', 'desc')->orderBy('id', 'asc')->get();
  110. $tabs = [];
  111. foreach ($folders as $folder) {
  112. $tabs[$folder->id] = json_decode($folder->show_tabs);
  113. }
  114. return $tabs;
  115. }
  116. }