2
0

BlogController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // app/Http/Controllers/BlogController.php
  3. namespace App\Http\Controllers;
  4. use App\Http\Api\UserApi;
  5. use App\Models\Post;
  6. use App\Models\ProgressChapter;
  7. use App\Models\Tag;
  8. use App\Services\ProgressChapterService;
  9. class BlogController extends Controller
  10. {
  11. protected $categories = [
  12. ['id' => 'sutta', 'label' => 'suttapiṭaka'],
  13. ['id' => 'vinaya', 'label' => 'vinayapiṭaka'],
  14. ['id' => 'abhidhamma', 'label' => 'abhidhammapiṭaka'],
  15. ['id' => 'añña', 'label' => 'añña'],
  16. ['id' => 'mūla', 'label' => 'mūla'],
  17. ['id' => 'aṭṭhakathā', 'label' => 'aṭṭhakathā'],
  18. ['id' => 'ṭīkā', 'label' => 'ṭīkā'],
  19. ];
  20. // 首页 - 最新博文列表
  21. public function index($user)
  22. {
  23. $user = UserApi::getByName($user);
  24. $posts = ProgressChapter::with('channel')
  25. ->where('progress', '>', 0.9)
  26. ->whereHas('channel', function ($query) use ($user) {
  27. $query->where('status', 30)->where('owner_uid', $user['id']);
  28. })
  29. ->latest()
  30. ->paginate(10);
  31. $categories = $this->categories;
  32. /*
  33. $posts = Post::published()
  34. ->with(['category', 'tags'])
  35. ->latest()
  36. ->paginate(10);
  37. $categories = Category::withCount('posts')->get();
  38. $popularPosts = Post::published()
  39. ->orderBy('views_count', 'desc')
  40. ->take(5)
  41. ->get();
  42. */
  43. // return view('blog.index', compact('posts', 'categories', 'popularPosts'));
  44. return view('blog.index', compact('user', 'posts', 'categories'));
  45. }
  46. /*
  47. // 博文详情页
  48. public function show(Post $post)
  49. {
  50. if (!$post->is_published) {
  51. abort(404);
  52. }
  53. $post->incrementViews();
  54. $post->load(['category', 'tags']);
  55. // 相关文章
  56. $relatedPosts = Post::published()
  57. ->where('category_id', $post->category_id)
  58. ->where('id', '!=', $post->id)
  59. ->take(3)
  60. ->get();
  61. // 上一篇和下一篇
  62. $prevPost = Post::published()
  63. ->where('published_at', '<', $post->published_at)
  64. ->latest()
  65. ->first();
  66. $nextPost = Post::published()
  67. ->where('published_at', '>', $post->published_at)
  68. ->oldest()
  69. ->first();
  70. return view('blog.show', compact('post', 'relatedPosts', 'prevPost', 'nextPost'));
  71. }
  72. // 分类列表
  73. public function categories()
  74. {
  75. $categories = Category::withCount('posts')
  76. ->having('posts_count', '>', 0)
  77. ->orderBy('posts_count', 'desc')
  78. ->get();
  79. return view('blog.categories', compact('categories'));
  80. }
  81. */
  82. // 分类下的文章
  83. public function category(
  84. $user,
  85. $category1,
  86. $category2 = null,
  87. $category3 = null,
  88. $category4 = null,
  89. $category5 = null
  90. ) {
  91. $user = UserApi::getByName($user);
  92. $categories = $this->categories;
  93. $chapterService = new ProgressChapterService;
  94. $tags = $this->getCategories($category1, $category2, $category3, $category4, $category5);
  95. $posts = $chapterService->setProgress(0.9)->setChannelOwnerId($user['id'])
  96. ->setTags($tags)
  97. ->get();
  98. $count = count($posts);
  99. $current = array_map(function ($item) {
  100. return ['id' => $item, 'label' => $item];
  101. }, $tags);
  102. $tagOptions = $chapterService->setProgress(0.9)->setChannelOwnerId($user['id'])
  103. ->setTags($tags)
  104. ->getTags();
  105. return view('blog.category', compact('user', 'categories', 'posts', 'current', 'tagOptions', 'count'));
  106. }
  107. private function getCategories($category1, $category2, $category3, $category4, $category5)
  108. {
  109. $category = [];
  110. if ($category1) {
  111. $category[] = $category1;
  112. }
  113. if ($category2) {
  114. $category[] = $category2;
  115. }
  116. if ($category3) {
  117. $category[] = $category3;
  118. }
  119. if ($category4) {
  120. $category[] = $category4;
  121. }
  122. if ($category5) {
  123. $category[] = $category5;
  124. }
  125. return $category;
  126. }
  127. /*
  128. // 年度归档
  129. public function archives()
  130. {
  131. $archives = Post::published()
  132. ->selectRaw('YEAR(published_at) as year, COUNT(*) as count')
  133. ->groupBy('year')
  134. ->orderBy('year', 'desc')
  135. ->get();
  136. return view('blog.archives', compact('archives'));
  137. }
  138. // 指定年份的文章
  139. public function archivesByYear($year)
  140. {
  141. $posts = Post::published()
  142. ->whereYear('published_at', $year)
  143. ->with(['category', 'tags'])
  144. ->latest()
  145. ->paginate(15);
  146. // 按月分组
  147. $postsByMonth = $posts->getCollection()->groupBy(function ($post) {
  148. return $post->published_at->format('Y-m');
  149. });
  150. return view('blog.archives-year', compact('posts', 'postsByMonth', 'year'));
  151. }
  152. // 标签页面
  153. public function tag(Tag $tag)
  154. {
  155. $posts = $tag->posts()
  156. ->published()
  157. ->with(['category', 'tags'])
  158. ->latest()
  159. ->paginate(10);
  160. return view('blog.tag', compact('tag', 'posts'));
  161. }
  162. // 搜索
  163. public function search(Request $request)
  164. {
  165. $query = $request->input('q');
  166. if (empty($query)) {
  167. return redirect()->route('blog.index');
  168. }
  169. $posts = Post::published()
  170. ->where(function ($q) use ($query) {
  171. $q->where('title', 'LIKE', "%{$query}%")
  172. ->orWhere('content', 'LIKE', "%{$query}%")
  173. ->orWhere('excerpt', 'LIKE', "%{$query}%");
  174. })
  175. ->with(['category', 'tags'])
  176. ->latest()
  177. ->paginate(10);
  178. return view('blog.search', compact('posts', 'query'));
  179. }
  180. */
  181. }