TemplateRender.php 50 KB

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