1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Admin\User\Traits; |
4
|
|
|
|
5
|
|
|
trait Update |
6
|
|
|
{ |
7
|
|
|
public function update() |
8
|
|
|
{ |
9
|
|
|
$id = userId(); |
10
|
|
|
|
11
|
|
|
$posts = $this->request->posts(); |
12
|
|
|
$name = array_keys($posts)[0]; |
13
|
|
|
$allows = $this->file->call('config/admin/users/pages/update.php'); |
14
|
|
|
|
15
|
|
|
$columns = $this->file->fileContent('config/admin/users/columns.json'); |
16
|
|
|
$columns = json_decode($columns); |
17
|
|
|
$table = $columns->$name->table; |
18
|
|
|
$column = $columns->$name; |
19
|
|
|
$filters = $columns->$name->filters; |
20
|
|
|
$value = ($posts[$name] == '') ? null : isset($filters->date) ? date('Y-m-d', strtotime($posts[$name])) : $posts[$name]; |
21
|
|
|
$user_id_table_name = $column->user_id_table_name; |
22
|
|
|
|
23
|
|
|
$methods = $this->updateMethodsToCheckBeforeContinue([ |
24
|
|
|
'id' => $id, |
25
|
|
|
'name' => $name, |
26
|
|
|
'allows' => $allows, |
27
|
|
|
'table' => $table, |
28
|
|
|
'user_id_table_name' => $user_id_table_name, |
29
|
|
|
'value' => $value, |
30
|
|
|
'filters' => $filters, |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$error = $this->checkForErrorsInUpdateMethods($methods); |
34
|
|
|
|
35
|
|
|
if ($error) { |
36
|
|
|
return json_encode($error); |
37
|
|
|
} |
38
|
|
|
$msg = $this->userUpdateMsg($name, $value, $filters); |
|
|
|
|
39
|
|
|
return json_encode($msg); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function checkForErrorsInUpdateMethods($methods) |
43
|
|
|
{ |
44
|
|
|
foreach ($methods as $method => $options) { |
45
|
|
|
if (call_user_func_array(array($this, $method), $options[0]) == false) { |
46
|
|
|
return $this->updateErrorMsg($options); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function updateErrorMsg($options) |
53
|
|
|
{ |
54
|
|
|
$msg = null; |
55
|
|
|
|
56
|
|
|
if (array_keys($options[1])[0] === 'msg') { |
57
|
|
|
$msg = array_values($options[1]); |
58
|
|
|
} else { |
59
|
|
|
if (array_keys($options[1])[0] === 'error') { |
60
|
|
|
$msg['error'] = $this->validator->getErrors(); |
61
|
|
|
} else { |
62
|
|
|
$msg[array_keys($options[1])[0]] = array_values($options[1]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return $msg; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function updateMethodsToCheckBeforeContinue($args) |
69
|
|
|
{ |
70
|
|
|
extract($args); |
71
|
|
|
return [ |
72
|
|
|
'isUserFound' => [ |
73
|
|
|
[$id], |
74
|
|
|
['msg' => 'reload'], |
75
|
|
|
], |
76
|
|
|
'checkPostParametersUpdate' => [ |
77
|
|
|
[$name, $allows], |
78
|
|
|
['msg' => 'reload'], |
79
|
|
|
], |
80
|
|
|
'isValueChanged' => [ |
81
|
|
|
[$name, $table, $user_id_table_name, $id, $value], |
82
|
|
|
['same' => $value ? strtolower($value) : ''], |
83
|
|
|
], |
84
|
|
|
'validatorPasses' => [ |
85
|
|
|
[$filters, $name], |
86
|
|
|
['error' => ''], |
87
|
|
|
], |
88
|
|
|
'updateUser' => [ |
89
|
|
|
[$name, $value, $user_id_table_name, $id, $table], |
90
|
|
|
['msg' => 'reload'], |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function userUpdateMsg($name, $value, $filters) |
96
|
|
|
{ |
97
|
|
|
$msg = null; |
98
|
|
|
|
99
|
|
|
if ($name === 'country') { |
100
|
|
|
$msg['country'] = [ |
101
|
|
|
$value => $this->countries($value), |
|
|
|
|
102
|
|
|
]; |
103
|
|
|
} else { |
104
|
|
|
$msg['text'] = isset($filters->date) ? $this->changeFormatDate($value, ['Y-m-d', 'd M Y']) : _e($value); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
return $msg; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function updateUser($name, $value, $user_id_table_name, $id, $table) |
110
|
|
|
{ |
111
|
|
|
return $this->db->data($name, $value)->where($user_id_table_name . ' = ?', $id)->update($table); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function isUserFound($id) |
115
|
|
|
{ |
116
|
|
|
return $this->load->model('User')->get($id); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function isValueChanged($name, $table, $user_id_table_name, $id, $value) |
120
|
|
|
{ |
121
|
|
|
$current_value = $this->db->select($name)->from($table)->where($user_id_table_name . ' = ?', [$id])->fetch()->$name; |
122
|
|
|
if (($current_value === strtolower($value)) || ($value == null && $current_value == null)) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function checkPostParametersUpdate($name, $allows) |
129
|
|
|
{ |
130
|
|
|
if (!in_array($name, $allows)) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.