TemplateRender.php 50 KB

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