<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

require_once('dao/TenancyDao.php');

class TenancyMgr {

	function insert ($tenancy) {
		return TenancyDao::insert ($tenancy);
	}
	function update ($tenancy) {
		return TenancyDao::update ($tenancy);
	}
	function delete ($tenancyId) {
		// Deleting Files linked to a Tenancy
		require_once ('business/TenancyFileMgr.php');
		$tenancyFileMgr = new TenancyFileMgr();
		$arrFiles = $tenancyFileMgr->getFiles($tenancyId);
		foreach ($arrFiles  as $file) {
			$tenancyFileMgr->delete($file->file_id);
			if (file_exists($file->filepath)){
				unlink($file->filepath);
			}else{
			}
		}
		// Deleting Reservations linked to a Tenancy
		require_once ('business/ReservationMgr.php');
		$reservationMgr = new ReservationMgr();
		$arrReserv = $reservationMgr->getReservationFromTenancy($tenancyId);
		foreach ($arrReserv as $reserv){
			$res = $reservationMgr->delete($reserv->reservation_id);
		}
		// Deleting Image linked to a Tenancy
		$arrTenancy = self::getTenancy($tenancyId);
		if ((file_exists(TENANCY_IMAGE_PATH_RESS. "/" . $arrTenancy[0]->imagepath)) && ($arrTenancy[0]->imagepath != "no-image.jpg")){
			unlink(TENANCY_IMAGE_PATH_RESS ."/" . $arrTenancy[0]->imagepath);
		}
		// Deleting Lease content linked to a Tenancy
		require_once ('business/LeaseContentMgr.php');
		$leaseContentMgr = new LeaseContentMgr();
		$res = $leaseContentMgr->deleteLeaseByTenancy($tenancyId);
		// Deleting Seasons linked to a Tenancy
		require_once ('business/SeasonMgr.php');
		$seasonMgr = new SeasonMgr();
		$arrSeason = $seasonMgr->getSeasonByTenancy($tenancyId);
		foreach ($arrSeason as $season){
			$res = $seasonMgr->delete($season->season_id);
		}
		// Deleting Extra linked to a Tenancy
		require_once ('business/ExtraMgr.php');
		$extraMgr = new ExtraMgr();
		$res = $extraMgr->deleteExtrasByTenancy($tenancyId);
		// Deleting the Tenancy itself
		return TenancyDao::delete ($tenancyId);
	}
	function getTenancys ($str = null){
		return TenancyDao::getTenancys ($str);
	}
	function searchTenancy ($str = null){
		return TenancyDao::searchTenancy ($str);
	}
	function getTenancy ($id){
		return TenancyDao::getTenancy ($id);
	}
}
