TemplateRender.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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,$this->channel_id[0]);
  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. $props = [
  495. "type" => $type,
  496. "id" => $id,
  497. "channel" => $channel,
  498. 'style' => $style,
  499. ];
  500. if(!empty($title)){
  501. $props['title'] = $title;
  502. }
  503. if(!empty($book)){
  504. $props['book'] = $book;
  505. }
  506. if(!empty($paragraphs)){
  507. $props['paragraphs'] = $paragraphs;
  508. }
  509. switch ($this->format) {
  510. case 'react':
  511. $output = [
  512. 'props'=>base64_encode(\json_encode($props)),
  513. 'html'=>"",
  514. 'text'=>$title,
  515. 'tag'=>'span',
  516. 'tpl'=>'article',
  517. ];
  518. break;
  519. case 'unity':
  520. $output = [
  521. 'props'=>base64_encode(\json_encode($props)),
  522. 'tpl'=>'article',
  523. ];
  524. break;
  525. case 'text':
  526. $output = $title;
  527. break;
  528. case 'tex':
  529. $output = $title;
  530. break;
  531. case 'simple':
  532. $output = $title;
  533. break;
  534. default:
  535. $output = '';
  536. break;
  537. }
  538. return $output;
  539. }
  540. private function render_quote(){
  541. $paraId = $this->get_param($this->param,"para",1);
  542. $channelId = $this->channel_id[0];
  543. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  544. config('mint.cache.expire'),
  545. function() use($paraId,$channelId){
  546. $para = \explode('-',$paraId);
  547. $output = [
  548. "paraId" => $paraId,
  549. "channel" => $channelId,
  550. "innerString" => $paraId,
  551. ];
  552. if(count($para)<2){
  553. return $output;
  554. }
  555. $PaliText = PaliText::where("book",$para[0])
  556. ->where("paragraph",$para[1])
  557. ->select(['toc','path'])
  558. ->first();
  559. if($PaliText){
  560. $output["pali"] = $PaliText->toc;
  561. $output["paliPath"] = \json_decode($PaliText->path);
  562. $output["innerString"]= $PaliText->toc;
  563. }
  564. return $output;
  565. });
  566. switch ($this->format) {
  567. case 'react':
  568. $output = [
  569. 'props'=>base64_encode(\json_encode($props)),
  570. 'html'=>$props["innerString"],
  571. 'tag'=>'span',
  572. 'tpl'=>'quote',
  573. ];
  574. break;
  575. case 'unity':
  576. $output = [
  577. 'props'=>base64_encode(\json_encode($props)),
  578. 'tpl'=>'quote',
  579. ];
  580. break;
  581. case 'text':
  582. $output = $props["innerString"];
  583. break;
  584. case 'tex':
  585. $output = $props["innerString"];
  586. break;
  587. case 'simple':
  588. $output = $props["innerString"];
  589. break;
  590. default:
  591. $output = $props["innerString"];
  592. break;
  593. }
  594. return $output;
  595. }
  596. private function render_quote_link(){
  597. $type = $this->get_param($this->param,"type",1);
  598. $title = $this->get_param($this->param,"title",6,'');
  599. $bookName = $this->get_param($this->param,"bookname",2,'');
  600. $volume = $this->get_param($this->param,"volume",3);
  601. $page = $this->get_param($this->param,"page",4,'');
  602. $style = $this->get_param($this->param,"style",5,'modal');
  603. $book = $this->get_param($this->param,"book",7,false);
  604. $para = $this->get_param($this->param,"para",8,false);
  605. $props = [
  606. 'type' => $type,
  607. 'style' => $style,
  608. 'found' => true,
  609. ];
  610. if(!empty($bookName) && $volume !== '' && !empty($page)){
  611. $props['bookName'] = $bookName;
  612. $props['volume'] = (int)$volume;
  613. $props['page'] = $page;
  614. $props['found'] = true;
  615. }else if($book && $para){
  616. /**
  617. * 没有指定书名,根据book para 查询
  618. */
  619. if($type==='c'){
  620. //按照章节名称显示
  621. $path = PaliTextApi::getChapterPath($book,$para);
  622. if($path){
  623. $path = json_decode($path,true);
  624. }
  625. if($path && is_array($path) && count($path)>2){
  626. $props['bookName'] = strtolower($path[0]['title']) ;
  627. $props['chapter'] = strtolower(end($path)['title']);
  628. $props['found'] = true;
  629. }else{
  630. $props['found'] = false;
  631. }
  632. }else{
  633. $pageInfo = $this->pageInfoByPara($type,$book,$para);
  634. if($pageInfo['found']){
  635. $props['bookName'] = $pageInfo['bookName'];
  636. $props['volume'] = $pageInfo['volume'];
  637. $props['page'] = $pageInfo['page'];
  638. $props['found'] = true;
  639. }else{
  640. $props['found'] = false;
  641. }
  642. }
  643. }else if($title){
  644. //没有书号用title查询
  645. $tmpTitle = explode('။',$title);
  646. if(count($tmpTitle)>1){
  647. $tmpBookTitle = $tmpTitle[0];
  648. $tmpBookPage = $tmpTitle[1];
  649. $tmpBookPage = (int)str_replace(
  650. ['၁','၂','၃','၄','၅','၆','၇','၈','၉','၀'],
  651. ['1','2','3','4','5','6','7','8','9','0'],
  652. $tmpBookPage);
  653. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  654. if($found_key !== false){
  655. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  656. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  657. $props['page'] = $tmpBookPage;
  658. if(!empty($props['bookName'])){
  659. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  660. if($found_title === false){
  661. $props['found'] = false;
  662. }
  663. }
  664. }else{
  665. //没找到,返回术语和页码
  666. $props['found'] = false;
  667. $props['bookName'] = $tmpBookTitle;
  668. $props['page'] = $tmpBookPage;
  669. $props['volume'] = 0;
  670. }
  671. }
  672. }else{
  673. $props['found'] = false;
  674. }
  675. if($book && $para){
  676. $props['book'] = $book;
  677. $props['para'] = $para;
  678. }
  679. if($title){
  680. $props['title'] = $title;
  681. }
  682. $text = '';
  683. if(isset($props['bookName'])){
  684. $searchField = '';
  685. switch ($type) {
  686. case 'm':
  687. $searchField = 'm_title';
  688. break;
  689. case 'p':
  690. $searchField = 'p_title';
  691. break;
  692. }
  693. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  694. if($found_title === false){
  695. $props['found'] = false;
  696. }
  697. $term = $this->getTermProps($props['bookName'],':quote:');
  698. $props['term'] = $term;
  699. if(isset($term['id'])){
  700. $props['bookNameLocal'] = $term['meaning'];
  701. $text .= $term['meaning'];
  702. }else{
  703. $text .= $bookName;
  704. }
  705. }
  706. if(isset($props['volume']) && isset($props['page'])){
  707. $text .= " {$volume}.{$page}";
  708. }
  709. switch ($this->format) {
  710. case 'react':
  711. $output = [
  712. 'props'=>base64_encode(\json_encode($props)),
  713. 'html'=>'',
  714. 'tag'=>'span',
  715. 'tpl'=>'quote-link',
  716. ];
  717. break;
  718. case 'unity':
  719. $output = [
  720. 'props'=>base64_encode(\json_encode($props)),
  721. 'tpl'=>'quote-link',
  722. ];
  723. break;
  724. default:
  725. $output = $text;
  726. break;
  727. }
  728. return $output;
  729. }
  730. private function pageInfoByPara($type,$book,$para){
  731. $output = array();
  732. $pageInfo = PageNumber::where('type',strtoupper($type))
  733. ->where('book',$book)
  734. ->where('paragraph','<=',$para)
  735. ->orderBy('paragraph','desc')
  736. ->first();
  737. if($pageInfo){
  738. foreach (BookTitle::get() as $value) {
  739. if($value['id']===$pageInfo->pcd_book_id){
  740. switch (strtoupper($type)) {
  741. case 'M':
  742. $key = 'm_title';
  743. break;
  744. case 'P':
  745. $key = 'p_title';
  746. break;
  747. case 'V':
  748. $key = 'v_title';
  749. break;
  750. default:
  751. $key = 'term';
  752. break;
  753. }
  754. $output['bookName'] = $value[$key];
  755. break;
  756. }
  757. }
  758. $output['volume'] = $pageInfo->volume;
  759. $output['page'] = $pageInfo->page;
  760. $output['found'] = true;
  761. }else{
  762. $output['found'] = false;
  763. }
  764. return $output;
  765. }
  766. private function render_sent(){
  767. $sid = $this->get_param($this->param,"sid",1);
  768. $channel = $this->get_param($this->param,"channel",2);
  769. if(!empty($channel)){
  770. $channels = explode(',',$channel);
  771. }else{
  772. $channels = $this->channel_id;
  773. }
  774. $sentInfo = explode('@',trim($sid));
  775. $sentId = $sentInfo[0];
  776. if(isset($sentInfo[1])){
  777. $channels = [$sentInfo[1]];
  778. }
  779. $Sent = new CorpusController();
  780. $props = $Sent->getSentTpl($sentId,$channels,
  781. $this->mode,true,
  782. $this->format);
  783. if($props === false){
  784. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  785. }
  786. if($this->mode==='read'){
  787. $tpl = "sentread";
  788. }else{
  789. $tpl = "sentedit";
  790. }
  791. switch ($this->format) {
  792. case 'react':
  793. $output = [
  794. 'props'=>base64_encode(\json_encode($props)),
  795. 'html'=>"",
  796. 'tag'=>'span',
  797. 'tpl'=>$tpl,
  798. ];
  799. break;
  800. case 'unity':
  801. $output = [
  802. 'props'=>base64_encode(\json_encode($props)),
  803. 'tpl'=>$tpl,
  804. ];
  805. break;
  806. case 'text':
  807. $output = '';
  808. if(isset($props['translation']) && is_array($props['translation'])){
  809. foreach ($props['translation'] as $key => $value) {
  810. $output .= $value['html'];
  811. }
  812. }
  813. break;
  814. case 'html':
  815. $output = '';
  816. if(isset($props['translation']) && is_array($props['translation'])){
  817. foreach ($props['translation'] as $key => $value) {
  818. $output .= '<span class="sentence">'.$value['html'].'</span>';
  819. }
  820. }
  821. break;
  822. case 'tex':
  823. $output = '';
  824. if(isset($props['translation']) && is_array($props['translation'])){
  825. foreach ($props['translation'] as $key => $value) {
  826. $output .= $value['html'];
  827. }
  828. }
  829. break;
  830. case 'simple':
  831. $output = '';
  832. if(isset($props['translation']) &&
  833. is_array($props['translation']) &&
  834. count($props['translation']) > 0
  835. ){
  836. $sentences = $props['translation'];
  837. foreach ($sentences as $key => $value) {
  838. $output .= $value['html'];
  839. }
  840. }
  841. if(empty($output)){
  842. if(isset($props['origin']) &&
  843. is_array($props['origin']) &&
  844. count($props['origin']) > 0
  845. ){
  846. $sentences = $props['origin'];
  847. foreach ($sentences as $key => $value) {
  848. $output .= $value['html'];
  849. }
  850. }
  851. }
  852. break;
  853. default:
  854. $output = '';
  855. break;
  856. }
  857. return $output;
  858. }
  859. private function render_mermaid(){
  860. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  861. $props = ["text" => implode("\n",$text)];
  862. switch ($this->format) {
  863. case 'react':
  864. $output = [
  865. 'props'=>base64_encode(\json_encode($props)),
  866. 'html'=>"mermaid",
  867. 'tag'=>'div',
  868. 'tpl'=>'mermaid',
  869. ];
  870. break;
  871. case 'unity':
  872. $output = [
  873. 'props'=>base64_encode(\json_encode($props)),
  874. 'tpl'=>'mermaid',
  875. ];
  876. break;
  877. case 'text':
  878. $output = 'mermaid';
  879. break;
  880. case 'tex':
  881. $output = 'mermaid';
  882. break;
  883. case 'simple':
  884. $output = 'mermaid';
  885. break;
  886. default:
  887. $output = 'mermaid';
  888. break;
  889. }
  890. return $output;
  891. }
  892. private function render_qa(){
  893. $id = $this->get_param($this->param,"id",1);
  894. $style = $this->get_param($this->param,"style",2);
  895. $props = [
  896. "type" => 'qa',
  897. "id" => $id,
  898. 'title' => '',
  899. 'style' => $style,
  900. ];
  901. $qa = Discussion::where('id',$id)->first();
  902. if($qa){
  903. $props['title'] = $qa->title;
  904. $props['resId'] = $qa->res_id;
  905. $props['resType'] = $qa->res_type;
  906. }
  907. switch ($this->format) {
  908. case 'react':
  909. $output = [
  910. 'props'=>base64_encode(\json_encode($props)),
  911. 'html'=>"",
  912. 'text'=>$props['title'],
  913. 'tag'=>'div',
  914. 'tpl'=>'qa',
  915. ];
  916. break;
  917. case 'unity':
  918. $output = [
  919. 'props'=>base64_encode(\json_encode($props)),
  920. 'tpl'=>'qa',
  921. ];
  922. break;
  923. default:
  924. $output = $props['title'];
  925. break;
  926. }
  927. return $output;
  928. }
  929. private function render_grammar_lookup(){
  930. $word = $this->get_param($this->param,"word",1);
  931. $props = ['word' => $word];
  932. $localTermChannel = ChannelApi::getSysChannel(
  933. "_System_Grammar_Term_".strtolower($this->lang)."_",
  934. "_System_Grammar_Term_en_"
  935. );
  936. $term = $this->getTermProps($word,null,$localTermChannel);
  937. $props['term'] = $term;
  938. switch ($this->format) {
  939. case 'react':
  940. $output = [
  941. 'props'=>base64_encode(\json_encode($props)),
  942. 'html'=>"",
  943. 'text'=>$props['word'],
  944. 'tag'=>'span',
  945. 'tpl'=>'grammar',
  946. ];
  947. break;
  948. case 'unity':
  949. $output = [
  950. 'props'=>base64_encode(\json_encode($props)),
  951. 'tpl'=>'grammar',
  952. ];
  953. break;
  954. default:
  955. $output = $props['word'];
  956. break;
  957. }
  958. return $output;
  959. }
  960. private function render_video(){
  961. $url = $this->get_param($this->param,"url",1);
  962. $style = $this->get_param($this->param,"style",2,'modal');
  963. $title = $this->get_param($this->param,"title",3);
  964. $props = [
  965. "url" => $url,
  966. 'title' => $title,
  967. 'style' => $style,
  968. ];
  969. switch ($this->format) {
  970. case 'react':
  971. $output = [
  972. 'props'=>base64_encode(\json_encode($props)),
  973. 'html'=>"",
  974. 'text'=>$props['title'],
  975. 'tag'=>'span',
  976. 'tpl'=>'video',
  977. ];
  978. break;
  979. case 'unity':
  980. $output = [
  981. 'props'=>base64_encode(\json_encode($props)),
  982. 'tpl'=>'video',
  983. ];
  984. break;
  985. default:
  986. $output = $props['title'];
  987. break;
  988. }
  989. return $output;
  990. }
  991. private function get_param(array $param,string $name,int $id,string $default=''){
  992. if(isset($param[$name])){
  993. return trim($param[$name]);
  994. }else if(isset($param["{$id}"])){
  995. return trim($param["{$id}"]);
  996. }else{
  997. return $default;
  998. }
  999. }
  1000. }