NissayaCardController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $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 = array();
  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[] = ['label'=>__("grammar.".$case),
  98. 'link'=>config('mint.server.dashboard_base_path').'/term/list/'.$case
  99. ];
  100. }
  101. # 格位
  102. $newLine['from']['case'] = $localCase;
  103. }
  104. if(isset($from->spell)){
  105. $newLine['from']['spell'] = $from->spell;
  106. }
  107. //连接到
  108. $linkTos = json_decode($relation->to);
  109. if(isset($linkTos->case) && is_array($linkTos->case) && count($linkTos->case)>0){
  110. $localTo =[];
  111. foreach ($linkTos->case as $to) {
  112. $localTo[] = ['label'=>__("grammar.".$to),
  113. 'link'=>config('mint.server.dashboard_base_path').'/term/list/'.$to
  114. ];
  115. }
  116. # 格位
  117. $newLine['to']['case'] = $localTo;
  118. }
  119. if(isset($linkTos->spell)){
  120. $newLine['to']['spell'] = $linkTos->spell;
  121. }
  122. //含义 用分类字段的term 数据
  123. if(isset($relation['category']) && !empty($relation['category'])){
  124. $newLine['category']['name'] = $relation['category'];
  125. $localCategory = DhammaTerm::where('channal',$localTerm)
  126. ->where('word',$relation['category'])
  127. ->first();
  128. if($localCategory){
  129. $newLine['category']['note'] = $localCategory->note;
  130. $newLine['category']['meaning'] =$localCategory->meaning;
  131. }else{
  132. $newLine['category']['note'] = $relation['category'];
  133. $newLine['category']['meaning'] = $relation['category'];
  134. }
  135. }
  136. /**
  137. * 翻译建议
  138. * relation 和 from 都匹配成功
  139. * from 为空 只匹配 relation
  140. */
  141. $arrLocalEnding = array();
  142. $localEndings = NissayaEnding::where('relation',$relation['name'])
  143. ->where('lang',$request->get('lang'))
  144. ->get();
  145. foreach ($localEndings as $localEnding) {
  146. if(empty($localEnding->from) || $localEnding->from===$relation->from){
  147. $arrLocalEnding[]=$localEnding->ending;
  148. }
  149. }
  150. $newLine['local_ending'] = implode(';',$arrLocalEnding);
  151. //本地语言 关系名称
  152. if($relationInTerm){
  153. $newLine['local_relation'] = $relationInTerm->meaning;
  154. }
  155. //关系名称
  156. $newLine['relation'] = $relation['name'];
  157. $newLine['relation_link'] = config('mint.server.dashboard_base_path').'/term/list/'.$relation['name'];
  158. $cardData['row'][] = $newLine;
  159. }
  160. }
  161. if($request->get('content_type','markdown') === 'markdown'){
  162. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES));
  163. $tpl = file_get_contents(resource_path("mustache/nissaya_ending_card.tpl"));
  164. $result = $m->render($tpl,$cardData);
  165. }else{
  166. $result = $cardData;
  167. }
  168. return $this->ok($result);
  169. }
  170. /**
  171. * Update the specified resource in storage.
  172. *
  173. * @param \Illuminate\Http\Request $request
  174. * @param \App\Models\NissayaEnding $nissayaEnding
  175. * @return \Illuminate\Http\Response
  176. */
  177. public function update(Request $request, NissayaEnding $nissayaEnding)
  178. {
  179. //
  180. }
  181. /**
  182. * Remove the specified resource from storage.
  183. *
  184. * @param \App\Models\NissayaEnding $nissayaEnding
  185. * @return \Illuminate\Http\Response
  186. */
  187. public function destroy(NissayaEnding $nissayaEnding)
  188. {
  189. //
  190. }
  191. }