<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of User class
 * Use for user relarated functionlities
 * @author Asha
 */
class User {
    //put your code here
   
     public static function is_seller_account_activated($buyerId)
    {
            $objResult                           = new stdClass();
           if(!empty($buyerId)) {
                 $tableName                      = DbHelper::setTablePrefix('users');
                 $selectCase                     =  "user_id = ".generalMethods::escapeString($buyerId). " ";
                 $objResultRow                   =  DbHelper::selectRecord($tableName,'is_seller,seller_id',$selectCase);
         // If user exists then assign user data to result object
                 if($objResultRow) {
                      $objResult->status         =    SUCCESS;
                      $objResult->data           =   $objResultRow; // Assign user record  to object
                 }
                 else{
                     $objResult->status          =  ERROR;
                  }
           }


         return $objResult;
    }

    // Check product already added to favourites list
    public static function user_favourites_count($userID,$productID,$productOptionID)
    {
            global $tableprefix;
            
            $favourites_count   =   0;

            if(!empty($productOptionID))
                {
                    $QueryWishItems="SELECT count(f.nId) as favouritesCount
                    FROM ".$tableprefix."favourites f
                    INNER JOIN ".$tableprefix."product_options po ON po.product_option_id = f.nProductOptionId
                    INNER JOIN ".$tableprefix."products p ON p.product_id = po.product_id
                    WHERE f.nUserId ='".$userID."'
                    AND f.nProductId = '".$productID."'
                    AND f.nProductOptionId = '".$productOptionID."' ";
                    $resSet = DbHelper::execute($QueryWishItems);
                    $data = DbHelper::fetchOne($resSet);
                    $favourites_count   =   $data['favouritesCount'];
                }
                
                return $favourites_count;
            
    }
    
    
    //Function to Insert Favourites details 
    
    public static function add_to_favourites_list($objProductVo)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('favourites');
            $favouriteId                         = DbHelper::insert($tableName, array(  'nProductId'                  =>  generalMethods::escapeString($objProductVo->nProductId),
                                                                                     'nProductOptionId'                     =>  generalMethods::escapeString($objProductVo->nProductOptionId),
                                                                                     'nUserId'              =>  generalMethods::escapeString($objProductVo->nUserId),
                                                                                     'seller_id'              =>  generalMethods::escapeString($objProductVo->seller_id),
                                                                                     'dDate'                    =>  generalMethods::escapeString($objProductVo->dDate)
                                                                                                                                                                         
                                                                                ));
         if($favouriteId) {  
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =  $favouriteId; 
         }else{
             $objResult->status                 =    ERROR;
         }
            
         return $objResult;
    }
    
    
    // function to delete user favourites list 
    public static function delete_user_favourite_list($wishId,$userid)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('favourites');
            $where                               = "nId =".$wishId. " AND nUserId =".$userid;
            $affectedRows                         = DbHelper::delete($tableName,$where);
         if($affectedRows>0) {  
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =  $affectedRows; 
         }else{
             $objResult->status                 =    ERROR;
         }
            
         return $objResult;
    }


    //  // Check seller  already added to favourites list
    public static function seller_favourites_count($userID,$sellerID)
    {
            global $tableprefix;

            $favourites_count   =   0;

            if(!empty($userID))
                {
                    $QueryWishItems="SELECT count(f.nId) as favouritesCount
                    FROM ".$tableprefix."seller_favourites f
                    WHERE f.nUserId ='".$userID."'
                    AND f.nSellerId = '".$sellerID."' ";
                    $resSet = DbHelper::execute($QueryWishItems);
                    $data = DbHelper::fetchOne($resSet);
                    $favourites_count   =   $data['favouritesCount'];
                }

                return $favourites_count;

    }



    //Function to Insert seller Favourites details

    public static function add_seller_to_favourites_list($objProductVo)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('seller_favourites');
            $sellerfavouriteId                   = DbHelper::insert($tableName, array('nSellerId'                  =>  generalMethods::escapeString($objProductVo->nSellerId),
                                                                                     'nUserId'              =>  generalMethods::escapeString($objProductVo->nUserId),
                                                                                     'dDate'                    =>  generalMethods::escapeString($objProductVo->dDate)

                                                                                ));
         if($sellerfavouriteId) {
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =    $sellerfavouriteId;
         }else{
             $objResult->status                 =    ERROR;
         }

         return $objResult;
    }


// Total seller favourites count 
    public static function total_seller_favourites_count($userID)
    {
            global $tableprefix;

            $favourites_count   =   0;

            if(!empty($userID))
                {
                    $QueryWishItems="SELECT count(f.nId) as favouritesCount
                    FROM ".$tableprefix."seller_favourites f
                    WHERE f.nUserId ='".$userID."' ";
                    $resSet = DbHelper::execute($QueryWishItems);
                    $data = DbHelper::fetchOne($resSet);
                    $favourites_count   =   $data['favouritesCount'];
                }

                return $favourites_count;

    }



    //

    // Total product  favourites count
    public static function total_product_favourites_count($userID)
    {
            global $tableprefix;

            $favourites_count   =   0;

            if(!empty($userID))
                {
                    $QueryWishItems="SELECT count(f.nId) as favouritesCount
                                    FROM ".$tableprefix."favourites f
                                    WHERE f.nUserId ='".$userID."' ";
                    $resSet = DbHelper::execute($QueryWishItems);
                    $data = DbHelper::fetchOne($resSet);
                    $favourites_count   =   $data['favouritesCount'];
                }

                return $favourites_count;

    }



      // function to delete seller  favourites list of a selected user
    
    public static function delete_favourite_seller_list($wishId,$userid)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('seller_favourites');
            $where                               = "nId =".$wishId. " AND nUserId =".$userid;
            $affectedRows                         = DbHelper::delete($tableName,$where);
         if($affectedRows>0) {
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =  $affectedRows;
         }else{
             $objResult->status                 =    ERROR;
         }

         return $objResult;
    }


     public static function getUserInfo($userid)
    {
            $objResult                           = new stdClass();            
           if(!empty($userid)) {
                 $tableName                      = DbHelper::setTablePrefix('users');
                 $selectCase                     =  "user_id = ".generalMethods::escapeString($userid). " ";
                 $objResultRow                   =  DbHelper::selectRecord($tableName,'*',$selectCase);
         // If user exists then assign user data to result object
                 if($objResultRow) {
                      $objResult->status         =    SUCCESS;
                      $objResult->data           =   $objResultRow; // Assign user record  to object
                 }
                 else{
                     $objResult->status          =  ERROR;
                  }
           }
         

         return $objResult;
    }
    //
    // Update user seller account status 

      public static function updateSellerAccountActivation($userObjVo)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('users');
            $where                               = "user_id = ".generalMethods::escapeString($userObjVo->userid). " ";
            $userId                              = DbHelper::update($tableName,array('is_seller' => generalMethods::escapeString('Y'),'seller_id' => generalMethods::escapeString($userObjVo->sellerid)),$where);
         if($userId) {
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =    $userId;
         }else{
             $objResult->status                 =    ERROR;
         }

         return $objResult;
    }


    // Update Buyer Password
       public static function updateBuyerPassword($userObjVo)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('users');
            $where                               = "user_id = ".generalMethods::escapeString($userObjVo->buyer_id). " ";
            $buyerId                             = DbHelper::update($tableName,array('password' => generalMethods::escapeString($userObjVo->password)),$where);
         if($buyerId) {
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =    $buyerId;
         }else{
             $objResult->status                 =    ERROR;
         }

         return $objResult;
    }


    // check user account exists

     public static function getBuyerAccountExists($buyerId)
    {
                 $objResult                      = new stdClass();
                 $tableName                      = DbHelper::setTablePrefix('users');
                 $selectCase                     =  "user_id =".generalMethods::escapeString($buyerId);
                 $objResultRow                   = DbHelper::selectSingleValue($tableName,'deleted',$selectCase);

         return $objResultRow;

    }


     // Save user ip address and account details

     public static function saveUserIpInfo($objIpVO)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('users');
            $where                               = "user_id = ".generalMethods::escapeString($objIpVO->user_id). " ";
            $userId                              = DbHelper::update($tableName,array('ip_address' => generalMethods::escapeString($objIpVO->ip_address),'ip_location' => generalMethods::escapeString($objIpVO->ip_location)),$where);
         if($userId) {
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =    $userId;
         }else{
             $objResult->status                 =    ERROR;
         }

         return $objResult;

    }


    // Select Distint Ip Adrrress

     public static function getAllIpAddress($ip)
    {
                $objResult                           = new stdClass();
                 $tableName                      =      DbHelper::setTablePrefix('users');
                 $selectCase                     =  '';
                 if($ip!='') {
                 $selectCase                     =  "ip_address = ".generalMethods::escapeString($ip). " ";
                 }else{
                  $selectCase                     =  "ip_address!= 'NULL'  ";

                 }
                 $objResultRow                   =  DbHelper::selectResult($tableName,'DISTINCT(ip_address)',$selectCase);
         
         return $objResultRow;
    }
    
    
       public static function check_user_email_exists($email)
    {
                 $objResult                      = new stdClass();
                 $tableName                      = DbHelper::setTablePrefix('users');
                 $selectCase                     = " email = '".generalMethods::escapeString($email). "' ";
                //echo $selectCase;
                 $objResultRow                   = DbHelper::selectRecord($tableName,'*',$selectCase);
                 return $objResultRow;

    }
    
      public static function check_user_name_exists($username)
    {
                 $objResult                      = new stdClass();
                 $tableName                      = DbHelper::setTablePrefix('users');
                 $selectCase                     = " user_name = '".generalMethods::escapeString($username). "'   ";
                //echo $selectCase;
                 $objResultRow                   = DbHelper::selectRecord($tableName,'*',$selectCase);
                 return $objResultRow;

    }
     public static function is_buyer_account_exists($artistid)
    {
            $objResult                           = new stdClass();
           if(!empty($artistid)) {
                 $tableName                      = DbHelper::setTablePrefix('users');
                 $selectCase                     =  "seller_id = ".generalMethods::escapeString($artistid). " ";
                 $objResultRow                   =  DbHelper::selectRecord($tableName,'user_id',$selectCase);
         // If user exists then assign user data to result object
                 if($objResultRow) {
                      $objResult->status         =    SUCCESS;
                      $objResult->data           =   $objResultRow; // Assign user record  to object
                 }
                 else{
                     $objResult->status          =  ERROR;
                  }
           }


         return $objResult;
    }
     public static function updateUserProfileInformation($userObjVo)
    {
            $objResult                           = new stdClass();
            $tableName                           = DbHelper::setTablePrefix('users');
            $where                               = "user_id = ".generalMethods::escapeString($userObjVo->user_id). " ";
            $sellerId                              = DbHelper::update($tableName,array('first_name' => generalMethods::escapeString($userObjVo->first_name),
                                                            'last_name' => generalMethods::escapeString($userObjVo->last_name),
                                                            'address1' => generalMethods::escapeString($userObjVo->address1),
                                                            'address2' => generalMethods::escapeString($userObjVo->address2),
                                                            'city' => generalMethods::escapeString($userObjVo->city),
                                                            'state' => generalMethods::escapeString($userObjVo->state),
                                                            'country' => generalMethods::escapeString($userObjVo->country),
                                                            'zip' => generalMethods::escapeString($userObjVo->zip),
                                                            'email' => generalMethods::escapeString($userObjVo->email),
                                                            'phone' => generalMethods::escapeString($userObjVo->phone),
                                                            'fax' => generalMethods::escapeString($userObjVo->fax)),$where);
         if($sellerId) {
            $objResult->status                  =    SUCCESS;
            $objResult->data                    =    $sellerId;
         }else{
             $objResult->status                 =    ERROR;
         }

         return $objResult;
    }
    
    
}
?>
