Răsfoiți Sursa

Merge pull request #2301 from visuddhinanda/development

Development
visuddhinanda 10 luni în urmă
părinte
comite
48340387be

+ 44 - 8
api-v8/app/Http/Controllers/BlogController.php

@@ -3,21 +3,24 @@
 namespace App\Http\Controllers;
 
 use App\Models\Post;
-use App\Models\Category;
 use App\Models\Tag;
 use App\Models\ProgressChapter;
 
-use Illuminate\Http\Request;
-use Carbon\Carbon;
 use App\Http\Api\UserApi;
 use Illuminate\Support\Facades\Log;
 
+use App\Services\ProgressChapterService;
+
 class BlogController extends Controller
 {
     protected         $categories = [
-        ['id' => 'sutta', 'label' => 'sutta'],
-        ['id' => 'vinaya', 'label' => 'vinaya'],
-        ['id' => 'abhidhamma', 'label' => 'abhidhamma'],
+        ['id' => 'sutta', 'label' => 'suttapiṭaka'],
+        ['id' => 'vinaya', 'label' => 'vinayapiṭaka'],
+        ['id' => 'abhidhamma', 'label' => 'abhidhammapiṭaka'],
+        ['id' => 'añña', 'label' => 'añña'],
+        ['id' => 'mūla', 'label' => 'mūla'],
+        ['id' => 'aṭṭhakathā', 'label' => 'aṭṭhakathā'],
+        ['id' => 'ṭīkā', 'label' => 'ṭīkā'],
     ];
     // 首页 - 最新博文列表
     public function index($user)
@@ -101,10 +104,43 @@ class BlogController extends Controller
         $category5 = null
     ) {
         $user = UserApi::getByName($user);
-        $posts = [];
         $categories = $this->categories;
+        $chapterService = new ProgressChapterService();
 
-        return view('blog.category', compact('user', 'categories', 'posts'));
+        $tags = $this->getCategories($category1, $category2, $category3, $category4, $category5);
+        $posts = $chapterService->setProgress(0.9)->setChannelOwnerId($user['id'])
+            ->setTags($tags)
+            ->get();
+        $count = count($posts);
+        $current = array_map(function ($item) {
+            return ['id' => $item, 'label' => $item];
+        }, $tags);
+
+        $tagOptions = $chapterService->setProgress(0.9)->setChannelOwnerId($user['id'])
+            ->setTags($tags)
+            ->getTags();
+        return view('blog.category', compact('user', 'categories', 'posts', 'current', 'tagOptions', 'count'));
+    }
+
+    private function getCategories($category1, $category2, $category3, $category4, $category5)
+    {
+        $category = [];
+        if ($category1) {
+            $category[] = $category1;
+        }
+        if ($category2) {
+            $category[] = $category2;
+        }
+        if ($category3) {
+            $category[] = $category3;
+        }
+        if ($category4) {
+            $category[] = $category4;
+        }
+        if ($category5) {
+            $category[] = $category5;
+        }
+        return $category;
     }
     /*
     // 年度归档

+ 7 - 1
api-v8/app/Models/ProgressChapter.php

@@ -35,7 +35,13 @@ class ProgressChapter extends Model
 
     public function tags()
     {
-        return $this->belongsToMany('App\Models\Tag', 'tag_maps', 'anchor_id', 'tag_id');
+        return $this->belongsToMany(
+            'App\Models\Tag',
+            'tag_maps',
+            'anchor_id',
+            'tag_id',
+            'uid'
+        );
     }
     /**
      * 关联到 Channel 模型

+ 3 - 0
api-v8/app/Models/Tag.php

@@ -17,6 +17,9 @@ class Tag extends Model
 
     use HasFactory;
 
+    /**
+     * 关联到ProgressChapter
+     */
     public function chapters()
     {
         return $this->belongsToMany('App\Models\ProgressChapter', 'tag_maps', 'tag_id', 'anchor_id');

+ 80 - 0
api-v8/app/Services/ProgressChapterService.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\ProgressChapter;
+use App\Models\TagMap;
+
+use Illuminate\Support\Facades\Log;
+
+class ProgressChapterService
+{
+    protected $tags = null;
+    protected $channelId = null;
+    protected $channelType = null;
+    protected $channelOwnerId = null;
+    protected $minProgress = 0.01;
+    public function setProgress($progress)
+    {
+        $this->minProgress = $progress;
+        return $this;
+    }
+    public function setChannel($channelId)
+    {
+        $this->channelId = $channelId;
+        return $this;
+    }
+    public function setChannelType($channelType)
+    {
+        $this->channelType = $channelType;
+        return $this;
+    }
+    public function setChannelOwnerId($channelOwnerId)
+    {
+        $this->channelOwnerId = $channelOwnerId;
+        return $this;
+    }
+    public function setTags($tags)
+    {
+        $this->tags = $tags;
+        return $this;
+    }
+    public function get()
+    {
+        $chapters = ProgressChapter::where('progress', '>', $this->minProgress)
+            ->whereHas('channel', function ($query) {
+                $query->where('owner_uid', $this->channelOwnerId);
+            })->whereHas('tags', function ($query) {
+                $query->whereIn('name', $this->tags);
+            })->get();
+        return $chapters;
+    }
+    public function getTags()
+    {
+        $chapters = ProgressChapter::where('progress', '>', $this->minProgress)
+            ->whereHas('channel', function ($query) {
+                $query->where('owner_uid', $this->channelOwnerId);
+            })->whereHas('tags', function ($query) {
+                $query->whereIn('name', $this->tags);
+            })->select('uid')->get();
+        $tagMaps = TagMap::with('tags')->whereIn('anchor_id', $chapters)
+            ->get();
+        $tags = [];
+        foreach ($tagMaps as $key => $value) {
+            if (isset($tags[$value->tag_id])) {
+                $tags[$value->tag_id]['count']++;
+            } else {
+                $tags[$value->tag_id] = [
+                    'tag' => $value->tags,
+                    'count' => 1
+                ];
+            }
+        }
+        $tagsValue = array_values($tags);
+        // 按 count 降序排序
+        usort($tagsValue, function ($a, $b) {
+            return $b['count'] <=> $a['count']; // PHP 7+ 使用 spaceship 运算符
+        });
+        return $tagsValue;
+    }
+}

+ 16 - 7
api-v8/resources/views/blog/category.blade.php

@@ -9,12 +9,21 @@
 
     <div class="section-card">
         <div class="section-details">
-            <h3 class="section-count">1 page</h3>
-            <h1 class="section-term">Example Category</h1>
-
-            <h2 class="section-description">
-                A description of this category
-            </h2>
+            <h3 class="section-count">{{ $count }} page</h3>
+            <h1 class="section-term">
+                @foreach ($current as $category)
+                /<a href="{{ route('category', ['user' => $user['userName'],'category1' => $category['id'],]) }}">
+                    {{ $category['label'] }}
+                </a>
+                @endforeach
+            </h1>
+            <section class="widget tagCloud">
+                <div class="tagCloud-tags">
+                    @foreach($tagOptions as $id => $tag)
+                    <a href="{{ $tag['tag']->name }}">{{ $tag['tag']->name }}</a>
+                    @endforeach
+                </div>
+            </section>
         </div>
     </div>
 </header>
@@ -22,7 +31,7 @@
 <section class="article-list--compact">
     @foreach ($posts as $post)
     <article>
-        <a href="https://demo.stack.jimmycai.com/p/hello-world/">
+        <a href="{{ route('book.read', $post['uid']) }}">
             <div class="article-details">
                 <h2 class="article-title">{{ $post->title }}</h2>
                 <footer class="article-time">

+ 1 - 4
api-v8/resources/views/blog/layouts/app.blade.php

@@ -378,10 +378,7 @@
                 <div class="tagCloud-tags">
                     @foreach($categories as $category)
                     <a
-                        href="{{ route('category', [
-    'user' => $user['userName'],
-    'category1' => $category['id'],
-]) }}"
+                        href="{{ route('category', ['user' => $user['userName'],'category1' => $category['id'],]) }}"
                         class="font_size_1">
                         {{ $category['label'] }}
                     </a>