NissayaCardController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\App;
  6. use App\Http\Api\ChannelApi;
  7. use App\Http\Api\MdRender;
  8. use App\Models\DhammaTerm;
  9. use App\Models\Relation;
  10. use App\Models\NissayaEnding;
  11. class NissayaCardController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index()
  19. {
  20. //
  21. }
  22. /**
  23. * Store a newly created resource in storage.
  24. *
  25. * @param \Illuminate\Http\Request $request
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function store(Request $request)
  29. {
  30. //
  31. }
  32. /**
  33. * Display the specified resource.
  34. *
  35. * @param string $nissayaEnding
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function show(Request $request, string $nissayaEnding)
  39. {
  40. //
  41. $cardData = [];
  42. App::setLocale($request->input('lang'));
  43. $localTerm = ChannelApi::getSysChannel(
  44. "_System_Grammar_Term_" . strtolower($request->input('lang')) . "_",
  45. "_System_Grammar_Term_en_"
  46. );
  47. Log::debug('$localTerm=' . $localTerm);
  48. if (!$localTerm) {
  49. return $this->error('no term channel');
  50. }
  51. $cardData['ending']['word'] = $nissayaEnding;
  52. $endingTerm = DhammaTerm::where('channal', $localTerm)
  53. ->where('word', $nissayaEnding)
  54. ->first();
  55. if ($endingTerm) {
  56. $cardData['ending']['id'] = $endingTerm->guid;
  57. $cardData['ending']['tag'] = $endingTerm->tag;
  58. $cardData['ending']['meaning'] = $endingTerm->meaning;
  59. $cardData['ending']['note'] = $endingTerm->note;
  60. if (!empty($endingTerm->note)) {
  61. $mdRender = new MdRender(
  62. [
  63. 'mode' => 'read',
  64. 'format' => 'react',
  65. 'lang' => $endingTerm->lang,
  66. ]
  67. );
  68. $cardData['ending']['html'] = $mdRender->convert($endingTerm->note, [], null);
  69. }
  70. }
  71. $myEnding = NissayaEnding::where('ending', $nissayaEnding)
  72. ->select('relation')->get();
  73. if (count($myEnding) === 0) {
  74. if (!isset($cardData['ending']['note'])) {
  75. $cardData['ending']['note'] = "no record\n";
  76. }
  77. }
  78. $relations = Relation::whereIn('name', $myEnding)->get();
  79. if (count($relations) > 0) {
  80. $cardData['title_case'] = "本词";
  81. $cardData['title_relation'] = "关系";
  82. $cardData['title_local_relation'] = "关系";
  83. $cardData['title_local_link_to'] = "目标词特征";
  84. $cardData['title_content'] = "含义";
  85. $cardData['title_local_ending'] = "翻译建议";
  86. foreach ($relations as $key => $relation) {
  87. $newLine = array();
  88. $relationInTerm = DhammaTerm::where('channal', $localTerm)
  89. ->where('word', $relation['name'])
  90. ->first();
  91. if (empty($relation->from)) {
  92. $cardData['row'][] = ["relation" => $relation->name];
  93. continue;
  94. }
  95. $from = json_decode($relation->from);
  96. if (isset($from->case)) {
  97. $cases = $from->case;
  98. $localCase = [];
  99. foreach ($cases as $case) {
  100. $localCase[] = [
  101. 'label' => DhammaTerm::where('channal', $localTerm)
  102. ->where('word', $case)
  103. ->value('meaning'),
  104. 'case' => $case,
  105. 'link' => config('mint.server.dashboard_base_path') . '/term/list/' . $case
  106. ];
  107. }
  108. # 格位
  109. $newLine['from']['case'] = $localCase;
  110. }
  111. if (isset($from->spell)) {
  112. $newLine['from']['spell'] = $from->spell;
  113. }
  114. //连接到
  115. $linkTos = json_decode($relation->to);
  116. if (isset($linkTos->case) && is_array($linkTos->case) && count($linkTos->case) > 0) {
  117. $localTo = [];
  118. foreach ($linkTos->case as $to) {
  119. $localTo[] = [
  120. 'label' => DhammaTerm::where('channal', $localTerm)->where('word', $to)->value('meaning'),
  121. 'case' => $to,
  122. 'link' => config('mint.server.dashboard_base_path') . '/term/list/' . $to
  123. ];
  124. }
  125. # 格位
  126. $newLine['to']['case'] = $localTo;
  127. }
  128. if (isset($linkTos->spell)) {
  129. $newLine['to']['spell'] = $linkTos->spell;
  130. }
  131. //含义 用分类字段的term 数据
  132. if (isset($relation['category']) && !empty($relation['category'])) {
  133. $newLine['category']['name'] = $relation['category'];
  134. $localCategory = DhammaTerm::where('channal', $localTerm)
  135. ->where('word', $relation['category'])
  136. ->first();
  137. if ($localCategory) {
  138. $mdRender = new MdRender(
  139. [
  140. 'mode' => 'read',
  141. 'format' => 'text',
  142. 'lang' => $endingTerm->lang,
  143. ]
  144. );
  145. $newLine['category']['note'] = $mdRender->convert($localCategory->note, [], null);
  146. $newLine['category']['meaning'] = $localCategory->meaning;
  147. } else {
  148. $newLine['category']['note'] = $relation['category'];
  149. $newLine['category']['meaning'] = $relation['category'];
  150. }
  151. }
  152. /**
  153. * 翻译建议
  154. * relation 和 from 都匹配成功
  155. * from 为空 只匹配 relation
  156. */
  157. $arrLocalEnding = array();
  158. $localEndings = NissayaEnding::where('relation', $relation['name'])
  159. ->where('lang', $request->input('lang'))
  160. ->get();
  161. foreach ($localEndings as $localEnding) {
  162. if (empty($localEnding->from) || $localEnding->from === $relation->from) {
  163. $arrLocalEnding[] = $localEnding->ending;
  164. }
  165. }
  166. $newLine['local_ending'] = implode(';', $arrLocalEnding);
  167. //本地语言 关系名称
  168. if ($relationInTerm) {
  169. $newLine['local_relation'] = $relationInTerm->meaning;
  170. }
  171. //关系名称
  172. $newLine['relation'] = $relation['name'];
  173. $newLine['relation_link'] = config('mint.server.dashboard_base_path') . '/term/list/' . $relation['name'];
  174. $cardData['row'][] = $newLine;
  175. }
  176. }
  177. if ($request->input('content_type', 'markdown') === 'markdown') {
  178. $m = new \Mustache_Engine(array('entity_flags' => ENT_QUOTES));
  179. $tpl = file_get_contents(resource_path("mustache/nissaya_ending_card.tpl"));
  180. $result = $m->render($tpl, $cardData);
  181. } else {
  182. $result = $cardData;
  183. }
  184. return $this->ok($result);
  185. }
  186. /**
  187. * Update the specified resource in storage.
  188. *
  189. * @param \Illuminate\Http\Request $request
  190. * @param \App\Models\NissayaEnding $nissayaEnding
  191. * @return \Illuminate\Http\Response
  192. */
  193. public function update(Request $request, NissayaEnding $nissayaEnding)
  194. {
  195. //
  196. }
  197. /**
  198. * Remove the specified resource from storage.
  199. *
  200. * @param \App\Models\NissayaEnding $nissayaEnding
  201. * @return \Illuminate\Http\Response
  202. */
  203. public function destroy(NissayaEnding $nissayaEnding)
  204. {
  205. //
  206. }
  207. }