ArticleNavController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\ArticleNavResource;
  4. use App\Models\PaliText;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Response;
  7. class ArticleNavController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return Response
  13. */
  14. public function index(Request $request)
  15. {
  16. //
  17. switch ($request->input('type')) {
  18. case 'chapter':
  19. $para = explode('-', $request->input('id'));
  20. $prev = PaliText::where('book', $para[0])
  21. ->where('paragraph', '<', $para[1])
  22. ->where('level', '<', 8)
  23. ->orderBy('paragraph', 'desc')
  24. ->first();
  25. $next = PaliText::where('book', $para[0])
  26. ->where('paragraph', '>', $para[1])
  27. ->where('level', '<', 8)
  28. ->orderBy('paragraph', 'asc')
  29. ->first();
  30. if ($prev) {
  31. $nav['prev']['id'] = $prev->book.'-'.$prev->paragraph;
  32. $nav['prev']['title'] = $prev->toc;
  33. $nav['prev']['subtitle'] = $prev->toc;
  34. }
  35. if ($next) {
  36. $nav['next']['id'] = $next->book.'-'.$next->paragraph;
  37. $nav['next']['title'] = $next->toc;
  38. $nav['next']['subtitle'] = $next->toc;
  39. }
  40. break;
  41. case 'para':
  42. $para = explode('-', $request->input('id'));
  43. $prev = PaliText::where('book', $para[0])
  44. ->where('paragraph', '<', $para[1])
  45. ->orderBy('paragraph', 'desc')
  46. ->first();
  47. $next = PaliText::where('book', $para[0])
  48. ->where('paragraph', '>', $para[1])
  49. ->orderBy('paragraph', 'asc')
  50. ->first();
  51. if ($prev) {
  52. $nav['prev']['id'] = $prev->book.'-'.$prev->paragraph;
  53. $nav['prev']['title'] = $prev->text;
  54. $nav['prev']['subtitle'] = $prev->text;
  55. }
  56. if ($next) {
  57. $nav['next']['id'] = $next->book.'-'.$next->paragraph;
  58. $nav['next']['title'] = $next->text;
  59. $nav['next']['subtitle'] = $next->text;
  60. }
  61. break;
  62. default:
  63. return $this->error('type?');
  64. break;
  65. }
  66. if (isset($nav)) {
  67. return $this->ok(new ArticleNavResource($nav));
  68. } else {
  69. return $this->error('no nav data', 200, 200);
  70. }
  71. }
  72. /**
  73. * Store a newly created resource in storage.
  74. *
  75. * @return Response
  76. */
  77. public function store(Request $request)
  78. {
  79. //
  80. }
  81. /**
  82. * Display the specified resource.
  83. *
  84. * @param int $id
  85. * @return Response
  86. */
  87. public function show($id)
  88. {
  89. //
  90. }
  91. /**
  92. * Update the specified resource in storage.
  93. *
  94. * @param int $id
  95. * @return Response
  96. */
  97. public function update(Request $request, $id)
  98. {
  99. //
  100. }
  101. /**
  102. * Remove the specified resource from storage.
  103. *
  104. * @param int $id
  105. * @return Response
  106. */
  107. public function destroy($id)
  108. {
  109. //
  110. }
  111. }