TemplateRender.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. case 'markdown':
  324. if(isset($props["meaning"])){
  325. $key = 'term-'.$props["word"];
  326. if(isset($GLOBALS[$key])){
  327. $output = $props["meaning"];
  328. }else{
  329. $GLOBALS[$key] = 1;
  330. $output = $props["meaning"].'('.$props["word"].')';
  331. }
  332. }else{
  333. $output = $props["word"];
  334. }
  335. break;
  336. default:
  337. if(isset($props["meaning"])){
  338. $output = $props["meaning"];
  339. }else{
  340. $output = $props["word"];
  341. }
  342. break;
  343. }
  344. return $output;
  345. }
  346. private function render_note(){
  347. $note = $this->get_param($this->param,"text",1);
  348. $trigger = $this->get_param($this->param,"trigger",2);
  349. $props = ["note" => $note ];
  350. $innerString = "";
  351. if(!empty($trigger)){
  352. $props["trigger"] = $trigger;
  353. $innerString = $props["trigger"];
  354. }
  355. if($this->format==='unity'){
  356. $props["note"] = MdRender::render($props["note"],
  357. $this->channel_id,
  358. null,
  359. 'read',
  360. 'translation',
  361. 'markdown',
  362. 'unity'
  363. );
  364. }
  365. $output = $note;
  366. switch ($this->format) {
  367. case 'react':
  368. $output=[
  369. 'props'=>base64_encode(\json_encode($props)),
  370. 'html'=>$innerString,
  371. 'tag'=>'span',
  372. 'tpl'=>'note',
  373. ];
  374. break;
  375. case 'unity':
  376. $output=[
  377. 'props'=>base64_encode(\json_encode($props)),
  378. 'tpl'=>'note',
  379. ];
  380. break;
  381. case 'html':
  382. if(isset($GLOBALS['note_sn'])){
  383. $GLOBALS['note_sn']++;
  384. }else{
  385. $GLOBALS['note_sn'] = 1;
  386. $GLOBALS['note'] = array();
  387. }
  388. $GLOBALS['note'][] = [
  389. 'sn' => $GLOBALS['note_sn'],
  390. 'trigger' => $trigger,
  391. 'content' => MdRender::render($props["note"],
  392. $this->channel_id,
  393. null,
  394. 'read',
  395. 'translation',
  396. 'markdown',
  397. 'html'
  398. ),
  399. ];
  400. $link="<a href='#footnote-".$GLOBALS['note_sn']."' name='note-".$GLOBALS['note_sn']."'>";
  401. if(empty($trigger)){
  402. $output = $link. "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  403. }else{
  404. $output = $link . $trigger . "</a>";
  405. }
  406. break;
  407. case 'text':
  408. $output = $trigger;
  409. break;
  410. case 'tex':
  411. $output = $trigger;
  412. break;
  413. case 'simple':
  414. $output = '';
  415. break;
  416. case 'markdown':
  417. if(isset($GLOBALS['note_sn'])){
  418. $GLOBALS['note_sn']++;
  419. }else{
  420. $GLOBALS['note_sn'] = 1;
  421. }
  422. $content = MdRender::render(
  423. $props["note"],
  424. $this->channel_id,
  425. null,
  426. 'read',
  427. 'translation',
  428. 'markdown',
  429. 'markdown'
  430. );
  431. $output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  432. break;
  433. default:
  434. $output = '';
  435. break;
  436. }
  437. return $output;
  438. }
  439. private function render_nissaya(){
  440. $pali = $this->get_param($this->param,"pali",1);
  441. $meaning = $this->get_param($this->param,"meaning",2);
  442. $innerString = "";
  443. $props = [
  444. "pali" => $pali,
  445. "meaning" => $meaning,
  446. "lang" => $this->lang,
  447. ];
  448. switch ($this->format) {
  449. case 'react':
  450. $output = [
  451. 'props'=>base64_encode(\json_encode($props)),
  452. 'html'=>$innerString,
  453. 'tag'=>'span',
  454. 'tpl'=>'nissaya',
  455. ];
  456. break;
  457. case 'unity':
  458. $output = [
  459. 'props'=>base64_encode(\json_encode($props)),
  460. 'tpl'=>'nissaya',
  461. ];
  462. break;
  463. case 'text':
  464. $output = $pali.'၊'.$meaning;
  465. break;
  466. case 'tex':
  467. $output = $pali.'၊'.$meaning;
  468. break;
  469. case 'simple':
  470. $output = $pali.'၊'.$meaning;
  471. break;
  472. default:
  473. $output = $pali.'၊'.$meaning;
  474. break;
  475. }
  476. return $output;
  477. }
  478. private function render_exercise(){
  479. $id = $this->get_param($this->param,"id",1);
  480. $title = $this->get_param($this->param,"title",1);
  481. $props = [
  482. "id" => $id,
  483. "title" => $title,
  484. "channel" => $this->channel_id[0],
  485. ];
  486. switch ($this->format) {
  487. case 'react':
  488. $output = [
  489. 'props'=>base64_encode(\json_encode($props)),
  490. 'html'=>"",
  491. 'tag'=>'span',
  492. 'tpl'=>'exercise',
  493. ];
  494. break;
  495. case 'unity':
  496. $output = [
  497. 'props'=>base64_encode(\json_encode($props)),
  498. 'tpl'=>'exercise',
  499. ];
  500. break;
  501. case 'text':
  502. $output = $title;
  503. break;
  504. case 'tex':
  505. $output = $title;
  506. break;
  507. case 'simple':
  508. $output = $title;
  509. break;
  510. default:
  511. $output = '';
  512. break;
  513. }
  514. return $output;
  515. }
  516. private function render_article(){
  517. $type = $this->get_param($this->param,"type",1);
  518. $id = $this->get_param($this->param,"id",2);
  519. $title = $this->get_param($this->param,"title",3);
  520. $channel = $this->get_param($this->param,"channel",4);
  521. $style = $this->get_param($this->param,"style",5);
  522. $book = $this->get_param($this->param,"book",6);
  523. $paragraphs = $this->get_param($this->param,"paragraphs",7);
  524. $anthology = $this->get_param($this->param,"anthology",8);
  525. $props = [
  526. "type" => $type,
  527. "id" => $id,
  528. 'style' => $style,
  529. ];
  530. if(!empty($channel)){
  531. $props['channel'] = $channel;
  532. }
  533. if(!empty($title)){
  534. $props['title'] = $title;
  535. }
  536. if(!empty($book)){
  537. $props['book'] = $book;
  538. }
  539. if(!empty($paragraphs)){
  540. $props['paragraphs'] = $paragraphs;
  541. }
  542. if(!empty($anthology)){
  543. $props['anthology'] = $anthology;
  544. }
  545. if(is_array($this->channel_id)){
  546. $props['parentChannels'] = $this->channel_id;
  547. }
  548. switch ($this->format) {
  549. case 'react':
  550. $output = [
  551. 'props'=>base64_encode(\json_encode($props)),
  552. 'html'=>"",
  553. 'text'=>$title,
  554. 'tag'=>'span',
  555. 'tpl'=>'article',
  556. ];
  557. break;
  558. case 'unity':
  559. $output = [
  560. 'props'=>base64_encode(\json_encode($props)),
  561. 'tpl'=>'article',
  562. ];
  563. break;
  564. case 'text':
  565. $output = $title;
  566. break;
  567. case 'tex':
  568. $output = $title;
  569. break;
  570. case 'simple':
  571. $output = $title;
  572. break;
  573. default:
  574. $output = '';
  575. break;
  576. }
  577. return $output;
  578. }
  579. private function render_quote(){
  580. $paraId = $this->get_param($this->param,"para",1);
  581. $channelId = $this->channel_id[0];
  582. $props = RedisClusters::remember("/quote/{$channelId}/{$paraId}",
  583. config('mint.cache.expire'),
  584. function() use($paraId,$channelId){
  585. $para = \explode('-',$paraId);
  586. $output = [
  587. "paraId" => $paraId,
  588. "channel" => $channelId,
  589. "innerString" => $paraId,
  590. ];
  591. if(count($para)<2){
  592. return $output;
  593. }
  594. $PaliText = PaliText::where("book",$para[0])
  595. ->where("paragraph",$para[1])
  596. ->select(['toc','path'])
  597. ->first();
  598. if($PaliText){
  599. $output["pali"] = $PaliText->toc;
  600. $output["paliPath"] = \json_decode($PaliText->path);
  601. $output["innerString"]= $PaliText->toc;
  602. }
  603. return $output;
  604. });
  605. switch ($this->format) {
  606. case 'react':
  607. $output = [
  608. 'props'=>base64_encode(\json_encode($props)),
  609. 'html'=>$props["innerString"],
  610. 'tag'=>'span',
  611. 'tpl'=>'quote',
  612. ];
  613. break;
  614. case 'unity':
  615. $output = [
  616. 'props'=>base64_encode(\json_encode($props)),
  617. 'tpl'=>'quote',
  618. ];
  619. break;
  620. case 'text':
  621. $output = $props["innerString"];
  622. break;
  623. case 'tex':
  624. $output = $props["innerString"];
  625. break;
  626. case 'simple':
  627. $output = $props["innerString"];
  628. break;
  629. default:
  630. $output = $props["innerString"];
  631. break;
  632. }
  633. return $output;
  634. }
  635. private function render_quote_link(){
  636. $type = $this->get_param($this->param,"type",1);
  637. $title = $this->get_param($this->param,"title",6,'');
  638. $bookName = $this->get_param($this->param,"bookname",2,'');
  639. $volume = $this->get_param($this->param,"volume",3);
  640. $page = $this->get_param($this->param,"page",4,'');
  641. $style = $this->get_param($this->param,"style",5,'modal');
  642. $book = $this->get_param($this->param,"book",7,false);
  643. $para = $this->get_param($this->param,"para",8,false);
  644. $props = [
  645. 'type' => $type,
  646. 'style' => $style,
  647. 'found' => true,
  648. ];
  649. if(!empty($bookName) && $volume !== '' && !empty($page)){
  650. $props['bookName'] = $bookName;
  651. $props['volume'] = (int)$volume;
  652. $props['page'] = $page;
  653. $props['found'] = true;
  654. }else if($book && $para){
  655. /**
  656. * 没有指定书名,根据book para 查询
  657. */
  658. if($type==='c'){
  659. //按照章节名称显示
  660. $path = PaliTextApi::getChapterPath($book,$para);
  661. if($path){
  662. $path = json_decode($path,true);
  663. }
  664. if($path && is_array($path) && count($path)>2){
  665. $props['bookName'] = strtolower($path[0]['title']) ;
  666. $props['chapter'] = strtolower(end($path)['title']);
  667. $props['found'] = true;
  668. }else{
  669. $props['found'] = false;
  670. }
  671. }else{
  672. $pageInfo = $this->pageInfoByPara($type,$book,$para);
  673. if($pageInfo['found']){
  674. $props['bookName'] = $pageInfo['bookName'];
  675. $props['volume'] = $pageInfo['volume'];
  676. $props['page'] = $pageInfo['page'];
  677. $props['found'] = true;
  678. }else{
  679. $props['found'] = false;
  680. }
  681. }
  682. }else if($title){
  683. //没有书号用title查询
  684. //$tmpTitle = explode('။',$title);
  685. for ($i=mb_strlen($title,'UTF-8'); $i > 0 ; $i--) {
  686. $mTitle = mb_substr($title,0,$i);
  687. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  688. Log::debug('run',['title'=>$mTitle,'has'=>$has]);
  689. if($has !== false){
  690. $tmpBookTitle = $mTitle;
  691. $tmpBookPage = mb_substr($title,$i);
  692. $tmpBookPage = $this->mb_trim($tmpBookPage,'၊။');
  693. break;
  694. }
  695. }
  696. if(isset($tmpBookTitle)){
  697. Log::debug('book title found',['title'=>$tmpBookTitle,'page'=>$tmpBookPage]);
  698. //$tmpBookTitle = $tmpTitle[0];
  699. //$tmpBookPage = $tmpTitle[1];
  700. $tmpBookPage = (int)str_replace(
  701. ['၁','၂','၃','၄','၅','၆','၇','၈','၉','၀'],
  702. ['1','2','3','4','5','6','7','8','9','0'],
  703. $tmpBookPage);
  704. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  705. if($found_key !== false){
  706. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  707. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  708. $props['page'] = $tmpBookPage;
  709. if(!empty($props['bookName'])){
  710. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  711. if($found_title === false){
  712. $props['found'] = false;
  713. }
  714. }
  715. }else{
  716. //没找到,返回术语和页码
  717. $props['found'] = false;
  718. $props['bookName'] = $tmpBookTitle;
  719. $props['page'] = $tmpBookPage;
  720. $props['volume'] = 0;
  721. }
  722. }
  723. }else{
  724. Log::debug('book title not found');
  725. $props['found'] = false;
  726. }
  727. if($book && $para){
  728. $props['book'] = $book;
  729. $props['para'] = $para;
  730. }
  731. if($title){
  732. $props['title'] = $title;
  733. }
  734. $text = '';
  735. if(isset($props['bookName'])){
  736. $searchField = '';
  737. switch ($type) {
  738. case 'm':
  739. $searchField = 'm_title';
  740. break;
  741. case 'p':
  742. $searchField = 'p_title';
  743. break;
  744. }
  745. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  746. if($found_title === false){
  747. $props['found'] = false;
  748. }
  749. $term = $this->getTermProps($props['bookName'],':quote:');
  750. $props['term'] = $term;
  751. if(isset($term['id'])){
  752. $props['bookNameLocal'] = $term['meaning'];
  753. $text .= $term['meaning'];
  754. }else{
  755. $text .= $bookName;
  756. }
  757. }
  758. if(isset($props['volume']) && isset($props['page'])){
  759. $text .= " {$volume}.{$page}";
  760. }
  761. switch ($this->format) {
  762. case 'react':
  763. $output = [
  764. 'props'=>base64_encode(\json_encode($props)),
  765. 'html'=>'',
  766. 'tag'=>'span',
  767. 'tpl'=>'quote-link',
  768. ];
  769. break;
  770. case 'unity':
  771. $output = [
  772. 'props'=>base64_encode(\json_encode($props)),
  773. 'tpl'=>'quote-link',
  774. ];
  775. break;
  776. default:
  777. $output = $text;
  778. break;
  779. }
  780. return $output;
  781. }
  782. private function pageInfoByPara($type,$book,$para){
  783. $output = array();
  784. $pageInfo = PageNumber::where('type',strtoupper($type))
  785. ->where('book',$book)
  786. ->where('paragraph','<=',$para)
  787. ->orderBy('paragraph','desc')
  788. ->first();
  789. if($pageInfo){
  790. foreach (BookTitle::get() as $value) {
  791. if($value['id']===$pageInfo->pcd_book_id){
  792. switch (strtoupper($type)) {
  793. case 'M':
  794. $key = 'm_title';
  795. break;
  796. case 'P':
  797. $key = 'p_title';
  798. break;
  799. case 'V':
  800. $key = 'v_title';
  801. break;
  802. default:
  803. $key = 'term';
  804. break;
  805. }
  806. $output['bookName'] = $value[$key];
  807. break;
  808. }
  809. }
  810. $output['volume'] = $pageInfo->volume;
  811. $output['page'] = $pageInfo->page;
  812. $output['found'] = true;
  813. }else{
  814. $output['found'] = false;
  815. }
  816. return $output;
  817. }
  818. private function render_sent(){
  819. $sid = $this->get_param($this->param,"id",1);
  820. $channel = $this->get_param($this->param,"channel",2);
  821. $text = $this->get_param($this->param,"text",2,'both');
  822. if(!empty($channel)){
  823. $channels = explode(',',$channel);
  824. }else{
  825. $channels = $this->channel_id;
  826. }
  827. $sentInfo = explode('@',trim($sid));
  828. $sentId = $sentInfo[0];
  829. if(isset($sentInfo[1])){
  830. $channels = [$sentInfo[1]];
  831. }
  832. $Sent = new CorpusController();
  833. $props = $Sent->getSentTpl($sentId,$channels,
  834. $this->mode,true,
  835. $this->format);
  836. if($props === false){
  837. $props['error']="句子模版渲染错误。句子参数个数不符。应该是四个。";
  838. }
  839. if($this->mode==='read'){
  840. $tpl = "sentread";
  841. }else{
  842. $tpl = "sentedit";
  843. }
  844. switch ($this->format) {
  845. case 'react':
  846. $output = [
  847. 'props'=>base64_encode(\json_encode($props)),
  848. 'html'=>"",
  849. 'tag'=>'span',
  850. 'tpl'=>$tpl,
  851. ];
  852. break;
  853. case 'unity':
  854. $output = [
  855. 'props'=>base64_encode(\json_encode($props)),
  856. 'tpl'=>$tpl,
  857. ];
  858. break;
  859. case 'text':
  860. $output = '';
  861. if(isset($props['origin']) && is_array($props['origin'])){
  862. foreach ($props['origin'] as $key => $value) {
  863. $output .= $value['html'];
  864. }
  865. }
  866. if(isset($props['translation']) && is_array($props['translation'])){
  867. foreach ($props['translation'] as $key => $value) {
  868. $output .= $value['html'];
  869. }
  870. }
  871. break;
  872. case 'html':
  873. $output = '';
  874. $output .= '<span class="sentence">';
  875. if($text === 'both' || $text === 'origin'){
  876. if(isset($props['origin']) && is_array($props['origin'])){
  877. foreach ($props['origin'] as $key => $value) {
  878. $output .= '<span class="origin">'.$value['html'].'</span>';
  879. }
  880. }
  881. }
  882. if($text === 'both' || $text === 'translation'){
  883. if(isset($props['translation']) && is_array($props['translation'])){
  884. foreach ($props['translation'] as $key => $value) {
  885. $output .= '<span class="translation">'.$value['html'].'</span>';
  886. }
  887. }
  888. }
  889. $output .= '</span>';
  890. break;
  891. case 'tex':
  892. $output = '';
  893. if(isset($props['translation']) && is_array($props['translation'])){
  894. foreach ($props['translation'] as $key => $value) {
  895. $output .= $value['html'];
  896. }
  897. }
  898. break;
  899. case 'simple':
  900. $output = '';
  901. if($text === 'both' || $text === 'origin'){
  902. if(empty($output)){
  903. if(isset($props['origin']) &&
  904. is_array($props['origin']) &&
  905. count($props['origin']) > 0
  906. ){
  907. foreach ($props['origin'] as $key => $value) {
  908. $output .= $value['html'];
  909. }
  910. }
  911. }
  912. }
  913. if($text === 'both' || $text === 'translation'){
  914. if(isset($props['translation']) &&
  915. is_array($props['translation']) &&
  916. count($props['translation']) > 0
  917. ){
  918. foreach ($props['translation'] as $key => $value) {
  919. $output .= $value['html'];
  920. }
  921. }
  922. }
  923. break;
  924. case 'markdown':
  925. $output = '';
  926. if($text === 'both' || $text === 'origin'){
  927. if(isset($props['origin']) && is_array($props['origin'])){
  928. foreach ($props['origin'] as $key => $value) {
  929. $output .= "\n\n". $value['html'];
  930. }
  931. }
  932. }
  933. if($text === 'both' || $text === 'translation'){
  934. if(isset($props['translation']) && is_array($props['translation'])){
  935. foreach ($props['translation'] as $key => $value) {
  936. $output .= "\n\n". $value['html'];
  937. }
  938. }
  939. }
  940. break;
  941. default:
  942. $output = '';
  943. break;
  944. }
  945. return $output;
  946. }
  947. private function render_mermaid(){
  948. $text = json_decode(base64_decode($this->get_param($this->param,"text",1)));
  949. $props = ["text" => implode("\n",$text)];
  950. switch ($this->format) {
  951. case 'react':
  952. $output = [
  953. 'props'=>base64_encode(\json_encode($props)),
  954. 'html'=>"mermaid",
  955. 'tag'=>'div',
  956. 'tpl'=>'mermaid',
  957. ];
  958. break;
  959. case 'unity':
  960. $output = [
  961. 'props'=>base64_encode(\json_encode($props)),
  962. 'tpl'=>'mermaid',
  963. ];
  964. break;
  965. case 'text':
  966. $output = 'mermaid';
  967. break;
  968. case 'tex':
  969. $output = 'mermaid';
  970. break;
  971. case 'simple':
  972. $output = 'mermaid';
  973. break;
  974. default:
  975. $output = 'mermaid';
  976. break;
  977. }
  978. return $output;
  979. }
  980. private function render_qa(){
  981. $id = $this->get_param($this->param,"id",1);
  982. $style = $this->get_param($this->param,"style",2);
  983. $props = [
  984. "type" => 'qa',
  985. "id" => $id,
  986. 'title' => '',
  987. 'style' => $style,
  988. ];
  989. $qa = Discussion::where('id',$id)->first();
  990. if($qa){
  991. $props['title'] = $qa->title;
  992. $props['resId'] = $qa->res_id;
  993. $props['resType'] = $qa->res_type;
  994. }
  995. switch ($this->format) {
  996. case 'react':
  997. $output = [
  998. 'props'=>base64_encode(\json_encode($props)),
  999. 'html'=>"",
  1000. 'text'=>$props['title'],
  1001. 'tag'=>'div',
  1002. 'tpl'=>'qa',
  1003. ];
  1004. break;
  1005. case 'unity':
  1006. $output = [
  1007. 'props'=>base64_encode(\json_encode($props)),
  1008. 'tpl'=>'qa',
  1009. ];
  1010. break;
  1011. default:
  1012. $output = $props['title'];
  1013. break;
  1014. }
  1015. return $output;
  1016. }
  1017. private function render_grammar_lookup(){
  1018. $word = $this->get_param($this->param,"word",1);
  1019. $props = ['word' => $word];
  1020. $localTermChannel = ChannelApi::getSysChannel(
  1021. "_System_Grammar_Term_".strtolower($this->lang)."_",
  1022. "_System_Grammar_Term_en_"
  1023. );
  1024. $term = $this->getTermProps($word,null,$localTermChannel);
  1025. $props['term'] = $term;
  1026. switch ($this->format) {
  1027. case 'react':
  1028. $output = [
  1029. 'props'=>base64_encode(\json_encode($props)),
  1030. 'html'=>"",
  1031. 'text'=>$props['word'],
  1032. 'tag'=>'span',
  1033. 'tpl'=>'grammar',
  1034. ];
  1035. break;
  1036. case 'unity':
  1037. $output = [
  1038. 'props'=>base64_encode(\json_encode($props)),
  1039. 'tpl'=>'grammar',
  1040. ];
  1041. break;
  1042. default:
  1043. $output = $props['word'];
  1044. break;
  1045. }
  1046. return $output;
  1047. }
  1048. private function render_video(){
  1049. $url = $this->get_param($this->param,"url",1);
  1050. $style = $this->get_param($this->param,"style",2,'modal');
  1051. $title = $this->get_param($this->param,"title",3);
  1052. $props = [
  1053. "url" => $url,
  1054. 'title' => $title,
  1055. 'style' => $style,
  1056. ];
  1057. switch ($this->format) {
  1058. case 'react':
  1059. $output = [
  1060. 'props'=>base64_encode(\json_encode($props)),
  1061. 'html'=>"",
  1062. 'text'=>$props['title'],
  1063. 'tag'=>'span',
  1064. 'tpl'=>'video',
  1065. ];
  1066. break;
  1067. case 'unity':
  1068. $output = [
  1069. 'props'=>base64_encode(\json_encode($props)),
  1070. 'tpl'=>'video',
  1071. ];
  1072. break;
  1073. default:
  1074. $output = $props['title'];
  1075. break;
  1076. }
  1077. return $output;
  1078. }
  1079. private function get_param(array $param,string $name,int $id,string $default=''){
  1080. if(isset($param[$name])){
  1081. return trim($param[$name]);
  1082. }else if(isset($param["{$id}"])){
  1083. return trim($param["{$id}"]);
  1084. }else{
  1085. return $default;
  1086. }
  1087. }
  1088. private function mb_trim($str,string $character_mask = ' ', $charset = "UTF-8") {
  1089. $start = 0;
  1090. $end = mb_strlen($str, $charset) - 1;
  1091. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1092. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset),$chars)) {
  1093. $start++;
  1094. }
  1095. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset),$chars) ) {
  1096. $end--;
  1097. }
  1098. return mb_substr($str, $start, $end - $start + 1, $charset);
  1099. }
  1100. }