TemplateRender.php 19 KB

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