TemplateRender.php 31 KB

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