ExportController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if($i < $products->count()) {
  44. $vars_for_template['page_br#' . $i] = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>';
  45. } else {
  46. $vars_for_template['page_br#' . $i] = '';
  47. }
  48. $i++;
  49. }
  50. //dd($vars_for_template);
  51. // prepared vars - run job
  52. $filename = 'Наш_Двор_' . date('YmdHis') . '.docx';
  53. if($products->count() > 200){
  54. GenerateDocxJob::dispatch($vars_for_template, count($products), $filename, $request->template)->onConnection('database');
  55. } else {
  56. GenerateDocxJob::dispatch($vars_for_template, count($products), $filename, $request->template)->onConnection('sync');
  57. }
  58. Log::notice('Created export job. Execution time: ' . microtime(true) - LARAVEL_START);
  59. $data['filename'] = $filename;
  60. return redirect()->route('wait_export', $data);
  61. }
  62. public function wait_export(Request $request, $filename){
  63. $fn = public_path('exported/docx/' . $filename . '.txt');
  64. if(file_exists($fn)){
  65. $data['percent'] = file_get_contents($fn);
  66. } else {
  67. $data['percent'] = 0;
  68. }
  69. $fn = public_path('exported/docx/' . $filename);
  70. if(file_exists($fn)) {
  71. $data['percent'] = 100;
  72. }
  73. $data['filename'] = $filename;
  74. return view('products.wait', $data);
  75. }
  76. }