TemplateRender.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. if(count($tmpTitle)>1){
  656. $tmpBookTitle = $tmpTitle[0];
  657. $tmpBookPage = $tmpTitle[1];
  658. $tmpBookPage = (int)str_replace(
  659. ['၁','၂','၃','၄','၅','၆','၇','၈','၉','၀'],
  660. ['1','2','3','4','5','6','7','8','9','0'],
  661. $tmpBookPage);
  662. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  663. if($found_key !== false){
  664. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  665. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  666. $props['page'] = $tmpBookPage;
  667. if(!empty($props['bookName'])){
  668. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  669. if($found_title === false){
  670. $props['found'] = false;
  671. }
  672. }
  673. }else{
  674. //没找到,返回术语和页码
  675. $props['found'] = false;
  676. $props['bookName'] = $tmpBookTitle;
  677. $props['page'] = $tmpBookPage;
  678. $props['volume'] = 0;
  679. }
  680. }
  681. }else{
  682. $props['found'] = false;
  683. }
  684. if($book && $para){
  685. $props['book'] = $book;
  686. $props['para'] = $para;
  687. }
  688. if($title){
  689. $props['title'] = $title;
  690. }
  691. $text = '';
  692. if(isset($props['bookName'])){
  693. $searchField = '';
  694. switch ($type) {
  695. case 'm':
  696. $searchField = 'm_title';
  697. break;
  698. case 'p':
  699. $searchField = 'p_title';
  700. break;
  701. }
  702. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  703. if($found_title === false){
  704. $props['found'] = false;
  705. }
  706. $term = $this->getTermProps($props['bookName'],':quote:');
  707. $props['term'] = $term;
  708. if(isset($term['id'])){
  709. $props['bookNameLocal'] = $term['meaning'];
  710. $text .= $term['meaning'];
  711. }else{
  712. $text .= $bookName;
  713. }
  714. }
  715. if(isset($props['volume']) && isset($props['page'])){
  716. $text .= " {$volume}.{$page}";
  717. }
  718. switch ($this->format) {
  719. case 'react':
  720. $output = [
  721. 'props'=>base64_encode(\json_encode($props)),
  722. 'html'=>'',
  723. 'tag'=>'span',
  724. 'tpl'=>'quote-link',
  725. ];
  726. break;
  727. case 'unity':
  728. $output = [
  729. 'props'=>base64_encode(\json_encode($props)),
  730. 'tpl'=>'quote-link',
  731. ];
  732. break;
  733. default:
  734. $output = $text;
  735. break;
  736. }
  737. return $output;
  738. }
  739. private function pageInfoByPara($type,$book,$para){
  740. $output = array();
  741. $pageInfo = PageNumber::where('type',strtoupper($type))
  742. ->where('book',$book)
  743. ->where('paragraph','<=',$para)
  744. ->orderBy('paragraph','desc')
  745. ->first();
  746. if($pageInfo){
  747. foreach (BookTitle::get() as $value) {
  748. if($value['id']===$pageInfo->pcd_book_id){
  749. switch (strtoupper($type)) {
  750. case 'M':
  751. $key = 'm_title';
  752. break;
  753. case 'P':
  754. $key = 'p_title';
  755. break;
  756. case 'V':
  757. $key = 'v_title';
  758. break;
  759. default:
  760. $key = 'term';
  761. break;
  762. }
  763. $output['bookName'] = $value[$key];
  764. break;
  765. }
  766. }
  767. $output['volume'] = $pageInfo->volume;
  768. $output['page'] = $pageInfo->page;
  769. $output['found'] = true;
  770. }else{
  771. $output['found'] = false;
  772. }
  773. return $output;
  774. }
  775. private function render_sent(){
  776. $sid = $this->get_param($this->param,"sid",1);
  777. $channel = $this->get_param($this->param,"channel",2);
  778. if(!empty($channel)){
  779. $channels = explode(',',$channel);
  780. }else{
  781. $channels = $this->channel_id;
  782. }
  783. $sentInfo = explode('@',trim($sid));
  784. $sentId = $sentInfo[0];
  785. if(isset($sentInfo[1])){
  786. $channels = [$sentInfo[1]];
  787. }
  788. $Sent = new CorpusController();
  789. $props = $Sent->getSentTpl($sentId,$channels,
  790. $this->mode,true,
  791. $this->format);
  792. if($props === false){
  793. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  794. }
  795. if($this->mode==='read'){
  796. $tpl = "sentread";
  797. }else{
  798. $tpl = "sentedit";
  799. }
  800. switch ($this->format) {
  801. case 'react':
  802. $output = [
  803. 'props'=>base64_encode(\json_encode($props)),
  804. 'html'=>"",
  805. 'tag'=>'span',
  806. 'tpl'=>$tpl,
  807. ];
  808. break;
  809. case 'unity':
  810. $output = [
  811. 'props'=>base64_encode(\json_encode($props)),
  812. 'tpl'=>$tpl,
  813. ];
  814. break;
  815. case 'text':
  816. $output = '';
  817. if(isset($props['translation']) && is_array($props['translation'])){
  818. foreach ($props['translation'] as $key => $value) {
  819. $output .= $value['html'];
  820. }
  821. }
  822. break;
  823. case 'html':
  824. $output = '';
  825. if(isset($props['translation']) && is_array($props['translation'])){
  826. foreach ($props['translation'] as $key => $value) {
  827. $output .= '<span class="sentence">'.$value['html'].'</span>';
  828. }
  829. }
  830. break;
  831. case 'tex':
  832. $output = '';
  833. if(isset($props['translation']) && is_array($props['translation'])){
  834. foreach ($props['translation'] as $key => $value) {
  835. $output .= $value['html'];
  836. }
  837. }
  838. break;
  839. case 'simple':
  840. $output = '';
  841. if(isset($props['translation']) &&
  842. is_array($props['translation']) &&
  843. count($props['translation']) > 0
  844. ){
  845. $sentences = $props['translation'];
  846. foreach ($sentences as $key => $value) {
  847. $output .= $value['html'];
  848. }
  849. }
  850. if(empty($output)){
  851. if(isset($props['origin']) &&
  852. is_array($props['origin']) &&
  853. count($props['origin']) > 0
  854. ){
  855. $sentences = $props['origin'];
  856. foreach ($sentences as $key => $value) {
  857. $output .= $value['html'];
  858. }
  859. }
  860. }
  861. break;
  862. default:
  863. $output = '';
  864. break;
  865. }
  866. return $output;
  867. }
  868. private function render_mermaid(){
  869. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  870. $props = ["text" => implode("\n",$text)];
  871. switch ($this->format) {
  872. case 'react':
  873. $output = [
  874. 'props'=>base64_encode(\json_encode($props)),
  875. 'html'=>"mermaid",
  876. 'tag'=>'div',
  877. 'tpl'=>'mermaid',
  878. ];
  879. break;
  880. case 'unity':
  881. $output = [
  882. 'props'=>base64_encode(\json_encode($props)),
  883. 'tpl'=>'mermaid',
  884. ];
  885. break;
  886. case 'text':
  887. $output = 'mermaid';
  888. break;
  889. case 'tex':
  890. $output = 'mermaid';
  891. break;
  892. case 'simple':
  893. $output = 'mermaid';
  894. break;
  895. default:
  896. $output = 'mermaid';
  897. break;
  898. }
  899. return $output;
  900. }
  901. private function render_qa(){
  902. $id = $this->get_param($this->param,"id",1);
  903. $style = $this->get_param($this->param,"style",2);
  904. $props = [
  905. "type" => 'qa',
  906. "id" => $id,
  907. 'title' => '',
  908. 'style' => $style,
  909. ];
  910. $qa = Discussion::where('id',$id)->first();
  911. if($qa){
  912. $props['title'] = $qa->title;
  913. $props['resId'] = $qa->res_id;
  914. $props['resType'] = $qa->res_type;
  915. }
  916. switch ($this->format) {
  917. case 'react':
  918. $output = [
  919. 'props'=>base64_encode(\json_encode($props)),
  920. 'html'=>"",
  921. 'text'=>$props['title'],
  922. 'tag'=>'div',
  923. 'tpl'=>'qa',
  924. ];
  925. break;
  926. case 'unity':
  927. $output = [
  928. 'props'=>base64_encode(\json_encode($props)),
  929. 'tpl'=>'qa',
  930. ];
  931. break;
  932. default:
  933. $output = $props['title'];
  934. break;
  935. }
  936. return $output;
  937. }
  938. private function render_grammar_lookup(){
  939. $word = $this->get_param($this->param,"word",1);
  940. $props = ['word' => $word];
  941. $localTermChannel = ChannelApi::getSysChannel(
  942. "_System_Grammar_Term_".strtolower($this->lang)."_",
  943. "_System_Grammar_Term_en_"
  944. );
  945. $term = $this->getTermProps($word,null,$localTermChannel);
  946. $props['term'] = $term;
  947. switch ($this->format) {
  948. case 'react':
  949. $output = [
  950. 'props'=>base64_encode(\json_encode($props)),
  951. 'html'=>"",
  952. 'text'=>$props['word'],
  953. 'tag'=>'span',
  954. 'tpl'=>'grammar',
  955. ];
  956. break;
  957. case 'unity':
  958. $output = [
  959. 'props'=>base64_encode(\json_encode($props)),
  960. 'tpl'=>'grammar',
  961. ];
  962. break;
  963. default:
  964. $output = $props['word'];
  965. break;
  966. }
  967. return $output;
  968. }
  969. private function render_video(){
  970. $url = $this->get_param($this->param,"url",1);
  971. $style = $this->get_param($this->param,"style",2,'modal');
  972. $title = $this->get_param($this->param,"title",3);
  973. $props = [
  974. "url" => $url,
  975. 'title' => $title,
  976. 'style' => $style,
  977. ];
  978. switch ($this->format) {
  979. case 'react':
  980. $output = [
  981. 'props'=>base64_encode(\json_encode($props)),
  982. 'html'=>"",
  983. 'text'=>$props['title'],
  984. 'tag'=>'span',
  985. 'tpl'=>'video',
  986. ];
  987. break;
  988. case 'unity':
  989. $output = [
  990. 'props'=>base64_encode(\json_encode($props)),
  991. 'tpl'=>'video',
  992. ];
  993. break;
  994. default:
  995. $output = $props['title'];
  996. break;
  997. }
  998. return $output;
  999. }
  1000. private function get_param(array $param,string $name,int $id,string $default=''){
  1001. if(isset($param[$name])){
  1002. return trim($param[$name]);
  1003. }else if(isset($param["{$id}"])){
  1004. return trim($param["{$id}"]);
  1005. }else{
  1006. return $default;
  1007. }
  1008. }
  1009. }