TemplateRender.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. default:
  45. # code...
  46. $result = [
  47. 'props'=>base64_encode(\json_encode([])),
  48. 'html'=>'',
  49. 'tag'=>'span',
  50. 'tpl'=>'unknown',
  51. ];
  52. break;
  53. }
  54. return $result;
  55. }
  56. private function render_term(){
  57. $word = $this->get_param($this->param,"word",1);
  58. $channelId = $this->channel_id;
  59. $props = Cache::remember("/term/{$this->channel_id}/{$word}",
  60. 60,
  61. function() use($word,$channelId){
  62. $tplParam = DhammaTerm::where("word",$word)->first();
  63. $output = [
  64. "word" => $word,
  65. "channel" => $channelId,
  66. ];
  67. $innerString = $output["word"];
  68. if($tplParam){
  69. $output["id"] = $tplParam->guid;
  70. $output["meaning"] = $tplParam->meaning;
  71. $innerString = "{$output["meaning"]}({$output["word"]})";
  72. if(!empty($tplParam->other_meaning)){
  73. $output["meaning2"] = $tplParam->other_meaning;
  74. }
  75. }
  76. $output['innerHtml'] = $innerString;
  77. return $output;
  78. });
  79. return [
  80. 'props'=>base64_encode(\json_encode($props)),
  81. 'html'=>$props['innerHtml'],
  82. 'tag'=>'span',
  83. 'tpl'=>'term',
  84. ];
  85. }
  86. private function render_note(){
  87. $props = ["note" => $this->get_param($this->param,"text",1)];
  88. $trigger = $this->get_param($this->param,"trigger",2);
  89. $innerString = "";
  90. if(!empty($trigger)){
  91. $props["trigger"] = $trigger;
  92. $innerString = $props["trigger"];
  93. }
  94. return [
  95. 'props'=>base64_encode(\json_encode($props)),
  96. 'html'=>$innerString,
  97. 'tag'=>'span',
  98. 'tpl'=>'note',
  99. ];
  100. }
  101. private function render_exercise(){
  102. $id = $this->get_param($this->param,"id",1);
  103. $title = $this->get_param($this->param,"title",1);
  104. $props = [
  105. "id" => $id,
  106. "title" => $title,
  107. "channel" => $this->channel_id,
  108. ];
  109. return [
  110. 'props'=>base64_encode(\json_encode($props)),
  111. 'html'=>"",
  112. 'tag'=>'span',
  113. 'tpl'=>'exercise',
  114. ];
  115. }
  116. private function render_article(){
  117. $type = $this->get_param($this->param,"type",1);
  118. $id = $this->get_param($this->param,"id",2);
  119. $title = $this->get_param($this->param,"title",3);
  120. $channel = $this->get_param($this->param,"channel",4);
  121. $props = [
  122. "type" => $type,
  123. "id" => $id,
  124. "channel" => $channel,
  125. ];
  126. if(empty($channel)){
  127. $props['channel'] = $this->channel_id;
  128. }
  129. if(!empty($title)){
  130. $props['title'] = $title;
  131. }
  132. return [
  133. 'props'=>base64_encode(\json_encode($props)),
  134. 'html'=>"",
  135. 'tag'=>'span',
  136. 'tpl'=>'article',
  137. ];
  138. }
  139. private function render_quote(){
  140. $paraId = $this->get_param($this->param,"para",1);
  141. $channelId = $this->channel_id;
  142. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  143. 60,
  144. function() use($paraId,$channelId){
  145. $para = \explode('-',$paraId);
  146. $output = [
  147. "paraId" => $paraId,
  148. "channel" => $channelId,
  149. "innerString" => $paraId,
  150. ];
  151. if(count($para)<2){
  152. return $output;
  153. }
  154. $PaliText = PaliText::where("book",$para[0])
  155. ->where("paragraph",$para[1])
  156. ->select(['toc','path'])
  157. ->first();
  158. if($PaliText){
  159. $output["pali"] = $PaliText->toc;
  160. $output["paliPath"] = \json_decode($PaliText->path);
  161. $output["innerString"]= $PaliText->toc;
  162. }
  163. return $output;
  164. });
  165. return [
  166. 'props'=>base64_encode(\json_encode($props)),
  167. 'html'=>$props["innerString"],
  168. 'tag'=>'span',
  169. 'tpl'=>'quote',
  170. ];
  171. }
  172. private function render_sent(){
  173. $sid = $this->get_param($this->param,"sid",1);
  174. $channel = $this->get_param($this->param,"channel",2);
  175. if(!empty($channel)){
  176. $mChannel = $channel;
  177. }else{
  178. $mChannel = $this->channel_id;
  179. }
  180. $sentInfo = explode('@',trim($sid));
  181. $sentId = $sentInfo[0];
  182. if(empty($mChannel)){
  183. $channels = [];
  184. }else{
  185. $channels = [$mChannel];
  186. }
  187. if(isset($sentInfo[1])){
  188. $channels = [$sentInfo[1]];
  189. }
  190. $Sent = new CorpusController();
  191. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  192. if($this->mode==='read'){
  193. $tpl = "sentread";
  194. }else{
  195. $tpl = "sentedit";
  196. }
  197. return [
  198. 'props'=>base64_encode(\json_encode($props)),
  199. 'html'=>"",
  200. 'tag'=>'span',
  201. 'tpl'=>$tpl,
  202. ];
  203. }
  204. private function get_param(array $param,string $name,int $id,string $default=''){
  205. if(isset($param[$name])){
  206. return trim($param[$name]);
  207. }else if(isset($param["{$id}"])){
  208. return trim($param["1"]);
  209. }else{
  210. return $default;
  211. }
  212. }
  213. }