CategoryController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\PaliText;
  7. use App\Models\ProgressChapter;
  8. use App\Models\Tag;
  9. use App\Models\TagMap;
  10. class CategoryController extends Controller
  11. {
  12. protected static int $nextId = 1;
  13. public function index()
  14. {
  15. $categories = $this->loadCategories();
  16. // 获取一级分类和对应的书籍
  17. $categoryData = [];
  18. foreach ($categories as $category) {
  19. if ($category['level'] == 1) {
  20. $categoryBooks = $this->getBooks($categories, $category['id']);
  21. $categoryData[] = [
  22. 'category' => $category,
  23. 'books' => array_slice(array_values($categoryBooks), 0, 3)
  24. ];
  25. }
  26. }
  27. return view('library.index', compact('categoryData', 'categories'));
  28. }
  29. public function show($id)
  30. {
  31. $categories = $this->loadCategories();
  32. $currentCategory = collect($categories)->firstWhere('id', $id);
  33. if (!$currentCategory) {
  34. abort(404);
  35. }
  36. // 获取子分类
  37. $subCategories = array_filter($categories, function ($cat) use ($id) {
  38. return $cat['parent_id'] == $id;
  39. });
  40. // 获取该分类下的书籍
  41. $categoryBooks = $this->getBooks($categories, $id);
  42. // 获取面包屑
  43. $breadcrumbs = $this->getBreadcrumbs($currentCategory, $categories);
  44. return view('library.category', compact('currentCategory', 'subCategories', 'categoryBooks', 'breadcrumbs'));
  45. }
  46. private function getBooks($categories, $id)
  47. {
  48. $currentCategory = collect($categories)->firstWhere('id', $id);
  49. if (!$currentCategory) {
  50. abort(404);
  51. }
  52. // 标签查章节
  53. $tagNames = $currentCategory['tag'];
  54. $tm = (new TagMap)->getTable();
  55. $tg = (new Tag)->getTable();
  56. $pt = (new PaliText)->getTable();
  57. $where1 = " where co = " . count($tagNames);
  58. $a = implode(",", array_fill(0, count($tagNames), '?'));
  59. $in1 = "and t.name in ({$a})";
  60. $param = $tagNames;
  61. $where2 = "where level = 1";
  62. $query = "
  63. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  64. select anchor_id as cid from (
  65. select tm.anchor_id , count(*) as co
  66. from $tm as tm
  67. left join $tg as t on tm.tag_id = t.id
  68. where tm.table_name = 'pali_texts'
  69. $in1
  70. group by tm.anchor_id
  71. ) T
  72. $where1
  73. ) CID
  74. left join $pt as pt on CID.cid = pt.uid
  75. $where2
  76. order by book,paragraph";
  77. $chapters = DB::select($query, $param);
  78. $chaptersParam = [];
  79. foreach ($chapters as $key => $chapter) {
  80. $chaptersParam[] = [$chapter->book, $chapter->paragraph];
  81. }
  82. // 获取该分类下的章节
  83. $books = ProgressChapter::with('channel.owner')->whereIns(['book', 'para'], $chaptersParam)
  84. ->whereHas('channel', function ($query) {
  85. $query->where('status', 30);
  86. })
  87. ->where('progress', '>', config('mint.library.list_min_progress'))
  88. ->get();
  89. $pali = PaliText::where('level', 1)->get();
  90. // 获取该分类下的书籍
  91. $categoryBooks = [];
  92. $books->each(function ($book) use (&$categoryBooks, $id, $pali) {
  93. $title = $book->title;
  94. if (empty($title)) {
  95. $title = $pali->firstWhere('book', $book->book)->toc;
  96. }
  97. $categoryBooks[] = [
  98. "id" => $book->uid,
  99. "title" => $title,
  100. "author" => $book->channel->name,
  101. "publisher" => $book->channel->owner,
  102. "type" => __('labels.' . $book->channel->type),
  103. "category_id" => $id,
  104. "cover" => "/assets/images/cover/1/214.jpg",
  105. "description" => $book->summary ?? "比库戒律的详细说明",
  106. "language" => __('language.' . $book->channel->lang),
  107. ];
  108. });
  109. return $categoryBooks;
  110. }
  111. private function loadCategories()
  112. {
  113. $json = file_get_contents(public_path("app/palicanon/category/default.json"));
  114. $tree = json_decode($json, true);
  115. $flat = self::flattenWithIds($tree);
  116. return $flat;
  117. }
  118. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  119. {
  120. $flat = [];
  121. foreach ($tree as $node) {
  122. $currentId = self::$nextId++;
  123. $item = [
  124. 'id' => $currentId,
  125. 'parent_id' => $parentId,
  126. 'name' => $node['name'] ?? null,
  127. 'tag' => $node['tag'] ?? [],
  128. "description" => "佛教戒律经典",
  129. 'level' => $level,
  130. ];
  131. $flat[] = $item;
  132. if (isset($node['children']) && is_array($node['children'])) {
  133. $childrenLevel = $level + 1;
  134. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  135. }
  136. }
  137. return $flat;
  138. }
  139. private function getBreadcrumbs($category, $categories)
  140. {
  141. $breadcrumbs = [];
  142. $current = $category;
  143. while ($current) {
  144. array_unshift($breadcrumbs, $current);
  145. $current = collect($categories)->firstWhere('id', $current['parent_id']);
  146. }
  147. return $breadcrumbs;
  148. }
  149. }