TemplateRender.php 11 KB

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