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