2
0

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