TemplateRender.php 12 KB

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