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