Index: pro-violet-viettel/sourcecode/application/modules/frontend/controllers/user.php
===================================================================
--- pro-violet-viettel/sourcecode/application/modules/frontend/controllers/user.php	(revision 517)
+++ pro-violet-viettel/sourcecode/application/modules/frontend/controllers/user.php	(revision 521)
@@ -1,57 +1,19 @@
-<?php 
-if (! defined ( 'BASEPATH' ))
-	exit ( 'No direct script access allowed' );
+<?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() {
-		$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 {
-		}
-=======
-
+
+    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');
@@ -61,4 +23,6 @@
             $token = md5($us_id . self::TOKENPW);
             $data = $this->get_data($src, $token, $us_id);
+            
+            $data['message'] = $this->session->flashdata('message');
             $data['current_tab'] = $this->uri->segment(4, 'edit-basic');
             $data['packages'] = $this->servicepackage_model->getAllPackages();
@@ -99,5 +63,5 @@
         echo json_encode($result);
     }
-
+    
     public function finishRegisterPackage() {
         $result = array();
@@ -112,5 +76,5 @@
             }
             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á»±";
+                $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'])) {
@@ -140,5 +104,5 @@
         }
         if (isset($result['errors'])) {
-            echo json_encode($result);
+            echo json_encode ($result);
             die();
         }
@@ -162,4 +126,37 @@
         $result['success'] = 1;
         echo json_encode($result);
+    }
+    
+    public function regpackage () 
+    {
+    	$user_info = $this->session->userdata('userInfo');
+    	parse_str($user_info['user'], $info);
+    	$p_id = $this->input->post('p_id', TRUE);
+    	
+    	$us_id = (int)$info['id'];
+    	$this->load->model('servicepackage_model');
+    	$this->load->model('user_model');
+    	
+    	$user = $this->user_model->get_user_by_id($us_id);
+    	$package = $this->servicepackage_model->getPackage($p_id);
+    	
+    	if ($user['acc_balanced'] < $package['p_price']) {
+    		$message = 'Sá» tiá»n trong TÃ i khoáº£n SBG Online hiá»n táº¡i cá»§a QuÃ­ vá» lÃ  '. $user['acc_balanced'] .' Äá»ng.<br /> Äá» ÄÄng kÃ­ Sá»­ dá»¥ng dá»ch vá»¥ Soáº¡n BÃ i giáº£ng trá»±c tuyáº¿n, QuÃ­ vá» cáº§n náº¡p thÃªm tiá»n vÃ o tÃ i khoáº£n.';
+    	}
+    	else {
+	    	$user['acc_balanced'] = $user['acc_balanced'] - $package['p_price'];
+	    	$currentExpireDate = $user['expire_date'] == NULL || trim($user['expire_date']) == '' ? date('Y-m-d 00:00:00') : $user['expire_date'];
+	    	
+	    	$user['expire_date'] = date('Y-m-d', strtotime("+" . $package['p_period'] ." ". $package['p_unit'], strtotime($currentExpireDate)));    	
+	    	//$user['expire_date'] = date("Y-m-d", (strtotime($user['expire_date']) - 1));
+	    	
+	    	$user['p_id'] = $package['p_id'];
+	    	
+	    	$this->user_model->updateUser($us_id, $user);
+	    	$message = 'QuÃœ vá» ÄÃ£ ÄÄng kÃ­ sá»­ dá»¥ng CÃŽng cá»¥ Soáº¡n BÃ i giáº£ng trá»©c tuyáº¿n. QuÃ­ vá» ÄÆ°á»£c sá»­ dá»¥ng dá»ch vá»¥ Äáº¿n háº¿t ngÃ y '. date('d-m-Y', strtotime($user['expire_date']));
+    	}
+    	
+    	$this->session->set_flashdata('message', $message);
+    	redirect('frontend/user/profile#services-register');
     }
 
@@ -215,5 +212,5 @@
                 if (array_key_exists('fullname', $input)) {
                     if (strlen($input['fullname']) == 0) {
-                        $result['errors']['fullname_err'] = "Há» tÃªn khÃŽng ÄÆ°á»£c Äá» trá»ng !";
+                        $result['errors']['fullname_err'] = "Há» tÃªn khÃŽng ÄÆ°á»£c Äá» trá»ng!";
                     }
                 } else {
@@ -223,9 +220,9 @@
                   if ($strlen($input['school']) == 0)
                   {
-                  $result['errors']['school_err'] = "ÄÆ¡n vá» khÃŽng ÄÆ°á»£c Äá» trá»ng !";
+                  $result['errors']['school_err'] = "ÄÆ¡n vá» khÃŽng ÄÆ°á»£c Äá» trá»ng!";
                   }
                   if ($strlen($input['province']) == 0)
                   {
-                  $result['errors']['province_err'] = "Tá»nh/thÃ nh khÃŽng ÄÆ°á»£c Äá» trá»ng !";
+                  $result['errors']['province_err'] = "Tá»nh/thÃ nh khÃŽng ÄÆ°á»£c Äá» trá»ng!";
                   } */
                 $input['password'] = '';
@@ -237,132 +234,4 @@
                     $result['success'] = 1;
                 }
->>>>>>> .r514
-<<<<<<< .mine
-		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 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' );
-		}
-	}
-	
-	public function change_password() {
-		$us_id = $this->uri->segment ( 4 );
-		$user_info = $this->session->userdata ( 'userInfo' );
-		if ($user_info) {
-			$this->load->model ( 'user_model' );
-			$us_id = $user_info ['us_id'];
-			$src = "SBG";
-			$token = md5 ( $us_id . self::TOKENPW );
-			if (strlen ( $input ['passwd'] ) == 0) {
-				$result ['errors'] ['passwd_old_err'] = "Máº­t kháº©u khÃŽng ÄÆ°á»£c Äá» trá»ng !";
-			}
-			if (strlen ( $input ['passwd_new'] ) == 0) {
-				$result ['errors'] ['passwd_new_err'] = "Máº­t kháº©u khÃŽng ÄÆ°á»£c Äá» trá»ng !";
-			} else if ($input ['passwd'] == $input ['passwd_new']) {
-				$result ['errors'] ['passwd_new_err'] = "Máº­t kháº©u trÃ¹ng vá»i máº­t kháº©u hiá»n táº¡i !";
-			}
-			if ($input ['passwd_new'] != $input ['confirm_passwd_new']) {
-				$result ['errors'] ['confirm_passwd_new_err'] = "Máº­t kháº©u khÃŽng trÃ¹ng nhau !";
-			}
-			if (! isset ( $result ['errors'] )) {
-				$user = array (
-						'username' => $username,
-						'fullname' => null 
-				);
-				$data = $this->user_model->update_user ( $src, $token, $user, $us_id );
-				parse_str ( $data );
-				switch ($status) {
-					case 0 :
-						$result ['success'] = 1;
-						break;
-					case 6 :
-						$result ['errors'] ['passwd_old_err'] = "Máº­t kháº©u cung cáº¥p khÃŽng ÄÃºng !";
-						break;
-					default :
-						break;
-				}
-			}
-			
-			echo json_encode ( $result );
-		} else {
-			redirect ( 'frontend/home' );
-		}
-	}
-}=======
                 echo json_encode($result);
             }
@@ -381,15 +250,19 @@
             $token = md5($us_id . self::TOKENPW);
             $input = $this->input->post();
+            
+            
+            
             if (strlen($input['passwd']) == 0) {
-                $result['errors']['passwd_old_err'] = "Máº­t kháº©u khÃŽng ÄÆ°á»£c Äá» trá»ng !";
+                $result['errors']['passwd_old_err'] = "Máº­t kháº©u khÃŽng ÄÆ°á»£c Äá» trá»ng!";
             }
             if (strlen($input['passwd_new']) == 0) {
-                $result['errors']['passwd_new_err'] = "Máº­t kháº©u khÃŽng ÄÆ°á»£c Äá» trá»ng !";
+                $result['errors']['passwd_new_err'] = "Máº­t kháº©u khÃŽng ÄÆ°á»£c Äá» trá»ng!";
             } else if ($input['passwd'] == $input['passwd_new']) {
-                $result['errors']['passwd_new_err'] = "Máº­t kháº©u trÃ¹ng vá»i máº­t kháº©u hiá»n táº¡i !";
-            }
-            if ($input['passwd_new'] != $input['confirm_passwd_new']) {
-                $result['errors']['confirm_passwd_new_err'] = "Máº­t kháº©u khÃŽng trÃ¹ng nhau !";
-            }
+                $result['errors']['passwd_new_err'] = "Máº­t kháº©u trÃ¹ng vá»i máº­t kháº©u hiá»n táº¡i!";
+            }
+            if ($input['passwd_new']!= $input['confirm_passwd_new']) {
+                $result['errors']['confirm_passwd_new_err'] = "Máº­t kháº©u khÃŽng trÃ¹ng nhau!";
+            }
+            
             if (!isset($result['errors'])) {
                 $user = array('username' => $user_info['username'], 'fullname' => null, 'gender' => null, 'email' => null, 'school' => null, 'province' => null, 'password' => $input['passwd_new'], 'oldpass' => $input['passwd']);
@@ -401,5 +274,5 @@
                         break;
                     case 6:
-                        $result['errors']['passwd_old_err'] = "Máº­t kháº©u cung cáº¥p khÃŽng ÄÃºng !";
+                        $result['errors']['passwd_old_err'] = "Máº­t kháº©u cung cáº¥p khÃŽng ÄÃºng!";
                         break;
                     default:
@@ -415,3 +288,2 @@
 
 }
->>>>>>> .r514
