2
0

ExportWbwController.php 4.5 KB

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