/app/maguttiCms/Exports/ExportHelper.class.php

https://github.com/marcoax/maguttiCms · PHP · 163 lines · 115 code · 24 blank · 24 comment · 2 complexity · a8306fae03be5f2dad671da7113adf30 MD5 · raw file

  1. <?php
  2. namespace App\maguttiCms\Exports;
  3. use Response;
  4. use Carbon\Carbon;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Support\Facades\Storage;
  7. /**
  8. * Class ExportHelper
  9. * @package App\maguttiCms\Exports
  10. */
  11. class ExportHelper
  12. {
  13. protected $model;
  14. protected $modelClass;
  15. protected $config;
  16. protected string $fileName="";
  17. protected string $storage = "app/report/";
  18. protected array $itemsArray = [];
  19. protected string $ext = 'xlsx';
  20. protected $request;
  21. function fileStorage()
  22. {
  23. (new Filesystem)->ensureDirectoryExists(storage_path($this->storage));
  24. return storage_path($this->storage . $this->fileName);
  25. }
  26. function storage($storage)
  27. {
  28. $this->storage = $storage;
  29. return $this;
  30. }
  31. public function setFilename($name)
  32. {
  33. $this->fileName = $name.'.'.$this->ext ;
  34. return $this;
  35. }
  36. function setExtension($ext){
  37. $this->ext =$ext;
  38. return $this;
  39. }
  40. public function getHeadersFromConfig()
  41. {
  42. return collect($this->config['field_exportable'])->pluck('label')->toArray();
  43. }
  44. function reportPath()
  45. {
  46. return "report/" . $this->fileName;
  47. }
  48. function getLatestGeneratedReport($model)
  49. {
  50. $report = (new self)->setFilename($model)->reportPath();
  51. return (Storage::exists($report)) ? Carbon::createFromTimestamp(Storage::lastModified($report))->toDateTimeString() : null;
  52. }
  53. /**
  54. * @return mixed
  55. */
  56. public function downloadData()
  57. {
  58. $time_start = microtime(true);
  59. $FH = fopen($this->fileStorage(), 'w');
  60. fputcsv($FH, $this->getHeadersFromConfig(), ';');
  61. $items = $this->getData();
  62. $items->each(function ($item) use ($FH) {
  63. $item = $this->mapData($item);
  64. fputcsv($FH, $item, ';');
  65. }, 50000);
  66. $time[] = 'Total execution time in seconds: ' . (microtime(true) - $time_start) . " - Created at " . Carbon::now();
  67. if (auth() && auth_user('admin')->isSu()) {
  68. fputcsv($FH, $time, ';');
  69. }
  70. fclose($FH);
  71. return $this->responseWithData();
  72. }
  73. /*
  74. * STORE DATA FOR REPORT
  75. */
  76. public function storeData()
  77. {
  78. $time_start = microtime(true);
  79. $FH = fopen($this->fileStorage(), 'w');
  80. fputcsv($FH, $this->getHeadersFromConfig(), ';');
  81. echo $end = 'Start: ' . (microtime(true) - $time_start) . " - Created at " . Carbon::now() . '' . PHP_EOL;
  82. $items = $this->getData();
  83. //echo $items->toSql() . '' . PHP_EOL;
  84. echo $end = 'Get Data: ' . (microtime(true) - $time_start) . " - Created at " . Carbon::now() . '' . PHP_EOL;
  85. $items->each(function ($item) use ($FH) {
  86. $item = $this->mapData($item);
  87. fputcsv($FH, $item, ';');
  88. }, 50000);
  89. /*$items = $this->gen();
  90. foreach ($items as $item){
  91. $item = $this->mapData($item);
  92. fputcsv($FH, $item,';');
  93. }*/
  94. echo $end = 'Total execution time in seconds: ' . (microtime(true) - $time_start) . " - Created at " . Carbon::now();
  95. $time[] = $end;
  96. fputcsv($FH, $time, ';');
  97. fclose($FH);
  98. }
  99. /** DOWNLOAD REPORT
  100. * @return mixed
  101. */
  102. function responseWithData(){
  103. $headers = [
  104. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
  105. , 'Content-type' => 'text/csv'
  106. , 'Content-Disposition' => 'attachment; filename='.$this->fileName
  107. , 'Expires' => '0'
  108. , 'Pragma' => 'public'
  109. ];
  110. return Response::download($this->fileStorage(),$this->fileName, $headers);
  111. }
  112. /**
  113. * STREAM REPORT
  114. * @param $data
  115. * @return mixed
  116. */
  117. function stream($data){
  118. $callback = function() use ($data)
  119. {
  120. $FH = fopen('php://output', 'w');
  121. foreach ($data as $row) {
  122. fputcsv($FH, $row,';');
  123. }
  124. fclose($FH);
  125. };
  126. $headers = [
  127. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
  128. , 'Content-type' => 'text/csv'
  129. , 'Content-Disposition' => 'attachment; filename='.$this->getFileName()
  130. , 'Expires' => '0'
  131. , 'Pragma' => 'public'
  132. ];
  133. return Response::stream($callback, 200, $headers);
  134. }
  135. static function exportFilename($filename)
  136. {
  137. return $filename . '_' . date('Y-m-d') . '.xlsx';
  138. }
  139. }