[521] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | if (!defined('BASEPATH'))
|
---|
| 4 | exit('No direct script access allowed');
|
---|
| 5 |
|
---|
[503] | 6 | class User extends MX_Controller {
|
---|
[466] | 7 |
|
---|
[521] | 8 | const TOKENPW = 'violet';
|
---|
[503] | 9 |
|
---|
[521] | 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');
|
---|
| 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);
|
---|
[521] | 25 |
|
---|
| 26 | $data['message'] = $this->session->flashdata('message');
|
---|
[508] | 27 | $data['current_tab'] = $this->uri->segment(4, 'edit-basic');
|
---|
[503] | 28 | $data['packages'] = $this->servicepackage_model->getAllPackages();
|
---|
| 29 | $this->load->view('frontend/user_profile', $data);
|
---|
[508] | 30 | } else {
|
---|
[503] | 31 | redirect('frontend/home');
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public function packageRegister() {
|
---|
| 36 | $result = array();
|
---|
| 37 | $input = $this->input->post();
|
---|
| 38 | $this->load->model('user_model');
|
---|
| 39 | $user_info = $this->session->userdata('userInfo');
|
---|
| 40 | if ($user_info) {
|
---|
| 41 | parse_str($user_info['user'], $info);
|
---|
| 42 | $this->load->model('servicepackage_model');
|
---|
[508] | 43 |
|
---|
[503] | 44 | $package = $this->servicepackage_model->getPackage($input['p_id']);
|
---|
| 45 | $user = $user_info['user'];
|
---|
| 46 | $us_id = $user_info['us_id'];
|
---|
| 47 | $user = $this->user_model->get_user_by_id($us_id);
|
---|
| 48 | $result['can_buy'] = 1;
|
---|
| 49 | $result['can_edit'] = 0;
|
---|
| 50 | if ($user['acc_balanced'] < $package['p_price']) {
|
---|
| 51 | $result['can_buy'] = 0;
|
---|
| 52 | }
|
---|
| 53 | if ($user['p_id'] < 1) {
|
---|
| 54 | $result['can_edit'] = 1;
|
---|
| 55 | }
|
---|
| 56 | $result['info'] = $info;
|
---|
| 57 | $result['user'] = $user;
|
---|
| 58 | $result['package'] = $package;
|
---|
| 59 | $result['modal'] = $this->load->view('frontend/package_register_modal', $result, true);
|
---|
| 60 | } else {
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 | echo json_encode($result);
|
---|
| 64 | }
|
---|
[521] | 65 |
|
---|
[503] | 66 | public function finishRegisterPackage() {
|
---|
| 67 | $result = array();
|
---|
| 68 | $result['success'] = 0;
|
---|
| 69 | $this->load->model('user_model');
|
---|
| 70 | $input = $this->input->post();
|
---|
| 71 | $user_info = $this->session->userdata('userInfo');
|
---|
[508] | 72 | parse_str($user_info['user'], $info);
|
---|
[503] | 73 | if (isset($input['fullname'])) {
|
---|
| 74 | if (strlen($input['fullname']) < 10) {
|
---|
| 75 | $result['errors'][] = "Vui lòng nháºp tên Äầy Äá»§ tá»i thiá»u 10 kà tá»±";
|
---|
| 76 | }
|
---|
| 77 | if (strlen($input['school']) < 10) {
|
---|
[521] | 78 | $result['errors'][] = "Vui lòng nháºp ÄÆ¡n vá» quà vá» Äang cÃŽng tác tá»i thiá»u 10 kà tá»±";
|
---|
[503] | 79 | }
|
---|
| 80 | if (!isset($result['errors'])) {
|
---|
[490] | 81 | $user_info = $this->session->userdata('userInfo');
|
---|
[503] | 82 | if ($user_info) {
|
---|
[508] | 83 |
|
---|
[503] | 84 | $src = "SBG";
|
---|
[508] | 85 | $us_id = $user_info['us_id'];
|
---|
[503] | 86 | $input['password'] = '';
|
---|
| 87 | $input['oldpass'] = '';
|
---|
| 88 | $input['gender'] = '';
|
---|
[508] | 89 | $input['email'] = '';
|
---|
[503] | 90 | $input['province'] = '';
|
---|
[508] | 91 | $info['fullname'] = $input['fullname'];
|
---|
| 92 | $info['school'] = $input['school'];
|
---|
[503] | 93 | $token = md5($us_id . self::TOKENPW);
|
---|
| 94 | $user = $this->get_data($src, $token, $us_id);
|
---|
| 95 | if ($input) {
|
---|
[490] | 96 | $this->load->model('user_model');
|
---|
[503] | 97 | $update = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
[508] | 98 | $info = urldecode(http_build_query($info));
|
---|
[503] | 99 | $userdata = array('username' => $user_info['username'], 'user' => $info, 'us_id' => $user_info['us_id'], 'logined_in' => TRUE);
|
---|
| 100 | $this->session->set_userdata('userInfo', $userdata);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | if (isset($result['errors'])) {
|
---|
[521] | 106 | echo json_encode ($result);
|
---|
[503] | 107 | die();
|
---|
| 108 | }
|
---|
| 109 | $this->load->model('servicepackage_model');
|
---|
| 110 | $us_id = $user_info['us_id'];
|
---|
| 111 | $user = $this->user_model->get_user_by_id($us_id);
|
---|
| 112 | $package = $this->servicepackage_model->getPackage($input['p_id']);
|
---|
[508] | 113 | $user['acc_balanced'] = $user['acc_balanced'] - $package['p_price'];
|
---|
[530] | 114 | $user['expire_date'] = date('Y-m-d 00:00:00', strtotime("+" . $package['p_period'] . " ".$package['p_unit'], strtotime($user['expire_date'])));
|
---|
[508] | 115 | $user['expire_date'] = date("Y-m-d 00:00:00", (strtotime($user['expire_date']) - 1));
|
---|
| 116 | $user['p_id'] = $package['p_id'];
|
---|
| 117 | $this->user_model->updateUser($us_id, $user);
|
---|
| 118 | $user['expire_date'] = date("d-m-Y", (strtotime($user['expire_date'])));
|
---|
| 119 | $result['expire_date'] = $user['expire_date'];
|
---|
[503] | 120 | $result['success'] = 1;
|
---|
| 121 | echo json_encode($result);
|
---|
| 122 | }
|
---|
[521] | 123 |
|
---|
| 124 | public function regpackage ()
|
---|
| 125 | {
|
---|
| 126 | $user_info = $this->session->userdata('userInfo');
|
---|
| 127 | parse_str($user_info['user'], $info);
|
---|
| 128 | $p_id = $this->input->post('p_id', TRUE);
|
---|
| 129 |
|
---|
| 130 | $us_id = (int)$info['id'];
|
---|
| 131 | $this->load->model('servicepackage_model');
|
---|
| 132 | $this->load->model('user_model');
|
---|
| 133 |
|
---|
| 134 | $user = $this->user_model->get_user_by_id($us_id);
|
---|
| 135 | $package = $this->servicepackage_model->getPackage($p_id);
|
---|
| 136 |
|
---|
| 137 | if ($user['acc_balanced'] < $package['p_price']) {
|
---|
| 138 | $message = 'Sá» tiá»n trong Tà i khoản SBG Online hiá»n tại cá»§a Quà vá» là '. $user['acc_balanced'] .' Äá»ng.<br /> Äá» ÄÄng kà Sá» dụng dá»ch vụ Soạn Bà i giảng trá»±c tuyến, Quà vá» cần nạp thêm tiá»n và o tà i khoản.';
|
---|
| 139 | }
|
---|
| 140 | else {
|
---|
| 141 | $user['acc_balanced'] = $user['acc_balanced'] - $package['p_price'];
|
---|
| 142 | $currentExpireDate = $user['expire_date'] == NULL || trim($user['expire_date']) == '' ? date('Y-m-d 00:00:00') : $user['expire_date'];
|
---|
| 143 |
|
---|
| 144 | $user['expire_date'] = date('Y-m-d', strtotime("+" . $package['p_period'] ." ". $package['p_unit'], strtotime($currentExpireDate)));
|
---|
| 145 | //$user['expire_date'] = date("Y-m-d", (strtotime($user['expire_date']) - 1));
|
---|
| 146 |
|
---|
| 147 | $user['p_id'] = $package['p_id'];
|
---|
| 148 |
|
---|
| 149 | $this->user_model->updateUser($us_id, $user);
|
---|
| 150 | $message = 'QuÜ vá» Äã ÄÄng kà sá» dụng CÃŽng cụ Soạn Bà i giảng trức tuyến. Quà vá» ÄÆ°á»£c sá» dụng dá»ch vụ Äến hết ngà y '. date('d-m-Y', strtotime($user['expire_date']));
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | $this->session->set_flashdata('message', $message);
|
---|
| 154 | redirect('frontend/user/profile#services-register');
|
---|
| 155 | }
|
---|
[503] | 156 |
|
---|
| 157 | private function get_data($src, $token, $us_id) {
|
---|
| 158 | $this->load->model('user_model');
|
---|
| 159 | $user = $this->user_model->get_user_info($src, $token, $us_id);
|
---|
| 160 |
|
---|
| 161 | if (strpos($user, '&')) {
|
---|
| 162 | parse_str($user);
|
---|
| 163 | $us = $this->user_model->get_user_by_id($us_id);
|
---|
| 164 | $data = array('us_id' => $us_id, 'username' => $username, 'fullname' => $fullname,
|
---|
| 165 | 'gender' => $gender, 'email' => $email, 'phone' => $phone, 'school' => $school, 'province' => $province,
|
---|
| 166 | 'acc_balanced' => $us['acc_balanced'], 'expire_date' => $us['expire_date'], 'date_diff' => $this->dateDiff($us['expire_date'])
|
---|
| 167 | );
|
---|
| 168 | return $data;
|
---|
| 169 | } else {
|
---|
| 170 | return "";
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[508] | 174 |
|
---|
[503] | 175 | private function dateDiff($expire_date) {
|
---|
| 176 | $date1 = date("Y-m-d");
|
---|
| 177 | if ((strtotime($expire_date) - strtotime($date1)) < 0)
|
---|
| 178 | return "0 ngà y còn lại";
|
---|
| 179 | $diff = strtotime($expire_date) - strtotime($date1);
|
---|
| 180 | $years = floor($diff / (365 * 60 * 60 * 24));
|
---|
| 181 | $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
|
---|
| 182 | $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
|
---|
| 183 | if ($years > 0) {
|
---|
| 184 | return $years . " nÄm, " . $months . " tháng, " . $days . " ngà y còn lại";
|
---|
| 185 | } else {
|
---|
| 186 | if ($months > 0) {
|
---|
| 187 | return $months . " tháng, " . $days . " ngà y còn lại";
|
---|
| 188 | } else {
|
---|
| 189 | return $days . " ngà y còn lại";
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
[508] | 193 |
|
---|
[503] | 194 | public function update_user() {
|
---|
| 195 | $us_id = $this->uri->segment(4);
|
---|
| 196 | $user_info = $this->session->userdata('userInfo');
|
---|
| 197 | if ($user_info) {
|
---|
| 198 | $result = array();
|
---|
| 199 | $result['success'] = 0;
|
---|
| 200 | $src = "SBG";
|
---|
| 201 | $token = md5($us_id . self::TOKENPW);
|
---|
| 202 | $user = $this->get_data($src, $token, $us_id);
|
---|
| 203 | $input = $this->input->post();
|
---|
| 204 | if ($input) {
|
---|
| 205 | $input['username'] = $user['username'];
|
---|
[508] | 206 | if (array_key_exists('fullname', $input)) {
|
---|
| 207 | if (strlen($input['fullname']) == 0) {
|
---|
[521] | 208 | $result['errors']['fullname_err'] = "Há» tên khÃŽng ÄÆ°á»£c Äá» trá»ng!";
|
---|
[508] | 209 | }
|
---|
| 210 | } else {
|
---|
| 211 | $input['fullname'] = $user['fullname'];
|
---|
| 212 | }
|
---|
| 213 | /*
|
---|
| 214 | if ($strlen($input['school']) == 0)
|
---|
| 215 | {
|
---|
[521] | 216 | $result['errors']['school_err'] = "ÄÆ¡n vá» khÃŽng ÄÆ°á»£c Äá» trá»ng!";
|
---|
[508] | 217 | }
|
---|
| 218 | if ($strlen($input['province']) == 0)
|
---|
| 219 | {
|
---|
[521] | 220 | $result['errors']['province_err'] = "Tá»nh/thà nh khÃŽng ÄÆ°á»£c Äá» trá»ng!";
|
---|
[508] | 221 | } */
|
---|
[503] | 222 | $input['password'] = '';
|
---|
| 223 | $input['oldpass'] = '';
|
---|
| 224 | $this->load->model('user_model');
|
---|
[508] | 225 | //$result['fullname'] = $input['fullname'];
|
---|
[503] | 226 | if (!isset($result['errors'])) {
|
---|
| 227 | $result['data'] = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
| 228 | $result['success'] = 1;
|
---|
| 229 | }
|
---|
[490] | 230 | echo json_encode($result);
|
---|
[503] | 231 | }
|
---|
| 232 | } else {
|
---|
| 233 | redirect('frontend/home');
|
---|
[490] | 234 | }
|
---|
[508] | 235 | }
|
---|
| 236 |
|
---|
| 237 | public function change_password() {
|
---|
| 238 | $us_id = $this->uri->segment(4);
|
---|
| 239 | $user_info = $this->session->userdata('userInfo');
|
---|
| 240 | if ($user_info) {
|
---|
| 241 | $this->load->model('user_model');
|
---|
| 242 | $us_id = $user_info['us_id'];
|
---|
| 243 | $src = "SBG";
|
---|
| 244 | $token = md5($us_id . self::TOKENPW);
|
---|
| 245 | $input = $this->input->post();
|
---|
[521] | 246 |
|
---|
| 247 |
|
---|
| 248 |
|
---|
[508] | 249 | if (strlen($input['passwd']) == 0) {
|
---|
[521] | 250 | $result['errors']['passwd_old_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng!";
|
---|
[508] | 251 | }
|
---|
| 252 | if (strlen($input['passwd_new']) == 0) {
|
---|
[521] | 253 | $result['errors']['passwd_new_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng!";
|
---|
[508] | 254 | } else if ($input['passwd'] == $input['passwd_new']) {
|
---|
[521] | 255 | $result['errors']['passwd_new_err'] = "Máºt khẩu trùng vá»i máºt khẩu hiá»n tại!";
|
---|
[508] | 256 | }
|
---|
[521] | 257 | if ($input['passwd_new']!= $input['confirm_passwd_new']) {
|
---|
| 258 | $result['errors']['confirm_passwd_new_err'] = "Máºt khẩu khÃŽng trùng nhau!";
|
---|
[508] | 259 | }
|
---|
[521] | 260 |
|
---|
[508] | 261 | if (!isset($result['errors'])) {
|
---|
| 262 | $user = array('username' => $user_info['username'], 'fullname' => null, 'gender' => null, 'email' => null, 'school' => null, 'province' => null, 'password' => $input['passwd_new'], 'oldpass' => $input['passwd']);
|
---|
| 263 | $data = $this->user_model->update_user($src, $token, $user, $us_id);
|
---|
| 264 | parse_str($data);
|
---|
| 265 | switch ($status) {
|
---|
| 266 | case 0:
|
---|
| 267 | $result['success'] = 1;
|
---|
| 268 | break;
|
---|
| 269 | case 6:
|
---|
[521] | 270 | $result['errors']['passwd_old_err'] = "Máºt khẩu cung cấp khÃŽng Äúng!";
|
---|
[508] | 271 | break;
|
---|
| 272 | default:
|
---|
| 273 | break;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | echo json_encode($result);
|
---|
| 278 | } else {
|
---|
| 279 | redirect('frontend/home');
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[521] | 283 | } |
---|