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