<?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 Frames
 */

/**
 * Frames Manager Class
 *
 * Manages Frame objects
 *
 * @author     vinadelmar <vinadelmar@users.sourceforge.net>
 * @since      2006-08-01
 * @package    Core
 * @subpackage Frames
 */
class FramesManager extends DBAccessorObject {
	/**
	 *
	 * @var    array Frame objects array
	 */
	var $framesTable;
	
	function FramesManager(&$dbManipulator) {
		parent::DBAccessorObject($dbManipulator);
		$this->framesTable = array();
	}
	
	/**
	 * Frame Exists
	 *
	 * Returns true if Frame of given id is defined into manager
	 *
	 * @param  integer $id Frame Id
	 * @return boolean
	 */
	function frameExists($id) {
		return isset($this->framesTable[$id]);
	}
	
	function &fetchObject($record) {
		$id       = (int)$record['id'];
		$name     = stripslashes($record['name']);
		$fileName = $record['file'];
		$frame = &new Frame($id, $name, $fileName);
		return $frame;
	}
	
	/**
	 * Loads Frame
	 *
	 * @param  integer $id Frame Id
	 * @return boolean
	 */
	function loadFrame($id, $loadStyle=true, $loadScript=true) {
		/*
		$framesIdList = (array)$id;
		$db = &$this->getDBManipulator();
		$where = '`id` in ('.implode(',', $framesIdList).')';
		$res = $db->getRecord(FRAMES_TABLE, -1, $where);
		if (!$res || !$db->getNumRows()) {
			return false;
		}
		while ($record = $db->fetchArray($res)) {
			$id   = (int)$record['id'];
			$name = $record['name'];
			$file = $record['file'];
			$frame = &new Frame($id, $name, $file);
			$this->addFrame($frame);
		}
		if ($loadStyle) {
			$this->loadFrameStyles($framesIdList);
		}
		if ($loadScript) {
			$this->loadFrameScripts($framesIdList);
		}
		return true;
		*/
		$db = &$this->getDBManipulator();
		
		$framesList = (array)$id;
		
		/*
		foreach ((array)$id as $frameId) {
			if (!$this->frameExists($frameId)) {
				$framesList[] = $frameId;
			}
		}
		
		if (!count($framesList)) {
			$this->debug('FramesManager::loadFrame, warning : '
				. 'Frame(id='.implode(',', (array)$id).') already loaded', 8);
			return true;
		}
		*/
		
		$csv = implode(',', $framesList);
		
		$res = $db->getRecord(FRAMES_TABLE, -1, '`id` in ('.$csv.')');
		
		if (!$res || !$db->getNumRows()) {
			$this->debug('FramesManager::loadFrame, error unable to load Frame(id='.$csv.')', 3);
			return false;
		}
		
		while ($record = $db->fetchArray($res)) {
			$id = (int)$record['id'];
			$name = $record['name'];
			$file = $record['file'];
			$this->framesTable[$id] = &new Frame($id, $name, $file);
			/*
			$frame = &$this->fetchObject($record);
			$frame->loadFrameXHTML();
			$this->setFrame($frame);
			*/
		}
		
		if ($loadStyle) {
			$this->loadFrameStyles($framesList);
		}
		
		if ($loadScript) {
			$this->loadFrameScripts($framesList);
		}
		
		$this->debug('FramesManager::loadFrame, success : Frame(id='.$csv.') loaded', 10);
		
		return true;
	}
	
	/**
	 * Loads Frame Styles
	 *
	 * @param  mixed $frameId
	 * @return boolean
	 */
	function loadFrameStyles($frameId){
		$db = &$this->getDBManipulator();
		$res = $db->getFrameStyles($frameId);
		if(!$res || !$db->getNumRows()){
			return false;
		}
		while($record = $db->fetchArray($res)){
			$id = (int)$record['frame_id'];
			$styleId = (int)$record['id'];
			$styleFile = $record['file'];
			$this->framesTable[$id]->addFrameStyle($styleId, $styleFile);
		}
		return true;
	}
	
	/**
	 * Loads Frame Scripts
	 *
	 * @param  mixed $frameId
	 * @return boolean
	 */
	function loadFrameScripts($frameId){
		$db = &$this->getDBManipulator();
		$res = $db->getFrameScripts($frameId);
		if(!$res || !$db->getNumRows()){
			return false;
		}
		while($record = $db->fetchArray($res)){
			$id = (int)$record['frame_id'];
			$scriptId = (int)$record['id'];
			$scriptFile = $record['file'];
			$this->framesTable[$id]->addFrameScript($scriptId, $scriptFile);
		}
		return true;
	}
	
	/**
	 * Gets Frame
	 *
	 * @param  integer $id
	 * @param  boolean $loadStyle Loads associated styles ?
	 * @param  boolean $loadScript Loads associated scripts ?
	 * @return object
	 */
	function &getFrame($id, $loadStyle=true, $loadScript=true) {
		if (!$this->frameExists($id)) {
			if (!$this->loadFrame($id, $loadStyle, $loadScript)) {
				$this->debug('FramesManager::getFrame, error : Frame(id='.$id.') unknown', 3);
				return null;
			}
		}
		$this->debug('FramesManager::getFrame, success : got Frame(id='.$id.')', 9);
		return $this->framesTable[$id];
	}
	
	/**
	 * Sets Frame
	 *
	 * Sets or updates Frame object in manager
	 *
	 * @param  object $frame
	 * @return boolean
	 */
	function setFrame(&$frame) {
		if (!is_object($frame)) {
			$this->debug('FramesManager::setFrame, error : '
				. 'Frame object expected', 4);
			return false;
		}
		$id = $frame->getFrameId();
		$this->framesTable[$id] = &$frame;
		return true;
	}
	
	/**
	 * Stores Frame
	 *
	 * @param  integer $frameId
	 * @return integer Frame Id or 0 if error
	 */
	function storeFrame($frameId) {
		if (!$this->frameExists($frameId)) {
			return 0;
		}
		$frame = &$this->getFrame($frameId);
		$record = array();
		$record['name'] = $frame->getFrameName();
		$record['file'] = $frame->getFrameFileName();
		$db = &$this->getDBManipulator();
		if (!$frameId) {
			$res = $db->addRecord(FRAMES_TABLE, $record);
			if (!$res || !$db->getAffectedRows()) {
				return 0;
			}
			$newId = $db->getLastId();
			$frame->setFrameId($newId);
			$frameId = $newId;
		} else {
			if (!$db->setRecord(FRAMES_TABLE, $frameId, $record)) {
				return 0;
			}
		}
		return $frameId;
	}
}
?>