1
|
|
|
<?php namespace Distilleries\Expendable\States; |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use \FormBuilder; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Distilleries\Expendable\Exports\BaseExport; |
7
|
|
|
|
8
|
|
|
trait ExportStateTrait { |
9
|
|
|
|
10
|
|
|
protected $export_form = 'Distilleries\Expendable\Http\Forms\Export\ExportForm'; |
11
|
|
|
// ------------------------------------------------------------------------------------------------ |
12
|
|
|
// ------------------------------------------------------------------------------------------------ |
13
|
|
|
// ------------------------------------------------------------------------------------------------ |
14
|
|
|
|
15
|
2 |
|
public function getExport() |
16
|
|
|
{ |
17
|
2 |
|
$form = FormBuilder::create($this->export_form, [ |
18
|
2 |
|
'model' => $this->model |
19
|
|
|
]); |
20
|
|
|
|
21
|
2 |
|
$form_content = view('expendable::admin.form.components.formgenerator.export', [ |
22
|
2 |
|
'form' => $form |
23
|
|
|
]); |
24
|
2 |
|
$content = view('expendable::admin.form.state.form', [ |
25
|
|
|
|
26
|
2 |
|
]); |
27
|
|
|
|
28
|
2 |
|
$this->layoutManager->add([ |
29
|
2 |
|
'form'=>$form_content, |
30
|
2 |
|
'content'=>$content, |
31
|
|
|
]); |
32
|
|
|
|
33
|
2 |
|
return $this->layoutManager->render(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// ------------------------------------------------------------------------------------------------ |
37
|
|
|
|
38
|
12 |
|
public function postExport(Request $request) |
39
|
|
|
{ |
40
|
|
|
|
41
|
12 |
|
$form = FormBuilder::create($this->export_form, [ |
42
|
12 |
|
'model' => $this->model |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
|
46
|
12 |
|
if ($form->hasError()) |
47
|
|
|
{ |
48
|
6 |
|
return $form->validateAndRedirectBack(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
6 |
|
$data = $request->all(); |
53
|
6 |
|
$dateStart = !empty($data['range']) && !empty($data['range']['start']) ? $data['range']['start'] : Carbon::now()->format('Y-m-d'); |
54
|
6 |
|
$dateEnd = !empty($data['range']) && !empty($data['range']['end']) ? $data['range']['end'] : Carbon::now()->format('Y-m-d'); |
55
|
6 |
|
$type = !empty($data['type']) ? $data['type'] : 'csv'; |
56
|
6 |
|
$filename = str_replace('/', '-', $dateStart.'_'.$dateEnd).'.'.mb_strtolower($type); |
57
|
|
|
|
58
|
6 |
|
return (new BaseExport($this->model, $data))->export($filename); |
59
|
|
|
} |
60
|
|
|
} |