ExportController.php 2.7 KB

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