BookController.php 14 KB

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