2
0

BlogController.php 6.0 KB

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