BookController.php 13 KB

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