ExportSentence.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\Sentence;
  6. use App\Models\Channel;
  7. class ExportSentence extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'export:sentence {--channel=} {--type=translation}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $channels = [];
  38. $channel_id = $this->option('channel');
  39. if($channel_id){
  40. $file_suf = $channel_id;
  41. $channels[] = $channel_id;
  42. }else{
  43. $file_suf = $channel_type;
  44. $channel_type = $this->option('type');
  45. $nissaya_channel = Channel::where('type',$channel_type)->where('status',30)->select('uid')->get();
  46. foreach ($nissaya_channel as $key => $value) {
  47. # code...
  48. $channels[] = $value->uid;
  49. }
  50. }
  51. $db = Sentence::whereIn('channel_uid',$channels);
  52. $file_name = "public/export/offline/sentence_{$file_suf}.csv";
  53. Storage::disk('local')->put($file_name, "");
  54. $file = fopen(storage_path("app/{$file_name}"),"w");
  55. fputcsv($file,['id','book','paragraph','word_start','word_end','content','content_type','html','channel_id','editor_id','language','updated_at']);
  56. $bar = $this->output->createProgressBar($db->count());
  57. foreach ($db->select(['uid','book_id','paragraph','word_start','word_end','content','content_type','channel_uid','editor_uid','language','updated_at'])->cursor() as $chapter) {
  58. $content = str_replace("\n","<br />",$chapter->content);
  59. fputcsv($file,[
  60. $chapter->uid,
  61. $chapter->book_id,
  62. $chapter->paragraph,
  63. $chapter->word_start,
  64. $chapter->word_end,
  65. $content,
  66. $chapter->content_type,
  67. $content,
  68. $chapter->channel_uid,
  69. $chapter->editor_uid,
  70. $chapter->language,
  71. $chapter->updated_at,
  72. ]);
  73. $bar->advance();
  74. }
  75. fclose($file);
  76. $bar->finish();
  77. return 0;
  78. }
  79. }