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