|
@@ -0,0 +1,96 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Jobs;
|
|
|
|
|
+
|
|
|
|
|
+use Illuminate\Bus\Queueable;
|
|
|
|
|
+use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
|
+use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
+use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
+use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
+use Illuminate\Queue\SerializesModels;
|
|
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
+use PhpOffice\PhpWord\TemplateProcessor;
|
|
|
|
|
+
|
|
|
|
|
+class GenerateSeparateDocxJob implements ShouldQueue
|
|
|
|
|
+{
|
|
|
|
|
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
+
|
|
|
|
|
+ protected $vars;
|
|
|
|
|
+ protected $products_count;
|
|
|
|
|
+ protected $filename;
|
|
|
|
|
+ protected $template;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Create a new job instance.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ public function __construct($vars, $products_count, $filename, $template)
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->vars = $vars;
|
|
|
|
|
+ $this->products_count = $products_count;
|
|
|
|
|
+ $this->filename = $filename;
|
|
|
|
|
+ $this->template = $template;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Execute the job.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ public function handle()
|
|
|
|
|
+ {
|
|
|
|
|
+ $tmpfile = public_path('exported/docx/' . Str::replace('.docx', '.zip', $this->filename) . '.txt');
|
|
|
|
|
+
|
|
|
|
|
+ $file_for_arch = [];
|
|
|
|
|
+ for ($i = 0; $i < $this->products_count; $i++) {
|
|
|
|
|
+ $my_template = new TemplateProcessor(storage_path('templates/' . $this->template));
|
|
|
|
|
+ $tmp_vars = [];
|
|
|
|
|
+ foreach ($this->vars as $k => $v) {
|
|
|
|
|
+ if (Str::endsWith($k, '#' . $i + 1)) {
|
|
|
|
|
+ $tmp_vars[Str::replace('#' . $i + 1, '#1', $k)] = $v;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $my_template->cloneBlock('product_block', 1, true, true, $tmp_vars);
|
|
|
|
|
+ foreach ($tmp_vars as $k => $v) {
|
|
|
|
|
+ $file = public_path() . '/' . env('IMAGES_PATH') . '/' . $v;
|
|
|
|
|
+ if (str_starts_with($k, 'image') && file_exists($file) && $v !== '') {
|
|
|
|
|
+ $my_template->setImageValue($k, ['path' => $file, 'width' => 270, 'height' => 180]);
|
|
|
|
|
+ } elseif (Str::startsWith($k, 'num')) {
|
|
|
|
|
+ $my_template->setValue($k, 1);
|
|
|
|
|
+ } elseif (Str::startsWith($k, 'page_br')) {
|
|
|
|
|
+ $my_template->setValue($k, '');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $my_template->setValue($k, $v);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (($i % 10) == 0) {
|
|
|
|
|
+ $f = fopen($tmpfile, 'w+');
|
|
|
|
|
+ $percent = round(($i - 2) / ($this->products_count / 100), 0);
|
|
|
|
|
+ fwrite($f, $percent);
|
|
|
|
|
+ fclose($f);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $filename = Str::replace('.docx', '_' . str_pad($i + 1, 4, 0, STR_PAD_LEFT) . '.docx', $this->filename);
|
|
|
|
|
+ $my_template->saveAs(public_path('exported/docx/') . $filename);
|
|
|
|
|
+ $file_for_arch[] = $filename;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $zip_file = public_path('exported/docx/') . Str::replace('.docx', '.zip', $this->filename);
|
|
|
|
|
+ $zip = new \ZipArchive();
|
|
|
|
|
+ $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|
|
|
|
+ foreach ($file_for_arch as $file_docx) {
|
|
|
|
|
+ $zip->addFile(public_path('exported/docx/') . $file_docx, $file_docx);
|
|
|
|
|
+ }
|
|
|
|
|
+ $zip->close();
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($file_for_arch as $file_docx) {
|
|
|
|
|
+ unlink(public_path('exported/docx/') . $file_docx);
|
|
|
|
|
+ }
|
|
|
|
|
+ unlink($tmpfile);
|
|
|
|
|
+
|
|
|
|
|
+ Log::notice('Generation finished: ' . $i . ' / ' . $this->products_count . ' Execution time: ' . microtime(true) - LARAVEL_START);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|