TemplateRender.php 37 KB

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