1
|
|
|
<?php namespace Distilleries\Expendable\States; |
2
|
|
|
|
3
|
|
|
use Distilleries\Expendable\Formatter\Message; |
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
use \View, \FormBuilder, \Input, \Redirect, \Lang, \File, \App; |
6
|
|
|
|
7
|
|
|
trait ImportStateTrait { |
8
|
|
|
|
9
|
|
|
protected $import_form = 'Distilleries\Expendable\Http\Forms\Import\ImportForm'; |
10
|
|
|
|
11
|
|
|
// ------------------------------------------------------------------------------------------------ |
12
|
|
|
// ------------------------------------------------------------------------------------------------ |
13
|
|
|
// ------------------------------------------------------------------------------------------------ |
14
|
|
|
|
15
|
4 |
View Code Duplication |
public function getImport() |
16
|
|
|
{ |
17
|
4 |
|
$form = FormBuilder::create($this->import_form, [ |
18
|
4 |
|
'model' => $this->model |
|
|
|
|
19
|
|
|
]); |
20
|
|
|
|
21
|
4 |
|
$form_content = view('expendable::admin.form.components.formgenerator.import', [ |
22
|
4 |
|
'form' => $form |
23
|
|
|
]); |
24
|
4 |
|
$content = view('expendable::admin.form.state.form', [ |
25
|
|
|
|
26
|
4 |
|
]); |
27
|
|
|
|
28
|
4 |
|
$this->layoutManager->add([ |
|
|
|
|
29
|
4 |
|
'form'=>$form_content, |
30
|
4 |
|
'content'=>$content, |
31
|
|
|
]); |
32
|
|
|
|
33
|
4 |
|
return $this->layoutManager->render(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// ------------------------------------------------------------------------------------------------ |
37
|
|
|
|
38
|
12 |
|
public function postImport(Request $request) |
39
|
|
|
{ |
40
|
|
|
|
41
|
12 |
|
$form = FormBuilder::create($this->import_form, [ |
42
|
12 |
|
'model' => $this->model |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
|
46
|
12 |
|
if ($form->hasError()) |
47
|
|
|
{ |
48
|
|
|
return $form->validateAndRedirectBack(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
12 |
|
$file = $request->get('file'); |
53
|
12 |
|
$file = urldecode(preg_replace('/\/app\//', '', $file)); |
54
|
|
|
|
55
|
12 |
|
if (!app('files')->exists($file)) |
56
|
|
|
{ |
57
|
4 |
|
return redirect()->back()->with(Message::WARNING, [trans('expendable::errors.file_not_found')]); |
58
|
|
|
} |
59
|
|
|
|
60
|
8 |
|
$contract = ucfirst(app('files')->extension($file)) . 'ImporterContract'; |
61
|
8 |
|
$exporter = app($contract); |
62
|
8 |
|
$data = $exporter->getArrayDataFromFile($file); |
63
|
|
|
|
64
|
8 |
|
foreach ($data as $item) |
65
|
|
|
{ |
66
|
8 |
|
$primary = isset($item[$this->model->getKeyName()]) ? $item[$this->model->getKeyName()] : ''; |
67
|
8 |
|
if (empty($primary)) |
68
|
|
|
{ |
69
|
8 |
|
$this->model = new $this->model; |
70
|
8 |
|
$this->model = $this->model->create($item); |
71
|
|
|
} else |
72
|
|
|
{ |
73
|
|
|
$this->model = $this->model->find($primary); |
74
|
8 |
|
$this->model->update($item); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
8 |
|
return redirect()->back()->with(Message::MESSAGE, [trans('expendable::success.imported')]); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
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: