1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Admin\User\Traits; |
4
|
|
|
|
5
|
|
|
use RandomLib\Factory as Factory; |
6
|
|
|
|
7
|
|
|
trait Add |
8
|
|
|
{ |
9
|
|
|
public function new() |
10
|
|
|
{ |
11
|
|
|
$countries = $this->countries('all', 'name'); |
|
|
|
|
12
|
|
|
|
13
|
|
|
$context = [ |
14
|
|
|
'countries' => $countries, |
15
|
|
|
]; |
16
|
|
|
return $this->view->render('admin/pages/users/new', $context); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function add() |
20
|
|
|
{ |
21
|
|
|
$msg = null; |
22
|
|
|
$posts = $this->request->posts(); |
23
|
|
|
$names = array_keys($posts); |
24
|
|
|
$allows = $this->file->call('config/admin/users/pages/add.php'); |
25
|
|
|
|
26
|
|
|
if (!$this->checkPostParametersAdd($names, $allows)) { |
27
|
|
|
$msg = 'reload'; |
28
|
|
|
return json_encode($msg); |
29
|
|
|
} |
30
|
|
|
$columns = $this->file->fileContent('config/admin/users/columns.json'); |
31
|
|
|
$columns = json_decode($columns); |
32
|
|
|
$table = $this->load->model('User')->getTable(); |
33
|
|
|
|
34
|
|
|
if (!$this->checkAddValidator($names, $columns)) { |
35
|
|
|
$msg = $this->validator->getErrors(); |
36
|
|
|
return json_encode($msg); |
37
|
|
|
} |
38
|
|
|
extract($this->generateUserIdCode()); |
39
|
|
|
|
40
|
|
|
$insertPersonalInfo = $this->insertPersonalInfo($posts, $table, $user_id, $code); |
41
|
|
|
|
42
|
|
|
if (!$insertPersonalInfo) { |
43
|
|
|
$msg = 'reload'; |
44
|
|
|
return json_encode($msg); |
45
|
|
|
} |
46
|
|
|
$insertInAddress = $this->insertUserAddress($posts, $user_id); |
47
|
|
|
$insertUserActivities = $this->insertUserActivities($user_id); |
48
|
|
|
|
49
|
|
|
if (!$insertInAddress || !$insertUserActivities) { |
50
|
|
|
$msg = 'reload'; |
51
|
|
|
return json_encode($msg); |
52
|
|
|
} |
53
|
|
|
$msg['success'] = $user_id; |
54
|
|
|
return json_encode($msg); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function checkAddValidator($names, $columns) |
58
|
|
|
{ |
59
|
|
|
foreach ($names as $name) { |
60
|
|
|
$filters = $columns->$name->filters; |
61
|
|
|
$this->validatorPasses($filters, $name); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
if ($this->validator->fails()) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function insertPersonalInfo($posts, $table, $user_id, $code) |
70
|
|
|
{ |
71
|
|
|
$birthday = date('Y-m-d', strtotime($posts['birthday'])); |
72
|
|
|
$registration = $this->changeFormatDate(microtime(true), ['U.u', 'Y-m-d H:i:s']); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $this->db->data([ |
75
|
|
|
'id' => $user_id, |
76
|
|
|
'code' => $code, |
77
|
|
|
'username' => $posts['username'], |
78
|
|
|
'fname' => $posts['fname'], |
79
|
|
|
'lname' => $posts['lname'], |
80
|
|
|
'gender' => $posts['gender'], |
81
|
|
|
'birthday' => $birthday, |
82
|
|
|
'email' => $posts['email'], |
83
|
|
|
'registration' => $registration, |
84
|
|
|
])->insert($table); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function insertUserAddress($posts, $user_id) |
88
|
|
|
{ |
89
|
|
|
return $this->db->data([ |
90
|
|
|
'user_id' => $user_id, |
91
|
|
|
'country' => $posts['country'] ?: null, |
92
|
|
|
'state' => $posts['state'] ?: null, |
93
|
|
|
'city' => $posts['city'] ?: null, |
94
|
|
|
'zip' => $posts['zip'] ?: null, |
95
|
|
|
'street' => $posts['street'] ?: null, |
96
|
|
|
'house_number' => $posts['house_number'] ?: null, |
97
|
|
|
'additional' => $posts['additional'] ?: null, |
98
|
|
|
])->insert('address'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function insertUserActivities($user_id) |
102
|
|
|
{ |
103
|
|
|
return $this->db->data([ |
104
|
|
|
'user_id' => $user_id, |
105
|
|
|
'is_login' => 0, |
106
|
|
|
])->insert('activity'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function generateUserIdCode() |
110
|
|
|
{ |
111
|
|
|
$factory = new Factory; |
112
|
|
|
$user_id = $factory->getMediumStrengthGenerator()->generateString(8, '0123456789'); |
113
|
|
|
$code = $factory->getMediumStrengthGenerator()->generateString(20, '0123456789abcdefghijklmnopqrstuvwxyz'); |
114
|
|
|
|
115
|
|
|
return [ |
116
|
|
|
'user_id' => $user_id, |
117
|
|
|
'code' => $code, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function checkPostParametersAdd($names, $allows) |
122
|
|
|
{ |
123
|
|
|
if (!array_equal($names, $allows)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|