[503] | 1 | <?php
|
---|
[454] | 2 |
|
---|
[503] | 3 | if (!defined('BASEPATH'))
|
---|
| 4 | exit('No direct script access allowed');
|
---|
[466] | 5 |
|
---|
[503] | 6 | class User extends MX_Controller {
|
---|
[466] | 7 |
|
---|
[503] | 8 | const TOKENPW = 'violet';
|
---|
| 9 |
|
---|
| 10 | public function __construct() {
|
---|
| 11 | parent::__construct();
|
---|
| 12 | $this->load->helper('cookie');
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | public function profile() {
|
---|
| 16 | $user_info = $this->session->userdata('userInfo');
|
---|
[505] | 17 |
|
---|
[503] | 18 | if ($user_info) {
|
---|
| 19 | $this->load->model('servicepackage_model');
|
---|
| 20 | $user = $user_info['user'];
|
---|
| 21 | $us_id = $user_info['us_id'];
|
---|
| 22 | $src = "SBG";
|
---|
| 23 | $token = md5($us_id . self::TOKENPW);
|
---|
| 24 | $data = $this->get_data($src, $token, $us_id);
|
---|
| 25 | $data['current_tab']=$this->uri->segment(4,'edit-basic');
|
---|
| 26 | $data['packages'] = $this->servicepackage_model->getAllPackages();
|
---|
| 27 | $this->load->view('frontend/user_profile', $data);
|
---|
[505] | 28 | } else {
|
---|
[503] | 29 | redirect('frontend/home');
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public function packageRegister() {
|
---|
| 34 | $result = array();
|
---|
| 35 | $input = $this->input->post();
|
---|
| 36 | $this->load->model('user_model');
|
---|
| 37 | $user_info = $this->session->userdata('userInfo');
|
---|
| 38 | if ($user_info) {
|
---|
| 39 | parse_str($user_info['user'], $info);
|
---|
| 40 | $this->load->model('servicepackage_model');
|
---|
| 41 |
|
---|
| 42 | $package = $this->servicepackage_model->getPackage($input['p_id']);
|
---|
| 43 | $user = $user_info['user'];
|
---|
| 44 | $us_id = $user_info['us_id'];
|
---|
| 45 | $user = $this->user_model->get_user_by_id($us_id);
|
---|
| 46 | $result['can_buy'] = 1;
|
---|
| 47 | $result['can_edit'] = 0;
|
---|
| 48 | if ($user['acc_balanced'] < $package['p_price']) {
|
---|
| 49 | $result['can_buy'] = 0;
|
---|
| 50 | }
|
---|
| 51 | if ($user['p_id'] < 1) {
|
---|
| 52 | $result['can_edit'] = 1;
|
---|
| 53 | }
|
---|
| 54 | $result['info'] = $info;
|
---|
| 55 | $result['user'] = $user;
|
---|
| 56 | $result['package'] = $package;
|
---|
| 57 | $result['modal'] = $this->load->view('frontend/package_register_modal', $result, true);
|
---|
| 58 | } else {
|
---|
| 59 |
|
---|
| 60 | }
|
---|
| 61 | echo json_encode($result);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public function finishRegisterPackage() {
|
---|
| 65 | $result = array();
|
---|
| 66 | $result['success'] = 0;
|
---|
| 67 | $this->load->model('user_model');
|
---|
| 68 | $input = $this->input->post();
|
---|
| 69 | $user_info = $this->session->userdata('userInfo');
|
---|
| 70 | parse_str($user_info['user'],$info);
|
---|
| 71 | if (isset($input['fullname'])) {
|
---|
| 72 | if (strlen($input['fullname']) < 10) {
|
---|
| 73 | $result['errors'][] = "Vui lòng nháºp tên Äầy Äá»§ tá»i thiá»u 10 kà tá»±";
|
---|
| 74 | }
|
---|
| 75 | if (strlen($input['school']) < 10) {
|
---|
| 76 | $result['errors'][] = "Vui lòng nháºp ÄÆ¡n vá» quà vá» Äang cÃŽng tác tá»i thiá»u 10 kà tá»±";
|
---|
| 77 | }
|
---|
| 78 | if (!isset($result['errors'])) {
|
---|
[490] | 79 | $user_info = $this->session->userdata('userInfo');
|
---|
[503] | 80 | if ($user_info) {
|
---|
| 81 |
|
---|
| 82 | $src = "SBG";
|
---|
| 83 | $us_id=$user_info['us_id'];
|
---|
| 84 | $input['password'] = '';
|
---|
| 85 | $input['oldpass'] = '';
|
---|
| 86 | $input['gender'] = '';
|
---|
| 87 | $input['email'] ='';
|
---|
| 88 | $input['province'] = '';
|
---|
| 89 | $info['fullname']=$input['fullname'];
|
---|
| 90 | $info['school']=$input['school'];
|
---|
| 91 | $token = md5($us_id . self::TOKENPW);
|
---|
| 92 | $user = $this->get_data($src, $token, $us_id);
|
---|
| 93 | if ($input) {
|
---|
[490] | 94 | $this->load->model('user_model');
|
---|
[503] | 95 | $update = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
| 96 | $info= urldecode(http_build_query($info));
|
---|
| 97 | $userdata = array('username' => $user_info['username'], 'user' => $info, 'us_id' => $user_info['us_id'], 'logined_in' => TRUE);
|
---|
| 98 | $this->session->set_userdata('userInfo', $userdata);
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | if (isset($result['errors'])) {
|
---|
| 104 | echo json_encode($result);
|
---|
| 105 | die();
|
---|
| 106 | }
|
---|
| 107 | $this->load->model('servicepackage_model');
|
---|
| 108 | $us_id = $user_info['us_id'];
|
---|
| 109 | $user = $this->user_model->get_user_by_id($us_id);
|
---|
| 110 | $package = $this->servicepackage_model->getPackage($input['p_id']);
|
---|
| 111 | $user['acc_balanced']=$user['acc_balanced']-$package['p_price'];
|
---|
| 112 |
|
---|
| 113 | if ($package['p_unit']=="months")
|
---|
| 114 | {
|
---|
| 115 | $user['expire_date']=date('Y-m-d 00:00:00', strtotime("+".$package['p_period']." months", strtotime($user['expire_date'])));
|
---|
| 116 | }
|
---|
| 117 | if ($package['p_unit']=="days")
|
---|
| 118 | {
|
---|
| 119 | $user['expire_date']=date('Y-m-d 00:00:00', strtotime("+3 days", strtotime($user['expire_date'])));
|
---|
| 120 | }
|
---|
| 121 | $user['expire_date']=date("Y-m-d 00:00:00", (strtotime($user['expire_date'])-1));
|
---|
| 122 | $user['p_id']=$package['p_id'];
|
---|
| 123 | $this->user_model->updateUser($us_id,$user);
|
---|
| 124 | $user['expire_date']=date("d-m-Y", (strtotime($user['expire_date'])));
|
---|
| 125 | $result['expire_date']=$user['expire_date'];
|
---|
| 126 | $result['success'] = 1;
|
---|
| 127 | echo json_encode($result);
|
---|
| 128 |
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | private function get_data($src, $token, $us_id) {
|
---|
| 133 | $this->load->model('user_model');
|
---|
| 134 | $user = $this->user_model->get_user_info($src, $token, $us_id);
|
---|
| 135 |
|
---|
| 136 | if (strpos($user, '&')) {
|
---|
| 137 | parse_str($user);
|
---|
| 138 | $us = $this->user_model->get_user_by_id($us_id);
|
---|
| 139 | $data = array('us_id' => $us_id, 'username' => $username, 'fullname' => $fullname,
|
---|
| 140 | 'gender' => $gender, 'email' => $email, 'phone' => $phone, 'school' => $school, 'province' => $province,
|
---|
| 141 | 'acc_balanced' => $us['acc_balanced'], 'expire_date' => $us['expire_date'], 'date_diff' => $this->dateDiff($us['expire_date'])
|
---|
| 142 | );
|
---|
| 143 | return $data;
|
---|
| 144 | } else {
|
---|
| 145 | return "";
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | private function dateDiff($expire_date) {
|
---|
| 150 | $date1 = date("Y-m-d");
|
---|
| 151 | if ((strtotime($expire_date) - strtotime($date1)) < 0)
|
---|
| 152 | return "0 ngà y còn lại";
|
---|
| 153 | $diff = strtotime($expire_date) - strtotime($date1);
|
---|
| 154 | $years = floor($diff / (365 * 60 * 60 * 24));
|
---|
| 155 | $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
|
---|
| 156 | $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
|
---|
| 157 | if ($years > 0) {
|
---|
| 158 | return $years . " nÄm, " . $months . " tháng, " . $days . " ngà y còn lại";
|
---|
| 159 | } else {
|
---|
| 160 | if ($months > 0) {
|
---|
| 161 | return $months . " tháng, " . $days . " ngà y còn lại";
|
---|
| 162 | } else {
|
---|
| 163 | return $days . " ngà y còn lại";
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | public function test()
|
---|
| 168 | {
|
---|
| 169 | $user_info = $this->session->userdata('userInfo');
|
---|
| 170 | echo "<pre>";
|
---|
| 171 | print_r ($user_info);
|
---|
| 172 | echo "</pre>";
|
---|
| 173 | }
|
---|
| 174 | public function update_user() {
|
---|
| 175 | $this->load->helper('email');
|
---|
| 176 | $us_id = $this->uri->segment(4);
|
---|
| 177 | $user_info = $this->session->userdata('userInfo');
|
---|
| 178 |
|
---|
| 179 | if ($user_info) {
|
---|
| 180 | $result = array();
|
---|
| 181 | $result['success'] = 0;
|
---|
| 182 | $src = "SBG";
|
---|
| 183 | $token = md5($us_id . self::TOKENPW);
|
---|
| 184 | $user = $this->get_data($src, $token, $us_id);
|
---|
| 185 | $input = $this->input->post();
|
---|
| 186 | if ($input) {
|
---|
| 187 | $input['username'] = $user['username'];
|
---|
| 188 | $input['fullname'] = $user['fullname'];
|
---|
| 189 | $input['password'] = '';
|
---|
| 190 | $input['oldpass'] = '';
|
---|
| 191 | $this->load->model('user_model');
|
---|
| 192 | $result['fullname'] = $input['fullname'];
|
---|
| 193 | if (strlen($input['email']) > 0) {
|
---|
| 194 | if (!valid_email($input['email'])) {
|
---|
| 195 | $result['errors']['email_err'] = "Email khÃŽng Äúng Äá»nh dạng";
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | if (!isset($result['errors'])) {
|
---|
| 199 | $result['data'] = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
| 200 | $result['success'] = 1;
|
---|
| 201 | }
|
---|
[490] | 202 | echo json_encode($result);
|
---|
[503] | 203 | }
|
---|
| 204 | } else {
|
---|
| 205 | redirect('frontend/home');
|
---|
[490] | 206 | }
|
---|
[466] | 207 | private function get_data($src, $token, $us_id)
|
---|
[454] | 208 | {
|
---|
| 209 | $this->load->model('user_model');
|
---|
[466] | 210 | $user = $this->user_model->get_user_info($src, $token, $us_id);
|
---|
| 211 |
|
---|
| 212 | if (strpos($user, '&')){
|
---|
| 213 | parse_str($user);
|
---|
| 214 | $us = $this->user_model->get_user_by_id($us_id);
|
---|
| 215 | $data = array('us_id'=>$us_id, 'username'=>$username, 'fullname'=>$fullname,
|
---|
| 216 | 'gender'=>$gender, 'email'=>$email, 'phone'=>$phone, 'school'=>$school, 'province'=>$province,
|
---|
[502] | 217 | 'acc_balanced'=>$us['acc_balanced'], 'expire_date'=>date('d/m/Y', strtotime($us['expire_date'])), 'date_diff'=>$this->dateDiff($us['expire_date']), 'use_package'=>$us['use_package']
|
---|
[466] | 218 | );
|
---|
| 219 | return $data;
|
---|
| 220 | }else
|
---|
| 221 | {
|
---|
| 222 | return "";
|
---|
| 223 | }
|
---|
[454] | 224 | }
|
---|
| 225 |
|
---|
| 226 | private function dateDiff($expire_date)
|
---|
| 227 | {
|
---|
| 228 | $date1 = date("Y-m-d");
|
---|
[475] | 229 | if ((strtotime($expire_date) - strtotime($date1))<0) return "0 ngà y còn lại";
|
---|
| 230 | $diff = strtotime($expire_date) - strtotime($date1);
|
---|
[454] | 231 | $years = floor($diff / (365*60*60*24));
|
---|
| 232 | $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
|
---|
| 233 | $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
|
---|
| 234 | if ($years>0){
|
---|
| 235 | return $years." nÄm, ".$months." tháng, ".$days." ngà y còn lại";
|
---|
| 236 | }
|
---|
| 237 | else{
|
---|
| 238 | if ($months>0)
|
---|
| 239 | {
|
---|
| 240 | return $months." tháng, ".$days." ngà y còn lại";
|
---|
| 241 | }
|
---|
| 242 | else
|
---|
| 243 | {
|
---|
| 244 | return $days." ngà y còn lại";
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | }
|
---|
[466] | 249 |
|
---|
| 250 | public function update_user()
|
---|
| 251 | {
|
---|
| 252 | $us_id=$this->uri->segment(4);
|
---|
| 253 | $user_info = $this->session->userdata('userInfo');
|
---|
| 254 | if ($user_info)
|
---|
| 255 | {
|
---|
| 256 | $result = array();
|
---|
| 257 | $result['success'] = 0;
|
---|
| 258 | $src = "SBG";
|
---|
| 259 | $token = md5($us_id.self::TOKENPW);
|
---|
| 260 | $user = $this->get_data($src, $token, $us_id);
|
---|
| 261 | $input = $this->input->post();
|
---|
| 262 | if ($input){
|
---|
| 263 | $input['username'] = $user['username'];
|
---|
[502] | 264 | if (array_key_exists('fullname', $input))
|
---|
| 265 | {
|
---|
| 266 | if (strlen($input['fullname'])==0)
|
---|
| 267 | {
|
---|
| 268 | $result['errors']['fullname_err'] = "Há» tên khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
| 269 | }
|
---|
| 270 | }else
|
---|
| 271 | {
|
---|
| 272 | $input['fullname'] = $user['fullname'];
|
---|
| 273 | }
|
---|
| 274 | /*
|
---|
| 275 | if ($strlen($input['school']) == 0)
|
---|
| 276 | {
|
---|
| 277 | $result['errors']['school_err'] = "ÄÆ¡n vá» khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
| 278 | }
|
---|
| 279 | if ($strlen($input['province']) == 0)
|
---|
| 280 | {
|
---|
| 281 | $result['errors']['province_err'] = "Tá»nh/thà nh khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
| 282 | }*/
|
---|
[466] | 283 | $input['password'] = '';
|
---|
| 284 | $input['oldpass'] = '';
|
---|
| 285 | $this->load->model('user_model');
|
---|
[502] | 286 | //$result['fullname'] = $input['fullname'];
|
---|
[466] | 287 | if (!isset($result['errors'])){
|
---|
| 288 | $result['data'] = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
| 289 | $result['success'] = 1;
|
---|
| 290 | }
|
---|
| 291 | echo json_encode($result);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | }else
|
---|
| 295 | {
|
---|
| 296 | redirect('frontend/home');
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | public function change_password()
|
---|
| 301 | {
|
---|
| 302 | $us_id=$this->uri->segment(4);
|
---|
| 303 | $user_info = $this->session->userdata('userInfo');
|
---|
| 304 | if ($user_info)
|
---|
| 305 | {
|
---|
| 306 | $this->load->model('user_model');
|
---|
| 307 | $us_id = $user_info['us_id'];
|
---|
| 308 | $src = "SBG";
|
---|
| 309 | $token = md5($us_id.self::TOKENPW);
|
---|
[502] | 310 | $input = $this->input->post();
|
---|
[466] | 311 | if (strlen($input['passwd']) == 0)
|
---|
| 312 | {
|
---|
| 313 | $result['errors']['passwd_old_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
| 314 | }
|
---|
| 315 | if (strlen($input['passwd_new']) == 0)
|
---|
| 316 | {
|
---|
| 317 | $result['errors']['passwd_new_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
| 318 | }else if($input['passwd'] == $input['passwd_new'])
|
---|
| 319 | {
|
---|
| 320 | $result['errors']['passwd_new_err'] = "Máºt khẩu trùng vá»i máºt khẩu hiá»n tại !";
|
---|
| 321 | }
|
---|
| 322 | if ($input['passwd_new'] != $input['confirm_passwd_new'])
|
---|
| 323 | {
|
---|
| 324 | $result['errors']['confirm_passwd_new_err'] = "Máºt khẩu khÃŽng trùng nhau !";
|
---|
| 325 | }
|
---|
| 326 | if (!isset($result['errors']))
|
---|
| 327 | {
|
---|
[502] | 328 | $user = array('username'=>$user_info['username'], 'fullname'=>null, 'gender'=>null, 'email'=>null, 'school'=>null, 'province'=>null, 'password'=>$input['passwd_new'], 'oldpass'=>$input['passwd']);
|
---|
[466] | 329 | $data = $this->user_model->update_user($src, $token, $user, $us_id);
|
---|
| 330 | parse_str($data);
|
---|
| 331 | switch($status)
|
---|
| 332 | {
|
---|
| 333 | case 0:
|
---|
| 334 | $result['success'] = 1;
|
---|
| 335 | break;
|
---|
| 336 | case 6:
|
---|
| 337 | $result['errors']['passwd_old_err'] = "Máºt khẩu cung cấp khÃŽng Äúng !";
|
---|
| 338 | break;
|
---|
| 339 | default:
|
---|
| 340 | break;
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | echo json_encode($result);
|
---|
| 345 | }else
|
---|
| 346 | {
|
---|
| 347 | redirect('frontend/home');
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
[454] | 350 | } |
---|