NissayaCardController.php 7.3 KB

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