BookController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace App\Http\Controllers\Library;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\ProgressChapter;
  6. use App\Models\PaliText;
  7. use App\Models\Sentence;
  8. use App\Services\ChapterService;
  9. class BookController extends Controller
  10. {
  11. protected $maxChapterLen = 50000;
  12. protected $minChapterLen = 100;
  13. public function show($id)
  14. {
  15. $bookRaw = $this->loadBook($id);
  16. if (!$bookRaw) {
  17. abort(404);
  18. }
  19. //查询章节
  20. $channelId = $bookRaw->channel_id; // 替换为具体的 channel_id 值
  21. $book = $this->getBookInfo($bookRaw);
  22. $book['contents'] = $this->getBookToc($bookRaw->book, $bookRaw->para, $channelId);
  23. // 获取其他版本
  24. $others = ProgressChapter::with('channel.owner')
  25. ->where('book', $bookRaw->book)
  26. ->where('para', $bookRaw->para)
  27. ->whereHas('channel', function ($query) {
  28. $query->where('status', 30);
  29. })
  30. ->where('progress', '>', 0.2)
  31. ->get();
  32. $otherVersions = [];
  33. $others->each(function ($book) use (&$otherVersions, $id) {
  34. $otherVersions[] = $this->getBookInfo($book);
  35. });
  36. return view('library.book.show', compact('book', 'otherVersions'));
  37. }
  38. public function read(Request $request, $id)
  39. {
  40. $chapterService = app(ChapterService::class);
  41. [$book, $para] = explode('-', $id);
  42. $channelId = $request->input('channel');
  43. $chapterUid = $chapterService->getUidByChannel($book, $para, $channelId);
  44. $bookRaw = $this->loadBook($chapterUid);
  45. if (!$bookRaw) {
  46. abort(404);
  47. }
  48. $channelId = $bookRaw->channel_id; // 替换为具体的 channel_id 值
  49. $book = $this->getBookInfo($bookRaw);
  50. $book['toc'] = $this->getBookToc($bookRaw->book, $bookRaw->para, $channelId, 2, 7);
  51. $book['categories'] = $this->getBookCategory($bookRaw->book, $bookRaw->para);
  52. $book['tags'] = [];
  53. $book['pagination'] = $this->pagination($bookRaw);
  54. $book['content'] = $this->getBookContent($chapterUid);
  55. $channels = $chapterService->publicChannels($bookRaw->book, $bookRaw->para);
  56. $editor_link = config('mint.server.dashboard_base_path') . "/workspace/tipitaka/chapter/{$id}?channel={$channelId}";
  57. return view('library.book.read', compact('book', 'channels', 'editor_link'));
  58. }
  59. private function loadBook($id)
  60. {
  61. $book = ProgressChapter::with('channel.owner')->find($id);
  62. return $book;
  63. }
  64. public function toggleTheme(Request $request)
  65. {
  66. $theme = $request->input('theme', 'light');
  67. session(['theme' => $theme]);
  68. return response()->json(['status' => 'success']);
  69. }
  70. private function getBookInfo($book)
  71. {
  72. $title = $book->title;
  73. if (empty($title)) {
  74. $title = PaliText::where('book', $book->book)
  75. ->where('paragraph', $book->para)->first()->toc;
  76. }
  77. return [
  78. "id" => $book->uid,
  79. "title" => $title,
  80. "author" => $book->channel->name,
  81. "publisher" => $book->channel->owner,
  82. "type" => __('label.' . $book->channel->type),
  83. "category_id" => 11,
  84. "cover" => "/assets/images/cover/1/214.jpg",
  85. "description" => $book->summary ?? "",
  86. "language" => __('language.' . $book->channel->lang),
  87. ];
  88. }
  89. private function getBookToc(int $book, int $paragraph, string $channelId, $minLevel = 2, $maxLevel = 2)
  90. {
  91. //先找到书的起始(书名)章节
  92. //一个book 里面可以有多本书
  93. $currBook = $this->bookStart($book, $paragraph);
  94. $start = $currBook->paragraph;
  95. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  96. $paliTexts = PaliText::where('book', $book)
  97. ->whereBetween('paragraph', [$start, $end])
  98. ->whereBetween('level', [$minLevel, $maxLevel])->orderBy('paragraph')->get();
  99. $chapters = ProgressChapter::where('book', $book)
  100. ->whereBetween('para', [$start, $end])
  101. ->where('channel_id', $channelId)->orderBy('para')->get();
  102. $toc = $paliTexts->map(function ($paliText) use ($chapters, $channelId) {
  103. $title = $paliText->toc;
  104. if (count($chapters) > 0) {
  105. $found = array_filter($chapters->toArray(), function ($chapter) use ($paliText) {
  106. return $chapter['book'] == $paliText->book && $chapter['para'] == $paliText->paragraph;
  107. });
  108. if (count($found) > 0) {
  109. $chapter = array_shift($found);
  110. if (!empty($chapter['title'])) {
  111. $title = $chapter['title'];
  112. }
  113. if (!empty($chapter['summary'])) {
  114. $summary = $chapter['summary'];
  115. }
  116. $progress = (int)($chapter['progress'] * 100);
  117. $id = $chapter['uid'];
  118. }
  119. }
  120. return [
  121. "id" => "{$paliText->book}-{$paliText->paragraph}",
  122. "channel" => $channelId,
  123. "title" => $title,
  124. "summary" => $summary ?? "",
  125. "progress" => $progress ?? 0,
  126. "level" => (int)$paliText->level,
  127. "disabled" => !isset($progress),
  128. ];
  129. })->toArray();
  130. return $toc;
  131. }
  132. public function getBookCategory($book, $paragraph)
  133. {
  134. $tags = PaliText::with('tagMaps.tags')
  135. ->where('book', $book)
  136. ->where('paragraph', $paragraph)
  137. ->first()->tagMaps->map(function ($tagMap) {
  138. return $tagMap->tags;
  139. })->toArray();
  140. return $tags;
  141. }
  142. private function bookStart($book, $paragraph)
  143. {
  144. $currBook = PaliText::where('book', $book)
  145. ->where('paragraph', '<=', $paragraph)
  146. ->where('level', 1)
  147. ->orderBy('paragraph', 'desc')
  148. ->first();
  149. return $currBook;
  150. }
  151. public function getBookContent($id)
  152. {
  153. //查询book信息
  154. $book = $this->loadBook($id);
  155. $currBook = $this->bookStart($book->book, $book->para);
  156. $start = $currBook->paragraph;
  157. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  158. // 查询起始段落
  159. $paragraphs = PaliText::where('book', $book->book)
  160. ->whereBetween('paragraph', [$start, $end])
  161. ->where('level', '<', 8)
  162. ->orderBy('paragraph')
  163. ->get();
  164. $curr = $paragraphs->firstWhere('paragraph', $book->para);
  165. $endParagraph = $curr->paragraph + $curr->chapter_len - 1;
  166. if ($curr->chapter_strlen > $this->maxChapterLen) {
  167. //太大了,修改结束位置 找到下一级
  168. foreach ($paragraphs as $key => $paragraph) {
  169. if ($paragraph->paragraph > $curr->paragraph) {
  170. if ($paragraph->chapter_strlen <= $this->maxChapterLen) {
  171. $endParagraph = $paragraph->paragraph + $paragraph->chapter_len - 1;
  172. break;
  173. }
  174. if ($paragraph->level <= $curr->level) {
  175. //不能往下走了,就是它了
  176. $endParagraph = $paragraphs[$key - 1]->paragraph + $paragraphs[$key - 1]->chapter_len - 1;
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. //获取句子数据
  183. $sentences = Sentence::where('book_id', $book->book)
  184. ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
  185. ->where('channel_uid', $book->channel_id)
  186. ->orderBy('paragraph')
  187. ->orderBy('word_start')
  188. ->get();
  189. $pali = PaliText::where('book', $book->book)
  190. ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
  191. ->orderBy('paragraph')
  192. ->get();
  193. $result = [];
  194. for ($i = $curr->paragraph; $i <= $endParagraph; $i++) {
  195. $texts = array_filter($sentences->toArray(), function ($sentence) use ($i) {
  196. return $sentence['paragraph'] == $i;
  197. });
  198. $contents = array_map(function ($text) {
  199. return $text['content'];
  200. }, $texts);
  201. $currPali = $pali->firstWhere('paragraph', $i);
  202. $paragraph = [
  203. 'id' => $i,
  204. 'level' => $currPali->level,
  205. 'text' => [[implode('', $contents)]],
  206. ];
  207. $result[] = $paragraph;
  208. }
  209. return $result;
  210. }
  211. public function pagination($book)
  212. {
  213. $currBook = $this->bookStart($book->book, $book->para);
  214. $start = $currBook->paragraph;
  215. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  216. // 查询起始段落
  217. $paragraphs = PaliText::where('book', $book->book)
  218. ->whereBetween('paragraph', [$start, $end])
  219. ->where('level', '<', 8)
  220. ->orderBy('paragraph')
  221. ->get();
  222. $curr = $paragraphs->firstWhere('paragraph', $book->para);
  223. $current = $curr; //实际显示的段落
  224. $endParagraph = $curr->paragraph + $curr->chapter_len - 1;
  225. if ($curr->chapter_strlen > $this->maxChapterLen) {
  226. //太大了,修改结束位置 找到下一级
  227. foreach ($paragraphs as $key => $paragraph) {
  228. if ($paragraph->paragraph > $curr->paragraph) {
  229. if ($paragraph->chapter_strlen <= $this->maxChapterLen) {
  230. $endParagraph = $paragraph->paragraph + $paragraph->chapter_len - 1;
  231. $current = $paragraph;
  232. break;
  233. }
  234. if ($paragraph->level <= $curr->level) {
  235. //不能往下走了,就是它了
  236. $endParagraph = $paragraphs[$key - 1]->paragraph + $paragraphs[$key - 1]->chapter_len - 1;
  237. $current = $paragraph;
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. $start = $curr->paragraph;
  244. $end = $endParagraph;
  245. $nextPali = $this->next($current->book, $current->paragraph, $current->level);
  246. $prevPali = $this->prev($current->book, $current->paragraph, $current->level);
  247. $next = null;
  248. if ($nextPali) {
  249. $nextTranslation = ProgressChapter::with('channel.owner')
  250. ->where('book', $nextPali->book)
  251. ->where('para', $nextPali->paragraph)
  252. ->where('channel_id', $book->channel_id)
  253. ->first();
  254. if ($nextTranslation) {
  255. if (!empty($nextTranslation->title)) {
  256. $next['title'] = $nextTranslation->title;
  257. } else {
  258. $next['title'] = $nextPali->toc;
  259. }
  260. $next['id'] = $nextTranslation->uid;
  261. }
  262. }
  263. $prev = null;
  264. if ($prevPali) {
  265. $prevTranslation = ProgressChapter::with('channel.owner')
  266. ->where('book', $prevPali->book)
  267. ->where('para', $prevPali->paragraph)
  268. ->where('channel_id', $book->channel_id)
  269. ->first();
  270. if ($prevTranslation) {
  271. if (!empty($prevTranslation->title)) {
  272. $prev['title'] = $prevTranslation->title;
  273. } else {
  274. $prev['title'] = $prevPali->toc;
  275. }
  276. $prev['id'] = $prevTranslation->uid;
  277. }
  278. }
  279. return compact('start', 'end', 'next', 'prev');
  280. }
  281. public function next($book, $paragraph, $level)
  282. {
  283. $next = PaliText::where('book', $book)
  284. ->where('paragraph', '>', $paragraph)
  285. ->where('level', $level)
  286. ->orderBy('paragraph')
  287. ->first();
  288. return $next ?? null;
  289. }
  290. public function prev($book, $paragraph, $level)
  291. {
  292. $prev = PaliText::where('book', $book)
  293. ->where('paragraph', '<', $paragraph)
  294. ->where('level', $level)
  295. ->orderBy('paragraph', 'desc')
  296. ->first();
  297. return $prev ?? null;
  298. }
  299. public function show2($id)
  300. {
  301. // Sample book data (replace with database query)
  302. $book = [
  303. 'title' => 'Sample Book Title',
  304. 'author' => 'John Doe',
  305. 'category' => 'Fiction',
  306. 'tags' => ['Adventure', 'Mystery', 'Bestseller'],
  307. 'toc' => ['Introduction', 'Chapter 1', 'Chapter 2', 'Conclusion'],
  308. 'content' => [
  309. 'This is the introduction to the book...',
  310. 'Chapter 1 content goes here...',
  311. 'Chapter 2 content goes here...',
  312. 'Conclusion of the book...',
  313. ],
  314. 'downloads' => [
  315. ['format' => 'PDF', 'url' => '#'],
  316. ['format' => 'EPUB', 'url' => '#'],
  317. ['format' => 'MOBI', 'url' => '#'],
  318. ],
  319. ];
  320. // Sample related books (replace with database query)
  321. $relatedBooks = [
  322. [
  323. 'title' => 'Related Book 1',
  324. 'description' => 'A thrilling adventure...',
  325. 'image' => 'https://via.placeholder.com/300x200',
  326. 'link' => '#',
  327. ],
  328. [
  329. 'title' => 'Related Book 2',
  330. 'description' => 'A mystery novel...',
  331. 'image' => 'https://via.placeholder.com/300x200',
  332. 'link' => '#',
  333. ],
  334. [
  335. 'title' => 'Related Book 3',
  336. 'description' => 'A bestseller...',
  337. 'image' => 'https://via.placeholder.com/300x200',
  338. 'link' => '#',
  339. ],
  340. ];
  341. return view('library.book.read2', compact('book', 'relatedBooks'));
  342. }
  343. }