<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | admin control panel page                                       |
// | File name : login.php                                                  |
// | PHP version >= 5.2                                                   |
// +----------------------------------------------------------------------+
// | Author: Meena Susan Joseph <meena.s@armiasystems.com>              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems ? 2010                                      |
// | All rights reserved                                                  |
// +------------------------------------------------------

class ControllerLogin extends BaseController
{
	/*
	Initializing the models
	*/
        public function init()
        {
            parent::init();
		$this->_common	 		= new ModelCommon;
        $this->_session = new LibSession();
        }
    
        public function index()
	{           
            //Index / Login
            //$this->view->setLayout();
            // if the user session exist , redirect to users page
            $this->view->disableLayout(); // To disable the layout section
            $this->view->disableView(); // To disable the layout view section

            $status = '';
	    if($this->isPost())
            {                
                $username 	= addslashes(($this->post('txtUsername')!='') ? $this->post('txtUsername') :$this->get('txtUsername'));
                $password 	= addslashes(($this->post('txtPassword')!='') ? $this->post('txtPassword') :$this->get('txtPassword'));
                if($username != '' && $password != '')
                {
                    $userLogin = $this->_common->selectRecord("users","nUserId,vLoginName,vEmail,vFirstName,vLastName,vDelStatus,vStatus,nSocialUserId","LOWER(vLoginName)='".strtolower($username)."' AND vPassword ='".md5($password)."'");
                    //echopre($userLogin);
                    if(sizeof($userLogin) > 0){
                        
			if($userLogin->vStatus == 1){
                            $_SESSION['user'] =  array("userID" => $userLogin->nUserId,
                                                        "userName" =>$userLogin->vLoginName,
                                                        "userEmail" =>$userLogin->vEmail,
                                                        "userFirstName" =>$userLogin->vFirstName,
                                                        "userLastName" =>$userLogin->vLastName);                           
                                                                                   
                            unset($_SESSION['errorLogin']);

                            $this->_session->Set('user_id',$userLogin->nUserId,'business');
 
                            $_SESSION['fbLogout'] = 0;
                            $_SESSION['loggedin'] = 1;
                            $_SESSION['forum_user'] = $userLogin->vLoginName;


                            $this->redirect('business/index/dashboard');

                        }                        
                        else {
                            $status = 'Your account is no longer active!';
                        }                                        
                     } else {
                        $status = 'Invalid login! Please try again.';
                     }
                } else {
                    $status = 'Please enter username or password!';
                }
            } // End IsPost
            
            if(!empty($status)) { // Error on Page
               $_SESSION['errorLogin'] = $status;
                setcookie('errorLogin',$status,0);
               
                echo "
            <script type=\"text/javascript\">
            alert('welcome');
           document.getElementById('btn_login').toggle();
            </script>
        ";
                $this->redirect('');
            }

	} // End Function
    
	
 	
 	public function logout()
	{
		session_destroy();
		header("location:".ConfigUrl::base().'default');
	}

        public function userloginfb()
        {
          
            // if the user session exist , redirect to users page
            $this->view->disableLayout();
            $this->view->disableView();         
            $status = '';
            if(isset($_GET['user_fbid'])) {
                $dataUser = $_GET;
                $dataUser['user_fbid'];
                $dataUser['user_fbname'];
                $dataUser['user_fname'];
                $dataUser['user_middle_name'];
                $dataUser['user_lname'];
                $dataUser['user_email'];

                // Validate Userlogin
                $userLogin = $this->_common->selectRecord("users","nUserId,vLoginName,vEmail,vFirstName,vLastName,vStatus,nSocialUserId","nSocialUserId ='".$dataUser['user_fbid']."'");

                    
                    if(sizeof($userLogin) > 0){


			if($userLogin->vStatus == 1){
                            $_SESSION['user'] =  array("userID" => $userLogin->nUserId,
                                                        "userName" =>$userLogin->vLoginName,
                                                         "userEmail" =>$userLogin->vEmail,
                                                         "userFirstName" =>$userLogin->vFirstName,
                                                         "userLastName" =>$userLogin->vLastName);
                            $_SESSION['fbLogout'] = 0;
                            $_SESSION['loggedin'] = 1;  // For forum
                            $_SESSION['forum_user'] = $userLogin->vLoginName; // For forum

                            unset($_SESSION['errorLogin']);

                            $this->_session->Set('user_id',$userLogin->nUserId,'business');

                            $status = 1; // Logged In

                        } else {
                            $status = 0; // Your account is no longer active!
                        }
                     } else { // Register
                         $firstName = $dataUser['user_fname'];
                         $firstName .= (!empty($dataUser['user_middle_name'])) ? ' '.$dataUser['user_middle_name'] : '';

                         $arrUser = array();
                         $arrUser['vLoginName'] = $dataUser['user_fbname'];
                         $arrUser['vFirstName'] = $firstName;
                         $arrUser['vLastName'] = $dataUser['user_lname'];
                         $arrUser['dDateReg'] = 'NOW()';
                         $arrUser['vAlertStatus'] = 'Y';
                         $arrUser['vNLStatus'] = 'N';
                         $arrUser['vStatus'] = '1';
                         $arrUser['nSocialUserId'] = $dataUser['user_fbid'];
                         
                         $newUser = $this->_common->addFields("users", $arrUser);


                         $query="INSERT INTO `users`
                                 (`username`,`password`,`email`,`fname`,`lname`,`reggedOn`)
                                 VALUES
                                 (
                                 '".$arrUser['vLoginName']."',
                                 '".$arrUser['vPassword']."',
                                 '".$arrUser['vEmail']."',
                                 '".$arrUser['vFirstName']."',
                                 '".$arrUser['vLastName']."',
                                 '".time()."'
                                 )";

                         $this->_common->execute($query);


                         $userLogin = $this->_common->selectRecord("users","nUserId,vLoginName,vEmail,vFirstName,vLastName,vStatus,nSocialUserId","nUserId ='".$newUser."'");
                         
                         $_SESSION['user'] =  array("userID" => $userLogin->nUserId,
                                                        "userName" =>$userLogin->vLoginName,
                                                         "userEmail" =>$userLogin->vEmail,
                                                         "userFirstName" =>$userLogin->vFirstName,
                                                         "userLastName" =>$userLogin->vLastName);
                         $_SESSION['fbLogout'] = 0;
                         $_SESSION['loggedin'] = 1;  // For forum
                         $_SESSION['forum_user'] = $userLogin->vLoginName;  // For forum
                         $this->_session->Set('user_id',$userLogin->nUserId,'business');
                         unset($_SESSION['errorLogin']);


                         $status = 1; // Logged In
                     }
                 
            } // End IsPost

            echo $status;

        } // End Function userlogin

        public function activation($activationkey)
        {
             
            $this->view->disableLayout();
            $this->view->disableView();

            $key = base64_decode($activationkey);
            $keyArr = explode("##",$key);
            $userId = $keyArr[1];

            if($userId>0)
            {
                
            $userLogin = $this->_common->selectRecord("users","nUserId,vLoginName,vEmail,vFirstName,vLastName,vStatus,nSocialUserId","nUserId ='".$userId."'");


            $_SESSION['user'] =  array("userID" => $userLogin->nUserId,
                                       "userName" =>$userLogin->vLoginName,
                                        "userEmail" =>$userLogin->vEmail,
                                        "userFirstName" =>$userLogin->vFirstName,
                                        "userLastName" =>$userLogin->vLastName);
            $_SESSION['fbLogout'] = 0;
            unset($_SESSION['errorLogin']);

            $this->_session->Set('user_id',$userLogin->nUserId,'business');
            $flag = '';
            if($userLogin->vStatus == 1) {
                 $flag = 2;
            } else {
                $flag = 1;
            }
            $arrUpdate       = array('vStatus'=>1);
            
            $this->_common->updateFields("users",$arrUpdate,"nUserId='".$userLogin->nUserId."'");

            $_SESSION['loggedin'] = 1;  // For forum
            $_SESSION['forum_user'] = $userLogin->vLoginName;  // For forum

            $this->redirect('business/index/dashboard/'.$flag);

            exit;

            
            }
            else
            {
                $this->redirect('business/index/');

                exit;
            }
        } //End User Login Auto
        
        public function checkLogin()
        {
            $dataUser = $_POST;
                
                

                // Validate Userlogin
                $userLogin = $this->_common->selectRecord("users","nUserId,vLoginName,vEmail,vFirstName,vLastName,vDelStatus,vStatus,nSocialUserId","LOWER(vLoginName)='".strtolower($dataUser['username'])."' AND vPassword ='".md5($dataUser['password'])."'");

                if(sizeof($userLogin) > 0){
                   if($userLogin->vStatus == 1){
                       $status = '';
                   }
                      else {
                            $status = 'Your account is no longer active!';
                        }                                        
                     } else {
                        $status = 'Invalid login! Please try again.';
                     }
                

            echo $status;
            exit;
        }
   
}
?>