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 | $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 | $this->load->view('frontend/user_profile', $data);
|
---|
25 |
|
---|
26 | }else
|
---|
27 | {
|
---|
28 | redirect('frontend/home');
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | private function get_data($src, $token, $us_id)
|
---|
33 | {
|
---|
34 | $this->load->model('user_model');
|
---|
35 | $user = $this->user_model->get_user_info($src, $token, $us_id);
|
---|
36 |
|
---|
37 | if (strpos($user, '&')){
|
---|
38 | parse_str($user);
|
---|
39 | $us = $this->user_model->get_user_by_id($us_id);
|
---|
40 | $data = array('us_id'=>$us_id, 'username'=>$username, 'fullname'=>$fullname,
|
---|
41 | 'gender'=>$gender, 'email'=>$email, 'phone'=>$phone, 'school'=>$school, 'province'=>$province,
|
---|
42 | 'acc_balanced'=>$us['acc_balanced'], 'expire_date'=>$us['expire_date'], 'date_diff'=>$this->dateDiff($us['expire_date'])
|
---|
43 | );
|
---|
44 | return $data;
|
---|
45 | }else
|
---|
46 | {
|
---|
47 | return "";
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private function dateDiff($expire_date)
|
---|
52 | {
|
---|
53 | $date1 = date("Y-m-d");
|
---|
54 | if ((strtotime($expire_date) - strtotime($date1))<0) return "0 ngà y còn lại";
|
---|
55 | $diff = strtotime($expire_date) - strtotime($date1);
|
---|
56 | $years = floor($diff / (365*60*60*24));
|
---|
57 | $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
|
---|
58 | $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
|
---|
59 | if ($years>0){
|
---|
60 | return $years." nÄm, ".$months." tháng, ".$days." ngà y còn lại";
|
---|
61 | }
|
---|
62 | else{
|
---|
63 | if ($months>0)
|
---|
64 | {
|
---|
65 | return $months." tháng, ".$days." ngà y còn lại";
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | return $days." ngà y còn lại";
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | public function update_user()
|
---|
76 | {
|
---|
77 | $this->load->helper('email');
|
---|
78 | $us_id=$this->uri->segment(4);
|
---|
79 | $user_info = $this->session->userdata('userInfo');
|
---|
80 | if ($user_info)
|
---|
81 | {
|
---|
82 | $result = array();
|
---|
83 | $result['success'] = 0;
|
---|
84 | $src = "SBG";
|
---|
85 | $token = md5($us_id.self::TOKENPW);
|
---|
86 | $user = $this->get_data($src, $token, $us_id);
|
---|
87 | $input = $this->input->post();
|
---|
88 | if ($input){
|
---|
89 | $input['username'] = $user['username'];
|
---|
90 | $input['fullname'] = $user['fullname'];
|
---|
91 | $input['password'] = '';
|
---|
92 | $input['oldpass'] = '';
|
---|
93 | $this->load->model('user_model');
|
---|
94 | $result['fullname'] = $input['fullname'];
|
---|
95 | if (strlen($input['email'])>0){
|
---|
96 | if(!valid_email($input['email']))
|
---|
97 | {
|
---|
98 | $result['errors']['email_err'] = "Email khÃŽng Äúng Äá»nh dạng";
|
---|
99 | }
|
---|
100 | }
|
---|
101 | if (!isset($result['errors'])){
|
---|
102 | $result['data'] = $this->user_model->update_user($src, $token, $input, $us_id);
|
---|
103 | $result['success'] = 1;
|
---|
104 | }
|
---|
105 | echo json_encode($result);
|
---|
106 | }
|
---|
107 |
|
---|
108 | }else
|
---|
109 | {
|
---|
110 | redirect('frontend/home');
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public function change_password()
|
---|
115 | {
|
---|
116 | $us_id=$this->uri->segment(4);
|
---|
117 | $user_info = $this->session->userdata('userInfo');
|
---|
118 | if ($user_info)
|
---|
119 | {
|
---|
120 | $this->load->model('user_model');
|
---|
121 | $us_id = $user_info['us_id'];
|
---|
122 | $src = "SBG";
|
---|
123 | $token = md5($us_id.self::TOKENPW);
|
---|
124 | if (strlen($input['passwd']) == 0)
|
---|
125 | {
|
---|
126 | $result['errors']['passwd_old_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
127 | }
|
---|
128 | if (strlen($input['passwd_new']) == 0)
|
---|
129 | {
|
---|
130 | $result['errors']['passwd_new_err'] = "Máºt khẩu khÃŽng ÄÆ°á»£c Äá» trá»ng !";
|
---|
131 | }else if($input['passwd'] == $input['passwd_new'])
|
---|
132 | {
|
---|
133 | $result['errors']['passwd_new_err'] = "Máºt khẩu trùng vá»i máºt khẩu hiá»n tại !";
|
---|
134 | }
|
---|
135 | if ($input['passwd_new'] != $input['confirm_passwd_new'])
|
---|
136 | {
|
---|
137 | $result['errors']['confirm_passwd_new_err'] = "Máºt khẩu khÃŽng trùng nhau !";
|
---|
138 | }
|
---|
139 | if (!isset($result['errors']))
|
---|
140 | {
|
---|
141 | $user = array('username'=>$username, 'fullname'=>null, );
|
---|
142 | $data = $this->user_model->update_user($src, $token, $user, $us_id);
|
---|
143 | parse_str($data);
|
---|
144 | switch($status)
|
---|
145 | {
|
---|
146 | case 0:
|
---|
147 | $result['success'] = 1;
|
---|
148 | break;
|
---|
149 | case 6:
|
---|
150 | $result['errors']['passwd_old_err'] = "Máºt khẩu cung cấp khÃŽng Äúng !";
|
---|
151 | break;
|
---|
152 | default:
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | echo json_encode($result);
|
---|
158 | }else
|
---|
159 | {
|
---|
160 | redirect('frontend/home');
|
---|
161 | }
|
---|
162 | }
|
---|
163 | } |
---|