PaliTextController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\DB;
  4. use App\Models\PaliText;
  5. use App\Models\Tag;
  6. use App\Models\TagMap;
  7. use Illuminate\Http\Request;
  8. class PaliTextController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. //
  18. switch ($request->get('view')) {
  19. case 'chapter-tag':
  20. $tm = (new TagMap)->getTable();
  21. $tg = (new Tag)->getTable();
  22. $pt = (new PaliText)->getTable();
  23. if($request->get('tags') && $request->get('tags')!==''){
  24. $tags = explode(',',$request->get('tags'));
  25. foreach ($tags as $tag) {
  26. # code...
  27. if(!empty($tag)){
  28. $tagNames[] = $tag;
  29. }
  30. }
  31. }
  32. if(isset($tagNames)){
  33. $where1 = " where co = ".count($tagNames);
  34. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  35. $in1 = "and t.name in ({$a})";
  36. $param = $tagNames;
  37. }else{
  38. $where1 = " ";
  39. $in1 = " ";
  40. }
  41. $query = "
  42. select tags.id,tags.name,co as count
  43. from (
  44. select tm.tag_id,count(*) as co from (
  45. select anchor_id as id from (
  46. select tm.anchor_id , count(*) as co
  47. from $tm as tm
  48. left join $tg as t on tm.tag_id = t.id
  49. left join $pt as pc on tm.anchor_id = pc.uid
  50. where tm.table_name = 'pali_texts'
  51. $in1
  52. group by tm.anchor_id
  53. ) T
  54. $where1
  55. ) CID
  56. left join $tm as tm on tm.anchor_id = CID.id
  57. group by tm.tag_id
  58. ) tid
  59. left join $tg on $tg.id = tid.tag_id
  60. order by count desc
  61. ";
  62. if(isset($param)){
  63. $chapters = DB::select($query,$param);
  64. }else{
  65. $chapters = DB::select($query);
  66. }
  67. $all_count = count($chapters);
  68. break;
  69. case 'chapter':
  70. $tm = (new TagMap)->getTable();
  71. $tg = (new Tag)->getTable();
  72. $pt = (new PaliText)->getTable();
  73. if($request->get('tags') && $request->get('tags')!==''){
  74. $tags = explode(',',$request->get('tags'));
  75. foreach ($tags as $tag) {
  76. # code...
  77. if(!empty($tag)){
  78. $tagNames[] = $tag;
  79. }
  80. }
  81. }
  82. if(isset($tagNames)){
  83. $where1 = " where co = ".count($tagNames);
  84. $a = implode(",",array_fill(0, count($tagNames), '?')) ;
  85. $in1 = "and t.name in ({$a})";
  86. $param = $tagNames;
  87. }else{
  88. $where1 = " ";
  89. $in1 = " ";
  90. }
  91. $query = "
  92. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  93. select anchor_id as cid from (
  94. select tm.anchor_id , count(*) as co
  95. from $tm as tm
  96. left join $tg as t on tm.tag_id = t.id
  97. where tm.table_name = 'pali_texts'
  98. $in1
  99. group by tm.anchor_id
  100. ) T
  101. $where1
  102. ) CID
  103. left join $pt as pt on CID.cid = pt.uid
  104. where level < 3
  105. order by book,paragraph";
  106. if(isset($param)){
  107. $chapters = DB::select($query,$param);
  108. }else{
  109. $chapters = DB::select($query);
  110. }
  111. $all_count = count($chapters);
  112. break;
  113. }
  114. if($chapters){
  115. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  116. }else{
  117. return $this->error("no data");
  118. }
  119. }
  120. /**
  121. * Store a newly created resource in storage.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @return \Illuminate\Http\Response
  125. */
  126. public function store(Request $request)
  127. {
  128. //
  129. }
  130. /**
  131. * Display the specified resource.
  132. *
  133. * @param \App\Models\PaliText $paliText
  134. * @return \Illuminate\Http\Response
  135. */
  136. public function show(PaliText $paliText)
  137. {
  138. //
  139. }
  140. /**
  141. * Update the specified resource in storage.
  142. *
  143. * @param \Illuminate\Http\Request $request
  144. * @param \App\Models\PaliText $paliText
  145. * @return \Illuminate\Http\Response
  146. */
  147. public function update(Request $request, PaliText $paliText)
  148. {
  149. //
  150. }
  151. /**
  152. * Remove the specified resource from storage.
  153. *
  154. * @param \App\Models\PaliText $paliText
  155. * @return \Illuminate\Http\Response
  156. */
  157. public function destroy(PaliText $paliText)
  158. {
  159. //
  160. }
  161. }