1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Exporters; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Grid; |
6
|
|
|
|
7
|
|
|
abstract class AbstractExporter implements ExporterInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \Encore\Admin\Grid |
11
|
|
|
*/ |
12
|
|
|
protected $grid; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new exporter instance. |
16
|
|
|
* |
17
|
|
|
* @param $grid |
18
|
|
|
*/ |
19
|
|
|
public function __construct(Grid $grid = null) |
20
|
|
|
{ |
21
|
|
|
if ($grid) { |
22
|
|
|
$this->setGrid($grid); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set grid for exporter. |
28
|
|
|
* |
29
|
|
|
* @param Grid $grid |
30
|
|
|
* |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function setGrid(Grid $grid) |
34
|
|
|
{ |
35
|
|
|
$this->grid = $grid; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get table of grid. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getTable() |
46
|
|
|
{ |
47
|
|
|
return $this->grid->model()->eloquent()->getTable(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get data with export query. |
52
|
|
|
* |
53
|
|
|
* @param bool $toArray |
54
|
|
|
* |
55
|
|
|
* @return array|\Illuminate\Support\Collection|mixed |
56
|
|
|
*/ |
57
|
|
|
public function getData($toArray = true) |
58
|
|
|
{ |
59
|
|
|
return $this->grid->getFilter()->execute($toArray); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param callable $callback |
64
|
|
|
* @param int $count |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function chunk(callable $callback, $count = 100) |
69
|
|
|
{ |
70
|
|
|
return $this->grid->getFilter()->chunk($callback, $count); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Illuminate\Support\Collection |
75
|
|
|
*/ |
76
|
|
|
public function getCollection() |
77
|
|
|
{ |
78
|
|
|
return collect($this->getData()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model |
83
|
|
|
*/ |
84
|
|
|
public function getQuery() |
85
|
|
|
{ |
86
|
|
|
return $this->grid->getFilter()->getModel()->getQueryBuilder(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Export data with scope. |
91
|
|
|
* |
92
|
|
|
* @param string $scope |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function withScope($scope) |
97
|
|
|
{ |
98
|
|
|
if ($scope == Grid\Exporter::SCOPE_ALL) { |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
list($scope, $args) = explode(':', $scope); |
103
|
|
|
|
104
|
|
|
if ($scope == Grid\Exporter::SCOPE_CURRENT_PAGE) { |
105
|
|
|
$this->grid->model()->usePaginate(true); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($scope == Grid\Exporter::SCOPE_SELECTED_ROWS) { |
109
|
|
|
$selected = explode(',', $args); |
110
|
|
|
$this->grid->model()->whereIn($this->grid->getKeyName(), $selected); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
abstract public function export(); |
120
|
|
|
} |
121
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: