TemplateRender.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\DhammaTerm;
  4. use App\Models\PaliText;
  5. use App\Models\Channel;
  6. use App\Http\Controllers\CorpusController;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Http\Api\ChannelApi;
  10. class TemplateRender{
  11. protected $param = [];
  12. protected $mode = "read";
  13. protected $channel_id = [];
  14. /**
  15. * Create a new command instance.
  16. * int $mode 'read' | 'edit'
  17. * @return void
  18. */
  19. public function __construct($param, $channelInfo, $mode)
  20. {
  21. $this->param = $param;
  22. foreach ($channelInfo as $value) {
  23. $this->channel_id[] = $value->uid;
  24. }
  25. $this->channelInfo = $channelInfo;
  26. $this->mode = $mode;
  27. }
  28. public function render($tpl_name){
  29. switch ($tpl_name) {
  30. case 'term':
  31. # 术语
  32. $result = $this->render_term();
  33. break;
  34. case 'note':
  35. $result = $this->render_note();
  36. break;
  37. case 'sent':
  38. $result = $this->render_sent();
  39. break;
  40. case 'quote':
  41. $result = $this->render_quote();
  42. break;
  43. case 'exercise':
  44. $result = $this->render_exercise();
  45. break;
  46. case 'article':
  47. $result = $this->render_article();
  48. break;
  49. case 'nissaya':
  50. $result = $this->render_nissaya();
  51. break;
  52. case 'mermaid':
  53. $result = $this->render_mermaid();
  54. break;
  55. default:
  56. # code...
  57. $result = [
  58. 'props'=>base64_encode(\json_encode([])),
  59. 'html'=>'',
  60. 'tag'=>'span',
  61. 'tpl'=>'unknown',
  62. ];
  63. break;
  64. }
  65. return $result;
  66. }
  67. private function render_term(){
  68. $word = $this->get_param($this->param,"word",1);
  69. $channelId = $this->channel_id[0];
  70. $channelInfo = $this->channelInfo[0];
  71. $key = "/term/{$channelId}/{$word}";
  72. Log::info("term cache key:{$key}");
  73. $props = Cache::remember($key,env('CACHE_EXPIRE',1),
  74. function() use($word,$channelId,$channelInfo){
  75. Log::info("term render:{$word}-{$channelId}");
  76. $lang = Channel::where('uid',$channelId)->value('lang');
  77. if(!empty($lang)){
  78. $langFamily = explode('-',$lang)[0];
  79. }
  80. //先查属于这个channel 的
  81. $tplParam = DhammaTerm::where("word",$word)->where('channal',$channelId)->first();
  82. if(!$tplParam){
  83. /**
  84. * 没有,再查这个studio的
  85. * 按照语言过滤
  86. * 完全匹配的优先
  87. * 语族匹配也行
  88. */
  89. $termsInStudio = DhammaTerm::where("word",$word)
  90. ->where('owner',$channelInfo->owner_uid)
  91. ->get();
  92. if(count($termsInStudio)>0){
  93. $list = array();
  94. foreach ($termsInStudio as $key=>$term) {
  95. if(empty($term->channal)){
  96. if($term->language===$lang){
  97. $list[$term->guid]=2;
  98. }else if(strpos($term->language,$langFamily)!==false){
  99. $list[$term->guid]=1;
  100. }
  101. }
  102. }
  103. if(count($list)>0){
  104. arsort($list);
  105. foreach ($list as $key => $one) {
  106. foreach ($termsInStudio as $term) {
  107. if($term->guid===$key){
  108. $tplParam = $term;
  109. break;
  110. }
  111. }
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. if(!$tplParam){
  118. //没有,再查社区
  119. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  120. $tplParam = DhammaTerm::where("word",$word)
  121. ->where('channal',$community_channel)
  122. ->first();
  123. if($tplParam){
  124. $isCommunity = true;
  125. }
  126. }
  127. $output = [
  128. "word" => $word,
  129. "parentChannelId" => $channelId,
  130. "parentStudioId" => $channelInfo->owner_uid,
  131. ];
  132. $innerString = $output["word"];
  133. if($tplParam){
  134. $output["id"] = $tplParam->guid;
  135. $output["meaning"] = $tplParam->meaning;
  136. $output["channel"] = $tplParam->channal;
  137. if(isset($isCommunity)){
  138. $output["isCommunity"] = true;
  139. }
  140. $innerString = "{$output["meaning"]}({$output["word"]})";
  141. if(!empty($tplParam->other_meaning)){
  142. $output["meaning2"] = $tplParam->other_meaning;
  143. }
  144. /*
  145. if($tplParam->note){
  146. $output["summary"] = $tplParam->note;
  147. }else{
  148. //本人没有解释内容的。用社区数据。
  149. //TODO 由作者(读者)设置是否使用社区数据
  150. //获取channel 语言
  151. //使用社区note
  152. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  153. //查找社区解释
  154. $community_note = DhammaTerm::where("word",$word)
  155. ->where('channal',$community_channel)
  156. ->value('note');
  157. $output["summary"] = $tplParam->note;
  158. $output["community"] = true;
  159. }
  160. */
  161. }
  162. $output['innerHtml'] = $innerString;
  163. return $output;
  164. });
  165. return [
  166. 'props'=>base64_encode(\json_encode($props)),
  167. 'html'=>$props['innerHtml'],
  168. 'tag'=>'span',
  169. 'tpl'=>'term',
  170. ];
  171. }
  172. private function render_note(){
  173. $props = ["note" => $this->get_param($this->param,"text",1)];
  174. $trigger = $this->get_param($this->param,"trigger",2);
  175. $innerString = "";
  176. if(!empty($trigger)){
  177. $props["trigger"] = $trigger;
  178. $innerString = $props["trigger"];
  179. }
  180. return [
  181. 'props'=>base64_encode(\json_encode($props)),
  182. 'html'=>$innerString,
  183. 'tag'=>'span',
  184. 'tpl'=>'note',
  185. ];
  186. }
  187. private function render_nissaya(){
  188. $pali = $this->get_param($this->param,"pali",1);
  189. $meaning = $this->get_param($this->param,"meaning",2);
  190. $innerString = "";
  191. $props = [
  192. "pali" => $pali,
  193. "meaning" => $meaning,
  194. ];
  195. return [
  196. 'props'=>base64_encode(\json_encode($props)),
  197. 'html'=>$innerString,
  198. 'tag'=>'span',
  199. 'tpl'=>'nissaya',
  200. ];
  201. }
  202. private function render_exercise(){
  203. $id = $this->get_param($this->param,"id",1);
  204. $title = $this->get_param($this->param,"title",1);
  205. $props = [
  206. "id" => $id,
  207. "title" => $title,
  208. "channel" => $this->channel_id[0],
  209. ];
  210. return [
  211. 'props'=>base64_encode(\json_encode($props)),
  212. 'html'=>"",
  213. 'tag'=>'span',
  214. 'tpl'=>'exercise',
  215. ];
  216. }
  217. private function render_article(){
  218. $type = $this->get_param($this->param,"type",1);
  219. $id = $this->get_param($this->param,"id",2);
  220. $title = $this->get_param($this->param,"title",3);
  221. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  222. $style = $this->get_param($this->param,"style",5);
  223. $props = [
  224. "type" => $type,
  225. "id" => $id,
  226. "channel" => $channel,
  227. 'style' => $style,
  228. ];
  229. if(!empty($title)){
  230. $props['title'] = $title;
  231. }
  232. return [
  233. 'props'=>base64_encode(\json_encode($props)),
  234. 'html'=>"",
  235. 'tag'=>'span',
  236. 'tpl'=>'article',
  237. ];
  238. }
  239. private function render_quote(){
  240. $paraId = $this->get_param($this->param,"para",1);
  241. $channelId = $this->channel_id[0];
  242. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  243. env('CACHE_EXPIRE',3600*24),
  244. function() use($paraId,$channelId){
  245. $para = \explode('-',$paraId);
  246. $output = [
  247. "paraId" => $paraId,
  248. "channel" => $channelId,
  249. "innerString" => $paraId,
  250. ];
  251. if(count($para)<2){
  252. return $output;
  253. }
  254. $PaliText = PaliText::where("book",$para[0])
  255. ->where("paragraph",$para[1])
  256. ->select(['toc','path'])
  257. ->first();
  258. if($PaliText){
  259. $output["pali"] = $PaliText->toc;
  260. $output["paliPath"] = \json_decode($PaliText->path);
  261. $output["innerString"]= $PaliText->toc;
  262. }
  263. return $output;
  264. });
  265. return [
  266. 'props'=>base64_encode(\json_encode($props)),
  267. 'html'=>$props["innerString"],
  268. 'tag'=>'span',
  269. 'tpl'=>'quote',
  270. ];
  271. }
  272. private function render_sent(){
  273. $sid = $this->get_param($this->param,"sid",1);
  274. $channel = $this->get_param($this->param,"channel",2);
  275. if(!empty($channel)){
  276. $channels = explode(',',$channel);
  277. }else{
  278. $channels = $this->channel_id;
  279. }
  280. $sentInfo = explode('@',trim($sid));
  281. $sentId = $sentInfo[0];
  282. if(isset($sentInfo[1])){
  283. $channels = [$sentInfo[1]];
  284. }
  285. $Sent = new CorpusController();
  286. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  287. if($props === false){
  288. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  289. }
  290. if($this->mode==='read'){
  291. $tpl = "sentread";
  292. }else{
  293. $tpl = "sentedit";
  294. }
  295. return [
  296. 'props'=>base64_encode(\json_encode($props)),
  297. 'html'=>"",
  298. 'tag'=>'span',
  299. 'tpl'=>$tpl,
  300. ];
  301. }
  302. private function render_mermaid(){
  303. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  304. $props = ["text" => implode("\n",$text)];
  305. return [
  306. 'props'=>base64_encode(\json_encode($props)),
  307. 'html'=>"mermaid",
  308. 'tag'=>'div',
  309. 'tpl'=>'mermaid',
  310. ];
  311. }
  312. private function get_param(array $param,string $name,int $id,string $default=''){
  313. if(isset($param[$name])){
  314. return trim($param[$name]);
  315. }else if(isset($param["{$id}"])){
  316. return trim($param["{$id}"]);
  317. }else{
  318. return $default;
  319. }
  320. }
  321. }