Exporter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Dcat\Admin\Grid;
  4. use Dcat\Admin\Grid\Exporters\CsvExporter;
  5. class Exporter
  6. {
  7. /**
  8. * Export scope constants.
  9. */
  10. const SCOPE_ALL = 'all';
  11. const SCOPE_CURRENT_PAGE = 'page';
  12. const SCOPE_SELECTED_ROWS = 'selected';
  13. /**
  14. * Available exporter drivers.
  15. *
  16. * @var array
  17. */
  18. protected static $drivers = [];
  19. /**
  20. * Export query name.
  21. *
  22. * @var string
  23. */
  24. protected $queryName = '_export_';
  25. /**
  26. * @var Grid
  27. */
  28. protected $grid;
  29. /**
  30. * Create a new Exporter instance.
  31. *
  32. * @param Grid $grid
  33. */
  34. public function __construct(Grid $grid)
  35. {
  36. $this->grid = $grid;
  37. }
  38. /**
  39. * Set export query name.
  40. *
  41. * @param $name
  42. *
  43. * @return $this
  44. */
  45. public function setQueryName($name)
  46. {
  47. $this->queryName = $name;
  48. return $this;
  49. }
  50. /**
  51. * Get export query name.
  52. *
  53. * @return string
  54. */
  55. public function getQueryName(): string
  56. {
  57. return $this->queryName;
  58. }
  59. /**
  60. * Extends new exporter driver.
  61. *
  62. * @param $driver
  63. * @param $extend
  64. */
  65. public static function extend($driver, $extend)
  66. {
  67. static::$drivers[$driver] = $extend;
  68. }
  69. /**
  70. * Resolve export driver.
  71. *
  72. * @param string $driver
  73. *
  74. * @return CsvExporter
  75. */
  76. public function resolve($driver)
  77. {
  78. if ($driver instanceof Grid\Exporters\AbstractExporter) {
  79. return $driver->setGrid($this->grid);
  80. }
  81. return $this->getExporter($driver);
  82. }
  83. /**
  84. * Get export driver.
  85. *
  86. * @param string $driver
  87. *
  88. * @return CsvExporter
  89. */
  90. protected function getExporter($driver)
  91. {
  92. if (!array_key_exists($driver, static::$drivers)) {
  93. return $this->getDefaultExporter();
  94. }
  95. return (new static::$drivers[$driver]())->setGrid($this->grid);
  96. }
  97. /**
  98. * Get default exporter.
  99. *
  100. * @return CsvExporter
  101. */
  102. public function getDefaultExporter()
  103. {
  104. return (new CsvExporter())->setGrid($this->grid);
  105. }
  106. /**
  107. * Format query for export url.
  108. *
  109. * @param int $scope
  110. * @param null $args
  111. *
  112. * @return array
  113. */
  114. public function formatExportQuery($scope = '', $args = null)
  115. {
  116. $query = '';
  117. if ($scope == static::SCOPE_ALL) {
  118. $query = 'all';
  119. }
  120. if ($scope == static::SCOPE_CURRENT_PAGE) {
  121. $query = "page:$args";
  122. }
  123. if ($scope == static::SCOPE_SELECTED_ROWS) {
  124. $query = "selected:$args";
  125. }
  126. return [$this->queryName => $query];
  127. }
  128. }