1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Concerns; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Grid; |
6
|
|
|
use Encore\Admin\Grid\Column; |
7
|
|
|
use Encore\Admin\Grid\Exporter; |
8
|
|
|
use Encore\Admin\Grid\Exporters\AbstractExporter; |
9
|
|
|
|
10
|
|
|
trait CanExportGrid |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Export driver. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $exporter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Handle export request. |
21
|
|
|
* |
22
|
|
|
* @param bool $forceExport |
23
|
|
|
*/ |
24
|
|
|
protected function handleExportRequest($forceExport = false) |
25
|
|
|
{ |
26
|
|
|
if (!$scope = request(Exporter::$queryName)) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// clear output buffer. |
31
|
|
|
if (ob_get_length()) { |
32
|
|
|
ob_end_clean(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->disablePagination(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if ($forceExport) { |
38
|
|
|
$this->getExporter($scope)->export(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $scope |
44
|
|
|
* |
45
|
|
|
* @return AbstractExporter |
46
|
|
|
*/ |
47
|
|
|
protected function getExporter($scope) |
48
|
|
|
{ |
49
|
|
|
return (new Exporter($this))->resolve($this->exporter)->withScope($scope); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set exporter driver for Grid to export. |
54
|
|
|
* |
55
|
|
|
* @param $exporter |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function exporter($exporter) |
60
|
|
|
{ |
61
|
|
|
$this->exporter = $exporter; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the export url. |
68
|
|
|
* |
69
|
|
|
* @param int $scope |
70
|
|
|
* @param null $args |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getExportUrl($scope = 1, $args = null) |
75
|
|
|
{ |
76
|
|
|
$input = array_merge(request()->all(), Exporter::formatExportQuery($scope, $args)); |
77
|
|
|
|
78
|
|
|
if ($constraints = $this->model()->getConstraints()) { |
|
|
|
|
79
|
|
|
$input = array_merge($input, $constraints); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->resource() . '?' . http_build_query($input); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* If grid show export btn. |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function showExportBtn() |
91
|
|
|
{ |
92
|
|
|
return $this->option('show_exporter'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Disable export. |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function disableExport(bool $disable = true) |
101
|
|
|
{ |
102
|
|
|
return $this->option('show_exporter', !$disable); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Render export button. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function renderExportButton() |
111
|
|
|
{ |
112
|
|
|
return (new Grid\Tools\ExportButton($this))->render(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \Closure $callback |
117
|
|
|
*/ |
118
|
|
|
public function export(\Closure $callback) |
119
|
|
|
{ |
120
|
|
|
if (!$scope = request(Exporter::$queryName)) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->getExporter($scope)->setCallback($callback); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.