1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
---|
2 |
|
---|
3 | class User extends MX_Controller
|
---|
4 | {
|
---|
5 |
|
---|
6 | const TOKENPW = 'violet';
|
---|
7 |
|
---|
8 | public function __construct()
|
---|
9 | {
|
---|
10 | parent::__construct();
|
---|
11 | $this->load->helper('cookie');
|
---|
12 | }
|
---|
13 |
|
---|
14 | public function profile()
|
---|
15 | {
|
---|
16 | $user_info = $this->session->userdata('userInfo');
|
---|
17 | if ($user_info)
|
---|
18 | {
|
---|
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['packages'] =$this->servicepackage_model->getAllPackages();
|
---|
26 | $this->load->view('frontend/user_profile', $data);
|
---|
27 |
|
---|
28 | }else
|
---|
29 | {
|
---|
30 | redirect('frontend/home');
|
---|
31 | }
|
---|
32 | }
|
---|
33 | public function packageRegister()
|
---|
34 | {
|
---|
35 | $result=array();
|
---|
36 | $input=$this->input->post();
|
---|
37 | $user_info = $this->session->userdata('userInfo');
|
---|
38 | if ($user_info)
|
---|
39 | {
|
---|
40 | $this->load->model('servicepackage_model');
|
---|
41 | $this->load->model('user_model');
|
---|
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 | {
|
---|
50 | $result['can_buy']=0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if ($user['p_id'] < 1)
|
---|
54 | {
|
---|
55 | $result['can_edit']=1;
|
---|
56 | }
|
---|
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 | }
|
---|
64 | echo json_encode($result);
|
---|
65 | }
|
---|
66 | private function get_data($src, $token, $us_id)
|
---|
67 | {
|
---|
68 | $this->load->model('user_model');
|
---|
69 | $user = $this->user_model->get_user_info($src, $token, $us_id);
|
---|
70 |
|
---|
71 | if (strpos($user, '&')){
|
---|
72 | parse_str($user);
|
---|
73 | $us = $this->user_model->get_user_by_id($us_id);
|
---|
74 | $data = array('us_id'=>$us_id, 'username'=>$username, 'fullname'=>$fullname,
|
---|
75 | 'gender'=>$gender, 'email'=>$email, 'phone'=>$phone, 'school'=>$school, 'province'=>$province,
|
---|
76 | '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']
|
---|
77 | );
|
---|
78 | return $data;
|
---|
79 | }else
|
---|
80 | {
|
---|
81 | return "";
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private function dateDiff($expire_date)
|
---|
86 | {
|
---|
87 | $date1 = date("Y-m-d");
|
---|
88 | if ((strtotime($expire_date) - strtotime($date1))<0) return "0 ngà y còn lại";
|
---|
89 | $diff = strtotime($expire_date) - strtotime($date1);
|
---|
90 | $years = floor($diff / (365*60*60*24));
|
---|
91 | $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
|
---|
92 | $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
|
---|
93 | if ($years>0){
|
---|
94 | return $years." nÄm, ".$months." tháng, ".$days." ngà y còn lại";
|
---|
95 | }
|
---|
96 | else{
|
---|
97 | if ($months>0)
|
---|
98 | {
|
---|
99 | return $months." tháng, ".$days." ngà y còn lại";
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | return $days." ngà y còn lại";
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | public function update_user()
|
---|
110 | {
|
---|
111 | $us_id=$this->uri->segment(4);
|
---|
112 | $user_info = $this->session->userdata('userInfo');
|
---|
113 | if ($user_info)
|
---|
114 | {
|
---|
115 | $result = array();
|
---|
116 | $result['success'] = 0;
|
---|
117 | $src = "SBG";
|
---|
118 | $token = md5($us_id.self::TOKENPW);
|
---|
119 | $user = $this->get_data($src, $token, $us_id);
|
---|
120 | $input = $this->input->post();
|
---|
121 | if ($input){
|
---|
122 | $input['username'] = $user['username'];
|
---|
123 | if (array_key_exists('fullname', $input))
|
---|
124 | {
|
---|
125 | if (strlen($input['fullname'])==0)
|
---|
126 | {
|
---|
127 | $result['errors']['fullname_err'] = "Há» tên khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
128 | }
|
---|
129 | }else
|
---|
130 | {
|
---|
131 | $input['fullname'] = $user['fullname'];
|
---|
132 | }
|
---|
133 | /*
|
---|
134 | if ($strlen($input['school']) == 0)
|
---|
135 | {
|
---|
136 | $result['errors']['school_err'] = "ÄÆ¡n vá» khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
137 | }
|
---|
138 | if ($strlen($input['province']) == 0)
|
---|
139 | {
|
---|
140 | $result['errors']['province_err'] = "Tá»nh/thà nh khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
141 | }*/
|
---|
142 | $input['password'] = '';
|
---|
143 | $input['oldpass'] = '';
|
---|
144 | $this->load->model('user_model');
|
---|
145 | //$result['fullname'] = $input['fullname'];
|
---|
146 | if (!isset($result['errors'])){
|
---|
147 | $result['data'] = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
148 | $result['success'] = 1;
|
---|
149 | }
|
---|
150 | echo json_encode($result);
|
---|
151 | }
|
---|
152 |
|
---|
153 | }else
|
---|
154 | {
|
---|
155 | redirect('frontend/home');
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public function change_password()
|
---|
160 | {
|
---|
161 | $us_id=$this->uri->segment(4);
|
---|
162 | $user_info = $this->session->userdata('userInfo');
|
---|
163 | if ($user_info)
|
---|
164 | {
|
---|
165 | $this->load->model('user_model');
|
---|
166 | $us_id = $user_info['us_id'];
|
---|
167 | $src = "SBG";
|
---|
168 | $token = md5($us_id.self::TOKENPW);
|
---|
169 | $input = $this->input->post();
|
---|
170 | if (strlen($input['passwd']) == 0)
|
---|
171 | {
|
---|
172 | $result['errors']['passwd_old_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
173 | }
|
---|
174 | if (strlen($input['passwd_new']) == 0)
|
---|
175 | {
|
---|
176 | $result['errors']['passwd_new_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
177 | }else if($input['passwd'] == $input['passwd_new'])
|
---|
178 | {
|
---|
179 | $result['errors']['passwd_new_err'] = "Máºt khẩu trùng vá»i máºt khẩu hiá»n tại !";
|
---|
180 | }
|
---|
181 | if ($input['passwd_new'] != $input['confirm_passwd_new'])
|
---|
182 | {
|
---|
183 | $result['errors']['confirm_passwd_new_err'] = "Máºt khẩu khÃŽng trùng nhau !";
|
---|
184 | }
|
---|
185 | if (!isset($result['errors']))
|
---|
186 | {
|
---|
187 | $user = array('username'=>$user_info['username'], 'fullname'=>null, 'gender'=>null, 'email'=>null, 'school'=>null, 'province'=>null, 'password'=>$input['passwd_new'], 'oldpass'=>$input['passwd']);
|
---|
188 | $data = $this->user_model->update_user($src, $token, $user, $us_id);
|
---|
189 | parse_str($data);
|
---|
190 | switch($status)
|
---|
191 | {
|
---|
192 | case 0:
|
---|
193 | $result['success'] = 1;
|
---|
194 | break;
|
---|
195 | case 6:
|
---|
196 | $result['errors']['passwd_old_err'] = "Máºt khẩu cung cấp khÃŽng Äúng !";
|
---|
197 | break;
|
---|
198 | default:
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | echo json_encode($result);
|
---|
204 | }else
|
---|
205 | {
|
---|
206 | redirect('frontend/home');
|
---|
207 | }
|
---|
208 | }
|
---|
209 | } |
---|