Index: pro-violet-viettel/sourcecode/application/modules/frontend/controllers/home.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/frontend/controllers/home.php	(revision 502)
+++ pro-violet-viettel/sourcecode/application/modules/frontend/controllers/home.php	(revision 503)
@@ -1,3 +1,6 @@
-﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+﻿<?php
+if (!defined('BASEPATH'))
+    exit('No direct script access allowed');
+
 /**
  * Home Class
@@ -6,138 +9,129 @@
  *
  */
+class Home extends MX_Controller {
 
-class Home extends MX_Controller 
-{
-	const TOKENPW = 'violet';
-	
-	public function __construct() 
-	{
-		parent::__construct();
-		$this->load->helper('cookie');
-	}
+    const TOKENPW = 'violet';
 
-	public function index() 
-	{
-		$user_info = $this->session->userdata('userInfo');
-		if ($user_info)
-		{
-			redirect('/frontend/lecture');
-		}else{
-			$cookie_name = "siteCookie";
-			if(isset($_COOKIE[$cookie_name])){
-				$arr_user = parse_str($_COOKIE[$cookie_name]);
-				$user_info = array( 'username' => $arr_user['username'], 'user' => $arr_user['user'], 'us_id'=>$arr_user['us_id'], 'logined_in' => TRUE);
-				$this->session->set_userdata('userInfo', $user_info);
-				redirect('/frontend/lecture');
-			}else{
-				$this->load->view ( 'home');
-			}
-		}
-	}
-	
-	public function signin() 
-	{
-		$username = $this->input->post('username', TRUE);
-		$password = $this->input->post('password', TRUE);
-		$autologin = ($this->input->post('remember_me') == 'on') ? 1 : 0;
-		$token = md5($username.self::TOKENPW);
-		
-		$this->load->model(array('user_model'));
-		$data = $this->user_model->checkLogin('violet', $token, $username, $password);
-		
-		if ($data == null)
-		{
-			$this->session->set_flashdata('login_error', TRUE);
-			redirect('/frontend/home');
-		}else{
-			parse_str($data);
-			switch($status)
-			{
-				case 0:
-				case 4:
-					
-					if($autologin == 1){
-						$cookie_name = 'siteCookie';
-						//$cookie_time = 7200;
-						$cookie_time = 3600*24*30;
-						setcookie($cookie_name, 'username='.$username.'&user='.$data.'&us_id='.$id, time() + $cookie_time);
-					}
-					$userdata = array('username' => $username, 'user'=>$data,  'us_id'=>$id, 'logined_in' => TRUE);
-					$this->session->set_userdata('userInfo', $userdata);
-					redirect('/frontend/lecture');
-					break;
-				case 1:
-					$this->session->set_flashdata('login_error', TRUE);
-					redirect('/frontend/home');
-					break;
-				case 2:
-				case 3:
-					$this->session->set_flashdata('login_error', TRUE);
-					redirect('/frontend/home');
-					break;
-				//case 4:
-					//redirect('/frontend/home');
-				case 10:
-					$this->session->set_flashdata('login_error_token', TRUE);
-					redirect('/frontend/home');
-					break;
-				default:
-					break;
-			}
-		}
-	}
-	
-	public function signout() 
-	{
-		$this->session->sess_destroy();
-		//unset($this->session->userdata); 
-		$cookie_name = "siteCookie";
-		setcookie($cookie_name, 'username='."", time() - 3600);
-		redirect('/frontend/home');
-	}
-	public function register()
-	{
-		$this->load->model('user_model');
-		$sentNumber = '0979947802';
-		$serviceID = 1;
-		$receiveID = '0985685735';
-		$commandcode = 'commandcode1';
-		$info = 'ABCD';
-		//$receiveTime = time();
-		$receiveTime = date("Y-m-d H:i:s");
-		$data = $this->user_model->register($sentNumber, $receiveID, $serviceID, $commandcode, $info, $receiveTime);
-		echo $data;
-	}
-	
-	public function change_profile()
-	{
-		$this->load->model('user_model');
-		$sentNumber = '01662860000';
-		$serviceID = 1;
-		$password = '123456';
-		$commandcode = "SBG";
-		$receiveID = '0979947802';
-		$info = "SBG TD";
-		$receiveTime = date("Y-m-d H:i:s");
-		$status = 0;
-		$data = $this->user_model->changeProfile($sentNumber, $receiveID, $password, $serviceID, $commandcode, $info, $receiveTime, $status);
-		echo $data;
-	}
-	
-	public function change_password()
-	{
-		$this->load->model('user_model');
-		$sentNumber = '01662860000';
-		$serviceID = 1;
-		$password = '123456';
-		//$oldpass = '123456';
-		$commandcode = "SBG";
-		//$receiveID = '0979947802';
-		$info = "SBG MK";
-		$receiveTime = date("Y-m-d H:i:s");
-		$status = 0;
-		$data = $this->user_model->changePassword($sentNumber, $password, $serviceID, $commandcode, $info, $receiveTime, $status);
-		echo $data;
-	}
+    public function __construct() {
+        parent::__construct();
+        $this->load->helper('cookie');
+    }
+
+    public function index() {
+        $user_info = $this->session->userdata('userInfo');
+        if ($user_info) {
+            redirect('/frontend/lecture');
+        } else {
+            $cookie_name = "siteCookie";
+            if (isset($_COOKIE[$cookie_name])) {
+                $arr_user = parse_str($_COOKIE[$cookie_name]);
+                $user_info = array('username' => $arr_user['username'], 'user' => $arr_user['user'], 'us_id' => $arr_user['us_id'], 'logined_in' => TRUE);
+                $this->session->set_userdata('userInfo', $user_info);
+                redirect('/frontend/lecture');
+            } else {
+                $this->load->view('home');
+            }
+        }
+    }
+
+    public function signin() {
+        $username = $this->input->post('username', TRUE);
+        $password = $this->input->post('password', TRUE);
+        $autologin = ($this->input->post('remember_me') == 'on') ? 1 : 0;
+        $token = md5($username . self::TOKENPW);
+
+        $this->load->model(array('user_model'));
+        $data = $this->user_model->checkLogin('violet', $token, $username, $password);
+
+        if ($data == null) {
+            $this->session->set_flashdata('login_error', TRUE);
+            redirect('/frontend/home');
+        } else {
+            parse_str($data);
+            switch ($status) {
+                case 0:
+                case 4:
+
+                    if ($autologin == 1) {
+                        $cookie_name = 'siteCookie';
+                        //$cookie_time = 7200;
+                        $cookie_time = 3600 * 24 * 30;
+                        setcookie($cookie_name, 'username=' . $username . '&user=' . $data . '&us_id=' . $id, time() + $cookie_time);
+                    }
+                    $userdata = array('username' => $username, 'user' => $data, 'us_id' => $id, 'logined_in' => TRUE);
+                    $this->session->set_userdata('userInfo', $userdata);
+                    redirect('/frontend/lecture');
+                    break;
+                case 1:
+                    $this->session->set_flashdata('login_error', TRUE);
+                    redirect('/frontend/home');
+                    break;
+                case 2:
+                case 3:
+                    $this->session->set_flashdata('login_error', TRUE);
+                    redirect('/frontend/home');
+                    break;
+                //case 4:
+                //redirect('/frontend/home');
+                case 10:
+                    $this->session->set_flashdata('login_error_token', TRUE);
+                    redirect('/frontend/home');
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+
+    public function signout() {
+        $this->session->sess_destroy();
+        //unset($this->session->userdata); 
+        $cookie_name = "siteCookie";
+        setcookie($cookie_name, 'username=' . "", time() - 3600);
+        redirect('/frontend/home');
+    }
+
+    public function register() {
+        $this->load->model('user_model');
+        $sentNumber = '0979947802';
+        $serviceID = 1;
+        $receiveID = '0985685735';
+        $commandcode = 'commandcode1';
+        $info = 'ABCD';
+        //$receiveTime = time();
+        $receiveTime = date("Y-m-d H:i:s");
+        $data = $this->user_model->register($sentNumber, $receiveID, $serviceID, $commandcode, $info, $receiveTime);
+        echo $data;
+    }
+
+    public function change_profile() {
+        $this->load->model('user_model');
+        $sentNumber = '01662860000';
+        $serviceID = 1;
+        $password = '123456';
+        $commandcode = "SBG";
+        $receiveID = '0979947802';
+        $info = "SBG TD";
+        $receiveTime = date("Y-m-d H:i:s");
+        $status = 0;
+        $data = $this->user_model->changeProfile($sentNumber, $receiveID, $password, $serviceID, $commandcode, $info, $receiveTime, $status);
+        echo $data;
+    }
+
+    public function change_password() {
+        $this->load->model('user_model');
+        $sentNumber = '01662860000';
+        $serviceID = 1;
+        $password = '123456';
+        //$oldpass = '123456';
+        $commandcode = "SBG";
+        //$receiveID = '0979947802';
+        $info = "SBG MK";
+        $receiveTime = date("Y-m-d H:i:s");
+        $status = 0;
+        $data = $this->user_model->changePassword($sentNumber, $password, $serviceID, $commandcode, $info, $receiveTime, $status);
+        echo $data;
+    }
+
 }
 
Index: pro-violet-viettel/sourcecode/application/modules/frontend/controllers/user.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/frontend/controllers/user.php	(revision 502)
+++ pro-violet-viettel/sourcecode/application/modules/frontend/controllers/user.php	(revision 503)
@@ -1,66 +1,206 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
-
-class User extends MX_Controller
-{
-
-	const TOKENPW = 'violet';
-
-	public function __construct()
-	{
-		parent::__construct();
-		$this->load->helper('cookie');
-	}
-	
-	public function profile()
-	{
-		$user_info = $this->session->userdata('userInfo');
-		if ($user_info)
-		{
-                        $this->load->model('servicepackage_model');
-			$user = $user_info['user'];
-			$us_id = $user_info['us_id'];
-			$src = "SBG";
-			$token = md5($us_id.self::TOKENPW);
-			$data = $this->get_data($src, $token, $us_id);
-                        $data['packages'] =$this->servicepackage_model->getAllPackages();
-                        $this->load->view('frontend/user_profile', $data);
-			
-		}else
-		{
-			redirect('frontend/home');
-		}
-	}
-	public function packageRegister()
+<?php
+
+if (!defined('BASEPATH'))
+    exit('No direct script access allowed');
+
+class User extends MX_Controller {
+
+    const TOKENPW = 'violet';
+
+    public function __construct() {
+        parent::__construct();
+        $this->load->helper('cookie');
+    }
+
+    public function profile() {
+        $user_info = $this->session->userdata('userInfo');
+        if ($user_info) {
+            $this->load->model('servicepackage_model');
+            $user = $user_info['user'];
+            $us_id = $user_info['us_id'];
+            $src = "SBG";
+            $token = md5($us_id . self::TOKENPW);
+            $data = $this->get_data($src, $token, $us_id);
+            $data['current_tab']=$this->uri->segment(4,'edit-basic');
+            $data['packages'] = $this->servicepackage_model->getAllPackages();
+            $this->load->view('frontend/user_profile', $data);
+        } else {
+            redirect('frontend/home');
+        }
+    }
+
+    public function packageRegister() {
+        $result = array();
+        $input = $this->input->post();
+        $this->load->model('user_model');
+        $user_info = $this->session->userdata('userInfo');
+        if ($user_info) {
+            parse_str($user_info['user'], $info);
+            $this->load->model('servicepackage_model');
+            
+            $package = $this->servicepackage_model->getPackage($input['p_id']);
+            $user = $user_info['user'];
+            $us_id = $user_info['us_id'];
+            $user = $this->user_model->get_user_by_id($us_id);
+            $result['can_buy'] = 1;
+            $result['can_edit'] = 0;
+            if ($user['acc_balanced'] < $package['p_price']) {
+                $result['can_buy'] = 0;
+            }
+            if ($user['p_id'] < 1) {
+                $result['can_edit'] = 1;
+            }
+            $result['info'] = $info;
+            $result['user'] = $user;
+            $result['package'] = $package;
+            $result['modal'] = $this->load->view('frontend/package_register_modal', $result, true);
+        } else {
+            
+        }
+        echo json_encode($result);
+    }
+
+    public function finishRegisterPackage() {
+        $result = array();
+        $result['success'] = 0;
+        $this->load->model('user_model');
+        $input = $this->input->post();
+        $user_info = $this->session->userdata('userInfo');
+        parse_str($user_info['user'],$info);
+        if (isset($input['fullname'])) {
+            if (strlen($input['fullname']) < 10) {
+                $result['errors'][] = "Vui lÃ²ng nháº­p tÃªn Äáº§y Äá»§ tá»i thiá»u 10 kÃ­ tá»±";
+            }
+            if (strlen($input['school']) < 10) {
+                $result['errors'][] = "Vui lÃ²ng nháº­p ÄÆ¡n vá» quÃ­ vá» Äang cÃŽng tÃ¡c tá»i thiá»u 10 kÃ­ tá»±";
+            }
+            if (!isset($result['errors'])) {
+                $user_info = $this->session->userdata('userInfo');
+                if ($user_info) {
+                    
+                    $src = "SBG";
+                    $us_id=$user_info['us_id'];
+                    $input['password'] = '';
+                    $input['oldpass'] = '';
+                    $input['gender'] = '';
+                    $input['email'] ='';
+                    $input['province'] = '';
+                    $info['fullname']=$input['fullname'];
+                    $info['school']=$input['school'];
+                    $token = md5($us_id . self::TOKENPW);
+                    $user = $this->get_data($src, $token, $us_id);
+                    if ($input) {
+                        $this->load->model('user_model');
+                        $update = $this->user_model->update_user($src, $token, $input, $us_id);
+                        $info= urldecode(http_build_query($info));
+                        $userdata = array('username' => $user_info['username'], 'user' => $info, 'us_id' => $user_info['us_id'], 'logined_in' => TRUE);
+                        $this->session->set_userdata('userInfo', $userdata);
+                    }
+                }
+            }
+        }
+        if (isset($result['errors'])) {
+            echo json_encode($result);
+            die();
+        }
+        $this->load->model('servicepackage_model');
+        $us_id = $user_info['us_id'];
+        $user = $this->user_model->get_user_by_id($us_id);
+        $package = $this->servicepackage_model->getPackage($input['p_id']);
+        $user['acc_balanced']=$user['acc_balanced']-$package['p_price'];
+       
+        if ($package['p_unit']=="months")
         {
-                $result=array();
-                $input=$this->input->post();
-                $user_info = $this->session->userdata('userInfo');
-		if ($user_info)
-		{       
-                        $this->load->model('servicepackage_model');
-                        $this->load->model('user_model');
-                        $package=$this->servicepackage_model->getPackage($input['p_id']);
-			$user = $user_info['user'];
-			$us_id = $user_info['us_id'];
-                        $user = $this->user_model->get_user_by_id($us_id);
-                        $result['can_buy']=1;
-                        $result['can_edit']=0;
-                        if ($user['acc_balanced'] < $package['p_price'])
-                        {
-                        $result['can_buy']=0;   
-                        }
-                        
-                        if ($user['p_id'] < 1)
-                        {
-                        $result['can_edit']=1;
-                        }
-			$result['user']=$user;
-                        $result['package']=$package;
-                        $result['modal']=$this->load->view('frontend/package_register_modal', $result,true);
-		}else
-		{
-			
-		}
+        $user['expire_date']=date('Y-m-d 00:00:00', strtotime("+".$package['p_period']." months", strtotime($user['expire_date'])));
+        }
+        if ($package['p_unit']=="days")
+        {
+        $user['expire_date']=date('Y-m-d 00:00:00', strtotime("+3 days", strtotime($user['expire_date'])));
+        }
+        $user['expire_date']=date("Y-m-d 00:00:00",  (strtotime($user['expire_date'])-1));
+        $user['p_id']=$package['p_id'];
+        $this->user_model->updateUser($us_id,$user);
+        $user['expire_date']=date("d-m-Y",  (strtotime($user['expire_date'])));
+        $result['expire_date']=$user['expire_date'];
+        $result['success'] = 1;
+        echo json_encode($result);
+        
+    }
+    
+
+    private function get_data($src, $token, $us_id) {
+        $this->load->model('user_model');
+        $user = $this->user_model->get_user_info($src, $token, $us_id);
+
+        if (strpos($user, '&')) {
+            parse_str($user);
+            $us = $this->user_model->get_user_by_id($us_id);
+            $data = array('us_id' => $us_id, 'username' => $username, 'fullname' => $fullname,
+                'gender' => $gender, 'email' => $email, 'phone' => $phone, 'school' => $school, 'province' => $province,
+                'acc_balanced' => $us['acc_balanced'], 'expire_date' => $us['expire_date'], 'date_diff' => $this->dateDiff($us['expire_date'])
+            );
+            return $data;
+        } else {
+            return "";
+        }
+    }
+
+    private function dateDiff($expire_date) {
+        $date1 = date("Y-m-d");
+        if ((strtotime($expire_date) - strtotime($date1)) < 0)
+            return "0 ngÃ y cÃ²n láº¡i";
+        $diff = strtotime($expire_date) - strtotime($date1);
+        $years = floor($diff / (365 * 60 * 60 * 24));
+        $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
+        $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
+        if ($years > 0) {
+            return $years . " nÄm, " . $months . " thÃ¡ng, " . $days . " ngÃ y cÃ²n láº¡i";
+        } else {
+            if ($months > 0) {
+                return $months . " thÃ¡ng, " . $days . " ngÃ y cÃ²n láº¡i";
+            } else {
+                return $days . " ngÃ y cÃ²n láº¡i";
+            }
+        }
+    }
+    public function test()
+    {
+        $user_info = $this->session->userdata('userInfo');
+        echo "<pre>";
+        print_r ($user_info);
+        echo "</pre>";
+    }
+    public function update_user() {
+        $this->load->helper('email');
+        $us_id = $this->uri->segment(4);
+        $user_info = $this->session->userdata('userInfo');
+       
+        if ($user_info) {
+            $result = array();
+            $result['success'] = 0;
+            $src = "SBG";
+            $token = md5($us_id . self::TOKENPW);
+            $user = $this->get_data($src, $token, $us_id);
+            $input = $this->input->post();
+            if ($input) {
+                $input['username'] = $user['username'];
+                $input['fullname'] = $user['fullname'];
+                $input['password'] = '';
+                $input['oldpass'] = '';
+                $this->load->model('user_model');
+                $result['fullname'] = $input['fullname'];
+                if (strlen($input['email']) > 0) {
+                    if (!valid_email($input['email'])) {
+                        $result['errors']['email_err'] = "Email khÃŽng ÄÃºng Äá»nh dáº¡ng";
+                    }
+                }
+                if (!isset($result['errors'])) {
+                    $result['data'] = $this->user_model->update_user($src, $token, $input, $us_id);
+                    $result['success'] = 1;
+                }
                 echo json_encode($result);
+            }
+        } else {
+            redirect('frontend/home');
         }
 	private function get_data($src, $token, $us_id)
