NissayaCardController.php 6.7 KB

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