<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: OtpCountryModel, v 1.0 2019/05/27 11:32:09
//All functions 
//
class OtpCountryModel extends Model{

public function listOtpCountry($params=array()){
        extract($params);
        $selectFields= "";
        if (isset($limitTo) && $limitTo)
                $selectFields .= " SQL_CALC_FOUND_ROWS ";	
        $sql=" 
                SELECT 
                        ".$selectFields."
                        *
                FROM 
                                dp_otp_country_code AS otpcountry
                INNER JOIN  dp_otp_country_code_details otpcountrydetails
                ON (otpcountrydetails.countryCodeID = otpcountry.countryCodeID AND  otpcountrydetails.languageID = '" . $languageIDs['Content'] . "')
                WHERE
                        1
        ";			
                        if(isset($countryCodeID) && $countryCodeID!=''){
                                $sql.= "
                                        AND
                                        countryCodeID ='".$countryCodeID."'
                                        ";
                        }			
                        if(isset($countryCode) && $countryCode!=''){
                                $sql.= "
                                        AND
                                        countryCode like '%".$this->escape($countryCode)."%'
                                        ";
                        }			
                        if(isset($countryName) && $countryName!=''){
                                $sql.= "
                                        AND
                                        countryName like '%".$this->escape($countryName)."%'
                                        ";
                        }			
                        if(isset($status) && $status!=''){
                                $sql.= "
                                        AND
                                        status ='".$status."'
                                        ";
                        }else {
                                $sql.= "
                                        AND
                                        status !='D'
                                        ";
                        }	

if(isset($orderby) && isset($sortby) && $orderby!="" && $sortby!="" ){
        $sql.="
                ORDER BY
                        ".substr($sortby, 5)." ".$orderby."
        ";
}
if (isset($limitTo) && $limitTo>0){
        $sql .= " 
        LIMIT  
                " . $limitFrom . ",
                " . $limitTo . "
        ";
}
return  $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC);
}

public function getOtpCountry($params=array()){
        extract($params);
        $selectFields= "";
        $sql=" 
                SELECT 
                        ".$selectFields."
                        *,otpcountrydetails.countryName
                FROM 
                                dp_otp_country_code AS otpcountry
                INNER JOIN  dp_otp_country_code_details otpcountrydetails 
                ON (otpcountrydetails.countryCodeID = otpcountry.countryCodeID AND  otpcountrydetails.languageID = '" . $languageIDs['Content'] . "')
                WHERE
                        otpcountry.countryCodeID = '".(int)$countryCodeID."'
        ";	

        $sql .= " 
        LIMIT  
                1
        ";
        return  $this->ObjDatabase->readValue($sql, MYSQLI_ASSOC);

}

public function changeStatus($params=array()){
        extract($params);
        $sql="
                UPDATE  
                        dp_otp_country_code 
                SET 
                        status = '".$status."' 
                WHERE
                        countryCodeID IN ('".implode("','",$countryCodeIDs)."')
                ";
        $this->ObjDatabase->setQuery($sql);	

}

public function insert($languageIDs,$countryCodeID=0){
        if($countryCodeID>0){
                $sql="
                        UPDATE 
                                dp_otp_country_code 
                        SET
                                countryCode = '".$this->escape($_POST['countryCode'])."',status = '".$this->escape($_POST['status'])."'
                        WHERE
                                countryCodeID = '".(int)$countryCodeID."'
                ";
                $this->ObjDatabase->setQuery($sql);
                $sql="
                        UPDATE 
                                dp_otp_country_code_details 
                        SET
                                countryName = '".$this->escape($_POST['countryName'])."'
                        WHERE
                                countryCodeID = '".(int)$countryCodeID."'
                                AND
                                languageID = '".(int)$languageIDs['Content']."'
                ";
                $this->ObjDatabase->setQuery($sql);	
        }
        else{
                $sql="
                        INSERT INTO dp_otp_country_code 
                                (
                                countryCode,status
                                )
                        VALUES
                                (
                                '".$this->escape($_POST['countryCode'])."','".$this->escape($_POST['status'])."'
                                )
                        ";
                $this->ObjDatabase->setQuery($sql);	
                $countryCodeID = $this->ObjDatabase->getInsertId();
                
                $resInsert = $this->ObjDatabase->getInsertId();
                foreach($languageIDs['All'] as $languageID){
                        $tempArr[] =  "('".$languageID."','".$this->escape($_POST['countryName'])."','".$this->escape($countryCodeID)."')";
                }

                $sql="
                        INSERT INTO dp_otp_country_code_details 
                                (
                                languageID,
                                countryName,
                                countryCodeID
                                )
                        VALUES".
                                implode(",",$tempArr);
                $this->ObjDatabase->setQuery($sql);	
        }
        return $countryCodeID;

}

public function listOtpCountryOptions($params=array()){
        extract($params);
        $selectFields= "";
        if (isset($limitTo) && $limitTo)
                $selectFields .= " SQL_CALC_FOUND_ROWS ";	
        $sql=" 
                SELECT 
                        countryCode,CONCAT('+',countryCode) AS displayCode
                FROM 
                                dp_otp_country_code AS otpcountry
                INNER JOIN  dp_otp_country_code_details otpcountrydetails 
                ON (otpcountrydetails.countryCodeID = otpcountry.countryCodeID AND  otpcountrydetails.languageID = '" . $languageIDs['Content'] . "')
                WHERE
                        1
        ";			
                        if(isset($countryCodeID) && $countryCodeID!=''){
                                $sql.= "
                                        AND
                                        countryCodeID ='".$countryCodeID."'
                                        ";
                        }			
                        if(isset($countryCode) && $countryCode!=''){
                                $sql.= "
                                        AND
                                        countryCode like '%".$this->escape($countryCode)."%'
                                        ";
                        }			
                        if(isset($countryName) && $countryName!=''){
                                $sql.= "
                                        AND
                                        countryName like '%".$this->escape($countryName)."%'
                                        ";
                        }			
                        if(isset($status) && $status!=''){
                                $sql.= "
                                        AND
                                        status ='".$status."'
                                        ";
                        }else {
                                $sql.= "
                                        AND
                                        status !='D'
                                        ";
                        }	

if(isset($orderby) && isset($sortby) && $orderby!="" && $sortby!="" ){
        $sql.="
                ORDER BY
                        ".substr($sortby, 5)." ".$orderby."
        ";
}
if (isset($limitTo) && $limitTo>0){
        $sql .= " 
        LIMIT  
                " . $limitFrom . ",
                " . $limitTo . "
        ";
}

return  $this->ObjDatabase->readValues($sql);
}
public function listOtpCountryOptionsForBroucher($params=array()){
        extract($params);
        $selectFields= "";
        if (isset($limitTo) && $limitTo)
                $selectFields .= " SQL_CALC_FOUND_ROWS ";	
        $sql=" 
                SELECT 
                        countryCode,CONCAT('+',countryCode) AS displayCode
                FROM 
                                dp_otp_country_code AS otpcountry
                INNER JOIN  dp_otp_country_code_details otpcountrydetails 
                ON (otpcountrydetails.countryCodeID = otpcountry.countryCodeID AND  otpcountrydetails.languageID = '" . $languageIDs['Current'] . "')
                WHERE
                        1
        ";			
                        if(isset($countryCodeID) && $countryCodeID!=''){
                                $sql.= "
                                        AND
                                        countryCodeID ='".$countryCodeID."'
                                        ";
                        }			
                        if(isset($countryCode) && $countryCode!=''){
                                $sql.= "
                                        AND
                                        countryCode like '%".$this->escape($countryCode)."%'
                                        ";
                        }			
                        if(isset($countryName) && $countryName!=''){
                                $sql.= "
                                        AND
                                        countryName like '%".$this->escape($countryName)."%'
                                        ";
                        }			
                        if(isset($status) && $status!=''){
                                $sql.= "
                                        AND
                                        status ='".$status."'
                                        ";
                        }else {
                                $sql.= "
                                        AND
                                        status !='D'
                                        ";
                        }	

if(isset($orderby) && isset($sortby) && $orderby!="" && $sortby!="" ){
        $sql.="
                ORDER BY
                        ".substr($sortby, 5)." ".$orderby."
        ";
}
if (isset($limitTo) && $limitTo>0){
        $sql .= " 
        LIMIT  
                " . $limitFrom . ",
                " . $limitTo . "
        ";
}

return  $this->ObjDatabase->readValues($sql);
}
}
