PaliTextController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. case 'chapter_children':
  116. $table = PaliText::where('book',$request->get('book'))
  117. ->where('parent',$request->get('para'))
  118. ->where('level','<',8);
  119. $all_count = $table->count();
  120. $chapters = $table->orderBy('paragraph')->get();
  121. break;
  122. case 'paragraph':
  123. $result = PaliText::where('book',$request->get('book'))->where('paragraph',$request->get('para'))->first();
  124. if($result){
  125. return $this->ok($result);
  126. }else{
  127. return $this->error("no data");
  128. }
  129. break;
  130. }
  131. if($chapters){
  132. return $this->ok(["rows"=>$chapters,"count"=>$all_count]);
  133. }else{
  134. return $this->error("no data");
  135. }
  136. }
  137. /**
  138. * Store a newly created resource in storage.
  139. *
  140. * @param \Illuminate\Http\Request $request
  141. * @return \Illuminate\Http\Response
  142. */
  143. public function store(Request $request)
  144. {
  145. //
  146. }
  147. /**
  148. * Display the specified resource.
  149. *
  150. * @param \Illuminate\Http\Request $request
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function show(Request $request)
  154. {
  155. //
  156. }
  157. /**
  158. * Update the specified resource in storage.
  159. *
  160. * @param \Illuminate\Http\Request $request
  161. * @param \App\Models\PaliText $paliText
  162. * @return \Illuminate\Http\Response
  163. */
  164. public function update(Request $request, PaliText $paliText)
  165. {
  166. //
  167. }
  168. /**
  169. * Remove the specified resource from storage.
  170. *
  171. * @param \App\Models\PaliText $paliText
  172. * @return \Illuminate\Http\Response
  173. */
  174. public function destroy(PaliText $paliText)
  175. {
  176. //
  177. }
  178. }