TemplateRender.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Facades\Http;
  7. use App\Models\DhammaTerm;
  8. use App\Models\PaliText;
  9. use App\Models\Channel;
  10. use App\Models\PageNumber;
  11. use App\Models\Discussion;
  12. use App\Http\Controllers\CorpusController;
  13. use App\Tools\RedisClusters;
  14. use App\Http\Api\ChannelApi;
  15. use App\Http\Api\MdRender;
  16. use App\Http\Api\PaliTextApi;
  17. class TemplateRender{
  18. protected $param = [];
  19. protected $mode = "read";
  20. protected $channel_id = [];
  21. protected $debug = [];
  22. protected $format = 'react';
  23. protected $studioId = null;
  24. protected $lang = 'en';
  25. protected $langFamily = 'en';
  26. protected $options = [
  27. 'mode' => 'read',
  28. 'channelType'=>'translation',
  29. 'contentType'=>"markdown",
  30. 'format'=>'react',
  31. 'debug'=>[],
  32. 'studioId'=>null,
  33. 'lang'=>'zh-Hans',
  34. 'footnote'=>false,
  35. 'paragraph'=>false,
  36. 'origin'=>true,
  37. 'translation'=>true,
  38. ];
  39. /**
  40. * Create a new command instance.
  41. * string $mode 'read' | 'edit'
  42. * string $format 'react' | 'text' | 'tex' | 'unity'
  43. * @return void
  44. */
  45. public function __construct($param, $channelInfo, $mode,$format='react',$studioId='',$debug=[],$lang='zh-Hans')
  46. {
  47. $this->param = $param;
  48. foreach ($channelInfo as $value) {
  49. $this->channel_id[] = $value->uid;
  50. }
  51. $this->channelInfo = $channelInfo;
  52. $this->mode = $mode;
  53. $this->format = $format;
  54. $this->studioId = $studioId;
  55. $this->debug = $debug;
  56. $this->glossaryKey = 'glossary';
  57. if(count($this->channel_id)>0){
  58. $channelId = $this->channel_id[0];
  59. if(Str::isUuid($channelId)){
  60. $lang = Channel::where('uid',$channelId)->value('lang');
  61. }
  62. }
  63. if(!empty($lang)){
  64. $this->lang = $lang;
  65. $this->langFamily = explode('-',$lang)[0];
  66. }
  67. }
  68. public function options($options=[]){
  69. foreach ($options as $key => $value) {
  70. $this->options[$key] = $value;
  71. }
  72. }
  73. public function glossaryKey(){
  74. return $this->glossaryKey;
  75. }
  76. /**
  77. * TODO 设置默认语言。在渲染某些内容的时候需要语言信息
  78. */
  79. public function setLang($lang){
  80. $this->lang = $lang;
  81. $this->langFamily = explode('-',$lang)[0];
  82. }
  83. private function info($message,$debug){
  84. if(in_array($debug,$this->debug)){
  85. Log::info($message);
  86. }
  87. }
  88. private function error($message,$debug){
  89. if(in_array($debug,$this->debug)){
  90. Log::error($message);
  91. }
  92. }
  93. public function render($tpl_name){
  94. switch ($tpl_name) {
  95. case 'term':
  96. # 术语
  97. $result = $this->render_term();
  98. break;
  99. case 'note':
  100. $result = $this->render_note();
  101. break;
  102. case 'sent':
  103. $result = $this->render_sent();
  104. break;
  105. case 'quote':
  106. $result = $this->render_quote();
  107. break;
  108. case 'ql':
  109. $result = $this->render_quote_link();
  110. break;
  111. case 'exercise':
  112. $result = $this->render_exercise();
  113. break;
  114. case 'article':
  115. $result = $this->render_article();
  116. break;
  117. case 'nissaya':
  118. $result = $this->render_nissaya();
  119. break;
  120. case 'mermaid':
  121. $result = $this->render_mermaid();
  122. break;
  123. case 'qa':
  124. $result = $this->render_qa();
  125. break;
  126. case 'v':
  127. $result = $this->render_video();
  128. break;
  129. case 'g':
  130. $result = $this->render_grammar_lookup();
  131. break;
  132. default:
  133. # code...
  134. $result = [
  135. 'props'=>base64_encode(\json_encode([])),
  136. 'html'=>'',
  137. 'tag'=>'span',
  138. 'tpl'=>'unknown',
  139. ];
  140. break;
  141. }
  142. return $result;
  143. }
  144. public function getTermProps($word,$tag=null,$channel=null){
  145. if($channel && !empty($channel)){
  146. $channelId = $channel;
  147. }else{
  148. if(count($this->channel_id)>0){
  149. $channelId = $this->channel_id[0];
  150. }else{
  151. $channelId = null;
  152. }
  153. }
  154. if(count($this->channelInfo)===0){
  155. if(!empty($channel)){
  156. $channelInfo = Channel::where('uid',$channel)->first();
  157. if(!$channelInfo){
  158. unset($channelInfo);
  159. }
  160. }
  161. if(!isset($channelInfo)){
  162. Log::error('channel is null');
  163. $output = [
  164. "word" => $word,
  165. 'innerHtml' => '',
  166. ];
  167. return $output;
  168. }
  169. }else{
  170. $channelInfo = $this->channelInfo[0];
  171. }
  172. if(Str::isUuid($channelId)){
  173. $lang = Channel::where('uid',$channelId)->value('lang');
  174. if(!empty($lang)){
  175. $langFamily = explode('-',$lang)[0];
  176. }else{
  177. $langFamily = 'zh';
  178. }
  179. $this->info("term:{$word} 先查属于这个channel 的",'term');
  180. $this->info('channel id'.$channelId,'term');
  181. $table = DhammaTerm::where("word",$word)
  182. ->where('channal',$channelId);
  183. if($tag && !empty($tag)){
  184. $table = $table->where('tag',$tag);
  185. }
  186. $tplParam = $table->orderBy('updated_at','desc')
  187. ->first();
  188. $studioId = $channelInfo->owner_uid;
  189. }else{
  190. $tplParam = false;
  191. $lang = '';
  192. $langFamily = '';
  193. $studioId = $this->studioId;
  194. }
  195. if(!$tplParam){
  196. if(Str::isUuid($studioId)){
  197. /**
  198. * 没有,再查这个studio的
  199. * 按照语言过滤
  200. * 完全匹配的优先
  201. * 语族匹配也行
  202. */
  203. $this->info("没有-再查这个studio的",'term');
  204. $table = DhammaTerm::where("word",$word);
  205. if(!empty($tag)){
  206. $table = $table->where('tag',$tag);
  207. }
  208. $termsInStudio = $table->where('owner',$channelInfo->owner_uid)
  209. ->orderBy('updated_at','desc')
  210. ->get();
  211. if(count($termsInStudio)>0){
  212. $list = array();
  213. foreach ($termsInStudio as $key=>$term) {
  214. if(empty($term->channal)){
  215. if($term->language===$lang){
  216. $list[$term->guid]=2;
  217. }else if(strpos($term->language,$langFamily) !== false){
  218. $list[$term->guid]=1;
  219. }
  220. }
  221. }
  222. if(count($list)>0){
  223. arsort($list);
  224. foreach ($list as $key => $one) {
  225. foreach ($termsInStudio as $term) {
  226. if($term->guid===$key){
  227. $tplParam = $term;
  228. break;
  229. }
  230. }
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. if(!$tplParam){
  238. $this->info("没有,再查社区",'term');
  239. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  240. $table = DhammaTerm::where("word",$word);
  241. if(!empty($tag)){
  242. $table = $table->where('tag',$tag);
  243. }
  244. $tplParam = $table->where('channal',$community_channel)
  245. ->first();
  246. if($tplParam){
  247. $isCommunity = true;
  248. }else{
  249. $this->info("查社区没有",'term');
  250. }
  251. }
  252. $output = [
  253. "word" => $word,
  254. "parentChannelId" => $channelId,
  255. "parentStudioId" => $channelInfo->owner_uid,
  256. ];
  257. $innerString = $output["word"];
  258. if($tplParam){
  259. $output["id"] = $tplParam->guid;
  260. $output["meaning"] = $tplParam->meaning;
  261. $output["channel"] = $tplParam->channal;
  262. if(!empty($tplParam->note)){
  263. $mdRender = new MdRender(['format'=>$this->format]);
  264. $output['note'] = $mdRender->convert($tplParam->note,$this->channel_id);
  265. }
  266. if(isset($isCommunity)){
  267. $output["isCommunity"] = true;
  268. }
  269. $innerString = "{$output["meaning"]}({$output["word"]})";
  270. if(!empty($tplParam->other_meaning)){
  271. $output["meaning2"] = $tplParam->other_meaning;
  272. }
  273. }
  274. $output['innerHtml'] = $innerString;
  275. return $output;
  276. }
  277. private function render_term(){
  278. $word = $this->get_param($this->param,"word",1);
  279. $props = $this->getTermProps($word,'');
  280. //$key = "/term/{$channelId}/{$word}";
  281. $output = $props['word'];
  282. switch ($this->format) {
  283. case 'react':
  284. $output=[
  285. 'props'=>base64_encode(\json_encode($props)),
  286. 'html'=>$props['innerHtml'],
  287. 'tag'=>'span',
  288. 'tpl'=>'term',
  289. ];
  290. break;
  291. case 'unity':
  292. $output=[
  293. 'props'=>base64_encode(\json_encode($props)),
  294. 'tpl'=>'term',
  295. ];
  296. break;
  297. case 'html':
  298. if(isset($props["meaning"])){
  299. $GLOBALS[$this->glossaryKey][$props["word"]] = $props['meaning'];
  300. $key = 'term-'.$props["word"];
  301. $termHead = "<a href='#'>".$props['meaning']."</a>";
  302. if(isset($GLOBALS[$key])){
  303. $output = $termHead;
  304. }else{
  305. $GLOBALS[$key] = 1;
  306. $output = $termHead.'(<em>'.$props["word"].'</em>)';
  307. }
  308. }else{
  309. $output = $props["word"];
  310. }
  311. break;
  312. case 'text':
  313. if(isset($props["meaning"])){
  314. $key = 'term-'.$props["word"];
  315. if(isset($GLOBALS[$key])){
  316. $output = $props["meaning"];
  317. }else{
  318. $GLOBALS[$key] = 1;
  319. $output = $props["meaning"].'('.$props["word"].')';
  320. }
  321. }else{
  322. $output = $props["word"];
  323. }
  324. break;
  325. case 'tex':
  326. if(isset($props["meaning"])){
  327. $key = 'term-'.$props["word"];
  328. if(isset($GLOBALS[$key])){
  329. $output = $props["meaning"];
  330. }else{
  331. $GLOBALS[$key] = 1;
  332. $output = $props["meaning"].'('.$props["word"].')';
  333. }
  334. }else{
  335. $output = $props["word"];
  336. }
  337. break;
  338. case 'simple':
  339. if(isset($props["meaning"])){
  340. $output = $props["meaning"];
  341. }else{
  342. $output = $props["word"];
  343. }
  344. break;
  345. case 'markdown':
  346. if(isset($props["meaning"])){
  347. $key = 'term-'.$props["word"];
  348. if(isset($GLOBALS[$key])){
  349. $output = $props["meaning"];
  350. }else{
  351. $GLOBALS[$key] = 1;
  352. $output = $props["meaning"].'('.$props["word"].')';
  353. }
  354. }else{
  355. $output = $props["word"];
  356. }
  357. //如果有内容,显示为脚注
  358. if(!empty($props["note"])){
  359. if(isset($GLOBALS['note_sn'])){
  360. $GLOBALS['note_sn']++;
  361. }else{
  362. $GLOBALS['note_sn'] = 1;
  363. $GLOBALS['note'] = array();
  364. }
  365. $content = $props["note"];
  366. $output .= '[^'.$GLOBALS['note_sn'].']';
  367. $GLOBALS['note'][] = [
  368. 'sn' => $GLOBALS['note_sn'],
  369. 'trigger' => '',
  370. 'content' => $content,
  371. ];
  372. }
  373. break;
  374. default:
  375. if(isset($props["meaning"])){
  376. $output = $props["meaning"];
  377. }else{
  378. $output = $props["word"];
  379. }
  380. break;
  381. }
  382. return $output;
  383. }
  384. private function render_note(){
  385. $note = $this->get_param($this->param,"text",1);
  386. $trigger = $this->get_param($this->param,"trigger",2);
  387. $props = ["note" => $note ];
  388. $innerString = "";
  389. if(!empty($trigger)){
  390. $props["trigger"] = $trigger;
  391. $innerString = $props["trigger"];
  392. }
  393. if($this->format==='unity'){
  394. $props["note"] = MdRender::render($props["note"],
  395. $this->channel_id,
  396. null,
  397. 'read',
  398. 'translation',
  399. 'markdown',
  400. 'unity'
  401. );
  402. }
  403. $output = $note;
  404. switch ($this->format) {
  405. case 'react':
  406. $output=[
  407. 'props'=>base64_encode(\json_encode($props)),
  408. 'html'=>$innerString,
  409. 'tag'=>'span',
  410. 'tpl'=>'note',
  411. ];
  412. break;
  413. case 'unity':
  414. $output=[
  415. 'props'=>base64_encode(\json_encode($props)),
  416. 'tpl'=>'note',
  417. ];
  418. break;
  419. case 'html':
  420. if(isset($GLOBALS['note_sn'])){
  421. $GLOBALS['note_sn']++;
  422. }else{
  423. $GLOBALS['note_sn'] = 1;
  424. $GLOBALS['note'] = array();
  425. }
  426. $GLOBALS['note'][] = [
  427. 'sn' => $GLOBALS['note_sn'],
  428. 'trigger' => $trigger,
  429. 'content' => MdRender::render($props["note"],
  430. $this->channel_id,
  431. null,
  432. 'read',
  433. 'translation',
  434. 'markdown',
  435. 'html'
  436. ),
  437. ];
  438. $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
  439. if(empty($trigger)){
  440. $output = $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  441. }else{
  442. $output = $link . $trigger . "</a>";
  443. }
  444. break;
  445. case 'text':
  446. $output = $trigger;
  447. break;
  448. case 'tex':
  449. $output = $trigger;
  450. break;
  451. case 'simple':
  452. $output = '';
  453. break;
  454. case 'markdown':
  455. if(isset($GLOBALS['note_sn'])){
  456. $GLOBALS['note_sn']++;
  457. }else{
  458. $GLOBALS['note_sn'] = 1;
  459. $GLOBALS['note'] = array();
  460. }
  461. $content = MdRender::render(
  462. $props["note"],
  463. $this->channel_id,
  464. null,
  465. 'read',
  466. 'translation',
  467. 'markdown',
  468. 'markdown'
  469. );
  470. $output = '[^'.$GLOBALS['note_sn'].']';
  471. $GLOBALS['note'][] = [
  472. 'sn' => $GLOBALS['note_sn'],
  473. 'trigger' => $trigger,
  474. 'content' => $content,
  475. ];
  476. //$output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  477. break;
  478. default:
  479. $output = '';
  480. break;
  481. }
  482. return $output;
  483. }
  484. private function render_nissaya(){
  485. $pali = $this->get_param($this->param,"pali",1);
  486. $meaning = $this->get_param($this->param,"meaning",2);
  487. $innerString = "";
  488. $props = [
  489. "pali" => $pali,
  490. "meaning" => $meaning,
  491. "lang" => $this->lang,
  492. ];
  493. switch ($this->format) {
  494. case 'react':
  495. $output = [
  496. 'props'=>base64_encode(\json_encode($props)),
  497. 'html'=>$innerString,
  498. 'tag'=>'span',
  499. 'tpl'=>'nissaya',
  500. ];
  501. break;
  502. case 'unity':
  503. $output = [
  504. 'props'=>base64_encode(\json_encode($props)),
  505. 'tpl'=>'nissaya',
  506. ];
  507. break;
  508. case 'text':
  509. $output = $pali.'၊'.$meaning;
  510. break;
  511. case 'tex':
  512. $output = $pali.'၊'.$meaning;
  513. break;
  514. case 'simple':
  515. $output = $pali.'၊'.$meaning;
  516. break;
  517. default:
  518. $output = $pali.'၊'.$meaning;
  519. break;
  520. }
  521. return $output;
  522. }
  523. private function render_exercise(){
  524. $id = $this->get_param($this->param,"id",1);
  525. $title = $this->get_param($this->param,"title",1);
  526. $props = [
  527. "id" => $id,
  528. "title" => $title,
  529. "channel" => $this->channel_id[0],
  530. ];
  531. switch ($this->format) {
  532. case 'react':
  533. $output = [
  534. 'props'=>base64_encode(\json_encode($props)),
  535. 'html'=>"",
  536. 'tag'=>'span',
  537. 'tpl'=>'exercise',
  538. ];
  539. break;
  540. case 'unity':
  541. $output = [
  542. 'props'=>base64_encode(\json_encode($props)),
  543. 'tpl'=>'exercise',
  544. ];
  545. break;
  546. case 'text':
  547. $output = $title;
  548. break;
  549. case 'tex':
  550. $output = $title;
  551. break;
  552. case 'simple':
  553. $output = $title;
  554. break;
  555. default:
  556. $output = '';
  557. break;
  558. }
  559. return $output;
  560. }
  561. private function render_article(){
  562. $type = $this->get_param($this->param,"type",1);
  563. $id = $this->get_param($this->param,"id",2);
  564. $title = $this->get_param($this->param,"title",3);
  565. $channel = $this->get_param($this->param,"channel",4);
  566. $style = $this->get_param($this->param,"style",5);
  567. $book = $this->get_param($this->param,"book",6);
  568. $paragraphs = $this->get_param($this->param,"paragraphs",7);
  569. $anthology = $this->get_param($this->param,"anthology",8);
  570. $props = [
  571. "type" => $type,
  572. "id" => $id,
  573. 'style' => $style,
  574. ];
  575. if(!empty($channel)){
  576. $props['channel'] = $channel;
  577. }
  578. if(!empty($title)){
  579. $props['title'] = $title;
  580. }
  581. if(!empty($book)){
  582. $props['book'] = $book;
  583. }
  584. if(!empty($paragraphs)){
  585. $props['paragraphs'] = $paragraphs;
  586. }
  587. if(!empty($anthology)){
  588. $props['anthology'] = $anthology;
  589. }
  590. if(is_array($this->channel_id)){
  591. $props['parentChannels'] = $this->channel_id;
  592. }
  593. switch ($this->format) {
  594. case 'react':
  595. $output = [
  596. 'props'=>base64_encode(\json_encode($props)),
  597. 'html'=>"",
  598. 'text'=>$title,
  599. 'tag'=>'span',
  600. 'tpl'=>'article',
  601. ];
  602. break;
  603. case 'unity':
  604. $output = [
  605. 'props'=>base64_encode(\json_encode($props)),
  606. 'tpl'=>'article',
  607. ];
  608. break;
  609. case 'text':
  610. $output = $title;
  611. break;
  612. case 'tex':
  613. $output = $title;
  614. break;
  615. case 'simple':
  616. $output = $title;
  617. break;
  618. default:
  619. $output = '';
  620. break;
  621. }
  622. return $output;
  623. }
  624. private function render_quote(){
  625. $paraId = $this->get_param($this->param,"para",1);
  626. $channelId = $this->channel_id[0];
  627. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  628. config('mint.cache.expire'),
  629. function() use($paraId,$channelId){
  630. $para = \explode('-',$paraId);
  631. $output = [
  632. "paraId" => $paraId,
  633. "channel" => $channelId,
  634. "innerString" => $paraId,
  635. ];
  636. if(count($para)<2){
  637. return $output;
  638. }
  639. $PaliText = PaliText::where("book",$para[0])
  640. ->where("paragraph",$para[1])
  641. ->select(['toc','path'])
  642. ->first();
  643. if($PaliText){
  644. $output["pali"] = $PaliText->toc;
  645. $output["paliPath"] = \json_decode($PaliText->path);
  646. $output["innerString"]= $PaliText->toc;
  647. }
  648. return $output;
  649. });
  650. switch ($this->format) {
  651. case 'react':
  652. $output = [
  653. 'props'=>base64_encode(\json_encode($props)),
  654. 'html'=>$props["innerString"],
  655. 'tag'=>'span',
  656. 'tpl'=>'quote',
  657. ];
  658. break;
  659. case 'unity':
  660. $output = [
  661. 'props'=>base64_encode(\json_encode($props)),
  662. 'tpl'=>'quote',
  663. ];
  664. break;
  665. case 'text':
  666. $output = $props["innerString"];
  667. break;
  668. case 'tex':
  669. $output = $props["innerString"];
  670. break;
  671. case 'simple':
  672. $output = $props["innerString"];
  673. break;
  674. default:
  675. $output = $props["innerString"];
  676. break;
  677. }
  678. return $output;
  679. }
  680. private function render_quote_link(){
  681. $type = $this->get_param($this->param,"type",1);
  682. $title = $this->get_param($this->param,"title",6,'');
  683. $bookName = $this->get_param($this->param,"bookname",2,'');
  684. $volume = $this->get_param($this->param,"volume",3);
  685. $page = $this->get_param($this->param,"page",4,'');
  686. $style = $this->get_param($this->param,"style",5,'modal');
  687. $book = $this->get_param($this->param,"book",7,false);
  688. $para = $this->get_param($this->param,"para",8,false);
  689. $props = [
  690. 'type' => $type,
  691. 'style' => $style,
  692. 'found' => true,
  693. ];
  694. if(!empty($bookName) && $volume !== '' && !empty($page)){
  695. $props['bookName'] = $bookName;
  696. $props['volume'] = (int)$volume;
  697. $props['page'] = $page;
  698. $props['found'] = true;
  699. }else if($book && $para){
  700. /**
  701. * 没有指定书名,根据book para 查询
  702. */
  703. if($type==='c'){
  704. //按照章节名称显示
  705. $path = PaliTextApi::getChapterPath($book,$para);
  706. if($path){
  707. $path = json_decode($path,true);
  708. }
  709. if($path && is_array($path) && count($path)>2){
  710. $props['bookName'] = strtolower($path[0]['title']) ;
  711. $props['chapter'] = strtolower(end($path)['title']);
  712. $props['found'] = true;
  713. }else{
  714. $props['found'] = false;
  715. }
  716. }else{
  717. $pageInfo = $this->pageInfoByPara($type,$book,$para);
  718. if($pageInfo['found']){
  719. $props['bookName'] = $pageInfo['bookName'];
  720. $props['volume'] = $pageInfo['volume'];
  721. $props['page'] = $pageInfo['page'];
  722. $props['found'] = true;
  723. }else{
  724. $props['found'] = false;
  725. }
  726. }
  727. }else if($title){
  728. //没有书号用title查询
  729. //$tmpTitle = explode('။',$title);
  730. for ($i=mb_strlen($title,'UTF-8'); $i > 0 ; $i--) {
  731. $mTitle = mb_substr($title,0,$i);
  732. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  733. Log::debug('run',['title'=>$mTitle,'has'=>$has]);
  734. if($has !== false){
  735. $tmpBookTitle = $mTitle;
  736. $tmpBookPage = mb_substr($title,$i);
  737. $tmpBookPage = $this->mb_trim($tmpBookPage,'၊။');
  738. break;
  739. }
  740. }
  741. if(isset($tmpBookTitle)){
  742. Log::debug('book title found',['title'=>$tmpBookTitle,'page'=>$tmpBookPage]);
  743. //$tmpBookTitle = $tmpTitle[0];
  744. //$tmpBookPage = $tmpTitle[1];
  745. $tmpBookPage = (int)str_replace(
  746. ['၁','၂','၃','၄','၅','၆','၇','၈','၉','၀'],
  747. ['1','2','3','4','5','6','7','8','9','0'],
  748. $tmpBookPage);
  749. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  750. if($found_key !== false){
  751. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  752. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  753. $props['page'] = $tmpBookPage;
  754. if(!empty($props['bookName'])){
  755. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  756. if($found_title === false){
  757. $props['found'] = false;
  758. }
  759. }
  760. }else{
  761. //没找到,返回术语和页码
  762. $props['found'] = false;
  763. $props['bookName'] = $tmpBookTitle;
  764. $props['page'] = $tmpBookPage;
  765. $props['volume'] = 0;
  766. }
  767. }
  768. }else{
  769. Log::debug('book title not found');
  770. $props['found'] = false;
  771. }
  772. if($book && $para){
  773. $props['book'] = $book;
  774. $props['para'] = $para;
  775. }
  776. if($title){
  777. $props['title'] = $title;
  778. }
  779. $text = '';
  780. if(isset($props['bookName'])){
  781. $searchField = '';
  782. switch ($type) {
  783. case 'm':
  784. $searchField = 'm_title';
  785. break;
  786. case 'p':
  787. $searchField = 'p_title';
  788. break;
  789. }
  790. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  791. if($found_title === false){
  792. $props['found'] = false;
  793. }
  794. $term = $this->getTermProps($props['bookName'],':quote:');
  795. $props['term'] = $term;
  796. if(isset($term['id'])){
  797. $props['bookNameLocal'] = $term['meaning'];
  798. $text .= $term['meaning'];
  799. }else{
  800. $text .= $bookName;
  801. }
  802. }
  803. if(isset($props['volume']) && isset($props['page'])){
  804. $text .= " {$volume}.{$page}";
  805. }
  806. switch ($this->format) {
  807. case 'react':
  808. $output = [
  809. 'props'=>base64_encode(\json_encode($props)),
  810. 'html'=>'',
  811. 'tag'=>'span',
  812. 'tpl'=>'quote-link',
  813. ];
  814. break;
  815. case 'unity':
  816. $output = [
  817. 'props'=>base64_encode(\json_encode($props)),
  818. 'tpl'=>'quote-link',
  819. ];
  820. break;
  821. default:
  822. $output = $text;
  823. break;
  824. }
  825. return $output;
  826. }
  827. private function pageInfoByPara($type,$book,$para){
  828. $output = array();
  829. $pageInfo = PageNumber::where('type',strtoupper($type))
  830. ->where('book',$book)
  831. ->where('paragraph','<=',$para)
  832. ->orderBy('paragraph','desc')
  833. ->first();
  834. if($pageInfo){
  835. foreach (BookTitle::get() as $value) {
  836. if($value['id']===$pageInfo->pcd_book_id){
  837. switch (strtoupper($type)) {
  838. case 'M':
  839. $key = 'm_title';
  840. break;
  841. case 'P':
  842. $key = 'p_title';
  843. break;
  844. case 'V':
  845. $key = 'v_title';
  846. break;
  847. default:
  848. $key = 'term';
  849. break;
  850. }
  851. $output['bookName'] = $value[$key];
  852. break;
  853. }
  854. }
  855. $output['volume'] = $pageInfo->volume;
  856. $output['page'] = $pageInfo->page;
  857. $output['found'] = true;
  858. }else{
  859. $output['found'] = false;
  860. }
  861. return $output;
  862. }
  863. private function render_sent(){
  864. $sid = $this->get_param($this->param,"id",1);
  865. $channel = $this->get_param($this->param,"channel",2);
  866. $text = $this->get_param($this->param,"text",2,'both');
  867. if(!empty($channel)){
  868. $channels = explode(',',$channel);
  869. }else{
  870. $channels = $this->channel_id;
  871. }
  872. $sentInfo = explode('@',trim($sid));
  873. $sentId = $sentInfo[0];
  874. if(isset($sentInfo[1])){
  875. $channels = [$sentInfo[1]];
  876. }
  877. $Sent = new CorpusController();
  878. $props = $Sent->getSentTpl($sentId,$channels,
  879. $this->mode,true,
  880. $this->format);
  881. if($props === false){
  882. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  883. }
  884. if($this->mode==='read'){
  885. $tpl = "sentread";
  886. }else{
  887. $tpl = "sentedit";
  888. }
  889. switch ($this->format) {
  890. case 'react':
  891. $output = [
  892. 'props'=>base64_encode(\json_encode($props)),
  893. 'html'=>"",
  894. 'tag'=>'span',
  895. 'tpl'=>$tpl,
  896. ];
  897. break;
  898. case 'unity':
  899. $output = [
  900. 'props'=>base64_encode(\json_encode($props)),
  901. 'tpl'=>$tpl,
  902. ];
  903. break;
  904. case 'text':
  905. $output = '';
  906. if(isset($props['origin']) && is_array($props['origin'])){
  907. foreach ($props['origin'] as $key => $value) {
  908. $output .= $value['html'];
  909. }
  910. }
  911. if(isset($props['translation']) && is_array($props['translation'])){
  912. foreach ($props['translation'] as $key => $value) {
  913. $output .= $value['html'];
  914. }
  915. }
  916. break;
  917. case 'html':
  918. $output = '';
  919. $output .= '<span class="sentence">';
  920. if($text === 'both' || $text === 'origin'){
  921. if(isset($props['origin']) && is_array($props['origin'])){
  922. foreach ($props['origin'] as $key => $value) {
  923. $output .= '<span class="origin">'.$value['html'].'</span>';
  924. }
  925. }
  926. }
  927. if($text === 'both' || $text === 'translation'){
  928. if(isset($props['translation']) && is_array($props['translation'])){
  929. foreach ($props['translation'] as $key => $value) {
  930. $output .= '<span class="translation">'.$value['html'].'</span>';
  931. }
  932. }
  933. }
  934. $output .= '</span>';
  935. break;
  936. case 'tex':
  937. $output = '';
  938. if(isset($props['translation']) && is_array($props['translation'])){
  939. foreach ($props['translation'] as $key => $value) {
  940. $output .= $value['html'];
  941. }
  942. }
  943. break;
  944. case 'simple':
  945. $output = '';
  946. if($text === 'both' || $text === 'origin'){
  947. if(empty($output)){
  948. if(isset($props['origin']) &&
  949. is_array($props['origin']) &&
  950. count($props['origin']) > 0
  951. ){
  952. foreach ($props['origin'] as $key => $value) {
  953. $output .= trim($value['html']);
  954. }
  955. }
  956. }
  957. }
  958. if($text === 'both' || $text === 'translation'){
  959. if(isset($props['translation']) &&
  960. is_array($props['translation']) &&
  961. count($props['translation']) > 0
  962. ){
  963. foreach ($props['translation'] as $key => $value) {
  964. $output .= trim($value['html']);
  965. }
  966. }
  967. }
  968. break;
  969. case 'markdown':
  970. $output = '';
  971. if($text === 'both' || $text === 'origin'){
  972. if($this->options['origin'] === true ||
  973. $this->options['origin'] === 'true'){
  974. if(isset($props['origin']) && is_array($props['origin'])){
  975. foreach ($props['origin'] as $key => $value) {
  976. $output .= trim($value['html']);
  977. }
  978. }
  979. }
  980. }
  981. if($text === 'both' || $text === 'translation'){
  982. if($this->options['translation'] === true ||
  983. $this->options['translation'] === 'true'){
  984. if(isset($props['translation']) && is_array($props['translation'])){
  985. foreach ($props['translation'] as $key => $value) {
  986. $output .= trim($value['html']);
  987. }
  988. }
  989. }
  990. }
  991. break;
  992. default:
  993. $output = '';
  994. break;
  995. }
  996. return $output;
  997. }
  998. private function render_mermaid(){
  999. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  1000. $props = ["text" => implode("\n",$text)];
  1001. switch ($this->format) {
  1002. case 'react':
  1003. $output = [
  1004. 'props'=>base64_encode(\json_encode($props)),
  1005. 'html'=>"mermaid",
  1006. 'tag'=>'div',
  1007. 'tpl'=>'mermaid',
  1008. ];
  1009. break;
  1010. case 'unity':
  1011. $output = [
  1012. 'props'=>base64_encode(\json_encode($props)),
  1013. 'tpl'=>'mermaid',
  1014. ];
  1015. break;
  1016. case 'text':
  1017. $output = 'mermaid';
  1018. break;
  1019. case 'tex':
  1020. $output = 'mermaid';
  1021. break;
  1022. case 'simple':
  1023. $output = 'mermaid';
  1024. break;
  1025. default:
  1026. $output = 'mermaid';
  1027. break;
  1028. }
  1029. return $output;
  1030. }
  1031. private function render_qa(){
  1032. $id = $this->get_param($this->param,"id",1);
  1033. $style = $this->get_param($this->param,"style",2);
  1034. $props = [
  1035. "type" => 'qa',
  1036. "id" => $id,
  1037. 'title' => '',
  1038. 'style' => $style,
  1039. ];
  1040. $qa = Discussion::where('id',$id)->first();
  1041. if($qa){
  1042. $props['title'] = $qa->title;
  1043. $props['resId'] = $qa->res_id;
  1044. $props['resType'] = $qa->res_type;
  1045. }
  1046. switch ($this->format) {
  1047. case 'react':
  1048. $output = [
  1049. 'props'=>base64_encode(\json_encode($props)),
  1050. 'html'=>"",
  1051. 'text'=>$props['title'],
  1052. 'tag'=>'div',
  1053. 'tpl'=>'qa',
  1054. ];
  1055. break;
  1056. case 'unity':
  1057. $output = [
  1058. 'props'=>base64_encode(\json_encode($props)),
  1059. 'tpl'=>'qa',
  1060. ];
  1061. break;
  1062. default:
  1063. $output = $props['title'];
  1064. break;
  1065. }
  1066. return $output;
  1067. }
  1068. private function render_grammar_lookup(){
  1069. $word = $this->get_param($this->param,"word",1);
  1070. $props = ['word' => $word];
  1071. $localTermChannel = ChannelApi::getSysChannel(
  1072. "_System_Grammar_Term_".strtolower($this->lang)."_",
  1073. "_System_Grammar_Term_en_"
  1074. );
  1075. $term = $this->getTermProps($word,null,$localTermChannel);
  1076. $props['term'] = $term;
  1077. switch ($this->format) {
  1078. case 'react':
  1079. $output = [
  1080. 'props'=>base64_encode(\json_encode($props)),
  1081. 'html'=>"",
  1082. 'text'=>$props['word'],
  1083. 'tag'=>'span',
  1084. 'tpl'=>'grammar',
  1085. ];
  1086. break;
  1087. case 'unity':
  1088. $output = [
  1089. 'props'=>base64_encode(\json_encode($props)),
  1090. 'tpl'=>'grammar',
  1091. ];
  1092. break;
  1093. default:
  1094. $output = $props['word'];
  1095. break;
  1096. }
  1097. return $output;
  1098. }
  1099. private function render_video(){
  1100. $url = $this->get_param($this->param,"url",1);
  1101. $style = $this->get_param($this->param,"style",2,'modal');
  1102. $title = $this->get_param($this->param,"title",3);
  1103. $props = [
  1104. "url" => $url,
  1105. 'title' => $title,
  1106. 'style' => $style,
  1107. ];
  1108. switch ($this->format) {
  1109. case 'react':
  1110. $output = [
  1111. 'props'=>base64_encode(\json_encode($props)),
  1112. 'html'=>"",
  1113. 'text'=>$props['title'],
  1114. 'tag'=>'span',
  1115. 'tpl'=>'video',
  1116. ];
  1117. break;
  1118. case 'unity':
  1119. $output = [
  1120. 'props'=>base64_encode(\json_encode($props)),
  1121. 'tpl'=>'video',
  1122. ];
  1123. break;
  1124. default:
  1125. $output = $props['title'];
  1126. break;
  1127. }
  1128. return $output;
  1129. }
  1130. private function get_param(array $param,string $name,int $id,string $default=''){
  1131. if(isset($param[$name])){
  1132. return trim($param[$name]);
  1133. }else if(isset($param["{$id}"])){
  1134. return trim($param["{$id}"]);
  1135. }else{
  1136. return $default;
  1137. }
  1138. }
  1139. private function mb_trim($str,string $character_mask = ' ', $charset = "UTF-8") {
  1140. $start = 0;
  1141. $end = mb_strlen($str, $charset) - 1;
  1142. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1143. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset),$chars)) {
  1144. $start++;
  1145. }
  1146. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset),$chars) ) {
  1147. $end--;
  1148. }
  1149. return mb_substr($str, $start, $end - $start + 1, $charset);
  1150. }
  1151. }