TemplateRender.php 6.2 KB

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