TemplateRender.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 App\Tools\RedisClusters;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Http\Api\ChannelApi;
  11. use App\Http\Api\MdRender;
  12. class TemplateRender{
  13. protected $param = [];
  14. protected $mode = "read";
  15. protected $channel_id = [];
  16. protected $debug = [];
  17. protected $format = 'react';
  18. /**
  19. * Create a new command instance.
  20. * string $mode 'read' | 'edit'
  21. * string $format 'react' | 'text' | 'tex' | 'unity'
  22. * @return void
  23. */
  24. public function __construct($param, $channelInfo, $mode,$format='react',$debug=[])
  25. {
  26. $this->param = $param;
  27. foreach ($channelInfo as $value) {
  28. $this->channel_id[] = $value->uid;
  29. }
  30. $this->channelInfo = $channelInfo;
  31. $this->mode = $mode;
  32. $this->format = $format;
  33. $this->debug = $debug;
  34. }
  35. private function info($message,$debug){
  36. if(in_array($debug,$this->debug)){
  37. Log::info($message);
  38. }
  39. }
  40. private function error($message,$debug){
  41. if(in_array($debug,$this->debug)){
  42. Log::error($message);
  43. }
  44. }
  45. public function render($tpl_name){
  46. switch ($tpl_name) {
  47. case 'term':
  48. # 术语
  49. $result = $this->render_term();
  50. break;
  51. case 'note':
  52. $result = $this->render_note();
  53. break;
  54. case 'sent':
  55. $result = $this->render_sent();
  56. break;
  57. case 'quote':
  58. $result = $this->render_quote();
  59. break;
  60. case 'exercise':
  61. $result = $this->render_exercise();
  62. break;
  63. case 'article':
  64. $result = $this->render_article();
  65. break;
  66. case 'nissaya':
  67. $result = $this->render_nissaya();
  68. break;
  69. case 'mermaid':
  70. $result = $this->render_mermaid();
  71. break;
  72. default:
  73. # code...
  74. $result = [
  75. 'props'=>base64_encode(\json_encode([])),
  76. 'html'=>'',
  77. 'tag'=>'span',
  78. 'tpl'=>'unknown',
  79. ];
  80. break;
  81. }
  82. return $result;
  83. }
  84. public function getTermProps($word,$channelId,$channelInfo){
  85. $lang = Channel::where('uid',$channelId)->value('lang');
  86. if(!empty($lang)){
  87. $langFamily = explode('-',$lang)[0];
  88. }else{
  89. $langFamily = 'zh';
  90. }
  91. $this->info("term:{$word} 先查属于这个channel 的",'term');
  92. $this->info('channel id'.$channelId,'term');
  93. $tplParam = DhammaTerm::where("word",$word)
  94. ->where('channal',$channelId)
  95. ->orderBy('updated_at','desc')
  96. ->first();
  97. if(!$tplParam){
  98. /**
  99. * 没有,再查这个studio的
  100. * 按照语言过滤
  101. * 完全匹配的优先
  102. * 语族匹配也行
  103. */
  104. $this->info("没有-再查这个studio的",'term');
  105. $termsInStudio = DhammaTerm::where("word",$word)
  106. ->where('owner',$channelInfo->owner_uid)
  107. ->orderBy('updated_at','desc')
  108. ->get();
  109. if(count($termsInStudio)>0){
  110. $list = array();
  111. foreach ($termsInStudio as $key=>$term) {
  112. if(empty($term->channal)){
  113. if($term->language===$lang){
  114. $list[$term->guid]=2;
  115. }else if(strpos($term->language,$langFamily)!==false){
  116. $list[$term->guid]=1;
  117. }
  118. }
  119. }
  120. if(count($list)>0){
  121. arsort($list);
  122. foreach ($list as $key => $one) {
  123. foreach ($termsInStudio as $term) {
  124. if($term->guid===$key){
  125. $tplParam = $term;
  126. break;
  127. }
  128. }
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. if(!$tplParam){
  135. $this->info("没有,再查社区",'term');
  136. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  137. $tplParam = DhammaTerm::where("word",$word)
  138. ->where('channal',$community_channel)
  139. ->first();
  140. if($tplParam){
  141. $isCommunity = true;
  142. }else{
  143. $this->info("查社区没有",'term');
  144. }
  145. }
  146. $output = [
  147. "word" => $word,
  148. "parentChannelId" => $channelId,
  149. "parentStudioId" => $channelInfo->owner_uid,
  150. ];
  151. $innerString = $output["word"];
  152. if($tplParam){
  153. $output["id"] = $tplParam->guid;
  154. $output["meaning"] = $tplParam->meaning;
  155. $output["channel"] = $tplParam->channal;
  156. if(isset($isCommunity)){
  157. $output["isCommunity"] = true;
  158. }
  159. $innerString = "{$output["meaning"]}({$output["word"]})";
  160. if(!empty($tplParam->other_meaning)){
  161. $output["meaning2"] = $tplParam->other_meaning;
  162. }
  163. /*
  164. if($tplParam->note){
  165. $output["summary"] = $tplParam->note;
  166. }else{
  167. //本人没有解释内容的。用社区数据。
  168. //TODO 由作者(读者)设置是否使用社区数据
  169. //获取channel 语言
  170. //使用社区note
  171. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  172. //查找社区解释
  173. $community_note = DhammaTerm::where("word",$word)
  174. ->where('channal',$community_channel)
  175. ->value('note');
  176. $output["summary"] = $tplParam->note;
  177. $output["community"] = true;
  178. }
  179. */
  180. }
  181. $output['innerHtml'] = $innerString;
  182. return $output;
  183. }
  184. private function render_term(){
  185. $word = $this->get_param($this->param,"word",1);
  186. $channelId = $this->channel_id[0];
  187. $channelInfo = $this->channelInfo[0];
  188. $key = "/term/{$channelId}/{$word}";
  189. $props = $this->getTermProps($word,$channelId,$channelInfo);
  190. $output = $props['word'];
  191. switch ($this->format) {
  192. case 'react':
  193. $output=[
  194. 'props'=>base64_encode(\json_encode($props)),
  195. 'html'=>$props['innerHtml'],
  196. 'tag'=>'span',
  197. 'tpl'=>'term',
  198. ];
  199. break;
  200. case 'unity':
  201. $output=[
  202. 'props'=>base64_encode(\json_encode($props)),
  203. 'tpl'=>'term',
  204. ];
  205. break;
  206. case 'text':
  207. if(isset($props["meaning"])){
  208. $key = 'term-'.$props["word"];
  209. if(isset($GLOBALS[$key])){
  210. $output = $props["meaning"];
  211. }else{
  212. $GLOBALS[$key] = 1;
  213. $output = $props["meaning"].'('.$props["word"].')';
  214. }
  215. }else{
  216. $output = $props["word"];
  217. }
  218. break;
  219. case 'tex':
  220. if(isset($props["meaning"])){
  221. $key = 'term-'.$props["word"];
  222. if(isset($GLOBALS[$key])){
  223. $output = $props["meaning"];
  224. }else{
  225. $GLOBALS[$key] = 1;
  226. $output = $props["meaning"].'('.$props["word"].')';
  227. }
  228. }else{
  229. $output = $props["word"];
  230. }
  231. break;
  232. }
  233. return $output;
  234. }
  235. private function render_note(){
  236. $note = $this->get_param($this->param,"text",1);
  237. $trigger = $this->get_param($this->param,"trigger",2);
  238. $props = ["note" => $note ];
  239. $innerString = "";
  240. if(!empty($trigger)){
  241. $props["trigger"] = $trigger;
  242. $innerString = $props["trigger"];
  243. }
  244. if($this->format==='unity'){
  245. $props["note"] = MdRender::render($props["note"],
  246. $this->channel_id,
  247. null,
  248. 'read',
  249. 'translation',
  250. 'markdown',
  251. 'unity'
  252. );
  253. }
  254. $output = $note;
  255. switch ($this->format) {
  256. case 'react':
  257. $output=[
  258. 'props'=>base64_encode(\json_encode($props)),
  259. 'html'=>$innerString,
  260. 'tag'=>'span',
  261. 'tpl'=>'note',
  262. ];
  263. break;
  264. case 'unity':
  265. $output=[
  266. 'props'=>base64_encode(\json_encode($props)),
  267. 'tpl'=>'note',
  268. ];
  269. break;
  270. case 'text':
  271. $output = $trigger;
  272. break;
  273. case 'tex':
  274. $output = $trigger;
  275. break;
  276. }
  277. return $output;
  278. }
  279. private function render_nissaya(){
  280. $pali = $this->get_param($this->param,"pali",1);
  281. $meaning = $this->get_param($this->param,"meaning",2);
  282. $innerString = "";
  283. $props = [
  284. "pali" => $pali,
  285. "meaning" => $meaning,
  286. ];
  287. switch ($this->format) {
  288. case 'react':
  289. $output = [
  290. 'props'=>base64_encode(\json_encode($props)),
  291. 'html'=>$innerString,
  292. 'tag'=>'span',
  293. 'tpl'=>'nissaya',
  294. ];
  295. break;
  296. case 'unity':
  297. $output = [
  298. 'props'=>base64_encode(\json_encode($props)),
  299. 'tpl'=>'nissaya',
  300. ];
  301. break;
  302. case 'text':
  303. $output = $pali.'၊'.$meaning;
  304. break;
  305. case 'tex':
  306. $output = $pali.'၊'.$meaning;
  307. break;
  308. default:
  309. $output = $pali.'၊'.$meaning;
  310. break;
  311. }
  312. return $output;
  313. }
  314. private function render_exercise(){
  315. $id = $this->get_param($this->param,"id",1);
  316. $title = $this->get_param($this->param,"title",1);
  317. $props = [
  318. "id" => $id,
  319. "title" => $title,
  320. "channel" => $this->channel_id[0],
  321. ];
  322. switch ($this->format) {
  323. case 'react':
  324. $output = [
  325. 'props'=>base64_encode(\json_encode($props)),
  326. 'html'=>"",
  327. 'tag'=>'span',
  328. 'tpl'=>'exercise',
  329. ];
  330. break;
  331. case 'unity':
  332. $output = [
  333. 'props'=>base64_encode(\json_encode($props)),
  334. 'tpl'=>'exercise',
  335. ];
  336. break;
  337. case 'text':
  338. $output = $title;
  339. break;
  340. case 'tex':
  341. $output = $title;
  342. break;
  343. default:
  344. $output = '';
  345. break;
  346. }
  347. return $output;
  348. }
  349. private function render_article(){
  350. $type = $this->get_param($this->param,"type",1);
  351. $id = $this->get_param($this->param,"id",2);
  352. $title = $this->get_param($this->param,"title",3);
  353. $channel = $this->get_param($this->param,"channel",4,$this->channel_id[0]);
  354. $style = $this->get_param($this->param,"style",5);
  355. $props = [
  356. "type" => $type,
  357. "id" => $id,
  358. "channel" => $channel,
  359. 'style' => $style,
  360. ];
  361. if(!empty($title)){
  362. $props['title'] = $title;
  363. }
  364. switch ($this->format) {
  365. case 'react':
  366. $output = [
  367. 'props'=>base64_encode(\json_encode($props)),
  368. 'html'=>"",
  369. 'text'=>$title,
  370. 'tag'=>'span',
  371. 'tpl'=>'article',
  372. ];
  373. break;
  374. case 'unity':
  375. $output = [
  376. 'props'=>base64_encode(\json_encode($props)),
  377. 'tpl'=>'article',
  378. ];
  379. break;
  380. case 'text':
  381. $output = $title;
  382. break;
  383. case 'tex':
  384. $output = $title;
  385. break;
  386. default:
  387. $output = '';
  388. break;
  389. }
  390. return $output;
  391. }
  392. private function render_quote(){
  393. $paraId = $this->get_param($this->param,"para",1);
  394. $channelId = $this->channel_id[0];
  395. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  396. config('mint.cache.expire'),
  397. function() use($paraId,$channelId){
  398. $para = \explode('-',$paraId);
  399. $output = [
  400. "paraId" => $paraId,
  401. "channel" => $channelId,
  402. "innerString" => $paraId,
  403. ];
  404. if(count($para)<2){
  405. return $output;
  406. }
  407. $PaliText = PaliText::where("book",$para[0])
  408. ->where("paragraph",$para[1])
  409. ->select(['toc','path'])
  410. ->first();
  411. if($PaliText){
  412. $output["pali"] = $PaliText->toc;
  413. $output["paliPath"] = \json_decode($PaliText->path);
  414. $output["innerString"]= $PaliText->toc;
  415. }
  416. return $output;
  417. });
  418. switch ($this->format) {
  419. case 'react':
  420. $output = [
  421. 'props'=>base64_encode(\json_encode($props)),
  422. 'html'=>$props["innerString"],
  423. 'tag'=>'span',
  424. 'tpl'=>'quote',
  425. ];
  426. break;
  427. case 'unity':
  428. $output = [
  429. 'props'=>base64_encode(\json_encode($props)),
  430. 'tpl'=>'quote',
  431. ];
  432. break;
  433. case 'text':
  434. $output = $props["innerString"];
  435. break;
  436. case 'tex':
  437. $output = $props["innerString"];
  438. break;
  439. default:
  440. $output = '';
  441. break;
  442. }
  443. return $output;
  444. }
  445. private function render_sent(){
  446. $sid = $this->get_param($this->param,"sid",1);
  447. $channel = $this->get_param($this->param,"channel",2);
  448. if(!empty($channel)){
  449. $channels = explode(',',$channel);
  450. }else{
  451. $channels = $this->channel_id;
  452. }
  453. $sentInfo = explode('@',trim($sid));
  454. $sentId = $sentInfo[0];
  455. if(isset($sentInfo[1])){
  456. $channels = [$sentInfo[1]];
  457. }
  458. $Sent = new CorpusController();
  459. $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
  460. if($props === false){
  461. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  462. }
  463. if($this->mode==='read'){
  464. $tpl = "sentread";
  465. }else{
  466. $tpl = "sentedit";
  467. }
  468. switch ($this->format) {
  469. case 'react':
  470. $output = [
  471. 'props'=>base64_encode(\json_encode($props)),
  472. 'html'=>"",
  473. 'tag'=>'span',
  474. 'tpl'=>$tpl,
  475. ];
  476. break;
  477. case 'unity':
  478. $output = [
  479. 'props'=>base64_encode(\json_encode($props)),
  480. 'tpl'=>$tpl,
  481. ];
  482. break;
  483. case 'text':
  484. $output = '';
  485. if(isset($props['translation']) && is_array($props['translation'])){
  486. foreach ($props['translation'] as $key => $value) {
  487. $output .= MdRender::render($value['content'],
  488. [$value['channel']['id']],
  489. null,
  490. 'read',
  491. $value['channel']['type'],
  492. 'translation',
  493. 'text'
  494. );
  495. }
  496. }
  497. break;
  498. case 'tex':
  499. $output = '';
  500. if(isset($props['translation']) && is_array($props['translation'])){
  501. foreach ($props['translation'] as $key => $value) {
  502. $output .= MdRender::render($value['content'],
  503. [$value['channel']['id']],
  504. null,
  505. 'read',
  506. $value['channel']['type'],
  507. 'translation',
  508. 'tex'
  509. );
  510. }
  511. }
  512. break;
  513. default:
  514. $output = '';
  515. break;
  516. }
  517. return $output;
  518. }
  519. private function render_mermaid(){
  520. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  521. $props = ["text" => implode("\n",$text)];
  522. switch ($this->format) {
  523. case 'react':
  524. $output = [
  525. 'props'=>base64_encode(\json_encode($props)),
  526. 'html'=>"mermaid",
  527. 'tag'=>'div',
  528. 'tpl'=>'mermaid',
  529. ];
  530. break;
  531. case 'unity':
  532. $output = [
  533. 'props'=>base64_encode(\json_encode($props)),
  534. 'tpl'=>'mermaid',
  535. ];
  536. break;
  537. case 'text':
  538. $output = 'mermaid';
  539. break;
  540. case 'tex':
  541. $output = 'mermaid';
  542. break;
  543. default:
  544. $output = 'mermaid';
  545. break;
  546. }
  547. return $output;
  548. }
  549. private function get_param(array $param,string $name,int $id,string $default=''){
  550. if(isset($param[$name])){
  551. return trim($param[$name]);
  552. }else if(isset($param["{$id}"])){
  553. return trim($param["{$id}"]);
  554. }else{
  555. return $default;
  556. }
  557. }
  558. }