ExportController.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\GenerateDocxJob;
  4. use App\Jobs\GenerateSeparateDocxJob;
  5. use App\Models\Product;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Str;
  9. class ExportController extends Controller
  10. {
  11. public function export_docx(Request $request){
  12. $ids = json_decode($request->ids,true);
  13. $products = Product::query()->whereIn('id', $ids)->orderBy('series', 'asc')->get();
  14. $vars_for_template = [];
  15. $i = 1;
  16. foreach ($products as $product){
  17. switch ($request->descr) {
  18. case 1:
  19. $descr = $product->characteristics . "\r\n" . $product->tech_description_short;
  20. break;
  21. case 2:
  22. $descr = $product->characteristics . "\r\n" . $product->tech_description;
  23. break;
  24. case 3:
  25. $descr = $product->tech_description_short;
  26. break;
  27. case 4:
  28. $descr = $product->tech_description;
  29. break;
  30. case 5:
  31. default:
  32. $descr = $product->characteristics;
  33. break;
  34. }
  35. $vars_for_template = array_merge($vars_for_template, [
  36. 'series#' . $i => $product->series,
  37. 'product_group#' .$i => $product->product_group,
  38. 'num#' . $i => $i,
  39. 'name#' . $i => $product->name,
  40. 'name_for_form#' . $i => $product->name_for_form,
  41. 'price#' . $i => number_format($product->price, 2, '.', ' '),
  42. 'image#' . $i => $product->image_path,
  43. 'description#' . $i => str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $descr),
  44. ]);
  45. if($i < $products->count()) {
  46. $vars_for_template['page_br#' . $i] = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>';
  47. } else {
  48. $vars_for_template['page_br#' . $i] = '';
  49. }
  50. $i++;
  51. }
  52. //dd($vars_for_template);
  53. // prepared vars - run job
  54. $filename = 'Наш_Двор_' . date('YmdHis') . '.docx';
  55. $connection = ($products->count() > 200) ? 'database' : 'sync';
  56. if(isset($request->separate_docs) && ($request->separate_docs == 'yes')){
  57. GenerateSeparateDocxJob::dispatch($vars_for_template, $products->count(), $filename, $request->template)->onConnection($connection);
  58. $filename = Str::replace('.docx', '.zip', $filename);
  59. } else {
  60. GenerateDocxJob::dispatch($vars_for_template, count($products), $filename, $request->template)->onConnection($connection);
  61. }
  62. Log::notice('Created export job. Execution time: ' . microtime(true) - LARAVEL_START);
  63. $data['filename'] = $filename;
  64. return redirect()->route('wait_export', $data);
  65. }
  66. public function wait_export(Request $request, $filename){
  67. $fn = public_path('exported/docx/' . $filename . '.txt');
  68. if(file_exists($fn)){
  69. $data['percent'] = file_get_contents($fn);
  70. if($data['percent'] < 0) $data['percent'] = 0;
  71. } else {
  72. $data['percent'] = 0;
  73. }
  74. $fn = public_path('exported/docx/' . $filename);
  75. if(file_exists($fn)) {
  76. $data['percent'] = 100;
  77. }
  78. $data['filename'] = $filename;
  79. return view('products.wait', $data);
  80. }
  81. }