TemplateRender.php 50 KB

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