TemplateRender.php 9.9 KB

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