TemplateRender.php 14 KB

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