793d7f438e28b74e13df82edb3c7ea955fdcae18
[living-lab-site.git] / application / controllers / user.php
1 <?php
2
3 /**
4  * Class User controls video hierarchy and searching
5  *
6  * @category    Controller
7  * @author              Călin-Andrei Burloiu
8  */
9 class User extends CI_Controller {
10
11         private $import = FALSE;
12         private $activated_account = TRUE;
13
14         public function __construct()
15         {
16                 parent::__construct();
17
18                 $this->lang->load('user');
19                 $this->load->model('users_model');
20         }
21
22         public function index()
23         {
24         }
25
26         /**
27         * Login a user and then redirect it to the last page which must be encoded
28         * in $redirect.
29         *
30         * @param string $redirect       contains the last page URI segments encoded
31         * with helper url_encode_segments.
32         */
33         public function login($redirect = '')
34         {
35                 $this->load->library('form_validation');
36                         
37                 $this->form_validation->set_error_delimiters('<span class="error">',
38                         '</span>');
39
40                 if ($this->form_validation->run('signin') === FALSE)
41                 {
42                         $params = array(        'title' =>
43                                                                         $this->lang->line('ui_nav_menu_login')
44                                                                                 .' &ndash; '
45                                                                                 . $this->config->item('site_name'),
46                                                                 //'metas' => array('description'=>'')
47                         );
48                         $this->load->library('html_head_params', $params);
49                                 
50                         // **
51                         // ** LOADING VIEWS
52                         // **
53                         $this->load->view('html_begin', $this->html_head_params);
54                         $this->load->view('header', array('selected_menu' => 'login'));
55
56                         $main_params['content'] = $this->load->view('user/login_view',
57                                 array('redirect'=> $redirect), TRUE);
58                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
59                         $this->load->view('main', $main_params);
60                                 
61                         $this->load->view('footer');
62                         $this->load->view('html_end');
63                 }
64                 else
65                 {
66                         if (! $this->activated_account)
67                                 header('Location: '. site_url('catalog/test'));
68                         else if (! $this->import)
69                         {
70                                 // Redirect to last page before login. 
71                                 header('Location: '. site_url(urldecode_segments($redirect)));
72                         }
73                         else
74                         {
75                                 // Redirect to account page because an user authenticates here
76                                 // for the first time with external authentication. The page
77                                 // will display imported data.
78                                 header('Location: '. site_url('user/account'));
79                         }
80                 }
81         }
82         
83         /**
84          * Logout user and then redirect it to the last page which must be encoded
85          * in $redirect.
86          * 
87          * @param string $redirect      contains the last page URI segments encoded
88          * with helper url_encode_segments.
89          */
90         public function logout($redirect = '')
91         {
92                 $this->session->unset_userdata('user_id');
93                 $this->session->unset_userdata('username');
94                 $this->session->unset_userdata('auth_src');
95                 $this->session->unset_userdata('time_zone');
96                 
97                 header('Location: '. site_url(urldecode_segments($redirect)));
98         }
99         
100         public function register($redirect = '')
101         {
102                 $this->load->library('form_validation');
103                 $this->load->helper('localization');
104                 $this->load->helper('date');
105                         
106                 $this->form_validation->set_error_delimiters('<span class="error">',
107                                         '</span>');
108                 
109                 if ($this->form_validation->run('register') === FALSE)
110                 {
111                         // Edit account data if logged in, otherwise register.
112                         if ($user_id = $this->session->userdata('user_id'))
113                         {
114                                 $userdata = $this->users_model->get_userdata(intval($user_id));
115                                 $selected_menu = 'account';
116                         }
117                         else
118                         {
119                                 $userdata = FALSE;
120                                 $selected_menu = 'register';
121                         }
122                         
123                         $params = array('title' =>
124                                                                 $this->lang->line('ui_nav_menu_register')
125                                                                         .' &ndash; '
126                                                                         . $this->config->item('site_name'),
127                                                         //'metas' => array('description'=>'')
128                         );
129                         $this->load->library('html_head_params', $params);
130                 
131                         // **
132                         // ** LOADING VIEWS
133                         // **
134                         $this->load->view('html_begin', $this->html_head_params);
135                         $this->load->view('header', 
136                                 array('selected_menu' => $selected_menu));
137                         
138                         $main_params['content'] = $this->load->view('user/register_view', 
139                                 array('userdata'=> $userdata, 'redirect'=> $redirect),
140                                 TRUE);
141                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
142                         $this->load->view('main', $main_params);
143                 
144                         $this->load->view('footer');
145                         $this->load->view('html_end');
146                 }
147                 else
148                 {
149                         $user_id = $this->input->post('user-id');
150                         $data['email'] = $this->input->post('email');
151                         $data['first_name'] = $this->input->post('first-name');
152                         $data['last_name'] = $this->input->post('last-name');
153                         $data['birth_date'] = $this->input->post('birth-date');
154                         $data['country'] = $this->input->post('country');
155                         $data['locality'] = $this->input->post('locality');
156                         $data['ui_lang'] = $this->input->post('ui-lang');
157                         $data['time_zone'] = $this->input->post('time-zone');
158                         
159                         // Update session user data.
160                         $this->_update_session_userdata($data);
161                         
162                         // Edit account data
163                         if ($user_id)
164                         {
165                                 $password = $this->input->post('new-password');
166                                 if ($password)
167                                         $data['password'] = $this->input->post('new-password');
168                                 
169                                 $this->users_model->set_userdata($user_id, $data);
170                         }
171                         // Registration
172                         else
173                         {
174                                 $data['username'] = $this->input->post('username');
175                                 $data['password'] = $this->input->post('password');
176                                 
177                                 $this->users_model->register($data);
178                         }
179                         
180                         // Redirect to last page before login.
181                         header('Location: '. site_url(urldecode_segments($redirect)));
182                 }
183         }
184         
185         public function account($redirect = '')
186         {
187                 $this->register($redirect);
188         }
189         
190         public function profile($username, $videos_offset = 0)
191         {
192                 // TODO handle user not found
193                 
194                 $this->load->config('localization');
195                 $this->load->helper('date');
196                 $this->lang->load('date');
197                 
198                 // **
199                 // ** LOADING MODEL
200                 // **
201                 // Logged in user time zone
202                 $time_zone = $this->session->userdata('time_zone');
203                 if (! $time_zone)
204                         $time_zone = 'UTC';
205                 
206                 // User data
207                 $userdata = $this->users_model->get_userdata($username);
208                 $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
209                 $country_list = $this->config->item('country_list');
210                 $userdata['country_name'] = $country_list[ $userdata['country'] ];
211                 $userdata['last_login'] = date('Y-m-d H:i:s',  
212                         gmt_to_local(
213                                 strtotime($userdata['last_login']), 
214                                 $time_zone, 
215                                 TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : '');
216                 $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
217                 
218                 // User's videos
219                 $this->load->model('videos_model');
220                 $vs_data['videos'] = $this->videos_model->get_videos_summary(
221                         NULL, $username, intval($videos_offset),
222                         $this->config->item('videos_per_page'));
223                 
224                 // Pagination
225                 $this->load->library('pagination');
226                 $pg_config['base_url'] = site_url("user/profile/$username/");
227                 $pg_config['uri_segment'] = 4;
228                 $pg_config['total_rows'] = $this->videos_model->get_videos_count(
229                         NULL, $username);
230                 $pg_config['per_page'] = $this->config->item('videos_per_page');
231                 $this->pagination->initialize($pg_config);
232                 $vs_data['pagination'] = $this->pagination->create_links();
233                 $vs_data['title'] = NULL;
234                 $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
235                 
236                 $params = array(
237                         'title'=> $this->lang->line('user_appelation').' '.$username
238                                 .' &ndash; '
239                                 . $this->config->item('site_name'),
240                         'css'=> array('catalog.css')
241                         //'metas' => array('description'=>'')
242                 );
243                 $this->load->library('html_head_params', $params);
244                 
245                 // Current user profile tab
246                 $tab = (! $videos_offset ? 0 : 1);
247                 
248                 // **
249                 // ** LOADING VIEWS
250                 // **
251                 $this->load->view('html_begin', $this->html_head_params);
252                 $this->load->view('header', array());
253                 
254                 $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
255                 
256                 $main_params['content'] = $this->load->view('user/profile_view',
257                         array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
258                         TRUE);
259                 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
260                 $this->load->view('main', $main_params);
261                 
262                 $this->load->view('footer');
263                 $this->load->view('html_end');
264         }
265         
266         public function activate($user_id, $activation_code)
267         {
268                 $user_id = intval($user_id);
269                 echo ''. $this->users_model->activate_account($user_id, $activation_code);
270         }
271         
272         public function _update_session_userdata($data)
273         {
274                 foreach ($data as $key=> $val)
275                         $this->session->set_userdata($key, $val);
276         }
277         
278         public function _valid_username($username)
279         {
280                 return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
281         }
282
283         public function _valid_username_or_email($username)
284         {
285                 $this->load->helper('email');
286
287                 if (valid_email($username))
288                         return TRUE;
289                 else
290                         return $this->_valid_username($username);
291         }
292         
293         public function _valid_date($date)
294         {
295                 if (! $date)
296                         return TRUE;
297                 
298                 return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
299         }
300         
301         public function _valid_old_password($old_password, $field_username)
302         {
303                 if (! $old_password)
304                         return TRUE;
305                 
306                 $username= $this->input->post($field_username);
307                 
308                 if ($this->users_model->login($username, $old_password))
309                         return TRUE;
310                 
311                 return FALSE;
312         }
313         
314         public function _change_password_cond($param)
315         {
316                 $old = $this->input->post('old-password');
317                 $new = $this->input->post('new-password');
318                 $newc = $this->input->post('new-password-confirmation');
319                 
320                 return (!$old && !$new && !$newc)
321                         || ($old && $new && $newc);
322         }
323         
324         public function _required_by_register($param)
325         {
326                 $user_id = $this->input->post('user-id');
327                 
328                 if (! $user_id && ! $param)
329                         return FALSE;
330                 
331                 return TRUE;
332         }
333
334         public function _do_login($username, $field_password)
335         {
336                 $password = $this->input->post($field_password);
337
338                 $user = $this->users_model->login($username, $password);
339
340                 // Authentication failed.
341                 if ($user === FALSE)
342                         return FALSE;
343                 
344                 // User has not activated the account.
345                 if ($user['activation_code'] !== NULL)
346                 {
347                         $this->activated_account = FALSE;
348                         return TRUE;
349                 }
350                 
351                 // Authentication successful: set session with user data.
352                 $this->session->set_userdata(array(
353                         'user_id'=> $user['id'],
354                         'username'=> $user['username'],
355                         'auth_src'=> $user['auth_src'],
356                         'time_zone'=> $user['time_zone']
357                 ));
358                 $this->import = (isset($user['import']) ? $user['import'] : FALSE);
359                 return TRUE;
360         }
361 }
362
363 /* End of file user.php */
364 /* Location: ./application/controllers/user.php */