<?php
  class UsersController extends AppController {
  var $name = 'Users';
  var $components = array('Session', 'Email', 'Auth');
 
   // Not necessary if declared in your app controller
  
    /**
   * The AuthComponent provides the needed functionality
  * for login, so you can leave this function blank.
   */
  function beforeFilter() {
	$this->Auth->allow('*');
	$this->Auth->fields = array('username' => 'username', 'password' => 'password');
	$this->Auth->loginAction = '/users/login';
	$this->Auth->loginError = 'Invalid username or password.';
    $this->Auth->authError = 'You need authorization.';
     $this->Auth->loginRedirect = '/Homes/published';
     $this->Auth->logoutRedirect = '/Users/Login';
	parent::beforeFilter();
	}
	
	
	
 
function isAuthorized() {

        if (!parent::isAuthorized()) {

            return false;

        }

        if (in_array($this->action, array('register', 'login'))) {

            $this->Auth->authError = 'You are already logged in.';

            return false;

        }

        return true;

    }
    
	
   function login() {
   	$this->layout = 'login';
   	
   	//print_r($_POST['data']['User']['password']);
   	
  
  }
   function logout() {
   $this->Session->destroy();
   $this->redirect($this->Auth->logout());
   }
   
   
   
   function register(){
   		//$this->User->recursive = -1;
   		    $confirmation_code=mt_rand();
			$confirmation_code .="-";
			$confirmation_code .=mt_rand();
			$confirmation_code .="-";
			$confirmation_code .=mt_rand();
		if (!empty($this->data)) { 
			
		   	$data=$this->data;
			$data['Users']['registration_date'] = date('Y-m-d');
			$data['Users']['uname']='admin';
			
			
			 //print_r($this->data);
			 //echo $this->User->validates();
			 
			 if ($data['Users']['password']!=''&&$data['Users']['confirm_password']!='') {
			
			$data['Users']['password'] = $this->Auth->password($data['Users']['password']); 
			$data['Users']['confirm_password'] = $this->Auth->password($data['Users']['confirm_password']); 
			
			$data['Users']['confirmation_code']=$confirmation_code;
			
			 }
			 
			//$this->User->create(); 
			
			if ($this->User->save($data['Users'])){ 
				
			 //$this->User->saveField('password', $this->data['password']);
			// $this->Session->setFlash(__('You have successfully registered.', true)); 
					$to_email=$data['Users']['email'];
				    $this->Email->from = 'info@businessghana.com';
    				$this->Email->to = $to_email;
   					$this->Email->subject = 'Confirmation';
   					$this->Email->send('Testing!');
   					$this->Email->sendAs = 'both';
				
			 $this->redirect('confirm');
			}
			echo $data['Users']['password'];
		   
		      
		  }
		
		  
		
		  $url=Router::url("/", true);
		  $url .="users/emailconfirmation/".$confirmation_code;
		  $this->set('url',$url);
		  


		  	  
 }
    
function confirm(){    
   
    
  }
  
function emailConfirmation(){
	
if(!empty($this->params['pass'])){
	
	$name=$this->User->find('all',array('fields'=>array('first_name','uid','last_name'),'conditions'=>array('confirmation_code'=>$this->params['pass'],'status'=>0)));
	
	if(empty($name)){
		$this->set('isValid','false');
		
	}
	else{
	$this->set('isValid','true');
	$this->set('first_name',$name[0]['User']['first_name']);
	$this->set('last_name',$name[0]['User']['last_name']);
	$this->User->updateAll(array('user.status' => 1),array('user.uid'=>$name[0]['User']['uid']));
	}
    }
    else{
    	$this->set('isValid','false');
    }




	
}

function settings(){

}
  
  }
?>