TemplateRender.php 10.0 KB

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