<?php
require_once (PATH_TABLES . "corporate_gift/CorporateGiftHeaderImageTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

class CorporateGiftHeaderImageClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= CorporateGiftHeaderImageTable::C_TABLE;
		$this->fldid 	= CorporateGiftHeaderImageTable::C_HEADER_ID;
	}
	function __destruct() {
	}

	function getCompanyIDHaveHeaderImage($id){
		$sql  = "SELECT * FROM " . $this->tbl . " WHERE " . CorporateGiftHeaderImageTable::C_ID . " = " . $id;
		$rows = $this->db->getTable($sql);
		
		if(!is_null($rows) && count($rows) > 0){
			$comp_ids = [];
			foreach($rows as $row){
				array_push($comp_ids, $row[CorporateGiftHeaderImageTable::C_COMPANY_ID]);
			}
			return $comp_ids;
		}else{
			return null;
		}
	}

	function updateHeaderImageByCompIDAndGiftID($img_path, $gift_id, $comp_id){
		$sql = "UPDATE " . CorporateGiftHeaderImageTable::C_TABLE .
					" SET "  . CorporateGiftHeaderImageTable::C_IMAGE_PATH . " = '" . $img_path . "'" .
				" WHERE " . CorporateGiftHeaderImageTable::C_ID . " = " . $gift_id.
				" AND " . CorporateGiftHeaderImageTable::C_COMPANY_ID . " = " . $comp_id;
		$this->db->executeSql($sql);
	}

	function deleteCorporateGiftHeaderImageByGiftID($id){
		$sql = "DELETE FROM " . CorporateGiftHeaderImageTable::C_TABLE . " WHERE " . CorporateGiftHeaderImageTable::C_ID . " = " . $id;

		$this->db->executeSql($sql);
	}

	function getCorporateGiftHeaderImageByGiftID($id){
		$sql = "SELECT * FROM " . CompanyProfileTable::C_TABLE . " C " .
				" LEFT JOIN " . CorporateGiftHeaderImageTable::C_TABLE . " CG ON C." . CompanyProfileTable::C_ID . " = CG." . CorporateGiftHeaderImageTable::C_COMPANY_ID .
				" WHERE CG." . CorporateGiftHeaderImageTable::C_ID . " = " . $id . " OR CG." . CorporateGiftHeaderImageTable::C_COMPANY_ID . " IS NULL";

		$rows = $this->db->getTable($sql);

		if(!is_null($rows) && count($rows) > 0){
			return $rows;
		}else{
			return null;
		}
	}

	function getCorporateGiftHeaderImageByGiftIDAndCompID($id, $comp_id){
		$sql = "SELECT * FROM " . CorporateGiftHeaderImageTable::C_TABLE . 
					" WHERE " . CorporateGiftHeaderImageTable::C_ID . " = " . $id . " AND " . CorporateGiftHeaderImageTable::C_COMPANY_ID . " = " . $comp_id;
		
		$rows = $this->db->getTable($sql);

		if(!is_null($rows) && count($rows) > 0){
			return $rows[0][CorporateGiftHeaderImageTable::C_IMAGE_PATH];
		}else{
			return null;
		}
	}
}
