GenerateSeparateDocxJob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Str;
  11. use PhpOffice\PhpWord\TemplateProcessor;
  12. class GenerateSeparateDocxJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $vars;
  16. protected $products_count;
  17. protected $filename;
  18. protected $template;
  19. /**
  20. * Create a new job instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct($vars, $products_count, $filename, $template)
  25. {
  26. $this->vars = $vars;
  27. $this->products_count = $products_count;
  28. $this->filename = $filename;
  29. $this->template = $template;
  30. }
  31. /**
  32. * Execute the job.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. $tmpfile = public_path('exported/docx/' . Str::replace('.docx', '.zip', $this->filename) . '.txt');
  39. $file_for_arch = [];
  40. for ($i = 0; $i < $this->products_count; $i++) {
  41. $my_template = new TemplateProcessor(storage_path('templates/' . $this->template));
  42. $tmp_vars = [];
  43. foreach ($this->vars as $k => $v) {
  44. if (Str::endsWith($k, '#' . $i + 1)) {
  45. $tmp_vars[Str::replace('#' . $i + 1, '#1', $k)] = $v;
  46. }
  47. }
  48. $my_template->cloneBlock('product_block', 1, true, true, $tmp_vars);
  49. foreach ($tmp_vars as $k => $v) {
  50. $file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v;
  51. if (str_starts_with($k, 'image') && file_exists($file) && $v !== '') {
  52. $my_template->setImageValue($k, ['path' => $file, 'width' => 270, 'height' => 180]);
  53. } elseif (Str::startsWith($k, 'num')) {
  54. $my_template->setValue($k, 1);
  55. } elseif (Str::startsWith($k, 'page_br')) {
  56. $my_template->setValue($k, '');
  57. } else {
  58. $my_template->setValue($k, $v);
  59. }
  60. }
  61. if (($i % 10) == 0) {
  62. $f = fopen($tmpfile, 'w+');
  63. $percent = round(($i - 2) / ($this->products_count / 100), 0);
  64. fwrite($f, $percent);
  65. fclose($f);
  66. }
  67. $filename = Str::replace('.docx', '_' . str_pad($i + 1, 4, 0, STR_PAD_LEFT) . '.docx', $this->filename);
  68. $my_template->saveAs(public_path('exported/docx/') . $filename);
  69. $file_for_arch[] = $filename;
  70. }
  71. $zip_file = public_path('exported/docx/') . Str::replace('.docx', '.zip', $this->filename);
  72. $zip = new \ZipArchive();
  73. $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  74. foreach ($file_for_arch as $file_docx) {
  75. $zip->addFile(public_path('exported/docx/') . $file_docx, $file_docx);
  76. }
  77. $zip->close();
  78. foreach ($file_for_arch as $file_docx) {
  79. unlink(public_path('exported/docx/') . $file_docx);
  80. }
  81. unlink($tmpfile);
  82. Log::notice('Generation finished: ' . $i . ' / ' . $this->products_count . ' Execution time: ' . microtime(true) - LARAVEL_START);
  83. }
  84. }