ExportController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. $filename = 'Наш_Двор_' . date('YmdHis') . '.docx';
  14. $connection = (count($ids) > 200) ? 'database' : 'sync';
  15. if(isset($request->separate_docs) && ($request->separate_docs == 'yes')){
  16. GenerateSeparateDocxJob::dispatch($ids, $request->descr, $filename, $request->template)->onConnection($connection);
  17. $filename = Str::replace('.docx', '.zip', $filename);
  18. } else {
  19. GenerateDocxJob::dispatch($ids, $request->descr, $filename, $request->template)->onConnection($connection);
  20. }
  21. Log::notice('Created export job. Execution time: ' . microtime(true) - LARAVEL_START);
  22. $data['filename'] = $filename;
  23. return redirect()->route('wait_export', $data);
  24. }
  25. public function wait_export(Request $request, $filename){
  26. $fn = public_path('exported/docx/' . $filename . '.txt');
  27. if(file_exists($fn)){
  28. $data['percent'] = file_get_contents($fn);
  29. if($data['percent'] < 0) $data['percent'] = 0;
  30. } else {
  31. $data['percent'] = 0;
  32. }
  33. $fn = public_path('exported/docx/' . $filename);
  34. if(file_exists($fn)) {
  35. $data['percent'] = 100;
  36. }
  37. $data['filename'] = $filename;
  38. return view('products.wait', $data);
  39. }
  40. }