<?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('classes/core/CoreObject.class.php');
include_once('includes/compat/php_xslt.php');

/**
 * Template Class
 *
 * Generates HTML for a given xml and its associated xsl code.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version    Template.class,v 0.1
 * @package	   Core
 * @subpackage Templates
 */
class Template extends CoreObject {
	/**
	 * @var integer Template Id
	 */
  var $id;

	/**
	 * @var string Template Type Name
	 */
  var $typeName;

	/**
	 * @var string Template Name
	 */
	var $name;

	/**
	 * @var string Template Content (XSL)
	 */
  var $xsl;

	/**
	 * @var integer Is Template Deleted ?
	 */
	var $deleted;

	/**
	 * Constructor
	 *
	 * @param string  $xsl
	 * @param string  $typeName Template Type
	 * @param integer $id Template Id
	 */
  function Template($id=0, $typeName='', $name='', $xsl='', $deleted=0){
    $this->setTemplateId($id);
		$this->setTemplateTypeName($typeName);
		$this->setTemplateName($name);
    $this->setTemplateXSL($xsl);
		$this->setTemplateDeleted($deleted);
	}

	/**
	 * Gets Template Id
	 *
	 * @return integer
	 */
  function getTemplateId() {
		return $this->id;
	}

	/**
	 * Sets Template Id
	 *
	 * @param integer $id Template Id
	 */
	function setTemplateId($id) {
		$this->id = $id;
	}

	/**
	 * Gets Template Type Name
	 *
	 * @return string
	 */
	function getTemplateTypeName() {
		return $this->typeName;
	}

	/**
	 * Sets Template Type Name
	 *
	 * @param string $typeName Template Type Name
	 */
	function setTemplateTypeName($typeName) {
		$this->typeName = $typeName;
	}

	/**
	 * Gets Template Name
	 *
	 * @return string
	 */
	function getTemplateName(){
		return $this->name;
	}

	/**
	 * Sets Template Name
	 *
	 * @param string $name Template Name
	 */
	function setTemplateName($name){
		$this->name = $name;
	}

	/**
	 * Gets Template XSL
	 *
	 * @return string
	 */
	function getTemplateXSL() {
		return $this->xsl;
	}

	/**
	 * Sets Template XSL
	 *
	 * @param string $xsl Template XSL
	 */
	function setTemplateXSL($xsl) {
		$this->xsl = $xsl;
	}

	/**
	 * Gets Template Deletion Flag
	 *
	 * @return integer
	 */
	function getTemplateDeleted(){
		return $this->deleted;
	}

	/**
	 * Sets Template Deletion Flag
	 *
	 * @param integer $deleted
	 */
	function setTemplateDeleted($deleted){
		$this->deleted = $deleted;
	}

	/**
	 * Process XML
	 *
	 * Computes template (XSL Transformation)
	 *
	 * @param  string $xml
	 * @param  string $mode
	 * @return string
	 */
	function processXML($xml, $mode) {
    $this->debug('Template::processXML, status : function invoked', 10, DBG_OPEN);
    $html     = '';
		$id       = $this->getTemplateId();
		$typeName = $this->getTemplateTypeName();
		$xsl      = $this->getTemplateXSL();
		if (!isset($xsl)) {
			$this->debug('Template::processXML, error : no xsl specified for template('.$id.', '.$typeName.')', 2);
		}
		else {
      $xmlParser = xml_parser_create('UTF-8');
      if(!xml_parse($xmlParser, $xml, true)) {
        $this->debug('Template::processXML, error : invalid xml specified for template('.$id.', '.$typeName.')', 3);
      }
      else {
      /**
       * the purpose of this snippet is to add some internal anchor to make searching contents and containers easier
       * status : temporarily disabled
       */
       /*
        if(($pos = strpos(strtolower($xsl), "<xsl:template match=\"$typeName\">")) !== false) {
          $begin = substr($xsl, 0, $pos);
          $end = substr($xsl, $pos+strlen("<xsl:template match=\"$typeName\">"));
          $xsl = $begin."<xsl:template match=\"$typeName\">"."<a name=\"{./parent-id}_{./container-id}_{./id}\"></a>\n<a name=\"{./container-id}_{./id}\"></a>\n".$end;
        }
        */
    		if ($mode == EDIT_MODE) {
    			if($typeName == "easy_cms_page") {
    				$xsl .= "<xsl:template match=\"page_controls\">\n";
    				$xsl .= "			<a class=\"easy-CMS_UI_propertiesIcon\" href=\"javascript:selectNode('{./menu-id}','{./node-id}','{./parent-node-id}','{./id}');egShowContextMenu(contextMenu_menu_page_node);\">\n";
    				$xsl .= "				<img src=\"/includes/images/edit.gif\" />\n";
    				$xsl .= "			</a>\n";
    				$xsl .= "</xsl:template>\n";
    			}
    			else {
    				$xsl .= "<xsl:template match=\"content_controls\">\n";
    				$xsl .= "			<a class=\"easy-CMS_UI_propertiesIcon\" href=\"javascript:selectContent('{./id}','$typeName','{./container-id}');egShowContextMenu(contextMenu_menu_content);\">\n";
    				$xsl .= "				<img src=\"/includes/images/edit.gif\" />\n";
    				$xsl .= "			</a>\n";
    				$xsl .= "</xsl:template>\n";
    			}
    		}
    		$xslSheet = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
    		$xslSheet .= "<xsl:output method=\"xhtml\" encoding=\"UTF-8\" indent=\"no\" />";
    		$xslSheet .= "<xsl:output omit-xml-declaration=\"yes\" />";
    		$xslSheet .= $xsl;
    		$xslSheet .= "</xsl:stylesheet>\n";

    		$arguments = array("/_xml" => $xml, "/_xsl" => $xslSheet);

    		$xsltProcessor = xslt_create();
    		if (!($html = xslt_process($xsltProcessor, "arg:/_xml", "arg:/_xsl", NULL, $arguments))) {
          trigger_error("Template::processXML, error : unable to process xslt for template ($templateName), (error ".xslt_errno($xsltProcessor).") : ".xslt_error($xsltProcessor), E_USER_ERROR);
    		}
    		xslt_free($xsltProcessor);
      }
      xml_parser_free($xmlParser);
    }
    $this->debug('Template::processXML, status : function completed', 10, DBG_CLOSE);
		return $html;
	}
}
?>