ExportSentence.php 3.0 KB

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