<?php
require_once (PATH_TABLES . "campaign/CampaignHeaderImageTable.php") ;
require_once (PATH_MODELS . "base/MasterBase.php") ;

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

	function getCampaignIDHaveHeaderImage($id){
		$sql  = "SELECT * FROM " . $this->tbl . " WHERE " . CampaignHeaderImageTable::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[CampaignHeaderImageTable::C_COMPANY_ID]);
			}
			return $comp_ids;
		}else{
			return null;
		}
	}

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

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

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

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

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

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

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

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