NissayaCardController.php 7.2 KB

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