<?php

include_once 'classes/core/Site.class.php';

/**
 * @author vinadelmar <vinadelmar@users.sourceforge.net>
 * @since  2006-04-05
 * @static
 */
class FamilyManager {
	var $site;

	function FamilyManager(&$site) {
		$this->site = &$site;
	}

	/**
	 * @return integer;
	 */
	function getParentObjAclId(&$obj) {
		$class_name = strtolower(get_class($obj));
		$aclId = 0;

		switch ($class_name) {
		case 'content':
			$aclId = Family::_getContentParentAclId($obj);
			break;
			
		case 'container':
			$parentObject = &$this->getContainerParentObject($obj);
			$parentClassName = strtolower(get_class($parentObject));
			if($parentClassName == 'containerslist') {
				$aclId = $parentObject->getContainersListAclId();
			}
			else if ($parentClassName == 'page') {
				$aclId = $parentObject->getPageAclId();
			}
			break;
			
		case 'containerslist':
			$parentObject = &$this->getContainersListParentObject($obj);
			$aclId = $parentObject->getPageAclId();
			break;
			
		case 'menu':
			$acl = &$obj->getMenuAcl();
			$aclId = $acl->getAclId();
			break;
			
		case 'page':
			$acl = &$obj->getPageAcl();
			$aclId = $acl->getAclId();
			break;
		}
		
		return $aclId;
	}

	/**
	 * @acces  private
	 * @return integer acl id or 0 if error
	 */
/*
	function _getContentParentAclid(&$content) {
		$site = &Site::getInstance();
		$dbManipulator = &$site->getDBManipulator();
		
		$contentId = $content->getContentId();
		$contentType = $content->getContentTypeName();
		
		$result = $dbManipulator->queryGetContainerOfContent($contentId, $contentType);
		
		if (!$result || !$dbManipulator->getNumRows())
			return 0;
			
		$row = $dbManipulator->fetchArray($result);
		
		$aclId = (int)$row['acl_id'];
		
		return $aclId;
	}
*/

	function &getContainersListParentObject(&$containersList) {
		$containersListId = $containersList->getContainersListId();
		$dbManipulator = &$this->site->getDBManipulator();

		$result = &$dbManipulator->getPageIdFromContainersListId($containersListId);
		if($row = $dbManipulator->fetchArray($result)) {
			$pageId = $row['id'];
			$pagesManager = &$this->site->getPagesManager();
			$page = &$pagesManager->getPage($pageId, false, false, false);
			return $page;
		}
		return null;
	}

	function &getContainerParentObject(&$container) {
		$containerId = $container->getContainerId();
		$dbManipulator = &$this->site->getDBManipulator();

		$result = &$dbManipulator->getContainersListIdFromContainerId($containerId);
		if($row = $dbManipulator->fetchArray($result)) {
			//there is a parent containersList
			$containersListId = $row['id'];
			$containersListsManager = &$this->site->getContainersListsManager();
			$containersList = &$containersListsManager->getContainersList($containersListId, false);
			return $containersList;
		}
		else {
			$result = &$dbManipulator->getPageIdFromContainerId($containerId);
			if($row = $dbManipulator->fetchArray($result)) {
				//there is a parent Page
				$pageId = $row['id'];
				$pagesManager = &$this->site->getPagesManager();
				$page = &$pagesManager->getPage($pageId, false, false, false);
				return $page;
			}
		}
		return null;
	}

	/**
	 * @acces  private
	 * @return integer acl id or 0 if error
	 */
/*
	function _getContainerParentAclId(&$container) {
		$site = &Site::getInstance();
		$dbManipulator = &$site->getDBManipulator();
		
		$containerId      = $container->getContainerId();
		$containerName    = $container->getContainerName();
		$containersListId = $container->getContainersListId();
		
		//echo '<pre>',var_dump($container->getContainersListId()),'</pre>';
		
		if (!$containersListId) {
			return Family::getPageAclId();
		}
		
		$result = $dbManipulator->queryGetContainersListOfContainer($containersListId, $containerId);
		
		if (!$result || !$dbManipulator->getNumRows()) {
			return 0;
		}
			
		$row = $dbManipulator->fetchArray($result);
		
		$aclId = (int )$row['acl_id'];
		
		return $aclId;
	}
*/

	/**
	 * @acces  private
	 * @return integer acl id or 0 if error
	 */
/*
	function _getContainersListParentAclId(&$containersList) {
		return Family::getPageAclId();
	}
*/

	/**
	 * @acces  private
	 * @return integer acl id or 0 if error
	 */
/*
	function _getMenuParentAclId(&$menu) {
		return Family::getPageAclId();
	}
*/
/*
	function getPageAclId() {
		$site = &Site::getInstance();
		$page = &$site->getCurrentPage();
		
		$pageAcl = &$page->getPageAcl();
		$aclId   = $pageAcl->getAclId();
		
		return $aclId;
	}
*/
}

?>