TemplateRender.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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($param,"trigger",2);
  86. if(!empty($trigger)){
  87. $props["trigger"] = $trigger;
  88. $innerString = $props["trigger"];
  89. }
  90. return [
  91. 'props'=>base64_encode(\json_encode($props)),
  92. 'html'=>$innerString,
  93. 'tag'=>'span',
  94. 'tpl'=>'note',
  95. ];
  96. }
  97. private function render_exercise(){
  98. $id = $this->get_param($this->param,"id",1);
  99. $title = $this->get_param($this->param,"title",1);
  100. $props = [
  101. "id" => $id,
  102. "title" => $title,
  103. "channel" => $this->channel_id,
  104. ];
  105. return [
  106. 'props'=>base64_encode(\json_encode($props)),
  107. 'html'=>"",
  108. 'tag'=>'span',
  109. 'tpl'=>'exercise',
  110. ];
  111. }
  112. private function render_quote(){
  113. $paraId = $this->get_param($this->param,"para",1);
  114. $channelId = $this->channel_id;
  115. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  116. 60,
  117. function() use($paraId,$channelId){
  118. $para = \explode('-',$paraId);
  119. $output = [
  120. "paraId" => $paraId,
  121. "channel" => $channelId,
  122. "innerString" => $paraId,
  123. ];
  124. if(count($para)<2){
  125. return $output;
  126. }
  127. $PaliText = PaliText::where("book",$para[0])
  128. ->where("paragraph",$para[1])
  129. ->select(['toc','path'])
  130. ->first();
  131. if($PaliText){
  132. $output["pali"] = $PaliText->toc;
  133. $output["paliPath"] = \json_decode($PaliText->path);
  134. $output["innerString"]= $PaliText->toc;
  135. }
  136. return $output;
  137. });
  138. return [
  139. 'props'=>base64_encode(\json_encode($props)),
  140. 'html'=>$props["innerString"],
  141. 'tag'=>'span',
  142. 'tpl'=>'quote',
  143. ];
  144. }
  145. private function render_sent(){
  146. $sid = $this->get_param($this->param,"sid",1);
  147. $channel = $this->get_param($this->param,"channel",2);
  148. if(!empty($channel)){
  149. $mChannel = $channel;
  150. }else{
  151. $mChannel = $this->channel_id;
  152. }
  153. $sentInfo = explode('@',trim($sid));
  154. $sentId = $sentInfo[0];
  155. if(empty($mChannel)){
  156. $channels = [];
  157. }else{
  158. $channels = [$mChannel];
  159. }
  160. if(isset($sentInfo[1])){
  161. $channels = [$sentInfo[1]];
  162. }
  163. $Sent = new CorpusController();
  164. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  165. if($this->mode==='read'){
  166. $tpl = "sentread";
  167. }else{
  168. $tpl = "sentedit";
  169. }
  170. return [
  171. 'props'=>base64_encode(\json_encode($props)),
  172. 'html'=>"",
  173. 'tag'=>'span',
  174. 'tpl'=>$tpl,
  175. ];
  176. }
  177. private function get_param(array $param,string $name,int $id,string $default=''){
  178. if(isset($param[$name])){
  179. return trim($param[$name]);
  180. }else if(isset($param["{$id}"])){
  181. return trim($param["1"]);
  182. }else{
  183. return $default;
  184. }
  185. }
  186. }