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

include_once('classes/core/CoreObject.class.php');
include_once('interface/egManager.class.php');

/**
 * Stores the different parts of an HTML output.
 *
 * @access   public
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	HTMLManager.class, Sun Nov 14 16:38:33 CET 2004
 * @package	Core
 */
class HTMLManager extends CoreObject {
	/**
	 * Head part of HTML document
	 *
	 * Contains all header tags (meta, title, link, ...)
	 */
    var $head;
	
	/**
	 * Body part of HTML document
	 *
	 * Contains all body tags
	 */
    var $body;
	
	/**
	 * End of HTML document
	 *
	 * Closing html tag
	 */
    var $tail;		
	
	/**
	 * Path to header file
	 */
    var $headerFile;
	
	/**
	 * Title of HTML document
	 */
    var $title;
	
	/**
	 * Body's onload event javascript function
	 */
    var $onload;

    var $mode;
	/**
	 * Class constructor
	 *
	 * @param string $title title of HTML document (see {@link title})
	 * @param string $headerFile path to header file
	 * @param string $onload javascript onload function
	 */
	function HTMLManager($title, $headerFile = NORMAL_HEADER, $onload = "", $mode=VIEW_MODE) {
		if ($headerFile == '') {
			$headerFile = 'includes/html/normal_header.txt';
		}
    $this->mode = $mode;
    $manager = &egManager::getInstance();

		$this->head = file_get_contents($headerFile);
		$this->body = '';

    if($this->mode == EDIT_MODE) {
		  $this->body .= $manager->getLayersHTML();
    }

		$this->title  = $title;

		$this->onload = $onload;		
		//$this->onload = "javascript:adjustContainersLists()";
		$this->headerFile = $headerFile;
		$this->tail = "</html>\n";
	}
	
	/**
	 * Returns title of HTML document (without tags)
	 *
	 * @return string
	 */
	function getTitle() {
		return $this->title;
	}

	/**
	 * Returns body's onload event javascript function
	 *
	 * @return string
	 */
	function getOnLoad() {
		return $this->onload;
	}

	/**
	 * Returns path to header file
	 *
	 * @return string
	 */
	function getHeaderFile() {
		return $this->headerFile;
	}
	
	/**
	 * Returns HTML inside head tags including them
	 *
	 * @return string
	 */
	function getHead() {
		return $this->head."<title>".$this->getTitle()."</title>\n"."</head>\n";
	}
	
	/**
	 * Returns HTML inside body tags including them
	 *
	 * @return string
	 */
	function getBody() {
		return "<body>".$this->body."</body>\n";
	}
	
	/**
	 * Returns HTML inside body tags not including them
	 *
	 * @return string
	 */
	function getBodyContent() {
		return $this->body;
	}	
	
	/**
	 * Returns tail part of HTML document
	 *
	 * @return string
	 */
	function getTail() {
    if($this->mode == EDIT_MODE) {
      $manager = &egManager::getInstance();
      return $manager->getRuntimeHTML().$this->tail;
    }
    return $this->tail;
	}
	
	/**
	 * Returns whole HTML
	 *
	 * @return string
	 * @see    $head
	 * @see    $body
	 * @see    $tail
	 */
	function &getHTML() {
    if($this->mode == EDIT_MODE) {
      $manager = &egManager::getInstance();
  		$this->head .= "<style>".$manager->getStyles()."</style>\n";
  		$this->head .= "<script>".$manager->getScripts()."</script>\n";
    }
		$html = $this->getHead().$this->getBody().$this->getTail();
		return $html;
	}
	
	/**
	 * Add tag to body part of HTML document
	 *
	 * @param string $html valid html tag
	 */
	function addHTML($html) {
		$this->body .= $html."\n";
	}

	/**
	 * Add meta tag
	 *
	 * @param string $name valid meta name (author, keywords, ...)
	 * @param string $content valid meta content
	 */
	function addMeta($name, $content) {
		$this->head .= "<meta name=\"{$name}\" content=\"$content\" />\n";
	}
	
	/**
	 * Add script
	 *
	 * @param string $string path to script
	 */
	function addScript($script) {
		$this->head .= "<script language=\"javascript\" type=\"text/javascript\" src=\"$script\"></script>\n";
	}

	/**
	 * Add Stylesheet
	 *
	 * @param string $style path to stylesheet
	 */
	function addStyle($style) {
		$this->head .= "<link rel=\"stylesheet\" href=\"$style\" type=\"text/css\" />\n";
	}
}
?>