PaliTextController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. $where2 = "where level < 3";
  88. }else{
  89. $where1 = " ";
  90. $in1 = " ";
  91. $where2 = "where level = 1";
  92. }
  93. $query = "
  94. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  95. select anchor_id as cid from (
  96. select tm.anchor_id , count(*) as co
  97. from $tm as tm
  98. left join $tg as t on tm.tag_id = t.id
  99. where tm.table_name = 'pali_texts'
  100. $in1
  101. group by tm.anchor_id
  102. ) T
  103. $where1
  104. ) CID
  105. left join $pt as pt on CID.cid = pt.uid
  106. $where2
  107. order by book,paragraph";
  108. if(isset($param)){
  109. $chapters = DB::select($query,$param);
  110. }else{
  111. $chapters = DB::select($query);
  112. }
  113. $all_count = count($chapters);
  114. break;
  115. }
  116. if($chapters){
  117. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  118. }else{
  119. return $this->error("no data");
  120. }
  121. }
  122. /**
  123. * Store a newly created resource in storage.
  124. *
  125. * @param \Illuminate\Http\Request $request
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function store(Request $request)
  129. {
  130. //
  131. }
  132. /**
  133. * Display the specified resource.
  134. *
  135. * @param \App\Models\PaliText $paliText
  136. * @return \Illuminate\Http\Response
  137. */
  138. public function show(PaliText $paliText)
  139. {
  140. //
  141. }
  142. /**
  143. * Update the specified resource in storage.
  144. *
  145. * @param \Illuminate\Http\Request $request
  146. * @param \App\Models\PaliText $paliText
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function update(Request $request, PaliText $paliText)
  150. {
  151. //
  152. }
  153. /**
  154. * Remove the specified resource from storage.
  155. *
  156. * @param \App\Models\PaliText $paliText
  157. * @return \Illuminate\Http\Response
  158. */
  159. public function destroy(PaliText $paliText)
  160. {
  161. //
  162. }
  163. }