HasExporter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Grid\Exporter;
  4. use Dcat\Admin\Grid\Exporters\AbstractExporter;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Grid\Tools;
  7. use Illuminate\Support\Facades\Input;
  8. trait HasExporter
  9. {
  10. /**
  11. * Export driver.
  12. *
  13. * @var string|Grid\Exporters\AbstractExporter
  14. */
  15. protected $exporter;
  16. /**
  17. * Set exporter driver for Grid to export.
  18. *
  19. * @param string|Grid\Exporters\AbstractExporter $exporter
  20. *
  21. * @return $this
  22. */
  23. public function exporter($exporter)
  24. {
  25. $this->exporter = $exporter;
  26. return $this;
  27. }
  28. /**
  29. * Handle export request.
  30. *
  31. * @param bool $forceExport
  32. */
  33. protected function handleExportRequest($forceExport = false)
  34. {
  35. if (!$scope = request(Exporter::$queryName)) {
  36. return;
  37. }
  38. // clear output buffer.
  39. if (ob_get_length()) {
  40. ob_end_clean();
  41. }
  42. $this->model()->usePaginate(false);
  43. if ($this->builder) {
  44. call_user_func($this->builder, $this);
  45. $this->getExporter($scope)->export();
  46. }
  47. if ($forceExport) {
  48. $this->getExporter($scope)->export();
  49. }
  50. }
  51. /**
  52. * @param string $scope
  53. *
  54. * @return AbstractExporter
  55. */
  56. protected function getExporter($scope)
  57. {
  58. return (new Exporter($this))->resolve($this->exporter)->withScope($scope);
  59. }
  60. /**
  61. * Get the export url.
  62. *
  63. * @param int $scope
  64. * @param null $args
  65. *
  66. * @return string
  67. */
  68. public function getExportUrl($scope = 1, $args = null)
  69. {
  70. $input = array_merge(Input::all(), Exporter::formatExportQuery($scope, $args));
  71. if ($constraints = $this->model()->getConstraints()) {
  72. $input = array_merge($input, $constraints);
  73. }
  74. return $this->getResource().'?'.http_build_query($input);
  75. }
  76. /**
  77. * @param array $options
  78. * @return $this
  79. */
  80. public function setExportOptions(array $options)
  81. {
  82. if (isset($options['limit'])) {
  83. $this->options['export_limit'] = $options['limit'];
  84. }
  85. if (isset($options['all'])) {
  86. $this->options['show_export_all'] = $options['show_all'];
  87. }
  88. if (isset($options['current_page'])) {
  89. $this->options['show_export_current_page'] = $options['current_page'];
  90. }
  91. if (isset($options['selected_rows'])) {
  92. $this->options['show_export_selected_rows'] = $options['selected_rows'];
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Render export button.
  98. *
  99. * @return string
  100. */
  101. public function renderExportButton()
  102. {
  103. if (!$this->options['show_exporter']) {
  104. return '';
  105. }
  106. return (new Tools\ExportButton($this))->render();
  107. }
  108. /**
  109. * Disable export.
  110. *
  111. * @return $this
  112. */
  113. public function disableExporter(bool $disable = true)
  114. {
  115. return $this->option('show_exporter', !$disable);
  116. }
  117. /**
  118. * Show export button.
  119. *
  120. * @return $this
  121. */
  122. public function showExporter(bool $val = true)
  123. {
  124. return $this->disableExporter(!$val);
  125. }
  126. /**
  127. * If grid show export btn.
  128. *
  129. * @return bool
  130. */
  131. public function allowExportBtn()
  132. {
  133. return $this->options['show_exporter'];
  134. }
  135. }