ExportWbwController.php 4.2 KB

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