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