ExportWbwController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Wbw;
  4. use App\Models\WbwBlock;
  5. use Illuminate\Http\Request;
  6. class ExportWbwController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index(Request $request)
  14. {
  15. //
  16. $sent = explode("\n",$request->get("sent"));
  17. $output = [];
  18. foreach ($sent as $key => $value) {
  19. # code...
  20. $sent = [];
  21. $value = trim($value);
  22. $sentId = explode("-",$value);
  23. //先查wbw block 拿到block id
  24. $block = WbwBlock::where('book_id',$sentId[0])
  25. ->where('paragraph',$sentId[1])
  26. ->select('uid')
  27. ->where('channel_uid',$request->get("channel"))->first();
  28. if(!$block){
  29. continue;
  30. }
  31. $wbwdata = Wbw::where('book_id',$sentId[0])
  32. ->where('paragraph',$sentId[1])
  33. ->where('wid','>=',$sentId[2])
  34. ->where('wid','<=',$sentId[3])
  35. ->where('block_uid',$block->uid)
  36. ->get();
  37. $sent['sid']=$value;
  38. $sent['data']=[];
  39. foreach ($wbwdata as $wbw) {
  40. # code...
  41. $data = str_replace("&nbsp;",' ',$wbw->data);
  42. $data = str_replace("<br>",' ',$data);
  43. $xmlString = "<root>" . $data . "</root>";
  44. try{
  45. $xmlWord = simplexml_load_string($xmlString);
  46. }catch(Exception $e){
  47. continue;
  48. }
  49. $wordsList = $xmlWord->xpath('//word');
  50. foreach ($wordsList as $word) {
  51. $pali = $word->real->__toString();
  52. $case = explode("#",$word->case->__toString()) ;
  53. if(isset($case[0])){
  54. $type = $case[0];
  55. }else{
  56. $type = "";
  57. }
  58. if(isset($case[1])){
  59. $grammar = $case[1];
  60. $grammar = str_replace("null","",$grammar);
  61. }else{
  62. $grammar = "";
  63. }
  64. $style = $word->style->__toString();
  65. $factormeaning = str_replace("
","",$word->om->__toString());
  66. $factormeaning = str_replace("↓↓","",$factormeaning);
  67. if($type !== '.ctl.' && $style !== 'note' && !empty($pali)){
  68. $sent['data'][]=[
  69. 'pali'=>$word->real->__toString(),
  70. 'mean' => str_replace("
","",$word->mean->__toString()),
  71. 'type' => ltrim($type,'.'),
  72. 'grammar' => ltrim(str_replace('$.',',',$grammar),'.') ,
  73. 'parent' => $word->parent->__toString(),
  74. 'factors' => $word->org->__toString(),
  75. 'factormeaning' => $factormeaning
  76. ];
  77. }
  78. }
  79. }
  80. $output[]=$sent;
  81. }
  82. return view('export_wbw',['sentences' => $output] );
  83. }
  84. /**
  85. * Store a newly created resource in storage.
  86. *
  87. * @param \Illuminate\Http\Request $request
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function store(Request $request)
  91. {
  92. //
  93. }
  94. /**
  95. * Display the specified resource.
  96. *
  97. * @param \App\Models\Wbw $wbw
  98. * @return \Illuminate\Http\Response
  99. */
  100. public function show(Wbw $wbw)
  101. {
  102. //
  103. }
  104. /**
  105. * Update the specified resource in storage.
  106. *
  107. * @param \Illuminate\Http\Request $request
  108. * @param \App\Models\Wbw $wbw
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function update(Request $request, Wbw $wbw)
  112. {
  113. //
  114. }
  115. /**
  116. * Remove the specified resource from storage.
  117. *
  118. * @param \App\Models\Wbw $wbw
  119. * @return \Illuminate\Http\Response
  120. */
  121. public function destroy(Wbw $wbw)
  122. {
  123. //
  124. }
  125. }