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('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['current_tab'] = $this->uri->segment(4, 'edit-basic');
|
---|
26 | $data['packages'] = $this->servicepackage_model->getAllPackages();
|
---|
27 | $this->load->view('frontend/user_profile', $data);
|
---|
28 | } else {
|
---|
29 | redirect('frontend/home');
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public function packageRegister() {
|
---|
34 | $result = array();
|
---|
35 | $input = $this->input->post();
|
---|
36 | $this->load->model('user_model');
|
---|
37 | $user_info = $this->session->userdata('userInfo');
|
---|
38 | if ($user_info) {
|
---|
39 | parse_str($user_info['user'], $info);
|
---|
40 | $this->load->model('servicepackage_model');
|
---|
41 |
|
---|
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 | $result['can_buy'] = 0;
|
---|
50 | }
|
---|
51 | if ($user['p_id'] < 1) {
|
---|
52 | $result['can_edit'] = 1;
|
---|
53 | }
|
---|
54 | $result['info'] = $info;
|
---|
55 | $result['user'] = $user;
|
---|
56 | $result['package'] = $package;
|
---|
57 | $result['modal'] = $this->load->view('frontend/package_register_modal', $result, true);
|
---|
58 | } else {
|
---|
59 |
|
---|
60 | }
|
---|
61 | echo json_encode($result);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public function finishRegisterPackage() {
|
---|
65 | $result = array();
|
---|
66 | $result['success'] = 0;
|
---|
67 | $this->load->model('user_model');
|
---|
68 | $input = $this->input->post();
|
---|
69 | $user_info = $this->session->userdata('userInfo');
|
---|
70 | parse_str($user_info['user'], $info);
|
---|
71 | if (isset($input['fullname'])) {
|
---|
72 | if (strlen($input['fullname']) < 10) {
|
---|
73 | $result['errors'][] = "Vui lòng nháºp tên Äầy Äá»§ tá»i thiá»u 10 kà tá»±";
|
---|
74 | }
|
---|
75 | if (strlen($input['school']) < 10) {
|
---|
76 | $result['errors'][] = "Vui lòng nháºp ÄÆ¡n vá» quà vá» Äang cÃŽng tác tá»i thiá»u 10 kà tá»±";
|
---|
77 | }
|
---|
78 | if (!isset($result['errors'])) {
|
---|
79 | $user_info = $this->session->userdata('userInfo');
|
---|
80 | if ($user_info) {
|
---|
81 |
|
---|
82 | $src = "SBG";
|
---|
83 | $us_id = $user_info['us_id'];
|
---|
84 | $input['password'] = '';
|
---|
85 | $input['oldpass'] = '';
|
---|
86 | $input['gender'] = '';
|
---|
87 | $input['email'] = '';
|
---|
88 | $input['province'] = '';
|
---|
89 | $info['fullname'] = $input['fullname'];
|
---|
90 | $info['school'] = $input['school'];
|
---|
91 | $token = md5($us_id . self::TOKENPW);
|
---|
92 | $user = $this->get_data($src, $token, $us_id);
|
---|
93 | if ($input) {
|
---|
94 | $this->load->model('user_model');
|
---|
95 | $update = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
96 | $info = urldecode(http_build_query($info));
|
---|
97 | $userdata = array('username' => $user_info['username'], 'user' => $info, 'us_id' => $user_info['us_id'], 'logined_in' => TRUE);
|
---|
98 | $this->session->set_userdata('userInfo', $userdata);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | if (isset($result['errors'])) {
|
---|
104 | echo json_encode($result);
|
---|
105 | die();
|
---|
106 | }
|
---|
107 | $this->load->model('servicepackage_model');
|
---|
108 | $us_id = $user_info['us_id'];
|
---|
109 | $user = $this->user_model->get_user_by_id($us_id);
|
---|
110 | $package = $this->servicepackage_model->getPackage($input['p_id']);
|
---|
111 | $user['acc_balanced'] = $user['acc_balanced'] - $package['p_price'];
|
---|
112 |
|
---|
113 | if ($package['p_unit'] == "months") {
|
---|
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 | $user['expire_date'] = date('Y-m-d 00:00:00', strtotime("+3 days", strtotime($user['expire_date'])));
|
---|
118 | }
|
---|
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 | private function get_data($src, $token, $us_id) {
|
---|
129 | $this->load->model('user_model');
|
---|
130 | $user = $this->user_model->get_user_info($src, $token, $us_id);
|
---|
131 |
|
---|
132 | if (strpos($user, '&')) {
|
---|
133 | parse_str($user);
|
---|
134 | $us = $this->user_model->get_user_by_id($us_id);
|
---|
135 | $data = array('us_id' => $us_id, 'username' => $username, 'fullname' => $fullname,
|
---|
136 | 'gender' => $gender, 'email' => $email, 'phone' => $phone, 'school' => $school, 'province' => $province,
|
---|
137 | 'acc_balanced' => $us['acc_balanced'], 'expire_date' => $us['expire_date'], 'date_diff' => $this->dateDiff($us['expire_date'])
|
---|
138 | );
|
---|
139 | return $data;
|
---|
140 | } else {
|
---|
141 | return "";
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | private function dateDiff($expire_date) {
|
---|
147 | $date1 = date("Y-m-d");
|
---|
148 | if ((strtotime($expire_date) - strtotime($date1)) < 0)
|
---|
149 | return "0 ngà y còn lại";
|
---|
150 | $diff = strtotime($expire_date) - strtotime($date1);
|
---|
151 | $years = floor($diff / (365 * 60 * 60 * 24));
|
---|
152 | $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
|
---|
153 | $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
|
---|
154 | if ($years > 0) {
|
---|
155 | return $years . " nÄm, " . $months . " tháng, " . $days . " ngà y còn lại";
|
---|
156 | } else {
|
---|
157 | if ($months > 0) {
|
---|
158 | return $months . " tháng, " . $days . " ngà y còn lại";
|
---|
159 | } else {
|
---|
160 | return $days . " ngà y còn lại";
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public function update_user() {
|
---|
166 | $us_id = $this->uri->segment(4);
|
---|
167 | $user_info = $this->session->userdata('userInfo');
|
---|
168 | if ($user_info) {
|
---|
169 | $result = array();
|
---|
170 | $result['success'] = 0;
|
---|
171 | $src = "SBG";
|
---|
172 | $token = md5($us_id . self::TOKENPW);
|
---|
173 | $user = $this->get_data($src, $token, $us_id);
|
---|
174 | $input = $this->input->post();
|
---|
175 | if ($input) {
|
---|
176 | $input['username'] = $user['username'];
|
---|
177 | if (array_key_exists('fullname', $input)) {
|
---|
178 | if (strlen($input['fullname']) == 0) {
|
---|
179 | $result['errors']['fullname_err'] = "Há» tên khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
180 | }
|
---|
181 | } else {
|
---|
182 | $input['fullname'] = $user['fullname'];
|
---|
183 | }
|
---|
184 | /*
|
---|
185 | if ($strlen($input['school']) == 0)
|
---|
186 | {
|
---|
187 | $result['errors']['school_err'] = "ÄÆ¡n vá» khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
188 | }
|
---|
189 | if ($strlen($input['province']) == 0)
|
---|
190 | {
|
---|
191 | $result['errors']['province_err'] = "Tá»nh/thà nh khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
192 | } */
|
---|
193 | $input['password'] = '';
|
---|
194 | $input['oldpass'] = '';
|
---|
195 | $this->load->model('user_model');
|
---|
196 | //$result['fullname'] = $input['fullname'];
|
---|
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 | }
|
---|
207 |
|
---|
208 | public function change_password() {
|
---|
209 | $us_id = $this->uri->segment(4);
|
---|
210 | $user_info = $this->session->userdata('userInfo');
|
---|
211 | if ($user_info) {
|
---|
212 | $this->load->model('user_model');
|
---|
213 | $us_id = $user_info['us_id'];
|
---|
214 | $src = "SBG";
|
---|
215 | $token = md5($us_id . self::TOKENPW);
|
---|
216 | $input = $this->input->post();
|
---|
217 | if (strlen($input['passwd']) == 0) {
|
---|
218 | $result['errors']['passwd_old_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
219 | }
|
---|
220 | if (strlen($input['passwd_new']) == 0) {
|
---|
221 | $result['errors']['passwd_new_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
222 | } else if ($input['passwd'] == $input['passwd_new']) {
|
---|
223 | $result['errors']['passwd_new_err'] = "Máºt khẩu trùng vá»i máºt khẩu hiá»n tại !";
|
---|
224 | }
|
---|
225 | if ($input['passwd_new'] != $input['confirm_passwd_new']) {
|
---|
226 | $result['errors']['confirm_passwd_new_err'] = "Máºt khẩu khÃŽng trùng nhau !";
|
---|
227 | }
|
---|
228 | if (!isset($result['errors'])) {
|
---|
229 | $user = array('username' => $user_info['username'], 'fullname' => null, 'gender' => null, 'email' => null, 'school' => null, 'province' => null, 'password' => $input['passwd_new'], 'oldpass' => $input['passwd']);
|
---|
230 | $data = $this->user_model->update_user($src, $token, $user, $us_id);
|
---|
231 | parse_str($data);
|
---|
232 | switch ($status) {
|
---|
233 | case 0:
|
---|
234 | $result['success'] = 1;
|
---|
235 | break;
|
---|
236 | case 6:
|
---|
237 | $result['errors']['passwd_old_err'] = "Máºt khẩu cung cấp khÃŽng Äúng !";
|
---|
238 | break;
|
---|
239 | default:
|
---|
240 | break;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | echo json_encode($result);
|
---|
245 | } else {
|
---|
246 | redirect('frontend/home');
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | }
|
---|