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

class CorporateGiftDetailClass extends MasterBase {
	function __construct($db) {
		$this->db 		= $db ;
		$this->tbl 		= CorporateGiftDetailTable::C_TABLE;
		$this->fldid 	= CorporateGiftDetailTable::C_DETAIL_ID;
	}
	function __destruct() {
	}
	function deleteCorporateGiftDetailByGiftID($id){
		$sql = "DELETE FROM " . CorporateGiftDetailTable::C_TABLE . " WHERE " . CorporateGiftDetailTable::C_ID . " = " . $id;

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

	function getCorporateGiftDetailByGiftID($id){
		$sql = "SELECT * FROM " . CorporateGiftDetailTable::C_TABLE . " WHERE " . CorporateGiftDetailTable::C_ID . " = " . $id;
		$rows = $this->db->getTable($sql);

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

	function deleteCorporateGiftDetailByDetailID($ids, $gift_id){
		$ids = implode(",", $ids);
		$sql = "SELECT * FROM " . CorporateGiftDetailTable::C_TABLE . " WHERE " . CorporateGiftDetailTable::C_DETAIL_ID . " IN (" . $ids . ")";
		$rows = $this->db->getTable($sql);

		foreach($rows as $row){//delete the image
			if($row[CorporateGiftDetailTable::C_DETAIL_ID] != "" && $row[CorporateGiftDetailTable::C_DETAIL_ID] != null){

				$picture_name           = explode("/", $row[CorporateGiftDetailTable::C_ITEM_IMAGE]);
            	$picture_name           = end($picture_name);

				unlink(PATH_CORPORATE_GIFT_ITEM . $gift_id . "/" . $picture_name);
			}
		}

		$sql = "DELETE FROM " . CorporateGiftDetailTable::C_TABLE . " WHERE " . CorporateGiftDetailTable::C_DETAIL_ID . " IN (" . $ids . ")";
		$this->db->executeSql($sql);
	}
}
