GenerateSeparateDocxJob.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Product;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Str;
  12. use PhpOffice\PhpWord\TemplateProcessor;
  13. class GenerateSeparateDocxJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $ids;
  17. protected $descr;
  18. protected $filename;
  19. protected $template;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct($ids, $descr, $filename, $template)
  26. {
  27. $this->ids = $ids;
  28. $this->descr = $descr;
  29. $this->filename = $filename;
  30. $this->template = $template;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $tmpfile = public_path('exported/docx/' . Str::replace('.docx', '.zip', $this->filename) . '.txt');
  40. $products = Product::query()->whereIn('id', $this->ids)->orderBy('series', 'asc')->get();
  41. $i = 1;
  42. $file_for_arch = [];
  43. foreach ($products as $product){
  44. switch ($this->descr) {
  45. case 1:
  46. $descr = $product->characteristics . "\r\n" . $product->tech_description_short;
  47. break;
  48. case 2:
  49. $descr = $product->characteristics . "\r\n" . $product->tech_description;
  50. break;
  51. case 3:
  52. $descr = $product->tech_description_short;
  53. break;
  54. case 4:
  55. $descr = $product->tech_description;
  56. break;
  57. case 5:
  58. default:
  59. $descr = $product->characteristics;
  60. break;
  61. }
  62. $vars_for_template = [
  63. 'series#1' => $product->series,
  64. 'article#1' => $product->article,
  65. 'product_group#1' => $product->product_group,
  66. 'num#1' => 1, // $i,
  67. 'name#1' => $product->name,
  68. 'name_for_form#1' => $product->name_for_form,
  69. 'price#1' => number_format($product->price, 2, '.', ' '),
  70. 'image#1' => $product->image_path,
  71. 'description#1' => str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $descr),
  72. 'page_br#1' => ''
  73. ];
  74. $article = '';
  75. $my_template = new TemplateProcessor(storage_path('templates/' . $this->template));
  76. $my_template->cloneBlock('product_block', 1, true, true, $vars_for_template);
  77. foreach ($vars_for_template as $k => $v) {
  78. $file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v;
  79. if (str_starts_with($k, 'image') && file_exists($file) && $v !== '') {
  80. $my_template->setImageValue($k, ['path' => $file, 'width' => 270, 'height' => 180]);
  81. } elseif (Str::startsWith($k, 'num')) {
  82. $my_template->setValue($k, 1);
  83. } elseif (Str::startsWith($k, 'page_br')) {
  84. $my_template->setValue($k, '');
  85. } elseif(Str::startsWith($k, 'article')){
  86. $article = $v;
  87. } else {
  88. $my_template->setValue($k, $v);
  89. }
  90. }
  91. $filename = Str::replace('Наш_Двор_', 'Наш_Двор_' . $article . '_', $this->filename);
  92. $filename = Str::replace('.docx', '_' . str_pad($i + 1, 4, 0, STR_PAD_LEFT) . '.docx', $filename);
  93. $my_template->saveAs(public_path('exported/docx/') . $filename);
  94. $file_for_arch[] = $filename;
  95. if (($i % 10) == 0) {
  96. $f = fopen($tmpfile, 'w+');
  97. $percent = round(($i - 2) / (count($this->ids) / 100), 0);
  98. fwrite($f, $percent);
  99. fclose($f);
  100. }
  101. $i++;
  102. }
  103. // creating zip file
  104. $zip_file = public_path('exported/docx/') . Str::replace('.docx', '.zip', $this->filename);
  105. $zip = new \ZipArchive();
  106. $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  107. foreach ($file_for_arch as $file_docx) {
  108. $zip->addFile(public_path('exported/docx/') . $file_docx, $file_docx);
  109. }
  110. $zip->close();
  111. foreach ($file_for_arch as $file_docx) {
  112. unlink(public_path('exported/docx/') . $file_docx);
  113. }
  114. unlink($tmpfile);
  115. Log::notice('Generation finished: ' . $i . ' / ' . count($this->ids) . ' Execution time: ' . microtime(true) - LARAVEL_START);
  116. }
  117. }