TemplateRender.php 24 KB

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