Location: PHPKode > projects > Entier Studio > entierstudio/framework/viewtemplate.php
<?php
/**
 * Entier Studio
 *
 * LICENSE
 *
 *	Copyright 2006 Entier Studio team.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 *
 * @package    entier.framework
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 * @version	   $Id: viewtemplate.php 97 2008-02-05 21:08:53Z yannromefort $
 */
// Comments
//
//    This work is derived from class.FastTemplate.php3 version 1.1.0 as
//    available from http://www.thewebmasters.net/.  That work makes
//    reference to the "GNU General Artistic License".  In correspondence
//    with the author, the intent was to use the GNU General Public License;
//    this work does the same.
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefViewTemplate")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefViewTemplate", "1");
    define(OUTPUT, 1);
    define(RENDER, 2);
    //-------------------------------------------------------------------------
    // Include
    @require (FRAMEWORK_DIR . "viewtemplate.model.php");
    @require (FRAMEWORK_DIR . "viewtemplate.parser.php");
    //-------------------------------------------------------------------------
    //	Class
    /*
    *
    *	Features :
    *
    *		Nested blocks
    *			<block:block1>
    *				<block:block2>
    *				</block:block2>
    *			</block:block1>
    *
    *		multiple output mode
    *			OUTPUT OR RENDER
    *	How to:
    *
    */
    class ViewTemplate Extends ViewTemplateParser {
        //---------------------------------------------------------------------
        //	Attributes
        
        /**
         * Template Path
         * @var	string
         * @see	setTemplatePath
         */
        var $m_templatePath = "";
        /**
         * Inherit Flag
         * @var	boolean
         * @see	setInheritFlag
         */
        var $m_inheritFlag = true;
        /**
         * Template models
         * @var	array
         */
        var $m_templets = array();
        /**
         * Current template
         * @var	string
         * @see	select, render, assign
         */
        var $m_currents = array();
        /**
         * Previous templates
         * @var	string
         * @see	select, render
         */
        var $m_previous = array();
        /**
         * Handles counter
         * @var	integer
         * @see	loadTemplateFile, handle
         */
        var $m_handles = 0;
        /**
         * Global fields/ values
         * @var	array
         * @see	assign_global
         */
        var $m_global = array();
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         * @param	string path to templates
         */
        function ViewTemplate($templatePath = "./") {
            $this->setTemplatePath($templatePath);
        }
        //---------------------------------------------------------------------
        //	Methods
        
        /**
         * Short description
         * @param	string	Target directory for the templates Files
         * @return	boolean	Return flag
         */
        function setTemplatePath($templatePath) {
            //
            if (empty($templatePath)) return (false);
            //
            if (@substr($templatePath, -1) != '/') $templatePath.= '/';
            //
            $this->m_templatePath = $templatePath;
            //
            return (true);
        }
        /**
         * Short description
         * @param	boolean	Inherit behavior
         */
        function setInheritFlag($inheritFlag) {
            $this->m_inheritFlag = $inheritFlag;
        }
        /**
         * Short description
         * @param	string	Target name for the template File
         * @return	boolean	Return flag
         */
        function loadTemplateFile($templateFile) {
            //	Make TemplateName
            $templateFile = $this->m_templatePath . $templateFile;
            //	Open TemplateFile
            if (!$f_id = @fopen($templateFile, "r")) {
                $this->set_error_message(5, $templateFile);
                return (false);
            }
            if (!$source = @fread($f_id, filesize($templateFile))) {
                $this->set_error_message(6, $templateFile);
                return (false);
            }
            @fclose($f_id);
            //	Parse source
            if ($this->parseTemplate($source) == true) {
                //	Create handle
                $handle = $this->m_handles++;
                // Create Templets
                foreach($this->m_layouts as $layout => $params)
					$this->m_templets[$handle][$layout] = new ViewTemplateModel($layout, $params["parent"], $params["source"], $params["insets"]);
                //	Cleanup
                unset($this->m_layouts);
                $this->m_layouts = NULL;
                //	Init
                return ($this->define(".", $handle));
            }
            //
            return (false);
        }
        //	Methods
        //	Private
        
        /**
         * Short description
         *
         * @access	private
         *
         * @param	string	Target field
         * @param	string	Layout name
         * @param	string	Target model
         * @return	string	Return flag
         * @see	compile
         */
        function inherit($field, $layout, $handle) {
            //
            if ($layout == "") {
                if (isset($this->m_global[$field]))
                    return ($this->m_global[$field]);
                else
                    return ("");
            }
            //
            if(isset($this->m_templets[$handle][$layout]->m_fields[$field])) {
				$value = $this->m_templets[$handle][$layout]->m_fields[$field];
				if ($value != "")
					return ($value);
			}
            //
            return ($this->inherit($field, $this->m_templets[$handle][$layout]->m_parent, $handle));
        }
        /**
         * Short description
         *
         * @access	private
         *
         * @param	string	Layout name
         * @param	string	Target model
         * @return	boolean	Return flag
         *
         * @see	render
         */
        function compile($layout, $handle) {
            //	Check status
            if ($this->m_templets[$handle][$layout]->m_status == 0) return $this->m_templets[$handle][$layout]->m_output;
            //
            $temp = $this->m_templets[$handle][$layout]->m_source;
            $parent = $this->m_templets[$handle][$layout]->m_parent;
            if (count($this->m_templets[$handle][$layout]->m_fields)) {
                //
                $inherit = $this->m_inheritFlag;
                foreach($this->m_templets[$handle][$layout]->m_fields as $field => $value) {
                    // side effect: ????
                    if (($inherit == true) && (!isset($value) || ($value == "")))
                        $value = $this->inherit($field, $parent, $handle);
                    //
                    $temp = @preg_replace("(\{$field\})", $value, $temp);
                }
            }
            //
            if (count($this->m_templets[$handle][$layout]->m_insets)) {
                //
                foreach($this->m_templets[$handle][$layout]->m_insets as $inset) {
                    $this->compile($inset, $handle);
                    $temp = @preg_replace("(\|$inset\|)", $this->m_templets[$handle][$inset]->m_output, $temp);
                    $this->m_templets[$handle][$inset]->m_output = NULL;
                }
            }
            //
            $this->m_templets[$handle][$layout]->m_output.= $temp;
        }
        /**
         * import a template file
         *
         * @access	public
         *
         * @param	string	Layout file
         * @return	string	Target model
         *
         * @see
         */
        function import($templateFile) {
            //
            if ($this->loadTemplateFile($templateFile) == false)
                return (-1);
            //
            return ($this->m_handles-1);
        }
        /**
         * define a template layout
         *
         * @access	public
         *
         * @param	string	Layout name
         * @param	string	Target model
         * @return	boolean	Return flag
         */
        function define($layout = ".", $handle = 0) {
            //	Check if defined
            if (!isset($this->m_templets[$handle][$layout]))
                return ($this->loadTemplateFile($layout));
            //	Check status
            if ($this->m_templets[$handle][$layout]->m_status == 1)
                $this->render($layout, $handle);
            //	set status:	active
            $this->m_templets[$handle][$layout]->m_status = 1;
            if(isset($this->m_currents[$handle]))
				$this->m_previous[$handle] = $this->m_currents[$handle];
			else
				$this->m_previous[$handle] = null;
            $this->m_currents[$handle] = $layout;
            //
            return (true);
        }
        /**
         * select a template layout
         *
         * @access	public
         *
         * @param	string	Layout name
         * @param	string	Target model
         * @return	boolean	Return flag
         */
        function select($layout = ".", $handle = 0) {
            //	Check if defined
            if (!isset($this->m_templets[$handle][$layout])) {
                $this->set_error_message(7, array(
                    $handle,
                    $layout
                ));
                return (false);
            }
            //	Check if status ok
            if ($this->m_templets[$handle][$layout]->m_status == 1) {
                $this->set_error_message(8, array(
                    $handle,
                    $layout
                ));
                return (false);
            }
            //	set status:	active
            $this->m_templets[$handle][$layout]->m_status = 1;
            $this->m_previous[$handle] = $this->m_currents[$handle];
            $this->m_currents[$handle] = $layout;
            //
            return (true);
        }
        /**
         * render a template layout
         *
         * @access	public
         *
         * @param	string	Layout name
         * @param	string	Target model
         * @return	boolean	Return flag
         */
        function render($layout = ".", $handle = 0) {
            //	Check defined
            if (!isset($this->m_templets[$handle][$layout])) {
                $this->set_error_message(7, array(
                    $handle,
                    $layout
                ));
                return (false);
            }
            //	Check status
            if ($this->m_templets[$handle][$layout]->m_status != 1) {
                $this->set_error_message(9, array(
                    $handle,
                    $layout
                ));
                return (false);
            }
            // Close insets
            if (count($this->m_templets[$handle][$layout]->m_insets)) {
                foreach($this->m_templets[$handle][$layout]->m_insets as $member) {
                    if ($this->m_templets[$handle][$member]->m_status == 1) $this->render($member, $handle);
                }
            }
            // Compile output
            $this->m_templets[$handle][$layout]->m_output.= $this->compile($layout, $handle);
            // Cleanup
            if (count($this->m_templets[$handle][$layout]->m_fields)) {
                foreach($this->m_templets[$handle][$layout]->m_fields as $field => $value) {
                    if (isset($this->m_global[$field])) $this->m_templets[$handle][$layout]->m_fields[$field] = $this->m_global[$field];
                    else $this->m_templets[$handle][$layout]->m_fields[$field] = "";
                }
            }
            // Set status
            $this->m_templets[$handle][$layout]->m_status = 0;
            $this->m_currents[$handle] = $this->m_previous[$handle];
            //
            return (true);
        }
        /**
         * assign a value to a templated field
         *
         * @access	public
         *
         * @param	string		Field name
         *		OR
         * @param	array		Field list
         * @param	variant		Value
         * @param	string		Target model
         * @return	boolean		Return flag
         */
        function assign($field, $value = NULL, $handle = 0) {
            //
            if (is_array($field)) {
                while (list($index, $value) = each($field)) {
                    if ($this->assign($index, $value, $handle) == false) return (false);
                }
            }
            // single field
            else {
                $tab = explode(".", $field);
                if (count($tab) == 2) {
                    $layout = $tab[0];
                    $field = $tab[1];
                } else {
                    $layout = $this->m_currents[$handle];
                    $field = $tab[0];
                }
                //
                if (!is_object($this->m_templets[$handle][$layout])) {
                    $this->set_error_message(7, array(
                        $handle,
                        $layout
                    ));
                    return (false);
                }
                //
                if ($this->m_templets[$handle][$layout]->m_status != 1) {
                    $this->set_error_message(10, array(
                        $handle,
                        $layout,
                        $value
                    ));
                    return (false);
                }
                //
                if ($this->m_templets[$handle][$layout]->setValue($field, $value) == false)
                    $this->set_error_message(1, $field);
                //
                return (true);
            }
            //
            return (false);
        }
        /**
         * conditional rendering of a block
         *
         * @access	public
         *
         * @param	string		Layout name
         * @param	boolean		Condition
         * @return	boolean		Return flag
         */
        function toggle($layout = ".", $condition) {
            //
            if ($condition == false)
                return (false);
            //
            if ($this->select($layout))
                return ($this->render($layout));
            //
            return (false);
        }
        /**
         * insert a template content
         *
         * @access	public
         *
         * @param	string	Field name
         * @param	integer	Source handle
         * @param	integer	Target handle
         * @param	string	Source layout
         * @param	string	Target layout
         * @return	boolean	Return flag
         */
        function insert($field, $source, $target = 0, $slayout = ".", $tlayout = ".") {
            //
            if (!is_object($this->m_templets[$source][$slayout])) {
                $this->set_error_message(7, array(
                    $source,
                    $slayout
                ));
                return (false);
            }
            //
            if (!is_object($this->m_templets[$target][$tlayout])) {
                $this->set_error_message(7, array(
                    $target,
                    $tlayout
                ));
                return (false);
            }
            //
            if ($this->m_templets[$target][$tlayout]->m_status != 1) {
                $this->set_error_message(10, array(
                    $target,
                    $tlayout
                ));
                return (false);
            }
            //
            $value = $this->m_templets[$source][$slayout]->m_output;
            if ($this->m_templets[$target][$tlayout]->setValue($field, $value) == false)
                $this->set_error_message(1, $field);
            //
            return (true);
        }
        /**
         * assign  a global value
         *
         * @access	public
         *
         * @param	string	Field name
         * @param	string	Value
         * @return	boolean	Return flag
         */
        function assign_global($field, $value) {
            //
            if (!empty($value)) {
                $this->m_global[$field] = $value;
                return (true);
            }
            //
            $this->set_error_message(20, $field);
            return (false);
        }
        /**
         * assign  a global value
         *
         * @access	public
         *
         * @param	integer print mode OUTPUT or RENDER
         * @param	integer	pointer
         * @param	string	layout
         * @param	boolean	compress
         * @return	string	rendered
         */
        function output($print = 1, $handle = 0, $layout = ".", $compress = false) {
            if ($print == RENDER)
                return;
            if ($layout == ".")
                $this->render($layout, $handle);
            if ($print) {
                if ($compress == true) {
                    @ob_start("ob_gzhandler");
                    print $this->m_templets[$handle][$layout]->m_output;
                    @ob_end_flush();
                } else print $this->m_templets[$handle][$layout]->m_output;
            } else return ($this->m_templets[$handle][$layout]->m_output);
        }
    };
    //	Class
    //-------------------------------------------------------------------------    
}
//	namespace
//-----------------------------------------------------------------------------
?>
Return current item: Entier Studio