NissayaCardController.php 7.8 KB

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