<?php
/**
 * Klasa obsługi tablicy mf_product_link
 *
 */
class MfProductLinkDAL extends DefaultDAL{


	protected static $objClassName =  'MfProductLink';
	protected static $objClassTable = 'mf_product_link';
	protected static $objClassTablePK = 'id_mf_product_link';
	private static $optClass;

	


	public static function Save($obj) {
		if($obj->GetId()=='-1') {
			$dalData = new DalData();
			$dalData->setObjClassTable(self::GetObjClassTable());
			$dalData->setObj($obj);
			return self::DefaultInsert($dalData);
		} else {
			$dalData = new DalData();
			$dalData->setObjClassTable(self::GetObjClassTable());
			$dalData->setObj($obj);
			return self::DefaultUpdate($dalData);
		}
	}

	public static function Insert(DalData $dalObj) {
		return self::DefaultInsert($dalObj);
	}

	public static function Update(DalData $dalObj) {
		return self::DefaultUpdate($dalObj);
	}

	public static function GetById($id, $lang = 'pl') {
		$dalData = self::GetDalDataObj();
		$dalData->setCondition(array(self::GetObjClassName().'.'.self::GetObjClassTablePK()=>$id, 'lang' => $lang));
		if (class_exists(self::GetObjClassName().'Description')) {			
			$dalData->setJoin(array(self::GetObjClassName().'Description' => ' LEFT JOIN '.self::GetObjClassTable().'_description ON '.self::GetObjClassTable().'.id_'.self::GetObjClassTable().'='.self::GetObjClassTable().'_description.id_'.self::GetObjClassTable().''));		
		}
		$dalData->setLimit(1);
		$result = self::GetResult($dalData);
		if ((count($result) > 0) && (is_object($result[0]))) {
			return $result[0];
		} else {
			//throw new Exception('Brak rekordu w tablicy '.self::GetObjClassTable().' o id <b>'.$id.'</b>!');
			return self::GetEmptyObj();
			
			
		}
	}
	
	public function GetArrayId($source , $destination, $idDestination, $lang = 'pl') {
		$db = Registry::Get('db');
		$sql = "SELECT id_source FROM `mf_product_link` WHERE `source_type` = '$source' AND `destination_type` = '$destination' AND `id_destination` = $idDestination AND lang = '$lang'";
		//Utils::ArrayDisplay($sql);
		$stmt=$db->prepare($sql)
		->execute();
		//$stmt->CacheStart($cacheTime);
		$returnArray = array();
		while(($row = $stmt->FetchArray())) {
			$returnArray[] = $row['id_source'];
		}
		return $returnArray;
	}

	public function GetIdString($source , $destination, $idDestination, $lang = 'pl', $idDestinationNotIN = null) {
		$db = Registry::Get('db');
		$sql = "SELECT id_source FROM `mf_product_link` WHERE `source_type` = '$source' AND `destination_type` = '$destination'  AND lang = '$lang'".
						($idDestination ? " AND `id_destination` = $idDestination " : "").
						($idDestinationNotIN ? " AND `id_destination` NOT IN ($idDestinationNotIN) " : "").
						" GROUP BY id_source ";
		$stmt=$db->prepare($sql)->execute();

		//Utils::ArrayDisplay($sql);
		if ($stmt->NumRows() > 0) {

		}
		//$stmt->CacheStart($cacheTime);
		$ids = ' ';
		$returnArray = array();
		while(($row = $stmt->FetchArray())) {
			$returnArray[] = $row['id_source'];
		}
		if (count($returnArray) > 0) {
			$ids .= implode(',', $returnArray);
		} else {
			$ids .= '-1';
		}
		$ids .= ' ';
		return $ids;
	}

	public function GetArrayIdDestinaion($source , $destination, $idSource, $lang = 'pl') {
		$db = Registry::Get('db');
		$sql = "SELECT `id_destination` FROM `mf_product_link` WHERE `source_type` = '$source' AND `destination_type` = '$destination' AND `id_source`  = $idSource AND lang = '$lang'";
		//Utils::ArrayDisplay($sql);
		$stmt=$db->prepare($sql)
		->execute();
		//$stmt->CacheStart($cacheTime);
		$returnArray = array();
		while(($row = $stmt->FetchArray())) {
			$returnArray[] = $row['id_destination'];
		}
		return $returnArray;
	}

	public function GetIdStringDestinaion($source , $destination, $idSource, $lang = 'pl', $idSourceNotIN = null) {
		$db = Registry::Get('db');
		$sql = "SELECT id_destination FROM `mf_product_link` WHERE `source_type` = '$source' AND `destination_type` = '$destination'  AND lang = '$lang'".
						($idSource ? " AND `id_source` = $idSource " : "").
						($idSourceNotIN ? " AND `id_source` NOT IN ($idSourceNotIN) " : "").
						" GROUP BY id_destination ";
		$stmt=$db->prepare($sql)->execute();
//Utils::ArrayDisplay($sql);
		if ($stmt->NumRows() > 0) {
			//$stmt->CacheStart($cacheTime);
			$ids = ' ';
			$returnArray = array();
			while(($row = $stmt->FetchArray())) {
				$returnArray[] = $row['id_destination'];
			}
			if (count($returnArray) > 0) {
				$ids .= implode(' ,', $returnArray);
			} else {
				$ids .= '-1';
			}
			$ids .= ' ';
			
			return $ids;

		} else {
			return '-1';
		}
	}

	public static function Delete(DalData $dalObj) {
		return self::DefaultDelete($dalObj);
	}

	public static function GetResult(DalData $dalObj) {
		return self::DefaultGetResult($dalObj);
	}
	
	
	
	public static function DeleteFromLink($idSource = null, $sourceType = null, $idDestination = null, $destinationType = null, $lang=null) {

		$db = Registry::Get('db');
		$sql = "DELETE FROM `mf_product_link` WHERE 1=1 ";

		if($idSource!= null )
		$sql.= " AND id_source = :01 ";

		if($sourceType!= null)
		$sql.= " AND source_type = :02 ";

		if($idDestination!= null)
		$sql.= " AND id_destination = :03 ";

		if($destinationType!= null)
		$sql.= " AND destination_type = :04 ";
		
		if($lang!= null)
		$sql.= " AND lang = :05 ";

		$stmt=$db->prepare($sql);

		if($idSource!= null )
		$stmt->bindParam('01', $idSource);

		if($sourceType!= null)
		$stmt->bindParam('02', $sourceType);

		if($idDestination!= null)
		$stmt->bindParam('03', $idDestination);
		if($destinationType!= null)
		
		$stmt->bindParam('04', $destinationType);
		if($lang!= null)
		
		$stmt->bindParam('05', $lang);
		
		$stmt->execute();
		//Utils::ArrayDisplay($stmt);
	}
	
	public function IsLinked($idSource = null, $sourceType = null, $idDestination = null, $destinationType = null, $lang = null ,$published = '-1', $idSourceSql = '=') {

		$db = Registry::Get('db');
		$sql = "SELECT count(id_mf_product_link) as count FROM `mf_product_link` WHERE 1=1 AND publication NOT IN (".$published.") ";

		if($idSource!= null )
		$sql.= " AND id_source ".$idSourceSql." $idSource ";

		if($sourceType!= null)
		$sql.= " AND source_type = :02 ";

		if($idDestination!= null)
		$sql.= " AND id_destination ".$idSourceSql." $idDestination ";

		if($destinationType!= null)
		$sql.= " AND destination_type = :04 ";

		if($lang!= null)
		$sql.= " AND lang = :05 ";

		$stmt=$db->prepare($sql);

		if($idSource!= null )
		$stmt->bindParam('01', $idSource);

		if($sourceType!= null)
		$stmt->bindParam('02', $sourceType);

		if($idDestination!= null)
		$stmt->bindParam('03', $idDestination);
		
		if($destinationType!= null)
		$stmt->bindParam('04', $destinationType);
		
		if($lang!= null)
		$stmt->bindParam('05', $lang);
		
		$stmt->execute();

		$array = $stmt->fetchAllAssoc();


		return $array[0]['count'];


	}
	
	

	public static function GetResultByLink($table, $id ,$destination = false, $data = array(), $queryFields = array(), $limit = 0, $sortBy = null,$count = null)
	{
		$dalData = new DalData();
		$dalData->setObjClassName(self::GetObjClassName());
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setOptClass(self::GetOptClass());
		$dalData->setCondition($data);
		$dalData->setQueryFields($queryFields);
		$dalData->setLimit($limit);
		$dalData->setSortBy($sortBy);
		$dalData->setCount($count);
		$dalData->setDataArray("mf_link_id", $id);
		$dalData->setDataArray("mf_link_table", $table);
		$dalData->setDataArray("mf_link_destination", $destination);
		return self::DefaultGetResultByLink($dalData);
	}


	public static function GetObjClassName() {
		if(self::$objClassName != '') {
			$class = self::$objClassName;
		} else {
			$class = ereg_replace('DAL', '', __CLASS__);
		}

		return $class;
	}

	public static function GetOptClass() {
		if(self::$optClass!=null) {
			return self::$optClass;
		} else {
			return self::GetObjClassName();
		}
	}

	public static function GetObjClassTablePK() {
		if(self::$objClassTablePK != '') {
			$return = self::$objClassTablePK;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetClassTablePK();
		}
		return $return;
	}

	public static function GetObjClassTable() {
		if(self::$objClassTable!='') {
			$return = self::$objClassTable;
		} else {
			$class = self::GetObjClassName();
			$classObj = new $class();
			$return = $classObj->GetTableName();
		}

		return $return;
	}

	/**
	 *
	 * @return DalData $dalData
	 */
	public static function GetDalDataObj($params = array()) {

		$dalData = new DalData($params);
		$dalData->setObjClassName(self::GetObjClassName());
		$dalData->setObjClassTable(self::GetObjClassTable());
		$dalData->setObjClassTablePK(self::GetObjClassTablePK());
		$dalData->setOptClass(self::GetOptClass());

		return $dalData;
	}

}

?>