1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\Controllers\CBController; |
4
|
|
|
|
5
|
|
|
use Crocodicstudio\Crudbooster\CBCoreModule\RelationHandler; |
6
|
|
|
use Crocodicstudio\Crudbooster\Controllers\FormValidator; |
7
|
|
|
use Crocodicstudio\Crudbooster\Helpers\CRUDBooster; |
8
|
|
|
use Crocodicstudio\Crudbooster\Helpers\DbInspector; |
9
|
|
|
use Illuminate\Support\Facades\Request; |
10
|
|
|
use Illuminate\Support\Facades\Schema; |
11
|
|
|
|
12
|
|
|
trait FormSubmitHandlers |
13
|
|
|
{ |
14
|
|
|
public $arr = []; |
15
|
|
|
|
16
|
|
|
public $return_url = null; |
17
|
|
|
|
18
|
|
|
public function inputAssignment($id = null) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$hide_form = (request('hide_form')) ? unserialize(request('hide_form')) : []; |
|
|
|
|
21
|
|
|
|
22
|
|
|
foreach ($this->form as $form) { |
23
|
|
|
$name = $form['name']; |
24
|
|
|
$type = $form['type'] ?: 'text'; |
25
|
|
|
$inputdata = request($name); |
26
|
|
|
|
27
|
|
|
if (! $name || in_array($name, $hide_form) || $form['exception']) { |
28
|
|
|
continue; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$hookPath = CbComponentsPath($type).DIRECTORY_SEPARATOR.'hookInputAssignment.php'; |
32
|
|
|
if (file_exists($hookPath)) { |
33
|
|
|
require_once($hookPath); |
34
|
|
|
} |
35
|
|
|
unset($hookPath); |
36
|
|
|
|
37
|
|
|
if (Request::hasFile($name)) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($inputdata == '' && DbInspector::isNullableColumn($this->table, $name)) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->arr[$name] = ''; |
46
|
|
|
|
47
|
|
|
if ($inputdata != '') { |
48
|
|
|
$this->arr[$name] = $inputdata; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function postAddSave() |
54
|
|
|
{ |
55
|
|
|
$this->genericLoader(); |
|
|
|
|
56
|
|
|
|
57
|
|
|
app(FormValidator::class)->validate(null, $this->form, $this); |
58
|
|
|
$this->inputAssignment(); |
59
|
|
|
|
60
|
|
|
$this->setTimeStamps('created_at'); |
61
|
|
|
|
62
|
|
|
$this->arr = $this->hookBeforeAdd($this->arr); |
|
|
|
|
63
|
|
|
$id = (int) $this->table()->insertGetId($this->arr); |
|
|
|
|
64
|
|
|
app(RelationHandler::class)->save($this->table, $id, $this->data_inputan); |
65
|
|
|
$this->hookAfterAdd($id); |
|
|
|
|
66
|
|
|
|
67
|
|
|
event('cb.dataInserted', [$this->table, $id, YmdHis(), cbUser()]); |
68
|
|
|
$this->sendResponseForSave('alert_add_data_success'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function postEditSave($id) |
72
|
|
|
{ |
73
|
|
|
$id = (int) $id; |
74
|
|
|
$this->genericLoader(); |
75
|
|
|
|
76
|
|
|
app(FormValidator::class)->validate($id, $this->form, $this); |
77
|
|
|
$this->inputAssignment($id); |
78
|
|
|
|
79
|
|
|
$this->setTimeStamps('updated_at'); |
80
|
|
|
|
81
|
|
|
$this->arr = $this->hookBeforeEdit($this->arr, $id); |
|
|
|
|
82
|
|
|
$this->findRow($id)->update($this->arr); |
|
|
|
|
83
|
|
|
app(RelationHandler::class)->save($this->table, $id, $this->data_inputan); |
84
|
|
|
$this->hookAfterEdit($id); |
|
|
|
|
85
|
|
|
|
86
|
|
|
event('cb.dataUpdated', [$this->table, $id, YmdHis(), cbUser()]); |
87
|
|
|
|
88
|
|
|
$this->sendResponseForSave('alert_update_data_success'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function sendResponseForSave($msg) |
92
|
|
|
{ |
93
|
|
|
$this->return_url = $this->return_url ?: request('return_url'); |
94
|
|
|
if ($this->return_url) { |
95
|
|
|
if (request('submit') == cbTrans('button_save_more')) { |
96
|
|
|
backWithMsg(cbTrans($msg), 'success'); |
97
|
|
|
} |
98
|
|
|
CRUDBooster::redirect($this->return_url, cbTrans($msg), 'success'); |
99
|
|
|
} |
100
|
|
|
if (request('submit') == cbTrans('button_save_more')) { |
101
|
|
|
CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans($msg), 'success'); |
102
|
|
|
} |
103
|
|
|
CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans($msg), 'success'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function setTimeStamps($col) |
107
|
|
|
{ |
108
|
|
|
if (Schema::hasColumn($this->table, $col)) { |
109
|
|
|
$this->arr[$col] = YmdHis(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.