TemplateRender.php 40 KB

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