Explorar o código

Merge pull request #2295 from visuddhinanda/development

Development
visuddhinanda hai 10 meses
pai
achega
e96c575709

+ 95 - 0
api-v8/app/Http/Controllers/BookController.php

@@ -58,6 +58,7 @@ class BookController extends Controller
         $book['toc'] = $this->getBookToc($bookRaw->book, $bookRaw->para, $channelId, 2, 7);
         $book['categories'] = $this->getBookCategory($bookRaw->book, $bookRaw->para);
         $book['tags'] = [];
+        $book['pagination'] = $this->pagination($bookRaw);
         $book['content'] = $this->getBookContent($id);
         return view('library.book.read', compact('book'));
     }
@@ -222,6 +223,100 @@ class BookController extends Controller
 
         return $result;
     }
+
+    public function pagination($book)
+    {
+        $currBook = $this->bookStart($book->book, $book->para);
+        $start = $currBook->paragraph;
+        $end = $currBook->paragraph + $currBook->chapter_len - 1;
+        // 查询起始段落
+        $paragraphs = PaliText::where('book', $book->book)
+            ->whereBetween('paragraph', [$start, $end])
+            ->where('level', '<', 8)
+            ->orderBy('paragraph')
+            ->get();
+        $curr = $paragraphs->firstWhere('paragraph', $book->para);
+        $current = $curr; //实际显示的段落
+        $endParagraph = $curr->paragraph + $curr->chapter_len - 1;
+        if ($curr->chapter_strlen > $this->maxChapterLen) {
+            //太大了,修改结束位置 找到下一级
+            foreach ($paragraphs as $key => $paragraph) {
+                if ($paragraph->paragraph > $curr->paragraph) {
+                    if ($paragraph->chapter_strlen <= $this->maxChapterLen) {
+                        $endParagraph = $paragraph->paragraph + $paragraph->chapter_len - 1;
+                        $current = $paragraph;
+                        break;
+                    }
+                    if ($paragraph->level <= $curr->level) {
+                        //不能往下走了,就是它了
+                        $endParagraph = $paragraphs[$key - 1]->paragraph + $paragraphs[$key - 1]->chapter_len - 1;
+                        $current = $paragraph;
+                        break;
+                    }
+                }
+            }
+        }
+        $start = $curr->paragraph;
+        $end = $endParagraph;
+        $nextPali = $this->next($current->book, $current->paragraph, $current->level);
+        $prevPali = $this->prev($current->book, $current->paragraph, $current->level);
+
+        $next = null;
+        if ($nextPali) {
+            $nextTranslation = ProgressChapter::with('channel.owner')
+                ->where('book', $nextPali->book)
+                ->where('para', $nextPali->paragraph)
+                ->where('channel_id', $book->channel_id)
+                ->first();
+            if ($nextTranslation) {
+                if (!empty($nextTranslation->title)) {
+                    $next['title'] = $nextTranslation->title;
+                } else {
+                    $next['title'] = $nextPali->toc;
+                }
+                $next['id'] = $nextTranslation->uid;
+            }
+        }
+
+        $prev = null;
+        if ($prevPali) {
+            $prevTranslation = ProgressChapter::with('channel.owner')
+                ->where('book', $prevPali->book)
+                ->where('para', $prevPali->paragraph)
+                ->where('channel_id', $book->channel_id)
+                ->first();
+            if ($prevTranslation) {
+                if (!empty($prevTranslation->title)) {
+                    $prev['title'] = $prevTranslation->title;
+                } else {
+                    $prev['title'] = $prevPali->toc;
+                }
+                $prev['id'] = $prevTranslation->uid;
+            }
+        }
+
+        return compact('start', 'end', 'next', 'prev');
+    }
+
+    public function next($book, $paragraph, $level)
+    {
+        $next = PaliText::where('book', $book)
+            ->where('paragraph', '>', $paragraph)
+            ->where('level', $level)
+            ->orderBy('paragraph')
+            ->first();
+        return $next ?? null;
+    }
+    public function prev($book, $paragraph, $level)
+    {
+        $prev = PaliText::where('book', $book)
+            ->where('paragraph', '<', $paragraph)
+            ->where('level', $level)
+            ->orderBy('paragraph', 'desc')
+            ->first();
+
+        return $prev ?? null;
+    }
     public function show2($id)
     {
         // Sample book data (replace with database query)

+ 8 - 4
api-v8/resources/views/library/book/read.blade.php

@@ -321,8 +321,9 @@
                 <!-- Nav buttons -->
                 <div class="mt-6 pt-6">
                     <ul class="pagination">
+                        @if(!empty($book["pagination"]["prev"]))
                         <li class="page-item page-prev">
-                            <a class="page-link" href="/ui/components/autosize/">
+                            <a class="page-link" href='{{ route("book.read",$book["pagination"]["prev"]["id"]) }}'>
                                 <div class="row align-items-center">
                                     <div class="col-auto">
                                         <!-- Download SVG icon from http://tabler.io/icons/icon/chevron-left -->
@@ -332,17 +333,19 @@
                                     </div>
                                     <div class="col">
                                         <div class="page-item-subtitle">previous</div>
-                                        <div class="page-item-title">Autosize</div>
+                                        <div class="page-item-title">{{ $book["pagination"]["prev"]["title"] }}</div>
                                     </div>
                                 </div>
                             </a>
                         </li>
+                        @endif
+                        @if(!empty($book["pagination"]["next"]))
                         <li class="page-item page-next">
-                            <a class="page-link" href="/ui/components/badges/">
+                            <a class="page-link" href='{{ route("book.read",$book["pagination"]["next"]["id"]) }}'>
                                 <div class="row align-items-center">
                                     <div class="col">
                                         <div class="page-item-subtitle">next</div>
-                                        <div class="page-item-title">Badges</div>
+                                        <div class="page-item-title">{{ $book["pagination"]["next"]["title"] }}</div>
                                     </div>
                                     <div class="col-auto">
                                         <!-- Download SVG icon from http://tabler.io/icons/icon/chevron-right -->
@@ -353,6 +356,7 @@
                                 </div>
                             </a>
                         </li>
+                        @endif
                     </ul>
                 </div>
                 <!-- Related Books -->