TemplateRender.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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\Models\BookTitle as BookSeries;
  13. use App\Http\Controllers\CorpusController;
  14. use App\Http\Api\ChannelApi;
  15. use App\Http\Api\MdRender;
  16. use App\Http\Api\PaliTextApi;
  17. use App\Tools\Tools;
  18. use App\Services\ArticleService;
  19. class TemplateRender
  20. {
  21. protected $param = [];
  22. protected $mode = "read";
  23. protected $channel_id = [];
  24. protected $debug = [];
  25. protected $format = 'react';
  26. protected $studioId = null;
  27. protected $lang = 'en';
  28. protected $langFamily = 'en';
  29. protected $glossaryKey = 'glossary';
  30. protected $channelInfo = [];
  31. protected $options = [
  32. 'mode' => 'read',
  33. 'channelType' => 'translation',
  34. 'contentType' => "markdown",
  35. 'format' => 'react',
  36. 'debug' => [],
  37. 'studioId' => null,
  38. 'lang' => 'zh-Hans',
  39. 'footnote' => false,
  40. 'paragraph' => false,
  41. 'origin' => true,
  42. 'translation' => true,
  43. ];
  44. /**
  45. * Create a new command instance.
  46. * string $mode 'read' | 'edit'
  47. * string $format 'react' | 'text' | 'tex' | 'unity'
  48. * @return void
  49. */
  50. public function __construct($param, $channelInfo, $mode, $format = 'react', $studioId = '', $debug = [], $lang = 'zh-Hans')
  51. {
  52. $this->param = $param;
  53. foreach ($channelInfo as $value) {
  54. $this->channel_id[] = $value->uid;
  55. }
  56. $this->channelInfo = $channelInfo;
  57. $this->mode = $mode;
  58. $this->format = $format;
  59. $this->studioId = $studioId;
  60. $this->debug = $debug;
  61. $this->glossaryKey = 'glossary';
  62. if (count($this->channel_id) > 0) {
  63. $channelId = $this->channel_id[0];
  64. if (Str::isUuid($channelId)) {
  65. $lang = Channel::where('uid', $channelId)->value('lang');
  66. }
  67. }
  68. if (!empty($lang)) {
  69. $this->lang = $lang;
  70. $this->langFamily = explode('-', $lang)[0];
  71. }
  72. }
  73. public function options($options = [])
  74. {
  75. foreach ($options as $key => $value) {
  76. $this->options[$key] = $value;
  77. }
  78. }
  79. public function glossaryKey()
  80. {
  81. return $this->glossaryKey;
  82. }
  83. /**
  84. * TODO 设置默认语言。在渲染某些内容的时候需要语言信息
  85. */
  86. public function setLang($lang)
  87. {
  88. $this->lang = $lang;
  89. $this->langFamily = explode('-', $lang)[0];
  90. }
  91. private function info($message, $debug)
  92. {
  93. if (in_array($debug, $this->debug)) {
  94. Log::info($message);
  95. }
  96. }
  97. private function error($message, $debug)
  98. {
  99. if (in_array($debug, $this->debug)) {
  100. Log::error($message);
  101. }
  102. }
  103. public function render($tpl_name)
  104. {
  105. switch ($tpl_name) {
  106. case 'term':
  107. # 术语
  108. $result = $this->render_term();
  109. break;
  110. case 'note':
  111. $result = $this->render_note();
  112. break;
  113. case 'sent':
  114. $result = $this->render_sent();
  115. break;
  116. case 'quote':
  117. $result = $this->render_quote();
  118. break;
  119. case 'ql':
  120. $result = $this->render_quote_link();
  121. break;
  122. case 'exercise':
  123. $result = $this->render_exercise();
  124. break;
  125. case 'article':
  126. $result = $this->render_article();
  127. break;
  128. case 'nissaya':
  129. $result = $this->render_nissaya();
  130. break;
  131. case 'mermaid':
  132. $result = $this->render_mermaid();
  133. break;
  134. case 'qa':
  135. $result = $this->render_qa();
  136. break;
  137. case 'v':
  138. $result = $this->render_video();
  139. break;
  140. case 'g':
  141. $result = $this->render_grammar_lookup();
  142. break;
  143. case 'ref':
  144. $result = $this->render_ref();
  145. break;
  146. case 'dict-pref':
  147. $result = $this->render_dict_pref();
  148. break;
  149. case 'ai':
  150. $result = $this->render_ai();
  151. break;
  152. case 'para':
  153. $result = $this->render_para();
  154. break;
  155. default:
  156. if (mb_substr($tpl_name, 0, 4, "UTF-8") === 'Tpl:') {
  157. $result = $this->render_tpl($tpl_name);
  158. } else {
  159. $result = [
  160. 'props' => base64_encode(\json_encode([])),
  161. 'html' => '',
  162. 'tag' => 'span',
  163. 'tpl' => 'unknown',
  164. ];
  165. }
  166. break;
  167. }
  168. return $result;
  169. }
  170. public function render_tpl($name)
  171. {
  172. $article = app(ArticleService::class)->getRawByTitle($name);
  173. $content = $article->content;
  174. if (count($this->param) > 0) {
  175. $m = new \Mustache_Engine(array(
  176. 'entity_flags' => ENT_QUOTES,
  177. 'escape' => function ($value) {
  178. return $value;
  179. }
  180. ));
  181. $content = $m->render($content, $this->param);
  182. }
  183. $output = [];
  184. switch ($this->format) {
  185. case 'react':
  186. $output = [
  187. 'props' => base64_encode(\json_encode(['content' => $content])),
  188. 'html' => $content,
  189. 'tag' => 'span',
  190. 'tpl' => 'tpl',
  191. ];
  192. break;
  193. default:
  194. $output = $content;
  195. break;
  196. }
  197. return $output;
  198. }
  199. public function render_para()
  200. {
  201. $props = [];
  202. $props['id'] = $this->get_param($this->param, "id", 1);
  203. $props['title'] = $this->get_param($this->param, "title", 2);
  204. $props['style'] = $this->get_param($this->param, "style", 3);
  205. $output = [];
  206. switch ($this->format) {
  207. case 'react':
  208. $output = [
  209. 'props' => base64_encode(\json_encode($props)),
  210. 'html' => $props['title'],
  211. 'tag' => 'span',
  212. 'tpl' => 'para',
  213. ];
  214. break;
  215. default:
  216. $output = $props['title'];
  217. break;
  218. }
  219. return $output;
  220. }
  221. public function getTermProps($word, $tag = null, $channel = null)
  222. {
  223. if ($channel && !empty($channel)) {
  224. $channelId = $channel;
  225. } else {
  226. if (count($this->channel_id) > 0) {
  227. $channelId = $this->channel_id[0];
  228. } else {
  229. $channelId = null;
  230. }
  231. }
  232. if (count($this->channelInfo) === 0) {
  233. if (!empty($channel)) {
  234. $channelInfo = Channel::where('uid', $channel)->first();
  235. if (!$channelInfo) {
  236. unset($channelInfo);
  237. }
  238. }
  239. if (!isset($channelInfo)) {
  240. Log::warning('channel is null');
  241. $output = [
  242. "word" => $word,
  243. 'innerHtml' => '',
  244. ];
  245. return $output;
  246. }
  247. } else {
  248. $channelInfo = $this->channelInfo[0];
  249. }
  250. if (Str::isUuid($channelId)) {
  251. $lang = Channel::where('uid', $channelId)->value('lang');
  252. if (!empty($lang)) {
  253. $langFamily = explode('-', $lang)[0];
  254. } else {
  255. $langFamily = 'zh';
  256. }
  257. $this->info("term:{$word} 先查属于这个channel 的", 'term');
  258. $this->info('channel id' . $channelId, 'term');
  259. $table = DhammaTerm::where("word", $word)
  260. ->where('channal', $channelId);
  261. if ($tag && !empty($tag)) {
  262. $table = $table->where('tag', $tag);
  263. }
  264. $tplParam = $table->orderBy('updated_at', 'desc')
  265. ->first();
  266. $studioId = $channelInfo->owner_uid;
  267. } else {
  268. $tplParam = false;
  269. $lang = '';
  270. $langFamily = '';
  271. $studioId = $this->studioId;
  272. }
  273. if (!$tplParam) {
  274. if (Str::isUuid($studioId)) {
  275. /**
  276. * 没有,再查这个studio的
  277. * 按照语言过滤
  278. * 完全匹配的优先
  279. * 语族匹配也行
  280. */
  281. $this->info("没有-再查这个studio的", 'term');
  282. $table = DhammaTerm::where("word", $word);
  283. if (!empty($tag)) {
  284. $table = $table->where('tag', $tag);
  285. }
  286. $termsInStudio = $table->where('owner', $channelInfo->owner_uid)
  287. ->orderBy('updated_at', 'desc')
  288. ->get();
  289. if (count($termsInStudio) > 0) {
  290. $list = array();
  291. foreach ($termsInStudio as $key => $term) {
  292. if (empty($term->channal)) {
  293. if ($term->language === $lang) {
  294. $list[$term->guid] = 2;
  295. } else if (strpos($term->language, $langFamily) !== false) {
  296. $list[$term->guid] = 1;
  297. }
  298. }
  299. }
  300. if (count($list) > 0) {
  301. arsort($list);
  302. foreach ($list as $key => $one) {
  303. foreach ($termsInStudio as $term) {
  304. if ($term->guid === $key) {
  305. $tplParam = $term;
  306. break;
  307. }
  308. }
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. if (!$tplParam) {
  316. $this->info("没有,再查社区", 'term');
  317. $community_channel = ChannelApi::getSysChannel("_community_term_zh-hans_");
  318. $table = DhammaTerm::where("word", $word);
  319. if (!empty($tag)) {
  320. $table = $table->where('tag', $tag);
  321. }
  322. $tplParam = $table->where('channal', $community_channel)
  323. ->first();
  324. if ($tplParam) {
  325. $isCommunity = true;
  326. } else {
  327. $this->info("查社区没有", 'term');
  328. }
  329. }
  330. $output = [
  331. "word" => $word,
  332. "parentChannelId" => $channelId,
  333. "parentStudioId" => $channelInfo->owner_uid,
  334. ];
  335. $innerString = $output["word"];
  336. if ($tplParam) {
  337. $output["id"] = $tplParam->guid;
  338. $output["meaning"] = $tplParam->meaning;
  339. $output["channel"] = $tplParam->channal;
  340. if (!empty($tplParam->note)) {
  341. $mdRender = new MdRender(['format' => $this->format]);
  342. $output['note'] = $mdRender->convert($tplParam->note, $this->channel_id);
  343. }
  344. if (isset($isCommunity)) {
  345. $output["isCommunity"] = true;
  346. }
  347. $innerString = "{$output["meaning"]}({$output["word"]})";
  348. if (!empty($tplParam->other_meaning)) {
  349. $output["meaning2"] = $tplParam->other_meaning;
  350. }
  351. }
  352. $output['innerHtml'] = $innerString;
  353. return $output;
  354. }
  355. private function render_term()
  356. {
  357. $word = $this->get_param($this->param, "word", 1);
  358. $props = $this->getTermProps($word, '');
  359. $output = $props['word'];
  360. switch ($this->format) {
  361. case 'react':
  362. $output = [
  363. 'props' => base64_encode(\json_encode($props)),
  364. 'html' => $props['innerHtml'],
  365. 'tag' => 'span',
  366. 'tpl' => 'term',
  367. ];
  368. break;
  369. case 'unity':
  370. $output = [
  371. 'props' => base64_encode(\json_encode($props)),
  372. 'tpl' => 'term',
  373. ];
  374. break;
  375. case 'html':
  376. if (isset($props["meaning"])) {
  377. $GLOBALS[$this->glossaryKey][$props["word"]] = $props['meaning'];
  378. $key = 'term-' . $props["word"];
  379. $termHead = "<a href='#'>" . $props['meaning'] . "</a>";
  380. if (isset($GLOBALS[$key])) {
  381. $output = $termHead;
  382. } else {
  383. $GLOBALS[$key] = 1;
  384. $output = $termHead . '(<em>' . $props["word"] . '</em>)';
  385. }
  386. } else {
  387. $output = $props["word"];
  388. }
  389. break;
  390. case 'text':
  391. if (isset($props["meaning"])) {
  392. $key = 'term-' . $props["word"];
  393. if (isset($GLOBALS[$key])) {
  394. $output = $props["meaning"];
  395. } else {
  396. $GLOBALS[$key] = 1;
  397. $output = $props["meaning"] . '(' . $props["word"] . ')';
  398. }
  399. } else {
  400. $output = $props["word"];
  401. }
  402. break;
  403. case 'tex':
  404. if (isset($props["meaning"])) {
  405. $key = 'term-' . $props["word"];
  406. if (isset($GLOBALS[$key])) {
  407. $output = $props["meaning"];
  408. } else {
  409. $GLOBALS[$key] = 1;
  410. $output = $props["meaning"] . '(' . $props["word"] . ')';
  411. }
  412. } else {
  413. $output = $props["word"];
  414. }
  415. break;
  416. case 'simple':
  417. if (isset($props["meaning"])) {
  418. $output = $props["meaning"];
  419. } else {
  420. $output = $props["word"];
  421. }
  422. break;
  423. case 'markdown':
  424. if (isset($props["meaning"])) {
  425. $key = 'term-' . $props["word"];
  426. if (isset($GLOBALS[$key]) && $GLOBALS[$key] === 1) {
  427. $GLOBALS[$key]++;
  428. $output = $props["meaning"];
  429. } else {
  430. $GLOBALS[$key] = 1;
  431. $output = $props["meaning"] . '(' . $props["word"] . ')';
  432. }
  433. } else {
  434. $output = $props["word"];
  435. }
  436. //如果有内容且第一次出现,显示为脚注
  437. if (!empty($props["note"]) && $GLOBALS[$key] === 1) {
  438. if (isset($GLOBALS['note_sn'])) {
  439. $GLOBALS['note_sn']++;
  440. } else {
  441. $GLOBALS['note_sn'] = 1;
  442. $GLOBALS['note'] = array();
  443. }
  444. $content = $props["note"];
  445. $output .= '[^' . $GLOBALS['note_sn'] . ']';
  446. $GLOBALS['note'][] = [
  447. 'sn' => $GLOBALS['note_sn'],
  448. 'trigger' => '',
  449. 'content' => $content,
  450. ];
  451. }
  452. break;
  453. default:
  454. if (isset($props["meaning"])) {
  455. $output = $props["meaning"];
  456. } else {
  457. $output = $props["word"];
  458. }
  459. break;
  460. }
  461. return $output;
  462. }
  463. private function render_note()
  464. {
  465. $note = $this->get_param($this->param, "text", 1);
  466. $trigger = $this->get_param($this->param, "trigger", 2);
  467. $props = ["note" => $note];
  468. $innerString = "";
  469. if (!empty($trigger)) {
  470. $props["trigger"] = $trigger;
  471. $innerString = $props["trigger"];
  472. }
  473. if ($this->format === 'unity') {
  474. $props["note"] = MdRender::render(
  475. $props["note"],
  476. $this->channel_id,
  477. null,
  478. 'read',
  479. 'translation',
  480. 'markdown',
  481. 'unity'
  482. );
  483. }
  484. $output = $note;
  485. switch ($this->format) {
  486. case 'react':
  487. $output = [
  488. 'props' => base64_encode(\json_encode($props)),
  489. 'html' => $innerString,
  490. 'tag' => 'span',
  491. 'tpl' => 'note',
  492. ];
  493. break;
  494. case 'unity':
  495. $output = [
  496. 'props' => base64_encode(\json_encode($props)),
  497. 'tpl' => 'note',
  498. ];
  499. break;
  500. case 'html':
  501. if (isset($GLOBALS['note_sn'])) {
  502. $GLOBALS['note_sn']++;
  503. } else {
  504. $GLOBALS['note_sn'] = 1;
  505. $GLOBALS['note'] = array();
  506. }
  507. $GLOBALS['note'][] = [
  508. 'sn' => $GLOBALS['note_sn'],
  509. 'trigger' => $trigger,
  510. 'content' => MdRender::render(
  511. $props["note"],
  512. $this->channel_id,
  513. null,
  514. 'read',
  515. 'translation',
  516. 'markdown',
  517. 'html'
  518. ),
  519. ];
  520. $link = "<a href='#footnote-" . $GLOBALS['note_sn'] . "' name='note-" . $GLOBALS['note_sn'] . "'>";
  521. if (empty($trigger)) {
  522. $output = $link . "<sup>[" . $GLOBALS['note_sn'] . "]</sup></a>";
  523. } else {
  524. $output = $link . $trigger . "</a>";
  525. }
  526. break;
  527. case 'text':
  528. $output = $trigger;
  529. break;
  530. case 'tex':
  531. $output = $trigger;
  532. break;
  533. case 'simple':
  534. $output = '';
  535. break;
  536. case 'markdown':
  537. if (isset($GLOBALS['note_sn'])) {
  538. $GLOBALS['note_sn']++;
  539. } else {
  540. $GLOBALS['note_sn'] = 1;
  541. $GLOBALS['note'] = array();
  542. }
  543. $content = MdRender::render(
  544. $props["note"],
  545. $this->channel_id,
  546. null,
  547. 'read',
  548. 'translation',
  549. 'markdown',
  550. 'markdown'
  551. );
  552. $output = '[^' . $GLOBALS['note_sn'] . ']';
  553. $GLOBALS['note'][] = [
  554. 'sn' => $GLOBALS['note_sn'],
  555. 'trigger' => $trigger,
  556. 'content' => $content,
  557. ];
  558. //$output = '<footnote id="'.$GLOBALS['note_sn'].'">'.$content.'</footnote>';
  559. break;
  560. default:
  561. $output = '';
  562. break;
  563. }
  564. return $output;
  565. }
  566. private function render_nissaya()
  567. {
  568. $pali = $this->get_param($this->param, "pali", 1);
  569. $meaning = $this->get_param($this->param, "meaning", 2);
  570. $innerString = "";
  571. $props = [
  572. "pali" => $pali,
  573. "meaning" => explode('=', $meaning),
  574. "lang" => $this->lang,
  575. ];
  576. switch ($this->format) {
  577. case 'react':
  578. $output = [
  579. 'props' => base64_encode(\json_encode($props)),
  580. 'html' => $innerString,
  581. 'tag' => 'span',
  582. 'tpl' => 'nissaya',
  583. ];
  584. break;
  585. case 'unity':
  586. $output = [
  587. 'props' => base64_encode(\json_encode($props)),
  588. 'tpl' => 'nissaya',
  589. ];
  590. break;
  591. case 'prompt':
  592. $output = Tools::MyToRm($pali) . ':' . end($props["meaning"]);
  593. break;
  594. default:
  595. $output = $pali . '၊' . $meaning;
  596. break;
  597. }
  598. return $output;
  599. }
  600. private function render_exercise()
  601. {
  602. $id = $this->get_param($this->param, "id", 1);
  603. $title = $this->get_param($this->param, "title", 1);
  604. $props = [
  605. "id" => $id,
  606. "title" => $title,
  607. "channel" => $this->channel_id[0],
  608. ];
  609. switch ($this->format) {
  610. case 'react':
  611. $output = [
  612. 'props' => base64_encode(\json_encode($props)),
  613. 'html' => "",
  614. 'tag' => 'span',
  615. 'tpl' => 'exercise',
  616. ];
  617. break;
  618. case 'unity':
  619. $output = [
  620. 'props' => base64_encode(\json_encode($props)),
  621. 'tpl' => 'exercise',
  622. ];
  623. break;
  624. case 'text':
  625. $output = $title;
  626. break;
  627. case 'tex':
  628. $output = $title;
  629. break;
  630. case 'simple':
  631. $output = $title;
  632. break;
  633. default:
  634. $output = '';
  635. break;
  636. }
  637. return $output;
  638. }
  639. private function render_article()
  640. {
  641. $type = $this->get_param($this->param, "type", 1);
  642. $id = $this->get_param($this->param, "id", 2);
  643. $title = $this->get_param($this->param, "title", 3);
  644. $channel = $this->get_param($this->param, "channel", 4);
  645. $style = $this->get_param($this->param, "style", 5);
  646. $book = $this->get_param($this->param, "book", 6);
  647. $paragraphs = $this->get_param($this->param, "paragraphs", 7);
  648. $anthology = $this->get_param($this->param, "anthology", 8);
  649. if ($type === 'chapter' && empty($id)) {
  650. $book = (int)$book;
  651. $paragraphs = (int)$paragraphs;
  652. $id = "{$book}-{$paragraphs}";
  653. }
  654. $props = [
  655. "type" => $type,
  656. "id" => $id,
  657. 'style' => $style,
  658. ];
  659. if (!empty($channel)) {
  660. $props['channel'] = $channel;
  661. }
  662. if (!empty($title)) {
  663. $props['title'] = $title;
  664. }
  665. if (!empty($book)) {
  666. $props['book'] = $book;
  667. }
  668. if (!empty($paragraphs)) {
  669. $props['paragraphs'] = $paragraphs;
  670. }
  671. if (!empty($anthology)) {
  672. $props['anthology'] = $anthology;
  673. }
  674. if (is_array($this->channel_id)) {
  675. $props['parentChannels'] = $this->channel_id;
  676. }
  677. switch ($this->format) {
  678. case 'react':
  679. $output = [
  680. 'props' => base64_encode(\json_encode($props)),
  681. 'html' => "",
  682. 'text' => $title,
  683. 'tag' => 'span',
  684. 'tpl' => 'article',
  685. ];
  686. break;
  687. case 'unity':
  688. $output = [
  689. 'props' => base64_encode(\json_encode($props)),
  690. 'tpl' => 'article',
  691. ];
  692. break;
  693. case 'text':
  694. $output = $title;
  695. break;
  696. case 'tex':
  697. $output = $title;
  698. break;
  699. case 'simple':
  700. $output = $title;
  701. break;
  702. default:
  703. $output = '';
  704. break;
  705. }
  706. return $output;
  707. }
  708. private function render_quote()
  709. {
  710. $paraId = $this->get_param($this->param, "para", 1);
  711. $channelId = $this->channel_id[0];
  712. $props = Cache::remember(
  713. "/quote/{$channelId}/{$paraId}",
  714. config('mint.cache.expire'),
  715. function () use ($paraId, $channelId) {
  716. $para = \explode('-', $paraId);
  717. $output = [
  718. "paraId" => $paraId,
  719. "channel" => $channelId,
  720. "innerString" => $paraId,
  721. ];
  722. if (count($para) < 2) {
  723. return $output;
  724. }
  725. $PaliText = PaliText::where("book", $para[0])
  726. ->where("paragraph", $para[1])
  727. ->select(['toc', 'path'])
  728. ->first();
  729. if ($PaliText) {
  730. $output["pali"] = $PaliText->toc;
  731. $output["paliPath"] = \json_decode($PaliText->path);
  732. $output["innerString"] = $PaliText->toc;
  733. }
  734. return $output;
  735. }
  736. );
  737. switch ($this->format) {
  738. case 'react':
  739. $output = [
  740. 'props' => base64_encode(\json_encode($props)),
  741. 'html' => $props["innerString"],
  742. 'tag' => 'span',
  743. 'tpl' => 'quote',
  744. ];
  745. break;
  746. case 'unity':
  747. $output = [
  748. 'props' => base64_encode(\json_encode($props)),
  749. 'tpl' => 'quote',
  750. ];
  751. break;
  752. case 'text':
  753. $output = $props["innerString"];
  754. break;
  755. case 'tex':
  756. $output = $props["innerString"];
  757. break;
  758. case 'simple':
  759. $output = $props["innerString"];
  760. break;
  761. default:
  762. $output = $props["innerString"];
  763. break;
  764. }
  765. return $output;
  766. }
  767. private function render_quote_link()
  768. {
  769. $type = $this->get_param($this->param, "type", 1);
  770. $title = $this->get_param($this->param, "title", 6, '');
  771. $bookName = $this->get_param($this->param, "bookname", 2, '');
  772. $volume = $this->get_param($this->param, "volume", 3);
  773. $page = $this->get_param($this->param, "page", 4, '');
  774. $style = $this->get_param($this->param, "style", 5, 'modal');
  775. $book = $this->get_param($this->param, "book", 7, false);
  776. $para = $this->get_param($this->param, "para", 8, false);
  777. $props = [
  778. 'type' => $type,
  779. 'style' => $style,
  780. 'found' => true,
  781. ];
  782. if (!empty($bookName) && $volume !== '' && !empty($page)) {
  783. $props['bookName'] = $bookName;
  784. $props['volume'] = (int)$volume;
  785. $props['page'] = $page;
  786. $props['found'] = true;
  787. } else if ($book && $para) {
  788. /**
  789. * 没有指定书名,根据book para 查询
  790. */
  791. if ($type === 'c') {
  792. //按照章节名称显示
  793. $path = PaliTextApi::getChapterPath($book, $para);
  794. if ($path) {
  795. $path = json_decode($path, true);
  796. }
  797. if ($path && is_array($path) && count($path) > 2) {
  798. $props['bookName'] = strtolower($path[0]['title']);
  799. $props['chapter'] = strtolower(end($path)['title']);
  800. $props['found'] = true;
  801. } else {
  802. $props['found'] = false;
  803. }
  804. } else {
  805. $pageInfo = $this->pageInfoByPara($type, $book, $para);
  806. if ($pageInfo['found']) {
  807. $props['bookName'] = $pageInfo['bookName'];
  808. $props['volume'] = $pageInfo['volume'];
  809. $props['page'] = $pageInfo['page'];
  810. $props['found'] = true;
  811. } else {
  812. $props['found'] = false;
  813. }
  814. }
  815. } else if ($title) {
  816. //没有书号用title查询
  817. //$tmpTitle = explode('။',$title);
  818. for ($i = mb_strlen($title, 'UTF-8'); $i > 0; $i--) {
  819. $mTitle = mb_substr($title, 0, $i);
  820. $has = array_search($mTitle, array_column(BookTitle::my(), 'title2'));
  821. Log::debug('run', ['title' => $mTitle, 'has' => $has]);
  822. if ($has !== false) {
  823. $tmpBookTitle = $mTitle;
  824. $tmpBookPage = mb_substr($title, $i);
  825. $tmpBookPage = $this->mb_trim($tmpBookPage, '၊။');
  826. break;
  827. }
  828. }
  829. if (isset($tmpBookTitle)) {
  830. Log::debug('book title found', ['title' => $tmpBookTitle, 'page' => $tmpBookPage]);
  831. //$tmpBookTitle = $tmpTitle[0];
  832. //$tmpBookPage = $tmpTitle[1];
  833. $tmpBookPage = (int)str_replace(
  834. ['၁', '၂', '၃', '၄', '၅', '၆', '၇', '၈', '၉', '၀'],
  835. ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  836. $tmpBookPage
  837. );
  838. $found_key = array_search($tmpBookTitle, array_column(BookTitle::my(), 'title2'));
  839. if ($found_key !== false) {
  840. $props['bookName'] = BookTitle::my()[$found_key]['bookname'];
  841. $props['volume'] = BookTitle::my()[$found_key]['volume'];
  842. $props['page'] = $tmpBookPage;
  843. if (!empty($props['bookName'])) {
  844. $found_title = array_search($props['bookName'], array_column(BookTitle::my(), 'bookname'));
  845. if ($found_title === false) {
  846. $props['found'] = false;
  847. }
  848. }
  849. } else {
  850. //没找到,返回术语和页码
  851. $props['found'] = false;
  852. $props['bookName'] = $tmpBookTitle;
  853. $props['page'] = $tmpBookPage;
  854. $props['volume'] = 0;
  855. }
  856. }
  857. } else {
  858. Log::debug('book title not found');
  859. $props['found'] = false;
  860. }
  861. if ($book && $para) {
  862. $props['book'] = $book;
  863. $props['para'] = $para;
  864. }
  865. if ($title) {
  866. $props['title'] = $title;
  867. }
  868. $text = '';
  869. if (isset($props['bookName'])) {
  870. $searchField = '';
  871. switch ($type) {
  872. case 'm':
  873. $searchField = 'm_title';
  874. break;
  875. case 'p':
  876. $searchField = 'p_title';
  877. break;
  878. }
  879. $found_title = array_search($props['bookName'], array_column(BookTitle::get(), $searchField));
  880. if ($found_title === false) {
  881. $props['found'] = false;
  882. }
  883. $term = $this->getTermProps($props['bookName'], ':quote:');
  884. $props['term'] = $term;
  885. if (isset($term['id'])) {
  886. $props['bookNameLocal'] = $term['meaning'];
  887. $text .= $term['meaning'];
  888. } else {
  889. $text .= $bookName;
  890. }
  891. }
  892. if (isset($props['volume']) && isset($props['page'])) {
  893. $text .= " {$volume}.{$page}";
  894. }
  895. switch ($this->format) {
  896. case 'react':
  897. $output = [
  898. 'props' => base64_encode(\json_encode($props)),
  899. 'html' => '',
  900. 'tag' => 'span',
  901. 'tpl' => 'quote-link',
  902. ];
  903. break;
  904. case 'unity':
  905. $output = [
  906. 'props' => base64_encode(\json_encode($props)),
  907. 'tpl' => 'quote-link',
  908. ];
  909. break;
  910. default:
  911. $output = $text;
  912. break;
  913. }
  914. return $output;
  915. }
  916. private function pageInfoByPara($type, $book, $para)
  917. {
  918. $output = array();
  919. $pageInfo = PageNumber::where('type', strtoupper($type))
  920. ->where('book', $book)
  921. ->where('paragraph', '<=', $para)
  922. ->orderBy('paragraph', 'desc')
  923. ->first();
  924. if ($pageInfo) {
  925. foreach (BookTitle::get() as $value) {
  926. if ($value['id'] === $pageInfo->pcd_book_id) {
  927. switch (strtoupper($type)) {
  928. case 'M':
  929. $key = 'm_title';
  930. break;
  931. case 'P':
  932. $key = 'p_title';
  933. break;
  934. case 'V':
  935. $key = 'v_title';
  936. break;
  937. default:
  938. $key = 'term';
  939. break;
  940. }
  941. $output['bookName'] = $value[$key];
  942. break;
  943. }
  944. }
  945. $output['volume'] = $pageInfo->volume;
  946. $output['page'] = $pageInfo->page;
  947. $output['found'] = true;
  948. } else {
  949. $output['found'] = false;
  950. }
  951. return $output;
  952. }
  953. private function render_sent()
  954. {
  955. $sid = $this->get_param($this->param, "id", 1);
  956. $channel = $this->get_param($this->param, "channel", 2);
  957. $show = $this->get_param($this->param, "text", 2, 'both');
  958. if (!empty($channel)) {
  959. $channels = explode(',', $channel);
  960. } else {
  961. $channels = $this->channel_id;
  962. }
  963. $sentInfo = explode('@', trim($sid));
  964. $sentId = $sentInfo[0];
  965. if (isset($sentInfo[1])) {
  966. $channels = [$sentInfo[1]];
  967. }
  968. $Sent = new CorpusController();
  969. $props = $Sent->getSentTpl(
  970. $sentId,
  971. $channels,
  972. $this->mode,
  973. true,
  974. $this->format
  975. );
  976. if ($props === false) {
  977. $props['error'] = "句子模版渲染错误。句子参数个数不符。应该是四个。";
  978. }
  979. if ($this->mode === 'read') {
  980. $tpl = "sentread";
  981. } else {
  982. $tpl = "sentedit";
  983. }
  984. $props['show'] = $show;
  985. //输出引用
  986. $arrSid = explode('-', $sid);
  987. $bookPara = array_slice($arrSid, 0, 2);
  988. if (!isset($GLOBALS['ref_sent'])) {
  989. $GLOBALS['ref_sent'] = array();
  990. }
  991. $GLOBALS['ref_sent'][] = $bookPara;
  992. switch ($this->format) {
  993. case 'react':
  994. $output = [
  995. 'props' => base64_encode(\json_encode($props)),
  996. 'html' => "",
  997. 'tag' => 'span',
  998. 'tpl' => $tpl,
  999. ];
  1000. break;
  1001. case 'unity':
  1002. $output = [
  1003. 'props' => base64_encode(\json_encode($props)),
  1004. 'tpl' => $tpl,
  1005. ];
  1006. break;
  1007. case 'text':
  1008. $output = '';
  1009. if (isset($props['origin']) && is_array($props['origin'])) {
  1010. foreach ($props['origin'] as $key => $value) {
  1011. $output .= $value['html'];
  1012. }
  1013. }
  1014. if (isset($props['translation']) && is_array($props['translation'])) {
  1015. foreach ($props['translation'] as $key => $value) {
  1016. $output .= $value['html'];
  1017. }
  1018. }
  1019. break;
  1020. case 'prompt':
  1021. $output = '';
  1022. if ($show === 'both' || $show === 'origin') {
  1023. if (isset($props['origin']) && is_array($props['origin'])) {
  1024. foreach ($props['origin'] as $key => $value) {
  1025. $output .= $value['html'];
  1026. }
  1027. }
  1028. }
  1029. if ($show === 'both' || $show === 'translation') {
  1030. if (isset($props['translation']) && is_array($props['translation'])) {
  1031. foreach ($props['translation'] as $key => $value) {
  1032. $output .= $value['html'];
  1033. }
  1034. }
  1035. }
  1036. break;
  1037. case 'html':
  1038. $output = '';
  1039. $output .= '<span class="sentence">';
  1040. if ($show === 'both' || $show === 'origin') {
  1041. if (isset($props['origin']) && is_array($props['origin'])) {
  1042. foreach ($props['origin'] as $key => $value) {
  1043. $output .= '<span class="origin">' . $value['html'] . '</span>';
  1044. }
  1045. }
  1046. }
  1047. if ($show === 'both' || $show === 'translation') {
  1048. if (isset($props['translation']) && is_array($props['translation'])) {
  1049. foreach ($props['translation'] as $key => $value) {
  1050. $output .= '<span class="translation">' . $value['html'] . '</span>';
  1051. }
  1052. }
  1053. }
  1054. $output .= '</span>';
  1055. break;
  1056. case 'tex':
  1057. $output = '';
  1058. if (isset($props['translation']) && is_array($props['translation'])) {
  1059. foreach ($props['translation'] as $key => $value) {
  1060. $output .= $value['html'];
  1061. }
  1062. }
  1063. break;
  1064. case 'simple':
  1065. $output = '';
  1066. if ($show === 'both' || $show === 'origin') {
  1067. if (empty($output)) {
  1068. if (
  1069. isset($props['origin']) &&
  1070. is_array($props['origin']) &&
  1071. count($props['origin']) > 0
  1072. ) {
  1073. foreach ($props['origin'] as $key => $value) {
  1074. $output .= trim($value['html']);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. if ($show === 'both' || $show === 'translation') {
  1080. if (
  1081. isset($props['translation']) &&
  1082. is_array($props['translation']) &&
  1083. count($props['translation']) > 0
  1084. ) {
  1085. foreach ($props['translation'] as $key => $value) {
  1086. $output .= trim($value['html']);
  1087. }
  1088. }
  1089. }
  1090. break;
  1091. case 'markdown':
  1092. $output = '';
  1093. if ($show === 'both' || $show === 'origin') {
  1094. if (
  1095. $this->options['origin'] === true ||
  1096. $this->options['origin'] === 'true'
  1097. ) {
  1098. if (isset($props['origin']) && is_array($props['origin'])) {
  1099. foreach ($props['origin'] as $key => $value) {
  1100. $output .= trim($value['html']);
  1101. }
  1102. }
  1103. }
  1104. }
  1105. if ($show === 'both' || $show === 'translation') {
  1106. if (
  1107. $this->options['translation'] === true ||
  1108. $this->options['translation'] === 'true'
  1109. ) {
  1110. if (
  1111. isset($props['translation']) &&
  1112. is_array($props['translation']) &&
  1113. count($props['translation']) > 0
  1114. ) {
  1115. foreach ($props['translation'] as $key => $value) {
  1116. $output .= trim($value['html']);
  1117. }
  1118. } else {
  1119. if ($show === 'translation') {
  1120. //无译文用原文代替
  1121. if (isset($props['origin']) && is_array($props['origin'])) {
  1122. foreach ($props['origin'] as $key => $value) {
  1123. $output .= trim($value['html']);
  1124. }
  1125. }
  1126. }
  1127. }
  1128. }
  1129. }
  1130. break;
  1131. default:
  1132. $output = '';
  1133. break;
  1134. }
  1135. return $output;
  1136. }
  1137. private function render_mermaid()
  1138. {
  1139. $text = json_decode(base64_decode($this->get_param($this->param, "text", 1)));
  1140. $props = ["text" => implode("\n", $text)];
  1141. switch ($this->format) {
  1142. case 'react':
  1143. $output = [
  1144. 'props' => base64_encode(\json_encode($props)),
  1145. 'html' => "mermaid",
  1146. 'tag' => 'div',
  1147. 'tpl' => 'mermaid',
  1148. ];
  1149. break;
  1150. case 'unity':
  1151. $output = [
  1152. 'props' => base64_encode(\json_encode($props)),
  1153. 'tpl' => 'mermaid',
  1154. ];
  1155. break;
  1156. case 'text':
  1157. $output = 'mermaid';
  1158. break;
  1159. case 'tex':
  1160. $output = 'mermaid';
  1161. break;
  1162. case 'simple':
  1163. $output = 'mermaid';
  1164. break;
  1165. default:
  1166. $output = 'mermaid';
  1167. break;
  1168. }
  1169. return $output;
  1170. }
  1171. private function render_qa()
  1172. {
  1173. $id = $this->get_param($this->param, "id", 1);
  1174. $style = $this->get_param($this->param, "style", 2);
  1175. $props = [
  1176. "type" => 'qa',
  1177. "id" => $id,
  1178. 'title' => '',
  1179. 'style' => $style,
  1180. ];
  1181. $qa = Discussion::where('id', $id)->first();
  1182. if ($qa) {
  1183. $props['title'] = $qa->title;
  1184. $props['resId'] = $qa->res_id;
  1185. $props['resType'] = $qa->res_type;
  1186. }
  1187. switch ($this->format) {
  1188. case 'react':
  1189. $output = [
  1190. 'props' => base64_encode(\json_encode($props)),
  1191. 'html' => "",
  1192. 'text' => $props['title'],
  1193. 'tag' => 'div',
  1194. 'tpl' => 'qa',
  1195. ];
  1196. break;
  1197. case 'unity':
  1198. $output = [
  1199. 'props' => base64_encode(\json_encode($props)),
  1200. 'tpl' => 'qa',
  1201. ];
  1202. break;
  1203. default:
  1204. $output = $props['title'];
  1205. break;
  1206. }
  1207. return $output;
  1208. }
  1209. private function render_grammar_lookup()
  1210. {
  1211. $word = $this->get_param($this->param, "word", 1);
  1212. $props = ['word' => $word];
  1213. $localTermChannel = ChannelApi::getSysChannel(
  1214. "_System_Grammar_Term_" . strtolower($this->lang) . "_",
  1215. "_System_Grammar_Term_en_"
  1216. );
  1217. $term = $this->getTermProps($word, null, $localTermChannel);
  1218. $props['term'] = $term;
  1219. switch ($this->format) {
  1220. case 'react':
  1221. $output = [
  1222. 'props' => base64_encode(\json_encode($props)),
  1223. 'html' => "",
  1224. 'text' => $props['word'],
  1225. 'tag' => 'span',
  1226. 'tpl' => 'grammar',
  1227. ];
  1228. break;
  1229. case 'unity':
  1230. $output = [
  1231. 'props' => base64_encode(\json_encode($props)),
  1232. 'tpl' => 'grammar',
  1233. ];
  1234. break;
  1235. default:
  1236. $output = $props['word'];
  1237. break;
  1238. }
  1239. return $output;
  1240. }
  1241. private function render_video()
  1242. {
  1243. $url = $this->get_param($this->param, "url", 1);
  1244. $style = $this->get_param($this->param, "style", 2, 'modal');
  1245. $title = $this->get_param($this->param, "title", 3);
  1246. $props = [
  1247. "url" => $url,
  1248. 'title' => $title,
  1249. 'style' => $style,
  1250. ];
  1251. switch ($this->format) {
  1252. case 'react':
  1253. $output = [
  1254. 'props' => base64_encode(\json_encode($props)),
  1255. 'html' => "",
  1256. 'text' => $props['title'],
  1257. 'tag' => 'span',
  1258. 'tpl' => 'video',
  1259. ];
  1260. break;
  1261. case 'unity':
  1262. $output = [
  1263. 'props' => base64_encode(\json_encode($props)),
  1264. 'tpl' => 'video',
  1265. ];
  1266. break;
  1267. default:
  1268. $output = $props['title'];
  1269. break;
  1270. }
  1271. return $output;
  1272. }
  1273. //论文后面的参考资料
  1274. private function render_ref()
  1275. {
  1276. $references = array();
  1277. $counter = 0;
  1278. if (isset($GLOBALS['ref_sent'])) {
  1279. $hasBooks = array();
  1280. $book_titles = BookSeries::select(['book', 'paragraph', 'title', 'sn'])
  1281. ->orderBy('sn', 'DESC')->get();
  1282. $bTitles = array();
  1283. foreach ($book_titles as $key => $book) {
  1284. $bTitles[] = [
  1285. 'book' => $book->book,
  1286. 'paragraph' => $book->paragraph,
  1287. 'title' => $book->title
  1288. ];
  1289. }
  1290. foreach ($GLOBALS['ref_sent'] as $key => $ref) {
  1291. $books = array_filter($bTitles, function ($value) use ($ref) {
  1292. return $value['book'] === (int)$ref[0];
  1293. });
  1294. if (count($books) > 0) {
  1295. foreach ($books as $key => $book) {
  1296. if ($book['paragraph'] < (int)$ref[1]) {
  1297. if (!isset($hasBooks[$book['title']])) {
  1298. $hasBooks[$book['title']] = 1;
  1299. $counter++;
  1300. $references[] = [
  1301. 'sn' => $counter,
  1302. 'title' => $book['title'],
  1303. 'copyright' => 'CSCD V4 VRI 2008'
  1304. ];
  1305. }
  1306. }
  1307. }
  1308. }
  1309. }
  1310. }
  1311. $props = [
  1312. "pali" => $references,
  1313. ];
  1314. switch ($this->format) {
  1315. case 'react':
  1316. $output = [
  1317. 'props' => base64_encode(\json_encode($props)),
  1318. 'html' => '',
  1319. 'tag' => 'div',
  1320. 'tpl' => 'reference',
  1321. ];
  1322. break;
  1323. case 'unity':
  1324. $output = [
  1325. 'props' => base64_encode(\json_encode($props)),
  1326. 'tpl' => 'reference',
  1327. ];
  1328. break;
  1329. case 'markdown':
  1330. $output = '';
  1331. foreach ($references as $key => $reference) {
  1332. $output .= '[' . $reference['sn'] . '] **' . ucfirst($reference['title']) . '** ';
  1333. $output .= $reference['copyright'] . "\n\n";
  1334. }
  1335. break;
  1336. default:
  1337. $output = '';
  1338. foreach ($references as $key => $reference) {
  1339. $output .= '[' . $reference['sn'] . '] ' . ucfirst($reference['title']) . ' ';
  1340. $output .= $reference['copyright'] . "\n";
  1341. }
  1342. break;
  1343. }
  1344. return $output;
  1345. }
  1346. private function render_dict_pref()
  1347. {
  1348. $currPage = $this->get_param($this->param, "page", 1, 1);
  1349. $pageSize = $this->get_param($this->param, "size", 2, 100);
  1350. $props = [
  1351. "currPage" => $currPage,
  1352. 'pageSize' => $pageSize,
  1353. ];
  1354. switch ($this->format) {
  1355. case 'react':
  1356. $output = [
  1357. 'props' => base64_encode(\json_encode($props)),
  1358. 'html' => "",
  1359. 'text' => '',
  1360. 'tag' => 'div',
  1361. 'tpl' => 'dict-pref',
  1362. ];
  1363. break;
  1364. case 'unity':
  1365. $output = [
  1366. 'props' => base64_encode(\json_encode($props)),
  1367. 'tpl' => 'dict-pref',
  1368. ];
  1369. break;
  1370. default:
  1371. $output = 'dict-pref';
  1372. break;
  1373. }
  1374. return $output;
  1375. }
  1376. private function render_ai()
  1377. {
  1378. $model = $this->get_param($this->param, "model", 1, 1);
  1379. $props = [
  1380. "model" => $model,
  1381. ];
  1382. switch ($this->format) {
  1383. case 'react':
  1384. $output = [
  1385. 'props' => base64_encode(\json_encode($props)),
  1386. 'html' => "",
  1387. 'text' => '',
  1388. 'tag' => 'div',
  1389. 'tpl' => 'ai',
  1390. ];
  1391. break;
  1392. case 'unity':
  1393. $output = [
  1394. 'props' => base64_encode(\json_encode($props)),
  1395. 'tpl' => 'ai',
  1396. ];
  1397. break;
  1398. case 'text':
  1399. $output = 'ai';
  1400. break;
  1401. case 'prompt':
  1402. $output = '';
  1403. break;
  1404. default:
  1405. $output = 'ai';
  1406. break;
  1407. }
  1408. return $output;
  1409. }
  1410. private function get_param(array $param, string $name, int $id, string $default = '')
  1411. {
  1412. if (isset($param[$name])) {
  1413. return trim($param[$name]);
  1414. } else if (isset($param["{$id}"])) {
  1415. return trim($param["{$id}"]);
  1416. } else {
  1417. return $default;
  1418. }
  1419. }
  1420. private function mb_trim($str, string $character_mask = ' ', $charset = "UTF-8")
  1421. {
  1422. $start = 0;
  1423. $end = mb_strlen($str, $charset) - 1;
  1424. $chars = preg_split('//u', $character_mask, -1, PREG_SPLIT_NO_EMPTY);
  1425. while ($start <= $end && in_array(mb_substr($str, $start, 1, $charset), $chars)) {
  1426. $start++;
  1427. }
  1428. while ($end >= $start && in_array(mb_substr($str, $end, 1, $charset), $chars)) {
  1429. $end--;
  1430. }
  1431. return mb_substr($str, $start, $end - $start + 1, $charset);
  1432. }
  1433. }