TemplateRender.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. switch ($this->format) {
  271. case 'react':
  272. $output = [
  273. 'props'=>base64_encode(\json_encode($props)),
  274. 'html'=>$innerString,
  275. 'tag'=>'span',
  276. 'tpl'=>'nissaya',
  277. ];
  278. break;
  279. case 'unity':
  280. $output = [
  281. 'props'=>base64_encode(\json_encode($props)),
  282. 'tpl'=>'nissaya',
  283. ];
  284. break;
  285. case 'text':
  286. $output = $pali.'၊'.$meaning;
  287. break;
  288. case 'tex':
  289. $output = $pali.'၊'.$meaning;
  290. break;
  291. default:
  292. $output = $pali.'၊'.$meaning;
  293. break;
  294. }
  295. return $output;
  296. }
  297. private function render_exercise(){
  298. $id = $this->get_param($this->param,"id",1);
  299. $title = $this->get_param($this->param,"title",1);
  300. $props = [
  301. "id" => $id,
  302. "title" => $title,
  303. "channel" => $this->channel_id[0],
  304. ];
  305. switch ($this->format) {
  306. case 'react':
  307. $output = [
  308. 'props'=>base64_encode(\json_encode($props)),
  309. 'html'=>"",
  310. 'tag'=>'span',
  311. 'tpl'=>'exercise',
  312. ];
  313. break;
  314. case 'unity':
  315. $output = [
  316. 'props'=>base64_encode(\json_encode($props)),
  317. 'tpl'=>'exercise',
  318. ];
  319. break;
  320. case 'text':
  321. $output = $title;
  322. break;
  323. case 'tex':
  324. $output = $title;
  325. break;
  326. default:
  327. $output = '';
  328. break;
  329. }
  330. return $output;
  331. }
  332. private function render_article(){
  333. $type = $this->get_param($this->param,"type",1);
  334. $id = $this->get_param($this->param,"id",2);
  335. $title = $this->get_param($this->param,"title",3);
  336. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  337. $style = $this->get_param($this->param,"style",5);
  338. $props = [
  339. "type" => $type,
  340. "id" => $id,
  341. "channel" => $channel,
  342. 'style' => $style,
  343. ];
  344. if(!empty($title)){
  345. $props['title'] = $title;
  346. }
  347. switch ($this->format) {
  348. case 'react':
  349. $output = [
  350. 'props'=>base64_encode(\json_encode($props)),
  351. 'html'=>"",
  352. 'text'=>$title,
  353. 'tag'=>'span',
  354. 'tpl'=>'article',
  355. ];
  356. break;
  357. case 'unity':
  358. $output = [
  359. 'props'=>base64_encode(\json_encode($props)),
  360. 'tpl'=>'article',
  361. ];
  362. break;
  363. case 'text':
  364. $output = $title;
  365. break;
  366. case 'tex':
  367. $output = $title;
  368. break;
  369. default:
  370. $output = '';
  371. break;
  372. }
  373. return $output;
  374. }
  375. private function render_quote(){
  376. $paraId = $this->get_param($this->param,"para",1);
  377. $channelId = $this->channel_id[0];
  378. $props = Cache::remember("/quote/{$channelId}/{$paraId}",
  379. env('CACHE_EXPIRE',3600*24),
  380. function() use($paraId,$channelId){
  381. $para = \explode('-',$paraId);
  382. $output = [
  383. "paraId" => $paraId,
  384. "channel" => $channelId,
  385. "innerString" => $paraId,
  386. ];
  387. if(count($para)<2){
  388. return $output;
  389. }
  390. $PaliText = PaliText::where("book",$para[0])
  391. ->where("paragraph",$para[1])
  392. ->select(['toc','path'])
  393. ->first();
  394. if($PaliText){
  395. $output["pali"] = $PaliText->toc;
  396. $output["paliPath"] = \json_decode($PaliText->path);
  397. $output["innerString"]= $PaliText->toc;
  398. }
  399. return $output;
  400. });
  401. switch ($this->format) {
  402. case 'react':
  403. $output = [
  404. 'props'=>base64_encode(\json_encode($props)),
  405. 'html'=>$props["innerString"],
  406. 'tag'=>'span',
  407. 'tpl'=>'quote',
  408. ];
  409. break;
  410. case 'unity':
  411. $output = [
  412. 'props'=>base64_encode(\json_encode($props)),
  413. 'tpl'=>'quote',
  414. ];
  415. break;
  416. case 'text':
  417. $output = $props["innerString"];
  418. break;
  419. case 'tex':
  420. $output = $props["innerString"];
  421. break;
  422. default:
  423. $output = '';
  424. break;
  425. }
  426. return $output;
  427. }
  428. private function render_sent(){
  429. $sid = $this->get_param($this->param,"sid",1);
  430. $channel = $this->get_param($this->param,"channel",2);
  431. if(!empty($channel)){
  432. $channels = explode(',',$channel);
  433. }else{
  434. $channels = $this->channel_id;
  435. }
  436. $sentInfo = explode('@',trim($sid));
  437. $sentId = $sentInfo[0];
  438. if(isset($sentInfo[1])){
  439. $channels = [$sentInfo[1]];
  440. }
  441. $Sent = new CorpusController();
  442. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  443. if($props === false){
  444. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  445. }
  446. if($this->mode==='read'){
  447. $tpl = "sentread";
  448. }else{
  449. $tpl = "sentedit";
  450. }
  451. switch ($this->format) {
  452. case 'react':
  453. $output = [
  454. 'props'=>base64_encode(\json_encode($props)),
  455. 'html'=>"",
  456. 'tag'=>'span',
  457. 'tpl'=>$tpl,
  458. ];
  459. break;
  460. case 'unity':
  461. $output = [
  462. 'props'=>base64_encode(\json_encode($props)),
  463. 'tpl'=>$tpl,
  464. ];
  465. break;
  466. case 'text':
  467. $output = '';
  468. break;
  469. case 'tex':
  470. $output = '';
  471. break;
  472. default:
  473. $output = '';
  474. break;
  475. }
  476. return $output;
  477. }
  478. private function render_mermaid(){
  479. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  480. $props = ["text" => implode("\n",$text)];
  481. switch ($this->format) {
  482. case 'react':
  483. $output = [
  484. 'props'=>base64_encode(\json_encode($props)),
  485. 'html'=>"mermaid",
  486. 'tag'=>'div',
  487. 'tpl'=>'mermaid',
  488. ];
  489. break;
  490. case 'unity':
  491. $output = [
  492. 'props'=>base64_encode(\json_encode($props)),
  493. 'tpl'=>'mermaid',
  494. ];
  495. break;
  496. case 'text':
  497. $output = 'mermaid';
  498. break;
  499. case 'tex':
  500. $output = 'mermaid';
  501. break;
  502. default:
  503. $output = 'mermaid';
  504. break;
  505. }
  506. return $output;
  507. }
  508. private function get_param(array $param,string $name,int $id,string $default=''){
  509. if(isset($param[$name])){
  510. return trim($param[$name]);
  511. }else if(isset($param["{$id}"])){
  512. return trim($param["{$id}"]);
  513. }else{
  514. return $default;
  515. }
  516. }
  517. }