<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/**
 * @package    Core
 * @subpackage Pages
 */
 
/**
 */
include_once('config.inc.php');
include_once('classes/core/DBAccessorObject.class.php');
include_once('classes/core/DBManipulator.class.php');
include_once('classes/core/pages/Page.class.php');

/**
 * Manage the instances of Page objects.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version    PagesManager.class,v 0.1
 * @package	   Core
 * @subpackage Pages
 */
class PagesManager extends DBAccessorObject {
	/**#@+
	 * @access private
	 */
	/**
	 * @var object
	 */
	var $framesManager;
	
	/**
	 * @var object
	 */
	var $aclManager;
  
  /**
   * @var object
   */
  var $menusManager;
  
  /**
   * @var object
   */
  var $containersListsManager;
  
  /**
   * @var object
   */
  var $containersManager;
    
	/**
	 * @var	array
	 */		
	var $pagesTable;
	/**#@-*/

	/**#@+
	 * @access public
	 */
	/**
	 * Constructor.
	 * 
 	 * @param	object	$dbManipulator
	 * @param	integer $langId Language Id
	 */	
	function PagesManager(&$dbManipulator, &$framesManager, &$aclManager, &$menusManager, &$containersListsManager, &$containersManager) {
    $this->debug('PagesManager::PagesManager, status : function invoked', 10, DBG_OPEN);
		parent::DBAccessorObject($dbManipulator);
		$this->framesManager = &$framesManager;
		$this->aclManager = &$aclManager;
    $this->menusManager = &$menusManager;
    $this->containersListsManager = &$containersListsManager;
    $this->containersManager = &$containersManager;
		$this->pagesTable = array();
    $this->debug('PagesManager::PagesManager, status : function completed', 10, DBG_CLOSE);
	}

	function setMenusManager($menusManager) {
		$this->menusManager = &$menusManager;
	}

	/**
	 * Gets Page Table
	 *
	 * @return array
	 */
	function &getPagesTable() {
		return $this->pagesTable;
	}
	
	/**
	 * Sets Page Table
	 *
	 * @param array $table
	 */
	function setPagesTable(&$table) {
		$this->pagesTable = &$table;
	}
    
	/**
	 * Loads Page
	 *
	 * Loads Page into manager
	 *
	 * @access private
	 * @param  mixed $id Page Id or array of Page Id
	 * @param  boolean $loadDeleted
	 * @return boolean
	 */
	function loadPage($id, $loadDeleted, $loadFrame=true, $loadAcl=true, $langId=LOAD_ALL_LANGUAGES){
    $this->debug('PagesManager::loadPage, status : function invoked', 10, DBG_OPEN);
    $result = true;
		$db = &$this->getDBManipulator();
		$pagesIdList = (array)$id;
		$csv = implode(',', $pagesIdList);
		$res = $db->getRecord(PAGES_TABLE, -1, '`id` in ('.$csv.')');
		if(!$res || !$db->getNumRows()){
			$this->debug('PagesManager::loadPage, error : unable to load Page ('.$csv.')', 3);
			$result = false;
		}
		else {
  		while($record = $db->fetchArray($res)){
  			$pageId        = $record['id'];
  			$parentId      = $record['parent_id'];
  			$nameId        = $record['name_id'];
  			$aliasId       = $record['alias_id'];
  			$titleId       = $record['title_id'];
  			$descriptionId = $record['description_id'];
  			$keywordsId    = $record['keywords_id'];
  			$frameId       = $record['frame_id'];
  			$cache         = $record['cache'];
  			$deleted       = $record['deleted'];
  			$published     = $record['published'];
  			$link					 = $record['link'];
  			$aclId         = $record['acl_id'];

  			if((!$loadDeleted && !$deleted) || $loadDeleted){
  				$this->pagesTable[$pageId] = &new Page($pageId, $parentId, $nameId, $aliasId,	$titleId, $descriptionId, $keywordsId, $frameId, $cache, $deleted, $aclId);
  				$this->pagesTable[$pageId]->setPagePublished($published);
  				$this->pagesTable[$pageId]->setPageLink($link);

  				if($loadFrame){
  					$this->loadPageFrame($pageId);
  				}
  				if($loadAcl){
  					$this->loadPageAcl($pageId);
  				}
  				$this->loadPageTranslations($pageId, $langId);
  			}
  		}
    }
    $this->debug('PagesManager::loadPage, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
	
	/**
	 * Loads Page Frame
	 *
	 * @param  integer $pageId Page Id
	 * @return boolean
	 */
	function loadPageFrame($pageId){
    $this->debug('PagesManager::loadPageFrame, status : function invoked', 10, DBG_OPEN);
		$page = &$this->getPage($pageId);
		$frameId = $page->getPageFrameId();
		$frame = &$this->framesManager->getFrame($frameId);
		$page->setPageFrame($frame);
    $this->debug('PagesManager::loadPageFrame, status : function completed', 10, DBG_CLOSE);
		return true;
	}
	
	/**
	 * Loads Page ACL
	 *
	 * @param  integer $pageId Page Id
	 * @return boolean
	 */
	function loadPageAcl($pageId){
    $this->debug('PagesManager::loadPageAcl, status : function invoked', 10, DBG_OPEN);
		$page = &$this->getPage($pageId);
		$aclId = $page->getPageAclId();
		$acl = &$this->aclManager->getACL($aclId);
		$page->setPageAcl($acl);
    $this->debug('PagesManager::loadPageAcl, status : function completed', 10, DBG_CLOSE);
		return true;
	}

	/**
	 * Loads Page Translations
	 *
	 * @todo approve
	 */
	function loadPageTranslations($pageId, $langId=LOAD_ALL_LANGUAGES) {
    $this->debug('PagesManager::loadPageTranslations, status : function invoked', 10, DBG_OPEN);
    $result = true;
		if (!$pageId || !($page = &$this->getPage($pageId))) {
      $this->debug('PagesManager::loadPageTranslations, status : unable to get page ('.$pageId.')', 3);
      $result = false;
		}
		else {
  		$dbManipulator = &$this->getDBManipulator();

  		$load = array();
  		$load['name'] = $page->getPageNameId();
  		$load['alias'] = $page->getPageAliasId();
  		$load['title'] = $page->getPageTitleId();
  		$load['description'] = $page->getPageDescriptionId();
  		$load['keywords'] = $page->getPageKeywordsId();

  		$cond = '`id` in ('.implode(',', $load).')';

  		if ($langId == LOAD_ALL_LANGUAGES) {
  			$res = $dbManipulator->getRecord(NAMES_TABLE, 0, $cond);
  		}
  		else {
  			$res = $dbManipulator->getRecord(NAMES_TABLE, $langId, $cond, 'lang_id');
  		}

  		if (!$res || !$dbManipulator->getNumRows()) {
  			return;
  		}

  		while ($record = $dbManipulator->fetchArray($res)) {
  			switch ($record['id']) {
  				case $load['name']:
  					$page->setPageName($record['value'], $record['lang_id']);
  					break;
  				case $load['alias']:
  					$page->setPageAlias($record['value'], $record['lang_id']);
  					break;
  				case $load['title']:
  					$page->setPageTitle($record['value'], $record['lang_id']);
  					break;
  				case $load['description']:
  					$page->setPageDescription($record['value'], $record['lang_id']);
  					break;
  				case $load['keywords']:
  					$page->setPageKeywords($record['value'], $record['lang_id']);
  					break;
  			}
  		}
    }
    $this->debug('PagesManager::loadPageTranslations, status : function completed', 10, DBG_CLOSE);
    return $result;
	}

	/**
	 * Stores Page Translation
	 *
	 * @param  integer $pageId Page Id
	 * @param  integer $langId Language Id
	 * @return boolean
	 */
	function storePageTranslation($pageId, $langId) {
    $this->debug('PagesManager::storePageTranslation, status : function invoked', 10, DBG_OPEN);
// todo : store only for the specified langId
		$db = $this->getDBManipulator();
		$page = &$this->getPage($pageId);
		
		if (!$page) {
			return false;
		}
		if ($pageId == 0) {
		// create new label id's for the page fields
			$labels = array(
				'name'        => '',
				'alias'       => '',
				'title'       => '',
				'description' => '',
				'keywords'    => ''
			);
			$pageValues = array();
			foreach ($labels as $key => $value) {
				$record = array();
				$record['lang_id'] = '1';
				$record['value'] = '';
				$res = $db->addRecord(NAMES_TABLE, $record);
				if ($res && $db->getLastId()) {
					$pageValues[$key.'_id'] = $db->getLastId();
				}
			}
			$page->setPageValues($pageValues);
		}
		$labels = array(
			 'name'        => $page->getPageNameId()
			,'alias'       => $page->getPageAliasId()
			,'title'       => $page->getPageTitleId()
			,'description' => $page->getPageDescriptionId()
			,'keywords'    => $page->getPageKeywordsId()
		);
		$pageValues = array();
		$valuesTableLang = &$page->getPageValuesTable();
  	foreach($valuesTableLang as $langId => $valuesTable) {
			if(!is_int($langId)) {
				continue;
			}
			foreach ($labels as $key => $labelId) {
				$record = array();
				$record['value'] = $this->html_encode($valuesTable[$key]);
				if ($labelId == 0) {
					// create new record
					$record['lang_id'] = $langId;
					$res = $db->addRecord(NAMES_TABLE, $record);
					if ($res && $db->getLastId()) {
						$pageValues[$key.'_id'] = $db->getLastId();
					}
				}
				else {
					// update record or create new record
					$res = $db->getRecord(NAMES_TABLE, $labelId, '`lang_id`='.$langId);
					if ($res && $db->getNumRows()) {
						$res = $db->setRecord(NAMES_TABLE, $labelId, $record, '`lang_id`='.$langId);
					}
					elseif ($res && !$db->getNumRows()) {
						$record['id'] = $labelId;
						$record['lang_id'] = $langId;
						$res = $db->addRecord(NAMES_TABLE, $record);
					}
				}
			}
		}
		$page->setPageValues($pageValues);
    $this->debug('PagesManager::storePageTranslation, status : function completed', 10, DBG_CLOSE);
		return true;
	}

	/**
	 * Checks if a page-id is parent referenced by a subNode
	 *
	 * @param  integer $parentId Page ID for Comparison
	 * @param  integer $childId Page ID for Comparison
	 * @return integer
	 */
	function isSubPage($parentId, $childId){
    $this->debug('PagesManager::isSubPage, status : function invoked', 10, DBG_OPEN);
    $result = false;
		$subPageIdList = $this->getChildPagesIdList($parentId);
		foreach($subPageIdList as $subPageId) {
			if($subPageId == $childId) {
				$result = true;
				break;
			}
			else {
  			if(($result = $this->isSubPage($subPageId, $childId)) === true) {
          break;
  			}
      }
		}
    $this->debug('PagesManager::isSubPage, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
	
	/**
	 * Queries the total of Child Pages
	 */
	function countSubPages($pageId) {
		$result = $this->dbManipulator->sendQuery("SELECT count(*) as `count` FROM `".PAGES_TABLE."` WHERE `parent_id` = '$pageId' and `deleted` = '0';");
		if(!($row = $this->dbManipulator->fetchArray($result))){
			return 0;
		}
		return $row['count'];
	}

	/**
	 * Loads Child Pages
	 *
	 * @param  mixed   $pageId
	 * @param  boolean $loadDeleted
	 * @param  boolean $loadFrame
	 * @param  boolean $loadAcl
	 * @param  integer $langId
	 * @param  boolean $loadChild
	 * @return boolean
	 */
	function loadChildPages($pageId, $loadDeleted=false, $loadFrame=true, $loadAcl=true, $langId=LOAD_ALL_LANGUAGES, $loadChild=false) {
    $this->debug('PagesManager::loadChildPages, status : function invoked', 10, DBG_OPEN);
		$rootPagesIdList = (array)$pageId;
		$db = &$this->getDBManipulator();
		$cond = '`parent_id` in ('.implode(',', $rootPagesIdList).')';
		$res = $db->getRecord(PAGES_TABLE, -1, $cond);
		if(!$res || !$db->getNumRows()){
			return false;
		}
		$childPagesIdList = array();
		while($record = $db->fetchArray($res)){
			$childPageId = (int)$record['id'];
			if(!$this->pageExists($childPageId)){
				$childPagesIdList[] = $childPageId;
			}
		}
		$retVal = true;
		if(count($childPagesIdList)>0){
			$retVal = $this->loadPage($childPagesIdList, $loadDeleted, $loadFrame, $loadAcl, $langId, $loadChild);
		}
    $this->debug('PagesManager::loadChildPages, status : function completed', 10, DBG_CLOSE);
		return $retVal;
	}
	
	function getChildPagesIdList($rootPageId){
    $this->debug('PagesManager::getChildPagesIdList, status : function invoked', 10, DBG_OPEN);
		$db = &$this->getDBManipulator();
		$res = $db->getRecord(PAGES_TABLE, -1, '`parent_id`='.$rootPageId);
		$childPagesIdList = array();
		if($res && $db->getNumRows()){
			while($record = $db->fetchArray($res)){
				$childPagesIdList[] = (int)$record['id'];
			}
		}
    $this->debug('PagesManager::getChildPagesIdList, status : function completed', 10, DBG_CLOSE);
		return $childPagesIdList;
	}

	/**
	 * Stores Page
	 *
	 * @param  integer $pageId
	 * @return integer Page Id or 0 if error
	 */
	function storePage($pageId, $langId=LOAD_ALL_LANGUAGES) {
    $this->debug('PagesManager::storePage, status : function invoked ('.$pageId.')', 10, DBG_OPEN);
		$db = &$this->getDBManipulator();

		$this->storePageTranslation($pageId, $langId);
		
		$page = &$this->getPage($pageId);

		$pageFieldsArray = array(
			'name_id'        => $page->getPageNameId(),
			'parent_id'      => $page->getPageParentId(),
			'alias_id'       => $page->getPageAliasId(),
			'title_id'       => $page->getPageTitleId(),
			'description_id' => $page->getPageDescriptionId(),
			'keywords_id'    => $page->getPageKeywordsId(),
			'parent_id'      => $page->getPageParentId(),
			'frame_id'       => $page->getPageFrameId(),
			'acl_id'         => $page->getPageAclId(),
			'cache'          => $page->getPageCache(),
			'deleted'        => $page->getPageDeleted(),
			'published'      => $page->getPagePublished(),
			'link'			     => $page->getPageLink()
		);
		
		// new page : id 0 is not allowed in the DB
		if ($pageId <= 0) {
			if (!$db->addRecord(PAGES_TABLE, $pageFieldsArray)) {
				$this->debug('PagesManager::storePage, error : unable to create new page', 1);
				return 0;			
			}
			$newId = $db->getLastId();
			$page->setPageId($newId);
			$this->setPage($page);
			$this->debug('PagesManager::storePage, success: new page created successfully', 9);
			return $newId;
		}
		else{		
			if (!$db->setRecord(PAGES_TABLE, $pageId, $pageFieldsArray)){
				$this->debug('PagesManager::storePage, error : unable to update data for page #'.$pageId, 1);
				return 0;
			}
		}
    $this->debug('PagesManager::storePage, status : function completed', 10, DBG_CLOSE);
		return $pageId;	
	}

	/**
	 * Page Objects Count
	 *
	 * Returns number of Page objects currently defined into manager
	 *
	 * @return integer
	 */
	function getPagesCount() {
		if (isset($this->pagesTable))
			return count($this->pagesTable);
		return 0;
	}

	/**
	 * Gets Page
	 *
	 * If not already defined into manager, tries to load it
	 *
	 * @param  integer $pageId Page Id
	 * @param  boolean $loadDeleted
	 * @return mixed
	 */
	function &getPage($pageId, $loadDeleted=false, $loadFrame=true, $loadAcl=true, $langId=LOAD_ALL_LANGUAGES) {
    $this->debug('PagesManager::getPage, status : function invoked ('.$pageId.')', 10, DBG_OPEN);
    $result = null;
  	if (!$this->checkPage($pageId)) {
			if (!$this->loadPage($pageId, $loadDeleted, $loadFrame, $loadAcl, $langId)) {
				$this->debug('PagesManager::getPage, error : unable to get page #'.$pageId, 2);
			}
			else {
		    $result = &$this->pagesTable[$pageId];
			}
		}
		else {
		  $result = &$this->pagesTable[$pageId];
		}
    $this->debug('PagesManager::getPage, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
	
	/**
	 * Gets Root Pages Id
	 *
	 * @return array
	 */
	function getRootPagesId(){
		$db = &$this->getDBManipulator();
		$res = $db->getRecord(PAGES_TABLE, -1, '`parent_id`=0');
		$array = array();
		if ($res && $db->getNumRows()){
			while($record = $db->fetchArray($res)){
				$array[] = (int)$record['id'];
			}
		}
		return $array;
	}
	
	/**
	 * Gets Child Pages Id
	 *
	 * @param  integer $rootPageId
	 * @return array
	 */
	function getChildPagesId($rootPageId){
		$db = &$this->getDBManipulator();
		$res = $db->getRecord(PAGES_TABLE, $rootPageId, '', 'parent_id');
		$array = array();
		if($res && $db->getNumRows()){
			while($record = $db->fetchArray($res)){
				$array[] = (int)$record['id'];
			}
		}
		return $array;
	}
	
	/**
	 * Sets Page Into Manager
	 *
	 * @param  object $page
	 * @return boolean
	 */
	function setPage(&$page) {
		$pageId = $page->getPageId();
		$this->pagesTable[$pageId] = &$page;
	}
    
	/**
	 * Removes Page
	 *
	 * Sets Page for deletion
	 *
	 * @param  integer $id Page Id
	 * @return boolean
	 */
	function removePage($id) {
		if (!($page = &$this->getPage($id))) {
			$this->debug("PagesManager.removePage, error : trying to remove unexisting page '$id'.", 2);
			return false;
		}
		if($id == 1) {
			$this->debug("PagesManager.removePage, error : trying to remove root page.", 2);
			return false;
		}
		$page->setPageDeleted(1);
		if (!$this->storePage($id)) {
			return false;
    }
		return true;
	}
    
	/**
	 * Force re-generation of Page
	 *
	 * Sets Page's cache for renewal
	 *
	 * @param  integer $id Page Id
	 * @return boolean
	 */
	function setPageForCacheRenewalAndStore($id) {
		if (!($page = &$this->getPage($id))) {
			$this->debug("PagesManager.setPageForCacheRenewalAndStore, error : trying to get unexisting page '$id'.", 2);
			return false;
		}
		$db = &$this->getDBManipulator();
		$pageFieldsArray = array('cache'=>0);
		if (!$db->setRecord(PAGES_TABLE, $id, $pageFieldsArray)){
			$this->debug('PagesManager::setPageForCacheRenewalAndStore, error : unable to update data for page #'.$id, 2);
			return false;
		}
		return true;
	}

	/**
	 * Set Page as cached
	 *
	 * Sets Page's cache bit to 1
	 *
	 * @param  integer $id Page Id
	 * @return boolean
	 */
	function setPageCachedAndStore($id) {
		if (!($page = &$this->getPage($id))) {
			$this->debug("PagesManager.setPageCachedAndStore, error : trying to get unexisting page '$id'.", 2);
			return false;
		}
		$db = &$this->getDBManipulator();
		$pageFieldsArray = array('cache'=>1);
		if (!$db->setRecord(PAGES_TABLE, $id, $pageFieldsArray)){
			$this->debug('PagesManager::setPageCachedAndStore, error : unable to update data for page #'.$id, 2);
			return false;
		}
		return true;
	}
	
	/**
	 * Page Exists
	 *
	 * Returns true if Page object of given id is defined into manager
	 *
	 * @param  integer $pageId Page Id
	 * @return boolean
	 */
	function pageExists($pageId) {
		$this->getPage($pageId);
		return $this->checkPage($pageId);
	}
	
	/**
	 * @todo check deprecation
	 */
	function toString() {
    $str = "pages loaded :\n";
    foreach ($this->getPagesTable() as $key => $value) {
        $str .= "   $key\n";
    } 
    return $str;
	}
	
	/**
	 * Gets New Page
	 *
	 * @param integer $aclId
	 * @param integer $parentId
	 */
	function &getNewPage($aclId=0, $parentId=0) {
		$this->pagesTable[0] = &new Page();
		$this->pagesTable[0]->setPageAclId($aclId);
		$this->pagesTable[0]->setPageParentId($parentId);
    $this->pagesTable[0]->setPagePublished(PUBLICATION_DEFAULT_VALUE);
		return $this->pagesTable[0];
	}

	/**
	 * Gets Frames
	 *
	 * Returns associative array of available frames
	 *
	 * @return array
	 * @todo discussion about the right place for this function (managers interface ? / dbManipulator ?)
	 */
	function getFramesArray() {
		$dbManipulator = &$this->getDBManipulator();
		$result        = $dbManipulator->getRecord(FRAMES_TABLE, -1, '', 'id', 'name');
		if (!$result) {
			return null;
		}
		$framesArray = array();
		while ($row = $dbManipulator->fetchArray($result)) {
			$frameId = $row['id'];
			$framesArray[$frameId] = $row['name'];
		}
		return $framesArray;
	}

	/**
	 * Gets Languages
	 *
	 * Returns associative array of available languages
	 *
	 * @return array
	 * @todo discussion about the right place for this function (managers interface ? / dbManipulator ?)
	 */
	function getLangsArray() {
		$dbManipulator = &$this->getDBManipulator();
		$result        = $dbManipulator->getRecord(LANGUAGES_TABLE, -1, '', 'id', 'name');
		if (!$result) {
			return null;
		}
		$langsArray = array();
		while ($row = $dbManipulator->fetchArray($result)) {
			$langId = $row['id'];
			$langsArray[$langId] = $row['name'];
		}
		return $langsArray;
	}

	/**
	 * Gets Parent Object
	 *
	 * Returns parent Page object if any
	 *
	 * @param  integer $pageId Page Id
	 * @return object null if error
	 */
	function getParentObject($pageId, $quickLoad=false) {
		$page = &$this->getPage($pageId);		
		if (!$page || !$page->getPageParentId()) {
			return null;
		}
		$parentPage = &$this->getPage($page->getPageParentId());
		return $parentPage;
	}
  
  /**
   * Updates Page ACL
   *
   * @param  integer $id
   * @param  integer $aclId
   * @return boolean
   */
  function updatePageAcl($pageId, $aclId){
    $this->debug('PagesManager::updatePageAcl, status : function invoked ('.$pageId.', '.$aclId.')', 10, DBG_OPEN);
		$page = &$this->getPage($pageId);
		$oldAclId = $page->getPageAclId();
    $result = true;
    $db = &$this->getDBManipulator();
    $fields = array('acl_id' => $aclId);

    if(!$db->setRecord(PAGES_TABLE, -1, $fields, "`id`= '$pageId'")){
			$this->debug("PagesManager::updatePageAcl : error, unable to set new acl_id for Page (id=$pageId).", 3);
			$result = false;
		}
    else {
  		// pages
  		$subPagesList = &$this->getSubPagesList($pageId);
  		foreach($subPagesList as $subPageId) {
  			$this->updatePageAcl($subPageId, $aclId);
  		}

  		// menus
  		if(!$res = $db->getPageMenus($page->getPageFrameId())){
  			$this->debug("PagesManager::updatePageAcl : error, unable to get menus for Page (id=$pageId).", 3);
        $result = false;
  		}
  		else {
  			while($record = $db->fetchArray($res)){
  				$menuId = $record['id'];
  				$menu = &$this->menusManager->getMenu($menuId);
  				if($menu->getMenuAclId() == $oldAclId) {
  					$this->menusManager->updateMenuACL($menuId, $aclId);
  				}
  			}
  		}

  		// containers
  		if(!$res = $db->getPageContainers($pageId, $page->getPageFrameId(), true)){
  			$this->debug("PagesManager::updatePageAcl : error, unable to get containers for Page (id=$pageId).", 3);
        $result = false;
  		}
  		else {
  			while($record = $db->fetchArray($res)){
  				$containerId = $record['id'];
  				$container = &$this->containersManager->getContainer($containerId);
  				if($container->getContainerAclId() == $oldAclId) {
  					$this->containersManager->updateContainerACL($containerId, $aclId);
  				}
  			}
  		}

  		// containersLists
  		if(!$res = $db->getPageContainersLists($pageId, $page->getPageFrameId(), true)){
  			$this->debug("PagesManager::updatePageAcl : error, unable to get containersLists for Page (id=$pageId).", 3);
        $result = false;
  		}
  		else {
  			while($record = $db->fetchArray($res)){
  				$containersListId = $record['id'];
  				$containersList = &$this->containersListsManager->getContainersList($containersListId);
  				if($containersList->getContainersListAclId() == $oldAclId) {
  					$this->containersListsManager->updateContainersListACL($containersListId, $aclId);
  				}
  			}
  		}
    }
    $this->debug('PagesManager::updatePageAcl, status : function completed', 10, DBG_CLOSE);
		return $result;
  }
	

	
	/*
	 *
	 * DEPRECATED CODE BELOW
	 *
	 */
	
	/**
	 * @deprecated
	 * @todo approve and clean
	 */
	function &getAclManager() {
		return $this->aclManager;
	}
	


	/**
	 * @deprecated use {@link storePageTranslation}
	 * @todo approve and clean
	 */
	function storeLanguageDependencies($pageId, $langId) {
		$this->debug('PagesManager::storeLanguageDependencies, warning : use of deprecated function', 3);
		return true;
	}
	
	/**
	 * @deprecated use {@link getParentObject}
	 * @todo approve and clean
	 */
	function getParentObjectValuesArray($pageId) {
		$page = &$this->getPage($pageId);
		
		if (is_null($page)) {
			return null;
		}
		
		$result = $this->dbManipulator->queryGetPageOfPage($pageId);
		
		if (!$result) {
			$this->debug("Page.getParentObjectValuesArray, DB error : ".$this->dbManipulator->getErrMsg());
			return null;
		}
		
		if (!$this->dbManipulator->getNumRows()) {
			$row = array('acl_id' => -1);
		} else {
			$row = $this->dbManipulator->fetchArray($result);
		}
		
		return $row;
	}
	
	/**
	 * Gets Sub Pages List
	 *
	 * @return array
	 */
	function &getSubPagesList($pageId) {
		$list = array();
		if (($result = &$this->dbManipulator->getRecord(PAGES_TABLE, "", "parent_id =".$pageId." and `deleted` = '0'", "")) < 0 ){
			$this->debug("Page.getSubPagesList, error : unable to get subPages list for page '".$this->getPageId()."' (load skipped).", 2);
			return -1;
		}
		else {
			while($row = &$this->dbManipulator->fetchArray($result)){
				$list[] = $row['id'];
			}
		}
		$this->debug("Page.getSubPagesList, status : success.", 10);
		return $list;
	}

	/**
	 * Returns a reference to a page object if the alias matches an existing page.
	 * If it is the first access to the page, the manager loads it.
	 * 
	 * @access   public
 	 * @param    integer  The page alias of the page we want a reference to.
 	 * @return   object
	 * @deprecated
	 * @todo approve and clean
	 */
	function &getPageByAlias($alias, $langId) {
		if (($pageId = &$this->dbManipulator->getPageByAlias($alias, $langId)) > 0){
			return $this->getPage($pageId);
		}
		return null;
	}
	
	/**
	 * Checks if the page is already in the table. In this case, it returns true, otherwise it returns false.
	 * 
	 * @access   public
 	 * @param    integer  The page id (pid) of the page we want to check.
 	 * @return   bool
	 */
    function checkPage($pageId) {
        return isset($this->pagesTable[$pageId]);
    }
}
?>