ExportChapter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Channel;
  7. use App\Models\PaliText;
  8. use App\Models\Sentence;
  9. use App\Http\Api\ChannelApi;
  10. use App\Http\Api\MdRender;
  11. use App\Tools\Export;
  12. class ExportChapter extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'export:chapter {book} {para} {channel}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $book = $this->argument('book');
  43. $para = $this->argument('para');
  44. $channelId = $this->argument('channel');
  45. $channel = ChannelApi::getById($channelId);
  46. $chapter = PaliText::where('book',$book)->where('paragraph',$para)->first();
  47. if(!$chapter){
  48. return $this->error("no data");
  49. }
  50. $bookMeta = array();
  51. if(empty($chapter->toc)){
  52. $bookMeta['title'] = "unknown";
  53. }else{
  54. $title = ProgressChapter::where('book',$book)->where('para',$para)
  55. ->where('channel_id',$channelId)
  56. ->value('title');
  57. $bookMeta['book_title'] = $title?$title:$chapter->toc;
  58. $bookMeta['sub_title'] = $chapter->toc;
  59. }
  60. if($channel){
  61. $bookMeta['book_author'] = $channel['name'];
  62. }
  63. $subChapter = PaliText::where('book',$book)->where('parent',$para)
  64. ->where('level','<',8)
  65. ->orderBy('paragraph')
  66. ->get();
  67. $sections = array();
  68. foreach ($subChapter as $key => $sub) {
  69. # code...
  70. $chapter = ProgressChapter::where('book',$book)->where('para',$sub->paragraph)
  71. ->where('channel_id',$channelId)
  72. ->first();
  73. if($chapter){
  74. $filename = "{$sub->paragraph}.tex";
  75. $bookMeta['sections'][] = ['filename'=>$filename];
  76. $paliTitle = PaliText::where('book',$book)->where('paragraph',$sub->paragraph)->value('toc');
  77. $title = $chapter->title?$chapter->title:$paliTitle;
  78. $content = array();
  79. $chapterStart = $sub->paragraph+1;
  80. $chapterEnd = $sub->paragraph + $sub->lenght;
  81. $chapterBody = PaliText::where('book',$book)
  82. ->whereBetween('paragraph',[$chapterStart,$chapterEnd])
  83. ->orderBy('paragraph')->get();
  84. foreach ($chapterBody as $body) {
  85. # code...
  86. $translationData = Sentence::where('book_id',$book)
  87. ->where('paragraph',$body->paragraph)
  88. ->where('channel_uid',$channelId)
  89. ->orderBy('word_start')->get();
  90. $sentContent = array();
  91. foreach ($translationData as $sent) {
  92. /*
  93. $sentContent[] = MdRender::render($sent->content,
  94. [$sent->channel_uid],
  95. null,
  96. 'read',
  97. $channel['type'],
  98. $sent->content_type,
  99. 'unity'
  100. );*/
  101. $sentContent[] = $sent->content;
  102. }
  103. $content[] = '\par ';
  104. $content[] = implode(' ',$sentContent);
  105. $content[] = "\n";
  106. }
  107. $sections[] = [
  108. 'name'=>$filename,
  109. 'body'=>[
  110. 'title'=>$title,
  111. 'content'=>implode('',$content)
  112. ]
  113. ];
  114. }
  115. }
  116. $tex = array();
  117. $m = new \Mustache_Engine(array('entity_flags'=>ENT_QUOTES,'delimiters' => '[[ ]]'));
  118. $tpl = file_get_contents(resource_path("mustache/tex/main.tex"));
  119. $texContent = $m->render($tpl,$bookMeta);
  120. $tex[] = ['name'=>'main.tex',
  121. 'content'=>$texContent
  122. ];
  123. foreach ($sections as $key => $section) {
  124. $tpl = file_get_contents(resource_path("mustache/tex/section.tex"));
  125. $texContent = $m->render($tpl,$section['body']);
  126. $tex[] = ['name'=>$section['name'],
  127. 'content'=>$texContent
  128. ];
  129. }
  130. $data = Export::ToPdf($tex);
  131. if($data['ok']){
  132. $filename = "export/test.pdf";
  133. $this->info($data['content-type']);
  134. Storage::disk('local')->put($filename, $data['data']);
  135. }else{
  136. $this->error($data['code'].'-'.$data['message']);
  137. }
  138. return 0;
  139. }
  140. }