<?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: BroucherModel, v 1.0 2018/12/27 12:56:15
//All functions 
//
class BroucherModel extends Model{

public function listbroucher($params=array()){
        extract($params);
        $selectFields= "";
        
        if (isset($limitTo) && $limitTo)
                $selectFields .= " SQL_CALC_FOUND_ROWS ";	
        $sql=" 
                SELECT 
                        ".$selectFields."
                        broucher.*,pdtCatDet.productCategoryName
						FROM
                                " . DB_PREFIX . "broucher AS broucher
                        INNER JOIN 
                                " . DB_PREFIX . "product_category_details AS pdtCatDet
                                        ON	(
                                                broucher.productCategoryID = pdtCatDet.productCategoryID
                                                AND
                                                (pdtCatDet.languageID = '".$languageIDs['Current']."' OR pdtCatDet.languageID=0)
                                        )

                WHERE
                        1
        ";			
                        	
                        if(isset($status) && $status!=''){
                                $sql.= "
                                        AND
                                        status ='".$status."'
                                        ";
                        }else {
                                $sql.= "
                                        AND
                                        status !='D'
                                        ";
                        }	
                       


        $sql.="
                ORDER BY
                         broucher.broucherID ASC
        ";

if (isset($limitTo) && $limitTo){
        $sql .= " 
        LIMIT  
                " . $limitFrom . ",
                " . $limitTo . "
        ";
}
//echo $sql;exit;
return  $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC);
}

public function getBroucher($params=array()){
        extract($params);
        $selectFields= "";
        $sql=" 
                SELECT 
                        ".$selectFields."
                        *
               FROM 
                                " . DB_PREFIX . "broucher AS broucher

                WHERE
                        broucherID = '".$broucherID."' 
        ";	

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

}

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

}

public function insert($languageIDs,$POST,$broucherID = 0){
	if($broucherID  != ''){
	$sql = "
					UPDATE 
						" . DB_PREFIX . "broucher 
					SET
						productCategoryID = '" . $this->escape($POST['productCategoryID']) . "',
						bodyStyle = '" . $this->escape($POST['bodyStyle']) . "',
						broucherLink = '" . $this->escape($POST['broucherLink']) . "',
						status = '" . $this->escape($POST['status']) . "',
						createdDate = NOW()
					WHERE
						broucherID	 = '" . $broucherID	 . "'
				";
			$this->ObjDatabase->setQuery($sql);
	}else{
      $sql    = "
					INSERT INTO " . DB_PREFIX . "broucher 
						(
						productCategoryID,
						bodyStyle,
						broucherLink,
						status,
						createdDate
						)
					VALUES
						(
						'" . $this->escape($POST['productCategoryID']) . "',
						'" . $this->escape($POST['bodyStyle']) . "',
						'" . $this->escape($POST['broucherLink']) . "',
						'" . $this->escape($POST['status']) . "',
						NOW()
						)
					";
					//echo $sql;exit;
				$this->ObjDatabase->setQuery($sql);
				 $broucherID	 = $this->ObjDatabase->getInsertId();
           
	}
        return $broucherID;

}
public function listAllProductBodystyleDtls($params)
	{
		extract($params);
		$currentLanguageID	= $languageIDs['Current'];
		
		if(isset($type) && $type=='bodyStyle') {
			$fields 	= "DISTINCT bodyStyle";
			
		} 
		$sql 	= "	SELECT 
							" . $fields . "
								
							
						FROM 
							" . DB_PREFIX . "products AS product 
						INNER JOIN 
							" . DB_PREFIX . "product_details AS productDet 
							ON (
								product.productID = productDet.productID 
								AND 
								productDet.languageID = " . $currentLanguageID . "
							)
					" ;
		if(($categoryID !='' &&  $categoryID))
		{	
			$sql 	.= "
				LEFT JOIN 
				" . DB_PREFIX . "product_to_category   	AS productToCat 
					ON (product.productID = productToCat.productID) 
					";
		}
		$sql 	.= "WHERE
						1  AND product.brandID != '4'
					AND 
						product.productTypeID = '1'  
					AND 
						product.productStatus = 'Active' ";
		if(isset($categoryID) && $categoryID!='' && $categoryID!='0')
		{
			$sql 	.= "
				AND 
					productToCat.productCategoryID IN (" . $categoryID . ") ";
		}
		/* if(isset($shortDesc) && $shortDesc!='')
		{
			$sql 	.= "
				AND 
					productShortDescription = '" . $shortDesc . "' ";
		} */
		$sql 	.= "GROUP BY product.productID ";
                if($orderby!=''){
                    $sql .= "	ORDER BY ".$orderby."  ".$sortby." ";
                }else{
                    $sql .= "	ORDER BY product.productID  DESC ";
                }
				
	//echo $sql;
	return $this->ObjDatabase->readValuesSlave($sql, MYSQLI_ASSOC);
	}
	public function saveBroucherInfo($params){
		$params['subtitle'] = $this->encryptDecrypt("encrypt",$params['subtitle']);
        $params['userFirstName'] = $this->encryptDecrypt("encrypt",$params['userFirstName']);
        $params['userLastName']  = $this->encryptDecrypt("encrypt",$params['userLastName']);      
        $params['userEmail']     = $this->encryptDecrypt("encrypt",$params['userEmail']);      
        $params['userPhone']     = $this->encryptDecrypt("encrypt",$params['userPhone']);      
        //$arrExtraData['informationDownload']    =   $params;
		 $sql    = "
					INSERT INTO " . DB_PREFIX . "download_broucher_info 
						(
						productCategoryID,
						bodyStyle,
						subtitle,
						userFirstName,
						userLastName,
						phone,
						emailID,
						createdDate
						)
					VALUES
						(
						'" . $this->escape($params['productCategoryID']) . "',
						'" . $this->escape($params['bodyStyle']) . "',
						'" . $this->escape($params['subtitle']) . "',
						'" . $this->escape($params['userFirstName']) . "',
						'" . $this->escape($params['userLastName']) . "',
						'" . $this->escape($params['userPhone']) . "',
						'" . $this->escape($params['userEmail']) . "',
						NOW()
						)
					";
					//echo $sql;//exit;
				$this->ObjDatabase->setQuery($sql);
				 $broucherID	 = $this->ObjDatabase->getInsertId();
            return $broucherID;
	}
	public function getBroucherLinkByCategory($productCategoryID,$bodyStyle){
      
        $sql=" 
                SELECT broucherLink
               FROM 
                                " . DB_PREFIX . "broucher AS broucher

                WHERE
                        productCategoryID = '".$productCategoryID."' AND bodyStyle = '".$this->escape($bodyStyle)."'
        ";	

        $sql .= " 
        LIMIT  
                1
        ";
		//echo $sql;exit;
        return $this->ObjDatabase->getField($sql);

}
	public function getBroucherInfoByID($downloadBroucherID){
		$sql=" 
					SELECT productCategoryID,bodyStyle
				   FROM  " . DB_PREFIX . "download_broucher_info AS broucherInfo
				   
					WHERE
							downloadBroucherID = '".$downloadBroucherID."' ";	
							//echo $sql;exit;
		return  $this->ObjDatabase->readValue($sql, MYSQLI_ASSOC);
	}
	
}