|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larrock\Core\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Session; |
|
6
|
|
|
use Redirect; |
|
7
|
|
|
use Validator; |
|
8
|
|
|
use Larrock\Core\Component; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Larrock\Core\Helpers\MessageLarrock; |
|
11
|
|
|
use Larrock\Core\Events\ComponentItemStored; |
|
12
|
|
|
use Larrock\Core\Helpers\FormBuilder\FormDate; |
|
13
|
|
|
|
|
14
|
|
|
trait AdminMethodsStore |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Component */ |
|
17
|
|
|
protected $config; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool Разрешать ли делать редиректы |
|
21
|
|
|
* Возвращать пользователя на страницу вместо ответа api |
|
22
|
|
|
*/ |
|
23
|
|
|
public $allow_redirect = true; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Component $config |
|
27
|
|
|
* @return AdminMethodsStore |
|
28
|
|
|
*/ |
|
29
|
|
|
public function updateConfig($config) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->config = $config; |
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Store a newly created resource in storage. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Http\Request $request |
|
40
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response |
|
41
|
|
|
* @throws \Exception |
|
42
|
|
|
*/ |
|
43
|
|
|
public function store(Request $request) |
|
44
|
|
|
{ |
|
45
|
|
|
$validate_data_array = $request->all(); |
|
46
|
|
|
$data = $this->config->getModel(); |
|
47
|
|
|
$data->fill($request->all()); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($this->config->rows as $row) { |
|
50
|
|
|
if ($row->fillable && ! isset($data->{$row->name})) { |
|
51
|
|
|
if ($row instanceof FormDate) { |
|
52
|
|
|
$data->{$row->name} = $request->input('date', date('Y-m-d')); |
|
53
|
|
|
} else { |
|
54
|
|
|
$data->{$row->name} = $request->input($row->name, $row->default); |
|
55
|
|
|
} |
|
56
|
|
|
$validate_data_array[$row->name] = $data->{$row->name}; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$validator = Validator::make($validate_data_array, $this->config->getValid()); |
|
61
|
|
|
if ($validator->fails()) { |
|
62
|
|
|
if ($this->allow_redirect) { |
|
63
|
|
|
if (array_key_exists('url', $validator->failed())) { |
|
64
|
|
|
$search = $this->config->getModel()::whereUrl($data->url)->first(); |
|
65
|
|
|
MessageLarrock::danger('Материал с тарим url уже существует: /admin/'.$this->config->name.'/'.$search->id.'/edit'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return back()->withInput($request->except('password'))->withErrors($validator); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return response()->json(['status' => 'danger', 'message' => $validator->errors()->first()]); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$data->save(); |
|
75
|
|
|
event(new ComponentItemStored($this->config, $data, $request)); |
|
76
|
|
|
\Cache::flush(); |
|
77
|
|
|
Session::push('message.success', 'Материал '.$request->input('title').' добавлен'); |
|
78
|
|
|
if ($this->allow_redirect) { |
|
79
|
|
|
return Redirect::to('/admin/'.$this->config->name.'/'.$data->id.'/edit')->withInput(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $data; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|