|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers\Admin\User\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait UpdateTrait |
|
6
|
|
|
{ |
|
7
|
|
|
public function update() |
|
8
|
|
|
{ |
|
9
|
|
|
$id = userId(); |
|
10
|
|
|
|
|
11
|
|
|
$posts = $this->request->posts(); |
|
12
|
|
|
$name = array_keys($posts)[0]; |
|
13
|
|
|
$columns = $this->getUserConfigColumns(); |
|
|
|
|
|
|
14
|
|
|
$table = $columns->$name->table; |
|
15
|
|
|
$column = $columns->$name; |
|
16
|
|
|
$filters = $columns->$name->filters; |
|
17
|
|
|
$value = ($posts[$name] == '') ? null : isset($filters->date) ? date('Y-m-d', strtotime($posts[$name])) : $posts[$name]; |
|
18
|
|
|
$user_id_table_name = $column->user_id_table_name; |
|
19
|
|
|
|
|
20
|
|
|
$methods = $this->updateMethodsToCheckBeforeContinue([ |
|
21
|
|
|
'id' => $id, |
|
22
|
|
|
'name' => $name, |
|
23
|
|
|
'table' => $table, |
|
24
|
|
|
'user_id_table_name' => $user_id_table_name, |
|
25
|
|
|
'value' => $value, |
|
26
|
|
|
'filters' => $filters, |
|
27
|
|
|
]); |
|
28
|
|
|
|
|
29
|
|
|
$error = $this->checkForErrorsInUpdateMethods($methods); |
|
30
|
|
|
|
|
31
|
|
|
if ($error) { |
|
32
|
|
|
return json_encode($error); |
|
33
|
|
|
} |
|
34
|
|
|
$msg = $this->userUpdateMsg($name, $value, $filters); |
|
|
|
|
|
|
35
|
|
|
return json_encode($msg); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function checkForErrorsInUpdateMethods($methods) |
|
39
|
|
|
{ |
|
40
|
|
|
foreach ($methods as $method => $options) { |
|
41
|
|
|
if (call_user_func_array(array($this, $method), $options[0]) == false) { |
|
42
|
|
|
return $this->updateErrorMsg($options); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function updateErrorMsg($options) |
|
49
|
|
|
{ |
|
50
|
|
|
$msg = null; |
|
51
|
|
|
|
|
52
|
|
|
if (array_keys($options[1])[0] === 'msg') { |
|
53
|
|
|
$msg = array_values($options[1]); |
|
54
|
|
|
} else { |
|
55
|
|
|
if (array_keys($options[1])[0] === 'error') { |
|
56
|
|
|
$msg['error'] = $this->validator->getErrors(); |
|
57
|
|
|
} else { |
|
58
|
|
|
$msg[array_keys($options[1])[0]] = array_values($options[1]); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
return $msg; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function updateMethodsToCheckBeforeContinue($args) |
|
65
|
|
|
{ |
|
66
|
|
|
extract($args); |
|
67
|
|
|
return [ |
|
68
|
|
|
'isUserFound' => [ |
|
69
|
|
|
[$id], |
|
70
|
|
|
['msg' => 'reload'], |
|
71
|
|
|
], |
|
72
|
|
|
'isValueChanged' => [ |
|
73
|
|
|
[$name, $table, $user_id_table_name, $id, $value], |
|
74
|
|
|
['same' => $value ? strtolower($value) : ''], |
|
75
|
|
|
], |
|
76
|
|
|
'validatorPasses' => [ |
|
77
|
|
|
[$filters, $name], |
|
78
|
|
|
['error' => ''], |
|
79
|
|
|
], |
|
80
|
|
|
'updateUser' => [ |
|
81
|
|
|
[$name, $value, $user_id_table_name, $id, $table], |
|
82
|
|
|
['msg' => 'reload'], |
|
83
|
|
|
], |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function userUpdateMsg($name, $value, $filters) |
|
88
|
|
|
{ |
|
89
|
|
|
$msg = null; |
|
90
|
|
|
|
|
91
|
|
|
if ($name === 'country') { |
|
92
|
|
|
$msg['country'] = [ |
|
93
|
|
|
$value => $this->countries($value), |
|
|
|
|
|
|
94
|
|
|
]; |
|
95
|
|
|
} else { |
|
96
|
|
|
$msg['text'] = isset($filters->date) ? $this->changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
return $msg; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function updateUser($name, $value, $user_id_table_name, $id, $table) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->db->data($name, $value)->where($user_id_table_name . ' = ?', $id)->update($table); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function isUserFound($id) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->load->model('User')->get($id); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function isValueChanged($name, $table, $user_id_table_name, $id, $value) |
|
112
|
|
|
{ |
|
113
|
|
|
$current_value = $this->db->select($name)->from($table)->where($user_id_table_name . ' = ?', [$id])->fetch()->$name; |
|
114
|
|
|
if (($current_value === strtolower($value)) || ($value == null && $current_value == null)) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|