NissayaCardController.php 8.1 KB

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