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

include_once('config.inc.php');
include_once('classes/core/DBAccessorObject.class.php');
include_once('classes/core/templates/Template.class.php');

/**
 * TemplatesManager Class
 *
 * Manage instances of Template objects.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version    TemplatesManager.class,v 0.1 
 * @package	   Core
 * @subpackage Templates
 */
class TemplatesManager extends DBAccessorObject {
	/**
	 * @var array
	 */
    var $templatesTable;

	/**
	 * Constructor
	 *
	 * @param object $dbManipulator
	 */
	function TemplatesManager(&$dbManipulator) {
    $this->debug('TemplatesManager::TemplatesManager, status : function invoked', 10, DBG_OPEN);
		parent::DBAccessorObject($dbManipulator);
    $this->debug('TemplatesManager::TemplatesManager, status : function completed', 10, DBG_CLOSE);
	}
	
	/**
	 * Template Exists
	 *
	 * Checks if Template of given type and id is defined in database
	 *
	 * @param  string  $typeName Template Type
	 * @param  integer $id Template Id
	 * @return boolean
	 */
	function templateExists($id){
		// try to load the container
		$this->getTemplate($id);
		// returns true if the template exists
		return $this->checkTemplate($id);
	}

	/**
	 * Tells if a template has already been loaded
	 * @param  string $typeName Template Type
	 * @param  integer $id Template Id
	 * @return bool
	 */
	function checkTemplate($id){
  	return isset($this->templatesTable[$id]);
  }

	/**
	 * Loads Template
	 *
	 * @param  string $typeName Template Type
	 * @param  integer $templateId Template Id
	 * @return boolean
	 */
  function loadTemplate($templateId) {
    $this->debug('TemplatesManager::loadTemplate, status : function invoked ('.$templateId.')', 10, DBG_OPEN);
    $result = true;
		$templatesIdList = (array)$templateId;
		$csv = implode(',', $templatesIdList);
		$db = &$this->getDBManipulator();
		$where = '`id` in ('.$csv.')';
		$res = $db->getRecord(TEMPLATES_TABLE, -1, $where);
		if (!$res || !$db->getNumRows()) {
			$this->debug('TemplatesManager::loadTemplate, error : unable to load Template('.$csv.')', 3);
			$result = false;
		}
		else {
  		while ($record = $db->fetchArray($res)) {
  			$id   = (int)$record['id'];
  			$typeName = $record['type'];
  			$name = $record['name'];
  			$xsl  = $record['xsl'];
  			$this->templatesTable[$id] = &new Template($id, $typeName, $name, $xsl);
  		}
    }
    $this->debug('TemplatesManager::loadTemplate, status : function completed', 10, DBG_CLOSE);
		return $result;
  } 

	/**
	 * Stores Template
	 *
	 * @param  integer $templateId Template Id
	 * @return integer Template Id or 0 if error
	 */
  function storeTemplate($templateId){
    $this->debug('TemplatesManager::storeTemplate, status : function invoked ('.$templateId.')', 10, DBG_OPEN);

		if (is_null($template = &$this->getTemplate($templateId))) {
      trigger_error('TemplatesManager::storeTemplate, error : unable to get template ('.$templateId.')', E_USER_ERROR);
			$templateId = -1;
		}
		else {
  		$fieldsArray = array(
  			 'type'  => $template->getTemplateTypeName()
  			,'name'  => $template->getTemplateName()
  			,'xsl'   => $template->getTemplateXSL()
  		);

  		if ($templateId == 0) {
  			$res = $this->dbManipulator->addRecord(TEMPLATES_TABLE, $fieldsArray);
  			if (!$res || !$this->dbManipulator->getAffectedRows()) {
          $this->debug('TemplatesManager::storeTemplate, error : unable to create a new template', 3);
  			}
  			else {
    			$newId = $this->dbManipulator->getLastId();
          $template->setTemplateId($newId);
    			$this->setTemplate($template);
    			unset($this->templatesTable[0]);
    			$this->templatesTable[0] = null;
          $templateId = $newId;
        }
  		}
      else {
  			if (!$this->dbManipulator->setRecord(TEMPLATES_TABLE, $templateId, $fieldsArray)) {
          $this->debug('TemplatesManager::storeTemplate, error : unable to update template ('.$templateId.')', 3);
  			}
  		}
    }
    $this->debug('TemplatesManager::storeTemplate, status : function completed', 10, DBG_CLOSE);
		return $templateId;
  } 
	
	/**
	 * Removes Template
	 *
	 * @param  integer $templateId
	 * @return boolean
	 */
  function removeTemplate($templateId){
    $this->debug('TemplatesManager::removeTemplate, status : function invoked ('.$templateId.')', 10, DBG_OPEN);
    $result = true;
		$db = &$this->getDBManipulator();
		$record = array();
		$record['deleted'] = 1;
		if (!$db->setRecord(TEMPLATES_TABLE, $templateId, $record)) {
			$result = false;
		}
    $this->debug('TemplatesManager::removeTemplate, status : function completed', 10, DBG_CLOSE);
		return $result;
  } 
	
  /**
	 * Gets Template
	 *
	 * @param  string $typeName Template Type
	 * @param  integer $id Template Id
	 * @return object
	 */
  function &getTemplate($id) {
    $this->debug('TemplatesManager::getTemplate, status : function invoked ('.$id.')', 10, DBG_OPEN);
    if (!$this->checkTemplate($id)) {
			if (!$this->loadTemplate($id)) {
				$this->debug('TemplatesManager::getTemplate, error : unable to get Template('.$id.')', 3);
    	  return null;
			}
		}
    $this->debug('TemplatesManager::getTemplate, status : function completed', 10, DBG_CLOSE);
    return $this->templatesTable[$id];
  } 

	/**
	 * Gets Template Id
	 *
	 * @param  string $templateName Template Name
	 * @param  string $typeName Type Name
	 * @return integer
	 */
  function getTemplateId($templateName, $typeName){
    $this->debug('TemplatesManager::getTemplateId, status : function invoked ('.$typeName.', '.$typeName.')', 10, DBG_OPEN);
		$db = &$this->getDBManipulator();
    $templateId = $db->getTemplateId($templateName, $typeName);
    $this->debug('TemplatesManager::getTemplateId, status : function completed', 10, DBG_CLOSE);
		return $templateId;
  }

	/**
	 * Sets Template into manager
	 *
	 * @param  object $template
	 * @return boolean
	 */
	function setTemplate(&$template) {
    $this->debug('TemplatesManager::setTemplate, status : function invoked', 10, DBG_OPEN);
    $result = true;
		$template_id = $template->getTemplateId();
		if($template_id >= 0) {
  		$this->templatesTable[$template_id] = &$template;
    }
    else {
      trigger_error('TemplatesManager::setTemplate, error : template id < 0', E_USER_ERROR);
      $result = false;
    }
    $this->debug('TemplatesManager::setTemplate, status : function completed', 10, DBG_CLOSE);
		return $result;
	}
}
?>