NissayaCardController.php 8.0 KB

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