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

/**
 * Mother Class
 */
include_once('classes/core/parser/tags/Tag.class.php');

/**
 * Site Class
 */
include_once('classes/core/Site.class.php');

/**
 * Tag element.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   ContainerModeTag.class, Sun Dec 26 18:03:59 CET 2004
 * @package	   Core
 * @subpackage Parser
 */
class ContainerTag extends Tag {

	/**
	 * Constructor
	 *
	 * @access   public
 	 * @param    string  The complete tag syntax (i.e. : <tagname attr1="value1" attr2="value2">, <br />, </a>, ...)
 	 * @param    integer The position of the tag in the xml document
 	 * @return   void
	 */
	function ContainerTag($tagString, $name, $type, $offset, $length) {
		parent::Tag($tagString, $name, $type, $offset, $length);
	}

	/**
	 * Performs actions specific to the tag
	 *
 	 * @return string Returns the HTML part corresponding to the container.
	 */
	function evaluate() {
    $this->debug('ContainerTag::evaluate, status : function invoked', 10, DBG_OPEN);
    $html = '';
		$pageId = $this->page->getPageId();
		$frameId = $this->page->getPageFrameId();

		$containersManager = &$this->site->getContainersManager();
		$dbManipulator = &$this->site->getDBManipulator();

		$drawBorder         = $this->getAttribute('border', 1);
		$isStatic           = $this->getAttribute('static', 0);
		$containerSortField	= $this->getAttribute('sortfield', 'index');
		$frameContainerName	= $this->getAttribute('id');
		$containerTemplate	= $this->getAttribute('template');
		$containerType			= $this->getAttribute('type');
		
    if($frameContainerName == null || $containerTemplate == null || $containerType == null) {
      return '';
    }

		$langId = $this->site->getPersistentVar('lid', $this->site->getDefaultLangId());

		if( ($drawBorder == null) || ($drawBorder == '0') || (strcasecmp($drawBorder, 'no') == 0) ){
			$drawBorder = false;
		}
		else {
			$drawBorder = true;
		}

		if( ($isStatic == null) || ($isStatic == '0') || (strcasecmp($isStatic, 'no') == 0) ) {
			$isStatic = false;
		}
		else {
			$isStatic = true;
		}

		if (($result = &$dbManipulator->getRecord(FRAME_CONTAINERS_TABLE, $frameId, "container_name = '$frameContainerName' and page_id='".$pageId."'", 'frame_id')) < 0 ){
			$this->debug("ContainerTag.evaluate, error : unable to read DB for frame '".$frameId."'.", 2);
		}
    else {
      $needReload = true;

      if (($row = $dbManipulator->fetchArray($result))) {
        $containerId = $row['container_id'];
        if(is_null($container = &$containersManager->getContainer($containerId))) {
          // remove container_id from frame list
          $dbManipulator->removeFrameContainer($frameId, $this->page->getPageId(), $containerId, $frameContainerName);
        }
        else {
          $needReload = false;
        }
      }
      else {
        $container = null;
      }

      if (is_null($container)) {
  			// containerslist does not exist
  			// requires either a static list or a new one
  			$this->debug("ContainerTag::evaluate, notice : non-existent container.", 5);

  			if($isStatic){
  				$this->debug("ContainerTag::evaluate, notice : static container, trying to obtain reference to a previously created container.", 5);
  				// search for a container id of any page of the same frame with identical name
  				if(($cloneId = $dbManipulator->getStaticContainerId($frameId, $frameContainerName)) <= 0){
  					$this->debug("ContainerTag::evaluate, notice : unable to get any static container for frame '".$frameId." : requesting a new container'.", 5);
            $cloneId = 0;
  				}
  			}
        else {
          $cloneId = 0;
        }

  			$this->debug("ContainerTag::evaluate, notice : creating a new container.", 9);
  			// create a new container
  			$containerId = 0;
  			$templateId = $dbManipulator->getTemplateId($containerTemplate, $containerType);
  			$aclId = $this->acl->getAclId();
  			$container = &$containersManager->getNewContainer($aclId);
  			$container->setContainerName($frameContainerName, $langId);
  			$container->setContainerTypeName($containerType);
  			$container->setContainerTemplateId($templateId);
  			$container->setContainerSortField($containerSortField);
  			$container->setContainerCloneId($cloneId);
  			if($acl = &$containersManager->getContainerAcl($aclId)) {
  				$container->setContainerAcl($acl);
  			}
  			$containersManager->addContainer($container);
  			$containersManager->loadContainerTemplate($containerId);
  			$containersManager->loadContainerAcl($containerId);
  			$containerId = $containersManager->storeContainer($containerId);
  			$dbManipulator->addFrameContainer($frameId, $this->page->getPageId(), $containerId, $frameContainerName);
  		}

  		if(is_null($container = &$containersManager->getContainer($containerId, true, $needReload))){
  			$this->debug("ContainerTag::evaluate, error : unable to get container '$containerId' for frame '".$frameId."'.", 2);
  		}
  		else {
        $html = $container->toHTML($this->user, $langId, $this->mode, $drawBorder);
  		}
    }

    $this->debug('ContainerTag::evaluate, status : function completed', 10, DBG_CLOSE);

		return $html;
	}
}
?>