<?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
*/
	
include_once('classes/core/parser/tags/Tag.class.php');
include_once('classes/core/DBManipulator.class.php');

/**
* Tag element.
*
* @access   public
* @author	Cedric Francoys <cedric.francoys@easy-cms.org>
* @version	ContainersListControlsTag.class, Mon Jan 03 19:39:03 CET 2005
* @package	Tags
*/
class ContainersListControlsTag 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 ContainersListControlsTag($tagString, $name, $type, $offset, $length) {
		parent::Tag($tagString, $name, $type, $offset, $length);
	}

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

		$containersListsManager = &$this->site->getContainersListsManager();
		$dbManipulator = &$this->site->getDBManipulator();
				
		$isStatic									= $this->getAttribute('static', 0);
		$containersListSortField	= $this->getAttribute('sortfield', 'index');
		$frameContainersListName	= $this->getAttribute('id');

    if($frameContainersListName == 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_LISTS_TABLE, $frameId, "containers_list_name = '$frameContainersListName' and page_id='".$pageId."'", "frame_id")) < 0 ){
			$this->debug("ContainersListTag.evaluate, error : unable to read DB for frame '".$frameId."'.", 2);
		}
    else {
      $needReload = true;
      
      if (($row = $dbManipulator->fetchArray($result))) {
        $containersListId = $row['containers_list_id'];
        if(is_null($containersList = &$containersListsManager->getContainersList($containersListId))) {
          // remove container_id from frame list
          $dbManipulator->removeFrameContainersList($frameId, $this->page->getPageId(), $containersListId, $frameContainersListName);
        }
        else {
          $needReload = false;
        }
      }
      else {
        $containersList = null;
      }

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

  			if($isStatic) {
  				$this->debug("ContainersListTag.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->getStaticContainersListId($frameId, $frameContainersListName)) <= 0){
  					$this->debug("ContainersListTag.evaluate, notice : unable to get any static containersList for frame '".$frameId." : requesting a new containersList'.", 5);
            $cloneId = 0;
  				}
  			}
        else {
          $cloneId = 0;
        }

  			$this->debug("ContainersListTag::evaluate, notice : creating a new containersList.", 5);
  			$containersListId = 0;
  			$sortField = &$containersListSortField;
  			$aclId = $this->acl->getAclId();
  			$containersList = &$containersListsManager->getNewContainersList($aclId);
  			$containersList->setContainersListSortField($sortField);
        $containersList->setContainersListCloneId($cloneId);
  			if ($acl = &$containersListsManager->getContainersListAcl($aclId)) {
  				$containersList->setContainersListAcl($acl);
  			}
  			$containersListsManager->addContainersList($containersList);
  			$containersListId = $containersListsManager->storeContainersList($containersListId);
  			$dbManipulator->addFrameContainersList($frameId, $this->page->getPageId(), $containersListId, $frameContainersListName);
  		}

  		if(($containersList = &$containersListsManager->getContainersList($containersListId, true, false, $needReload)) <= 0){
  			$this->debug("ContainersListTag::evaluate, error : unable to get containersList '$containersListId' for frame '".$frameId."'.", 2);
  			return '';
  		}
    }
			
    $this->debug('ContainersListTag::evaluate, status : function completed', 10, DBG_CLOSE);
		return $containersList->getControls($this->user, $this->mode);
	}
}
?>