1
|
|
|
<?php namespace Distilleries\Expendable\States; |
2
|
|
|
|
3
|
|
|
use Distilleries\Expendable\Formatter\Message; |
4
|
|
|
use Distilleries\Expendable\Imports\BaseImport; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use \View, \FormBuilder, \Input, \Redirect, \Lang, \File, \App; |
7
|
|
|
|
8
|
|
|
trait ImportStateTrait { |
9
|
|
|
|
10
|
|
|
protected $import_form = 'Distilleries\Expendable\Http\Forms\Import\ImportForm'; |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------------------------------ |
13
|
|
|
// ------------------------------------------------------------------------------------------------ |
14
|
|
|
// ------------------------------------------------------------------------------------------------ |
15
|
|
|
|
16
|
2 |
View Code Duplication |
public function getImport() |
17
|
|
|
{ |
18
|
2 |
|
$form = FormBuilder::create($this->import_form, [ |
19
|
2 |
|
'model' => $this->model |
|
|
|
|
20
|
|
|
]); |
21
|
|
|
|
22
|
2 |
|
$form_content = view('expendable::admin.form.components.formgenerator.import', [ |
23
|
2 |
|
'form' => $form |
24
|
|
|
]); |
25
|
2 |
|
$content = view('expendable::admin.form.state.form', [ |
26
|
|
|
|
27
|
2 |
|
]); |
28
|
|
|
|
29
|
2 |
|
$this->layoutManager->add([ |
|
|
|
|
30
|
2 |
|
'form'=>$form_content, |
31
|
2 |
|
'content'=>$content, |
32
|
|
|
]); |
33
|
|
|
|
34
|
2 |
|
return $this->layoutManager->render(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// ------------------------------------------------------------------------------------------------ |
38
|
|
|
|
39
|
6 |
|
public function postImport(Request $request) |
40
|
|
|
{ |
41
|
|
|
|
42
|
6 |
|
$form = FormBuilder::create($this->import_form, [ |
43
|
6 |
|
'model' => $this->model |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
|
47
|
6 |
|
if ($form->hasError()) |
48
|
|
|
{ |
49
|
|
|
return $form->validateAndRedirectBack(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
6 |
|
$file = $request->get('file'); |
54
|
6 |
|
$file = urldecode($file); |
55
|
6 |
|
if (!app('files')->exists($file)) |
56
|
|
|
{ |
57
|
2 |
|
return redirect()->back()->with(Message::WARNING, [trans('expendable::errors.file_not_found')]); |
58
|
|
|
} |
59
|
4 |
|
$file = str_replace(storage_path('app/'), '', $file); |
60
|
|
|
|
61
|
4 |
|
(new BaseImport($this->model))->importFromFile($file); |
62
|
|
|
|
63
|
4 |
|
return redirect()->back()->with(Message::MESSAGE, [trans('expendable::success.imported')]); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: