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