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. $lang = Channel::where('uid',$channelId)->value('lang');
  69. if(!empty($lang)){
  70. $langFamily = explode('-',$lang)[0];
  71. }else{
  72. $langFamily = 'zh';
  73. }
  74. //先查属于这个channel 的
  75. $tplParam = DhammaTerm::where("word",$word)
  76. ->where('channal',$channelId)
  77. ->orderBy('updated_at','desc')
  78. ->first();
  79. if(!$tplParam){
  80. /**
  81. * 没有,再查这个studio的
  82. * 按照语言过滤
  83. * 完全匹配的优先
  84. * 语族匹配也行
  85. */
  86. $termsInStudio = DhammaTerm::where("word",$word)
  87. ->where('owner',$channelInfo->owner_uid)
  88. ->orderBy('updated_at','desc')
  89. ->get();
  90. if(count($termsInStudio)>0){
  91. $list = array();
  92. foreach ($termsInStudio as $key=>$term) {
  93. if(empty($term->channal)){
  94. if($term->language===$lang){
  95. $list[$term->guid]=2;
  96. }else if(strpos($term->language,$langFamily)!==false){
  97. $list[$term->guid]=1;
  98. }
  99. }
  100. }
  101. if(count($list)>0){
  102. arsort($list);
  103. foreach ($list as $key => $one) {
  104. foreach ($termsInStudio as $term) {
  105. if($term->guid===$key){
  106. $tplParam = $term;
  107. break;
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. if(!$tplParam){
  116. //没有,再查社区
  117. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  118. $tplParam = DhammaTerm::where("word",$word)
  119. ->where('channal',$community_channel)
  120. ->first();
  121. if($tplParam){
  122. $isCommunity = true;
  123. }
  124. }
  125. $output = [
  126. "word" => $word,
  127. "parentChannelId" => $channelId,
  128. "parentStudioId" => $channelInfo->owner_uid,
  129. ];
  130. $innerString = $output["word"];
  131. if($tplParam){
  132. $output["id"] = $tplParam->guid;
  133. $output["meaning"] = $tplParam->meaning;
  134. $output["channel"] = $tplParam->channal;
  135. if(isset($isCommunity)){
  136. $output["isCommunity"] = true;
  137. }
  138. $innerString = "{$output["meaning"]}({$output["word"]})";
  139. if(!empty($tplParam->other_meaning)){
  140. $output["meaning2"] = $tplParam->other_meaning;
  141. }
  142. /*
  143. if($tplParam->note){
  144. $output["summary"] = $tplParam->note;
  145. }else{
  146. //本人没有解释内容的。用社区数据。
  147. //TODO 由作者(读者)设置是否使用社区数据
  148. //获取channel 语言
  149. //使用社区note
  150. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  151. //查找社区解释
  152. $community_note = DhammaTerm::where("word",$word)
  153. ->where('channal',$community_channel)
  154. ->value('note');
  155. $output["summary"] = $tplParam->note;
  156. $output["community"] = true;
  157. }
  158. */
  159. }
  160. $output['innerHtml'] = $innerString;
  161. return $output;
  162. }
  163. private function render_term(){
  164. $word = $this->get_param($this->param,"word",1);
  165. $channelId = $this->channel_id[0];
  166. $channelInfo = $this->channelInfo[0];
  167. $key = "/term/{$channelId}/{$word}";
  168. //Log::info("term cache key:{$key}");
  169. $props = $this->getTermProps($word,$channelId,$channelInfo);
  170. return [
  171. 'props'=>base64_encode(\json_encode($props)),
  172. 'html'=>$props['innerHtml'],
  173. 'tag'=>'span',
  174. 'tpl'=>'term',
  175. ];
  176. }
  177. private function render_note(){
  178. $props = ["note" => $this->get_param($this->param,"text",1)];
  179. $trigger = $this->get_param($this->param,"trigger",2);
  180. $innerString = "";
  181. if(!empty($trigger)){
  182. $props["trigger"] = $trigger;
  183. $innerString = $props["trigger"];
  184. }
  185. return [
  186. 'props'=>base64_encode(\json_encode($props)),
  187. 'html'=>$innerString,
  188. 'tag'=>'span',
  189. 'tpl'=>'note',
  190. ];
  191. }
  192. private function render_nissaya(){
  193. $pali = $this->get_param($this->param,"pali",1);
  194. $meaning = $this->get_param($this->param,"meaning",2);
  195. $innerString = "";
  196. $props = [
  197. "pali" => $pali,
  198. "meaning" => $meaning,
  199. ];
  200. return [
  201. 'props'=>base64_encode(\json_encode($props)),
  202. 'html'=>$innerString,
  203. 'tag'=>'span',
  204. 'tpl'=>'nissaya',
  205. ];
  206. }
  207. private function render_exercise(){
  208. $id = $this->get_param($this->param,"id",1);
  209. $title = $this->get_param($this->param,"title",1);
  210. $props = [
  211. "id" => $id,
  212. "title" => $title,
  213. "channel" => $this->channel_id[0],
  214. ];
  215. return [
  216. 'props'=>base64_encode(\json_encode($props)),
  217. 'html'=>"",
  218. 'tag'=>'span',
  219. 'tpl'=>'exercise',
  220. ];
  221. }
  222. private function render_article(){
  223. $type = $this->get_param($this->param,"type",1);
  224. $id = $this->get_param($this->param,"id",2);
  225. $title = $this->get_param($this->param,"title",3);
  226. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  227. $style = $this->get_param($this->param,"style",5);
  228. $props = [
  229. "type" => $type,
  230. "id" => $id,
  231. "channel" => $channel,
  232. 'style' => $style,
  233. ];
  234. if(!empty($title)){
  235. $props['title'] = $title;
  236. }
  237. return [
  238. 'props'=>base64_encode(\json_encode($props)),
  239. 'html'=>"",
  240. 'text'=>$title,
  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. }