TemplateRender.php 33 KB

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