<?php
 
class User extends AppModel
{
    var $name='User';


       function isUserExist( $profile_id ){
         
        /*
        $unique_number = "234234-34234-234234-234234-324234"."4537"; // device id + six digit random number
        $salt      = 'Nop@ss%!*-+@s=_17';
        $generated_token    = hash('sha256', $unique_number . $salt);
        
        echo $generated_token;exit;
     */
        $result = true;
        
       // $profile_id = "'".$profile_id."'";
        
        $userObj = $this->find('first', array('conditions' => array('profile_id' =>$profile_id))); 
        //$log = $this->getDataSource()->getLog(false, false);
          
        
        if(empty($userObj)){
            $result = false;
        }
        
        return array($result,$userObj);
        
    }

    function isUserIdExist( $user_id ){
        $result = true;
                
        $userObj = $this->find('first', array('conditions' => array('id' =>$user_id)));   
        if(empty($userObj)){
            $result = false;
        }
        return array($result,$userObj);
    }
    
    function getUserByProfileId($profile_id) {
        $userObj = $this->find('first', array('fields' => array('id', 'first_name','last_name','password','profile_type', 'profile_id','is_verified','nick_name', 'preferences','version','create_date', 'update_date'),'conditions' => array('profile_id' =>$profile_id))); 
        return $userObj;
    }

    function getUserByUserId($user_id){
        //$userObj = $this->find('first', array('fields' => array('id', 'first_name','last_name','password','profile_type', 'profile_id','is_verified','nick_name', 'preferences','version','create_date', 'update_date'),'conditions' => array('id' =>$user_id))); 
        $userObj = $this->find('first', array('fields' => array('id','profile_type', 'profile_id','first_name','last_name'),'conditions' => array('id' =>$user_id)));
        return $userObj;
    }

    
    function insertUser($inputData){
        
        $result = false;
        
        //$form["first_name"] = trim($inputData["first_name"]);
        //$form["last_name"] = trim($inputData["last_name"]);
        
        //1=email, 2=gmail, 3=facebook, 4= twiitter; default = 1
        $form["profile_type"] = trim($inputData["profile_type"]);
        $form["profile_id"] = trim($inputData["profile_id"]);
        $form["password"] =  trim($inputData["password"]);
        //$form["preferences"] = trim($inputData["preferences"]);
        
        if (isset($inputData["is_verified"])) {
            $form["is_verified"] = trim($inputData["is_verified"]);
        }
        //$form["nick_name"] = trim($inputData["nick_name"]);
        
       
       
        
        //$form["ip"] = trim($inputData["ip"]);
        //$form["device_info"] = trim($inputData["device_info"]);
        
        $form["create_date"] = trim($inputData["create_date"]);
        $form["update_date"] = trim($inputData["update_date"]);  
        
        
        $fileds = array('first_name','last_name','profile_type','profile_id','password',
            'preferences','is_verified','nick_name', 'ip', 'device_info','create_date','update_date');
        
      
        
        
        $dbData["User"] = $form; 
        
        if($this->save($dbData,false,$fileds)){
            $result = true;
            
        }
        
        return $result;
    }
    
    
    function saveAllGroups($data){
        
    }

    function setPassTokenByProfileId( $profile_id, $resetPassToken, $resetPassTokenExpireDateTime )
    {
        return $this->updateAll(
            array(
                "resetpass_token" => '"'.$resetPassToken.'"',
                "resetpass_token_expire_date" => '"'.$resetPassTokenExpireDateTime.'"'
            ),
            array("profile_id" => $profile_id)
        );
    }

    function resetPassTokenByProfileId( $profile_id )
    {
        return $this->updateAll(
            array(
                "resetpass_token" => null,
                "resetpass_token_expire_date" => null
            ),
            array("profile_id" => $profile_id)
        );
    }

    function setVerificationTokenByProfileId( $profile_id, $verificationToken )
    {
        return $this->updateAll(
            array(
                "verification_token" => '"'.$verificationToken.'"'
            ),
            array("profile_id" => $profile_id)
        );
    }

    function resetVerificationTokenByProfileId( $profile_id )
    {
        return $this->updateAll(
            array(
                "verification_token" => null,
                "is_verified" => 1
            ),
            array("profile_id" => $profile_id)
        );
    }

    function setPasswordByProfileId( $profile_id, $password )
    {
        return $this->updateAll(
            array(
                "password" => '"'.$password.'"'
            ),
            array("profile_id" => $profile_id)
        );
    }

    function isUserExistByResetPassToken( $resetPassToken )
    {
        $result = true;
        if(!empty($resetPassToken)){
            $userObj = $this->find('first', array(
                'conditions' => array('resetpass_token' => $resetPassToken)
            ));
        }

        if(empty($userObj)){
            $result = false;
        }
        return array($result,$userObj);
    }

    function isUserExistByVerificationToken( $profile_id, $verificationToken )
    {
        $result = true;
        $userObj = $this->find('first', array(
            'conditions' => array(
                'profile_id' => $profile_id,
                'verification_token' => $verificationToken
            )
        ));

        if(empty($userObj)){
            $result = false;
        }
        return array($result,$userObj);
    }

    function updateFieldsById($id, $fields)
    {
        return $this->updateAll($fields,array("id" => $id));
    }

    function isExistUserById($id){
        $result = true;

        $userObj = $this->find('first', array('conditions' => array('id' =>$id)));

        if(empty($userObj)){
            $result = false;
        } else {
            $userObj = $userObj['User'];
        }
        return array($result,$userObj);
    }

    function isUserExistByProfile($profile_id, $profile_type) {
        $result = true;

        $userObj = $this->find('first', array(
            'conditions' => array(
                'profile_id' => $profile_id,
                'profile_type' => $profile_type,
            )
        ));

        if(empty($userObj)){
            $result = false;
        } else {
            $userObj = $userObj['User'];
        }
        return array($result,$userObj);
    }

    function findByProfileId( $profile_id ){

        return $this->find('first', array('conditions' => array('profile_id' =>$profile_id)));
    }

}
?>