Completed
Push — master ( 4cc8d8...e56a52 )
by Arjay
04:43
created

DataTable::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Yajra\Datatables\Services;
4
5
use Illuminate\Contracts\View\Factory;
6
use Yajra\Datatables\Contracts\DataTableButtonsContract;
7
use Yajra\Datatables\Contracts\DataTableContract;
8
use Yajra\Datatables\Contracts\DataTableScopeContract;
9
use Yajra\Datatables\Datatables;
10
11
abstract class DataTable implements DataTableContract, DataTableButtonsContract
12
{
13
    /**
14
     * @var \Yajra\Datatables\Datatables
15
     */
16
    protected $datatables;
17
18
    /**
19
     * @var \Illuminate\Contracts\View\Factory
20
     */
21
    protected $viewFactory;
22
23
    /**
24
     * Datatables print preview view.
25
     *
26
     * @var string
27
     */
28
    protected $printPreview;
29
30
    /**
31
     * List of columns to be exported.
32
     *
33
     * @var string|array
34
     */
35
    protected $exportColumns = '*';
36
37
    /**
38
     * List of columns to be printed.
39
     *
40
     * @var string|array
41
     */
42
    protected $printColumns = '*';
43
44
    /**
45
     * Query scopes.
46
     *
47
     * @var array
48
     */
49
    protected $scopes = [];
50
51
    /**
52
     * @param \Yajra\Datatables\Datatables $datatables
53
     * @param \Illuminate\Contracts\View\Factory $viewFactory
54
     */
55
    public function __construct(Datatables $datatables, Factory $viewFactory)
56
    {
57
        $this->datatables  = $datatables;
58
        $this->viewFactory = $viewFactory;
59
    }
60
61
    /**
62
     * Render view.
63
     *
64
     * @param $view
65
     * @param array $data
66
     * @param array $mergeData
67
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
68
     */
69
    public function render($view, $data = [], $mergeData = [])
70
    {
71
        if ($this->request()->ajax() &&  $this->request()->wantsJson()) {
72
            return $this->ajax();
73
        }
74
75
        switch ($this->datatables->getRequest()->get('action')) {
76
            case 'excel':
77
                return $this->excel();
78
79
            case 'csv':
80
                return $this->csv();
81
82
            case 'pdf':
83
                return $this->pdf();
84
85
            case 'print':
86
                return $this->printPreview();
87
88
            default:
89
                return $this->viewFactory->make($view, $data, $mergeData)->with('dataTable', $this->html());
90
        }
91
    }
92
93
    /**
94
     * Export results to Excel file.
95
     *
96
     * @return mixed
97
     */
98
    public function excel()
99
    {
100
        return $this->buildExcelFile()->download('xls');
101
    }
102
103
    /**
104
     * Build excel file and prepare for export.
105
     *
106
     * @return mixed
107
     */
108
    protected function buildExcelFile()
109
    {
110
        return app('excel')->create($this->filename(), function ($excel) {
111
            $excel->sheet('exported-data', function ($sheet) {
112
                $sheet->fromArray($this->getDataForExport());
113
            });
114
        });
115
    }
116
117
    /**
118
     * Get filename for export.
119
     *
120
     * @return string
121
     */
122
    protected function filename()
123
    {
124
        return 'export_' . time();
125
    }
126
127
    /**
128
     * Get mapped columns versus final decorated output.
129
     *
130
     * @return array
131
     */
132 View Code Duplication
    protected function getDataForExport()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $decoratedData = $this->getAjaxResponseData();
135
136
        return array_map(function ($row) {
137
            if (is_array($this->exportColumns)) {
138
                return array_only($row, $this->exportColumns);
139
            }
140
141
            return $row;
142
        }, $decoratedData);
143
    }
144
145
    /**
146
     * Get mapped columns versus final decorated output.
147
     *
148
     * @return array
149
     */
150 View Code Duplication
    protected function getDataForPrint()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $decoratedData = $this->getAjaxResponseData();
153
154
        return array_map(function ($row) {
155
            if (is_array($this->printColumns)) {
156
                return array_only($row, $this->printColumns);
157
            }
158
159
            return $row;
160
        }, $decoratedData);
161
    }
162
163
    /**
164
     * Get decorated data as defined in datatables ajax response.
165
     *
166
     * @return mixed
167
     */
168
    protected function getAjaxResponseData()
169
    {
170
        $this->datatables->getRequest()->merge(['length' => -1]);
171
172
        $response = $this->ajax();
173
        $data     = $response->getData(true);
174
175
        return $data['data'];
176
    }
177
178
    /**
179
     * Export results to CSV file.
180
     *
181
     * @return mixed
182
     */
183
    public function csv()
184
    {
185
        return $this->buildExcelFile()->download('csv');
186
    }
187
188
    /**
189
     * Export results to PDF file.
190
     *
191
     * @return mixed
192
     */
193
    public function pdf()
194
    {
195
        return $this->buildExcelFile()->download('pdf');
196
    }
197
198
    /**
199
     * Display printable view of datatables.
200
     *
201
     * @return \Illuminate\Contracts\View\View
202
     */
203
    public function printPreview()
204
    {
205
        $data = $this->getDataForPrint();
206
        $view = $this->printPreview ?: 'datatables::print';
207
208
        return $this->viewFactory->make($view, compact('data'));
209
    }
210
211
    /**
212
     * Optional method if you want to use html builder.
213
     *
214
     * @return mixed
215
     */
216
    public function html()
217
    {
218
        return $this->builder();
219
    }
220
221
    /**
222
     * Get Datatables Html Builder instance.
223
     *
224
     * @return \Yajra\Datatables\Html\Builder
225
     */
226
    public function builder()
227
    {
228
        return $this->datatables->getHtmlBuilder();
229
    }
230
231
    /**
232
     * Get Datatables Request instance.
233
     *
234
     * @return \Yajra\Datatables\Request
235
     */
236
    public function request()
237
    {
238
        return $this->datatables->getRequest();
239
    }
240
241
    /**
242
     * Add basic array query scopes.
243
     *
244
     * @param \Yajra\Datatables\Contracts\DataTableScopeContract $scope
245
     * @return $this
246
     */
247
    public function addScope(DataTableScopeContract $scope)
248
    {
249
        $this->scopes[] = $scope;
250
251
        return $this;
252
    }
253
254
    /**
255
     * Apply query scopes.
256
     *
257
     * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
258
     * @return mixed
259
     */
260
    public function applyScopes($query)
261
    {
262
        foreach ($this->scopes as $scope) {
263
            $scope->apply($query);
264
        }
265
266
        return $query;
267
    }
268
}
269