NissayaCardController.php 7.1 KB

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