TemplateRender.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. class TemplateRender{
  9. protected $param = [];
  10. protected $mode = "read";
  11. protected $channel_id = "";
  12. /**
  13. * Create a new command instance.
  14. * int $mode 'read' | 'edit'
  15. * @return void
  16. */
  17. public function __construct($param, $channel_id, $mode)
  18. {
  19. $this->param = $param;
  20. $this->channel_id = $channel_id;
  21. $this->mode = $mode;
  22. }
  23. public function render($tpl_name){
  24. switch ($tpl_name) {
  25. case 'term':
  26. # 术语
  27. $result = $this->render_term();
  28. break;
  29. case 'note':
  30. $result = $this->render_note();
  31. break;
  32. case 'sent':
  33. $result = $this->render_sent();
  34. break;
  35. case 'quote':
  36. $result = $this->render_quote();
  37. break;
  38. case 'exercise':
  39. $result = $this->render_exercise();
  40. break;
  41. case 'article':
  42. $result = $this->render_article();
  43. break;
  44. case 'nissaya':
  45. $result = $this->render_nissaya();
  46. break;
  47. default:
  48. # code...
  49. $result = [
  50. 'props'=>base64_encode(\json_encode([])),
  51. 'html'=>'',
  52. 'tag'=>'span',
  53. 'tpl'=>'unknown',
  54. ];
  55. break;
  56. }
  57. return $result;
  58. }
  59. private function render_term(){
  60. $word = $this->get_param($this->param,"word",1);
  61. $channelId = $this->channel_id;
  62. $props = Cache::remember("/term/{$this->channel_id}/{$word}",
  63. 60,
  64. function() use($word,$channelId){
  65. $tplParam = DhammaTerm::where("word",$word)->first();
  66. $output = [
  67. "word" => $word,
  68. "channel" => $channelId,
  69. ];
  70. $innerString = $output["word"];
  71. if($tplParam){
  72. $output["id"] = $tplParam->guid;
  73. $output["meaning"] = $tplParam->meaning;
  74. $innerString = "{$output["meaning"]}({$output["word"]})";
  75. if(!empty($tplParam->other_meaning)){
  76. $output["meaning2"] = $tplParam->other_meaning;
  77. }
  78. }
  79. $output['innerHtml'] = $innerString;
  80. return $output;
  81. });
  82. return [
  83. 'props'=>base64_encode(\json_encode($props)),
  84. 'html'=>$props['innerHtml'],
  85. 'tag'=>'span',
  86. 'tpl'=>'term',
  87. ];
  88. }
  89. private function render_note(){
  90. $props = ["note" => $this->get_param($this->param,"text",1)];
  91. $trigger = $this->get_param($this->param,"trigger",2);
  92. $innerString = "";
  93. if(!empty($trigger)){
  94. $props["trigger"] = $trigger;
  95. $innerString = $props["trigger"];
  96. }
  97. return [
  98. 'props'=>base64_encode(\json_encode($props)),
  99. 'html'=>$innerString,
  100. 'tag'=>'span',
  101. 'tpl'=>'note',
  102. ];
  103. }
  104. private function render_nissaya(){
  105. $pali = $this->get_param($this->param,"pali",1);
  106. $meaning = $this->get_param($this->param,"meaning",2);
  107. $innerString = "";
  108. $props = [
  109. "pali" => $pali,
  110. "meaning" => $meaning,
  111. ];
  112. return [
  113. 'props'=>base64_encode(\json_encode($props)),
  114. 'html'=>$innerString,
  115. 'tag'=>'span',
  116. 'tpl'=>'nissaya',
  117. ];
  118. }
  119. private function render_exercise(){
  120. $id = $this->get_param($this->param,"id",1);
  121. $title = $this->get_param($this->param,"title",1);
  122. $props = [
  123. "id" => $id,
  124. "title" => $title,
  125. "channel" => $this->channel_id,
  126. ];
  127. return [
  128. 'props'=>base64_encode(\json_encode($props)),
  129. 'html'=>"",
  130. 'tag'=>'span',
  131. 'tpl'=>'exercise',
  132. ];
  133. }
  134. private function render_article(){
  135. $type = $this->get_param($this->param,"type",1);
  136. $id = $this->get_param($this->param,"id",2);
  137. $title = $this->get_param($this->param,"title",3);
  138. $channel = $this->get_param($this->param,"channel",4);
  139. $props = [
  140. "type" => $type,
  141. "id" => $id,
  142. "channel" => $channel,
  143. ];
  144. if(empty($channel)){
  145. $props['channel'] = $this->channel_id;
  146. }
  147. if(!empty($title)){
  148. $props['title'] = $title;
  149. }
  150. return [
  151. 'props'=>base64_encode(\json_encode($props)),
  152. 'html'=>"",
  153. 'tag'=>'span',
  154. 'tpl'=>'article',
  155. ];
  156. }
  157. private function render_quote(){
  158. $paraId = $this->get_param($this->param,"para",1);
  159. $channelId = $this->channel_id;
  160. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  161. 60,
  162. function() use($paraId,$channelId){
  163. $para = \explode('-',$paraId);
  164. $output = [
  165. "paraId" => $paraId,
  166. "channel" => $channelId,
  167. "innerString" => $paraId,
  168. ];
  169. if(count($para)<2){
  170. return $output;
  171. }
  172. $PaliText = PaliText::where("book",$para[0])
  173. ->where("paragraph",$para[1])
  174. ->select(['toc','path'])
  175. ->first();
  176. if($PaliText){
  177. $output["pali"] = $PaliText->toc;
  178. $output["paliPath"] = \json_decode($PaliText->path);
  179. $output["innerString"]= $PaliText->toc;
  180. }
  181. return $output;
  182. });
  183. return [
  184. 'props'=>base64_encode(\json_encode($props)),
  185. 'html'=>$props["innerString"],
  186. 'tag'=>'span',
  187. 'tpl'=>'quote',
  188. ];
  189. }
  190. private function render_sent(){
  191. $sid = $this->get_param($this->param,"sid",1);
  192. $channel = $this->get_param($this->param,"channel",2);
  193. if(!empty($channel)){
  194. $mChannel = $channel;
  195. }else{
  196. $mChannel = $this->channel_id;
  197. }
  198. $sentInfo = explode('@',trim($sid));
  199. $sentId = $sentInfo[0];
  200. if(empty($mChannel)){
  201. $channels = [];
  202. }else{
  203. $channels = [$mChannel];
  204. }
  205. if(isset($sentInfo[1])){
  206. $channels = [$sentInfo[1]];
  207. }
  208. $Sent = new CorpusController();
  209. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  210. if($props === false){
  211. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  212. }
  213. if($this->mode==='read'){
  214. $tpl = "sentread";
  215. }else{
  216. $tpl = "sentedit";
  217. }
  218. return [
  219. 'props'=>base64_encode(\json_encode($props)),
  220. 'html'=>"",
  221. 'tag'=>'span',
  222. 'tpl'=>$tpl,
  223. ];
  224. }
  225. private function get_param(array $param,string $name,int $id,string $default=''){
  226. if(isset($param[$name])){
  227. return trim($param[$name]);
  228. }else if(isset($param["{$id}"])){
  229. return trim($param["{$id}"]);
  230. }else{
  231. return $default;
  232. }
  233. }
  234. }