TemplateRender.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\DhammaTerm;
  4. use App\Models\PaliText;
  5. use App\Models\Channel;
  6. use App\Http\Controllers\CorpusController;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. use App\Http\Api\ChannelApi;
  10. class TemplateRender{
  11. protected $param = [];
  12. protected $mode = "read";
  13. protected $channel_id = [];
  14. /**
  15. * Create a new command instance.
  16. * int $mode 'read' | 'edit'
  17. * @return void
  18. */
  19. public function __construct($param, $channelInfo, $mode)
  20. {
  21. $this->param = $param;
  22. foreach ($channelInfo as $value) {
  23. $this->channel_id[] = $value->uid;
  24. }
  25. $this->channelInfo = $channelInfo;
  26. $this->mode = $mode;
  27. }
  28. public function render($tpl_name){
  29. switch ($tpl_name) {
  30. case 'term':
  31. # 术语
  32. $result = $this->render_term();
  33. break;
  34. case 'note':
  35. $result = $this->render_note();
  36. break;
  37. case 'sent':
  38. $result = $this->render_sent();
  39. break;
  40. case 'quote':
  41. $result = $this->render_quote();
  42. break;
  43. case 'exercise':
  44. $result = $this->render_exercise();
  45. break;
  46. case 'article':
  47. $result = $this->render_article();
  48. break;
  49. case 'nissaya':
  50. $result = $this->render_nissaya();
  51. break;
  52. case 'mermaid':
  53. $result = $this->render_mermaid();
  54. break;
  55. default:
  56. # code...
  57. $result = [
  58. 'props'=>base64_encode(\json_encode([])),
  59. 'html'=>'',
  60. 'tag'=>'span',
  61. 'tpl'=>'unknown',
  62. ];
  63. break;
  64. }
  65. return $result;
  66. }
  67. public function getTermProps($word,$channelId,$channelInfo){
  68. Log::info("term render:{$word}-{$channelId}");
  69. $lang = Channel::where('uid',$channelId)->value('lang');
  70. if(!empty($lang)){
  71. $langFamily = explode('-',$lang)[0];
  72. }
  73. //先查属于这个channel 的
  74. $tplParam = DhammaTerm::where("word",$word)
  75. ->where('channal',$channelId)
  76. ->orderBy('updated_at','desc')
  77. ->first();
  78. if(!$tplParam){
  79. /**
  80. * 没有,再查这个studio的
  81. * 按照语言过滤
  82. * 完全匹配的优先
  83. * 语族匹配也行
  84. */
  85. $termsInStudio = DhammaTerm::where("word",$word)
  86. ->where('owner',$channelInfo->owner_uid)
  87. ->orderBy('updated_at','desc')
  88. ->get();
  89. if(count($termsInStudio)>0){
  90. $list = array();
  91. foreach ($termsInStudio as $key=>$term) {
  92. if(empty($term->channal)){
  93. if($term->language===$lang){
  94. $list[$term->guid]=2;
  95. }else if(strpos($term->language,$langFamily)!==false){
  96. $list[$term->guid]=1;
  97. }
  98. }
  99. }
  100. if(count($list)>0){
  101. arsort($list);
  102. foreach ($list as $key => $one) {
  103. foreach ($termsInStudio as $term) {
  104. if($term->guid===$key){
  105. $tplParam = $term;
  106. break;
  107. }
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. if(!$tplParam){
  115. //没有,再查社区
  116. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  117. $tplParam = DhammaTerm::where("word",$word)
  118. ->where('channal',$community_channel)
  119. ->first();
  120. if($tplParam){
  121. $isCommunity = true;
  122. }
  123. }
  124. $output = [
  125. "word" => $word,
  126. "parentChannelId" => $channelId,
  127. "parentStudioId" => $channelInfo->owner_uid,
  128. ];
  129. $innerString = $output["word"];
  130. if($tplParam){
  131. $output["id"] = $tplParam->guid;
  132. $output["meaning"] = $tplParam->meaning;
  133. $output["channel"] = $tplParam->channal;
  134. if(isset($isCommunity)){
  135. $output["isCommunity"] = true;
  136. }
  137. $innerString = "{$output["meaning"]}({$output["word"]})";
  138. if(!empty($tplParam->other_meaning)){
  139. $output["meaning2"] = $tplParam->other_meaning;
  140. }
  141. /*
  142. if($tplParam->note){
  143. $output["summary"] = $tplParam->note;
  144. }else{
  145. //本人没有解释内容的。用社区数据。
  146. //TODO 由作者(读者)设置是否使用社区数据
  147. //获取channel 语言
  148. //使用社区note
  149. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  150. //查找社区解释
  151. $community_note = DhammaTerm::where("word",$word)
  152. ->where('channal',$community_channel)
  153. ->value('note');
  154. $output["summary"] = $tplParam->note;
  155. $output["community"] = true;
  156. }
  157. */
  158. }
  159. $output['innerHtml'] = $innerString;
  160. return $output;
  161. }
  162. private function render_term(){
  163. $word = $this->get_param($this->param,"word",1);
  164. $channelId = $this->channel_id[0];
  165. $channelInfo = $this->channelInfo[0];
  166. $key = "/term/{$channelId}/{$word}";
  167. //Log::info("term cache key:{$key}");
  168. $props = $this->getTermProps($word,$channelId,$channelInfo);
  169. return [
  170. 'props'=>base64_encode(\json_encode($props)),
  171. 'html'=>$props['innerHtml'],
  172. 'tag'=>'span',
  173. 'tpl'=>'term',
  174. ];
  175. }
  176. private function render_note(){
  177. $props = ["note" => $this->get_param($this->param,"text",1)];
  178. $trigger = $this->get_param($this->param,"trigger",2);
  179. $innerString = "";
  180. if(!empty($trigger)){
  181. $props["trigger"] = $trigger;
  182. $innerString = $props["trigger"];
  183. }
  184. return [
  185. 'props'=>base64_encode(\json_encode($props)),
  186. 'html'=>$innerString,
  187. 'tag'=>'span',
  188. 'tpl'=>'note',
  189. ];
  190. }
  191. private function render_nissaya(){
  192. $pali = $this->get_param($this->param,"pali",1);
  193. $meaning = $this->get_param($this->param,"meaning",2);
  194. $innerString = "";
  195. $props = [
  196. "pali" => $pali,
  197. "meaning" => $meaning,
  198. ];
  199. return [
  200. 'props'=>base64_encode(\json_encode($props)),
  201. 'html'=>$innerString,
  202. 'tag'=>'span',
  203. 'tpl'=>'nissaya',
  204. ];
  205. }
  206. private function render_exercise(){
  207. $id = $this->get_param($this->param,"id",1);
  208. $title = $this->get_param($this->param,"title",1);
  209. $props = [
  210. "id" => $id,
  211. "title" => $title,
  212. "channel" => $this->channel_id[0],
  213. ];
  214. return [
  215. 'props'=>base64_encode(\json_encode($props)),
  216. 'html'=>"",
  217. 'tag'=>'span',
  218. 'tpl'=>'exercise',
  219. ];
  220. }
  221. private function render_article(){
  222. $type = $this->get_param($this->param,"type",1);
  223. $id = $this->get_param($this->param,"id",2);
  224. $title = $this->get_param($this->param,"title",3);
  225. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  226. $style = $this->get_param($this->param,"style",5);
  227. $props = [
  228. "type" => $type,
  229. "id" => $id,
  230. "channel" => $channel,
  231. 'style' => $style,
  232. ];
  233. if(!empty($title)){
  234. $props['title'] = $title;
  235. }
  236. return [
  237. 'props'=>base64_encode(\json_encode($props)),
  238. 'html'=>"",
  239. 'tag'=>'span',
  240. 'tpl'=>'article',
  241. ];
  242. }
  243. private function render_quote(){
  244. $paraId = $this->get_param($this->param,"para",1);
  245. $channelId = $this->channel_id[0];
  246. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  247. env('CACHE_EXPIRE',3600*24),
  248. function() use($paraId,$channelId){
  249. $para = \explode('-',$paraId);
  250. $output = [
  251. "paraId" => $paraId,
  252. "channel" => $channelId,
  253. "innerString" => $paraId,
  254. ];
  255. if(count($para)<2){
  256. return $output;
  257. }
  258. $PaliText = PaliText::where("book",$para[0])
  259. ->where("paragraph",$para[1])
  260. ->select(['toc','path'])
  261. ->first();
  262. if($PaliText){
  263. $output["pali"] = $PaliText->toc;
  264. $output["paliPath"] = \json_decode($PaliText->path);
  265. $output["innerString"]= $PaliText->toc;
  266. }
  267. return $output;
  268. });
  269. return [
  270. 'props'=>base64_encode(\json_encode($props)),
  271. 'html'=>$props["innerString"],
  272. 'tag'=>'span',
  273. 'tpl'=>'quote',
  274. ];
  275. }
  276. private function render_sent(){
  277. $sid = $this->get_param($this->param,"sid",1);
  278. $channel = $this->get_param($this->param,"channel",2);
  279. if(!empty($channel)){
  280. $channels = explode(',',$channel);
  281. }else{
  282. $channels = $this->channel_id;
  283. }
  284. $sentInfo = explode('@',trim($sid));
  285. $sentId = $sentInfo[0];
  286. if(isset($sentInfo[1])){
  287. $channels = [$sentInfo[1]];
  288. }
  289. $Sent = new CorpusController();
  290. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  291. if($props === false){
  292. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  293. }
  294. if($this->mode==='read'){
  295. $tpl = "sentread";
  296. }else{
  297. $tpl = "sentedit";
  298. }
  299. return [
  300. 'props'=>base64_encode(\json_encode($props)),
  301. 'html'=>"",
  302. 'tag'=>'span',
  303. 'tpl'=>$tpl,
  304. ];
  305. }
  306. private function render_mermaid(){
  307. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  308. $props = ["text" => implode("\n",$text)];
  309. return [
  310. 'props'=>base64_encode(\json_encode($props)),
  311. 'html'=>"mermaid",
  312. 'tag'=>'div',
  313. 'tpl'=>'mermaid',
  314. ];
  315. }
  316. private function get_param(array $param,string $name,int $id,string $default=''){
  317. if(isset($param[$name])){
  318. return trim($param[$name]);
  319. }else if(isset($param["{$id}"])){
  320. return trim($param["{$id}"]);
  321. }else{
  322. return $default;
  323. }
  324. }
  325. }