Location: PHPKode > projects > Entier Studio > entierstudio/studio/classes/procs/proc.ComponentPublication.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.studio
 * @copyright  Copyright (c) 2006 Entier Studio team. All rights reserved.
 * @version	   $Id: proc.ComponentPublication.php 121 2008-03-11 20:17:41Z yannromefort $
 */
//-----------------------------------------------------------------------------
//	namespace
if (!defined("DefComponentPublication")) {
    //-------------------------------------------------------------------------
    //	Define
    define("DefComponentPublication", "1");
    //-------------------------------------------------------------------------
    //	Include
    //
    @require_once (COMPONENTS_DATA . "data.PublicationComponent.php");
    @require_once (COMPONENTS_DATA . "data.PublicationComponentSet.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryComponentTemplate.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryComponentReleasedSet.php");
    //
    @require_once (COMPONENTS_DATA . "data.ObjectoryResourceRelease.php");
    @require_once (COMPONENTS_DATA . "data.ObjectoryResourceReleaseGarbage.php");
    //
    @require_once (INTERCHANGE_PROC. "proc.InterchangeComponentClasspath.php");
    //-------------------------------------------------------------------------
    //	Class
    
    /**
     * Objectory Components publication
     *
     * @see	view.ObjectoryComponentExporter.php ObjectoryComponentExporter.RenderView ( $view=PROCVIEW )
     * @todo	optimization of directories lookup in expandDirectoryLocator by a hash
     * @todo	Revision Control implementation in ComponentPublicationAgent
     */
    class ComponentPublication {
        //---------------------------------------------------------------------
        //	Attributes

        /**
         * Parameters set
         * @var	array
         */
        var $m_parameters = array();
        /**
         * Publication agent
         * @var	object		class="ComponentPublicationAgent"
         */
        var $m_publisher = NULL;
        /**
         * Unique resource locator
         * @var	string         
         */
        var $m_URLocator = "";
        /**
         * Publication unique id
         * @var	integer
         */
        var $m_publicationId = 0;
        /**
         * Released components number
         * @var	integer
         */
        var $m_publishedCount = 0;
        /**
         * Discarded components number
         * @var	integer
         */
        var $m_discardedCount = 0;
        /**
         * Save flag
         * @var	boolean
         */
        var $m_saveFlag = true;
        /**
         * Bind flag
         * @var	boolean
         */
        var $m_bindFlag = false;
        //---------------------------------------------------------------------
        //	Constructor
        
        /**
         *
         * @param	array	name="postFlag" publication parameters
         */
        function ComponentPublication($postFlag = NULL) {
            //
            $this->m_parameters["saveFlag"] = ($postFlag[0] == "on");
            $this->m_parameters["bindFlag"] = ($postFlag[1] == "on");
            $this->m_parameters["classpath"] = false;
        }
        //---------------------------------------------------------------------
        //	Properties
        /*
        * publicationId property
        * @return	integer
        */
        function publicationId() {
            //
            return ($this->m_publicationId);
        }
        /*
        * publishedCount property
        * @return	integer
        */
        function publishedCount() {
            //
            return ($this->m_publishedCount);
        }
        /*
        * discardedCount property
        * @return	integer
        */
        function discardedCount() {
            //
            return ($this->m_discardedCount);
        }
        //---------------------------------------------------------------------
        //	Methods
        
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function expandFileLocator(&$datasource, &$component, &$resource, $locator) {
            //
            $nodePath = @dirname($locator);
            //
            return ($this->expandDirectoryLocator($datasource, $component, $resource, $nodePath));
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	string		name="locator"
         * @return	boolean      
         */
        function expandDirectoryPattern(&$datasource, &$component, $locator) {
            // Find first occurrence of "$"
			if( false !== @strstr($locator, "$")) {
				//
				if( false === $this->m_parameters["classpath"] ) {
					//						
					$key = $component["OBJECTOPKID"];
					// Expand patterns
					$this->m_parameters["classpath"] = InterchangeComponentClasspath::getClasspath($datasource, $key);
					$this->m_parameters["basedir"] = InterchangeComponentClasspath::getBasedir($datasource, $key);
					$this->m_parameters["date"] = Date("Ymd");
				}
				// Expand patterns
				$locator = @str_replace("\$classpath", $this->m_parameters["classpath"], $locator);
                $locator = @str_replace("\$basedir", $this->m_parameters["basedir"], $locator);
                $locator = @str_replace("\$date", $this->m_parameters["date"], $locator);
			}
			//
			return($locator);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean      
         */
        function expandDirectoryLocator(&$datasource, &$component, &$resource, $locator) {            
            //
            $nodePath = @ltrim(@addslashes($locator) , "/\\");
            $nodeList = @explode("/", $nodePath);
            $typeName = "directory";
            $nodeName = "";
            //
            for ($i = 0;$i < @sizeof($nodeList);$i++) {
                //
                $itemName = $nodeList[$i];
                // no "/" at start  see framework filemanagers...
                if ($i > 0)
                    $nodeName.= ("/" . $itemName);
                else
                    $nodeName = $itemName;
                //
                if ($this->m_publisher->findResource($nodeName, $typeName) == false) {
                    //	1.2* Post relevant component
                    $todoMask = array();
                    $todoMask[FILEMAN_MAKE] = FILECMD_CREATE;
                    $todoMask[FILEMAN_SAVE] = $this->m_parameters["saveFlag"];
                    $todoMask[FILEMAN_MODE] = "01777";
                    //
                    if ($this->m_publisher->postResource($nodeName, $typeName, $todoMask) == true) {
                        //	1.2.1* release
                        $release = new ObjectoryResourceRelease();
                        $release->set_field_value("OBJRESOPKID", $resource["OBJRESOPKID"]);
                        $release->set_field_value("OBJRRELMILE", $this->m_publicationId);
                        $release->set_field_value("OBJRRELPATH", $nodeName);
                        /**
                         * @quirk should be ObjectoryComponentType.OBJCTYPTYPE
                         */
                        $release->set_field_value("OBJRRELTYPE", DIRECTORY_TYPE);
                        //
                        if ($release->insertRow($datasource) == false) {
                            //
                            $this->m_errorSet["RELEASE_ERROR"] = true;
                            //
                            return (false);
                        }
                        unset($release);
                        //
                        $this->m_publishedCount++;
                    } else {
                        // Directory creation failed
                        //
                        //	1.2.2*	garbage
                        $garbage = new ObjectoryResourceReleaseGarbage();
                        $garbage->set_field_value("OBJCOMPPKID", $component["OBJCOMPPKID"]);
                        $garbage->set_field_value("OBJRESOPKID", $resource["OBJRESOPKID"]);
                        $garbage->set_field_value("OBJRRELMILE", $this->m_publicationId);
                        $garbage->set_field_value("OBJRRELCERR", $this->m_publisher->errorNumber());
                        $garbage->set_field_value("OBJRRELPATH", $nodeName);
                        $garbage->set_field_value("OBJRRGATYPE", $component["OBJCTYPTYPE"]);
                        //
                        if ($garbage->insertRow($datasource) == false) {
                            //
                            $this->m_errorSet["GARBAGE_ERROR"] = true;
                            //
                            return (false);
                        }
                        unset($garbage);
                        //
                        $this->m_discardedCount++;
                        //
                        return (false);
                    }
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function CreateOrUpdateDirectory(&$datasource, &$component, &$resource, &$locator) {
            // Check component type
            $nodeType = $component["OBJCTYPTYPE"];
            if (($nodeType != MODEL_ROOT_TYPE) && ($nodeType != MODEL_DIR_TYPE))
                return (false);
            //	directory full path
            $location = @preg_replace("/[.:*?\"<>|]/", "", $component["OBJCOMPPATH"]);
            if (!empty($location)) {
                //
                $locator.= ("/" . $location);
				$locator = $this->expandDirectoryPattern($datasource, $component, $locator);
				//
                return ($this->expandDirectoryLocator(&$datasource, &$component, &$resource, $locator));
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function CreateOrUpdateTemplate(&$datasource, &$component, &$resource, $locator) {
            //
            if ($component["OBJMAKEMODE"] != DYNAMIC_TYPE) return (false);
            //
            $releaseSet = new ObjectoryComponentReleasedSet($component["OBJCOMPPKID"], true);
            if ($releaseSet->selectSet($datasource) == true) {
                //
                $typeName = "file";
                $todoMask = array();
                $todoMask[FILEMAN_MAKE] = FILECMD_CREATE;
                $todoMask[FILEMAN_SAVE] = true;
                $todoMask[FILEMAN_MODE] = "01777";
                //
                for ($i = 0;$i < $releaseSet->rowCount();$i++) {
                    //	select next row
                    if ($releaseSet->fetchCursorRow($datasource) == false) return (false);
                    //
                    $nodeName = $locator . "/" . $releaseSet->get_field_value("OBJCRELNAME");
                    $nodeName = $this->expandDirectoryPattern($datasource, $component, $nodeName);
                    if ($this->expandFileLocator($datasource, $component, $resource, $nodeName) == true) {
                        //
                        $todoMask[FILEMAN_DATA] = $releaseSet->get_field_value("OBJCRELFILE");
                        $todoMask[FILEMAN_MIME] = $releaseSet->get_field_value("OBJCRELMIME");
                        //
                        if ($this->m_publisher->postResource($nodeName, $typeName, $todoMask) == true) {
                            //	Log release
                            $release = new ObjectoryResourceRelease();
                            $release->set_field_value("OBJRESOPKID", $resource["OBJRESOPKID"]);
                            $release->set_field_value("OBJCRELPKID", $releaseSet->get_field_value("OBJCRELPKID"));
                            $release->set_field_value("OBJRRELMILE", $this->m_publicationId);
                            $release->set_field_value("OBJRRELPATH", $nodeName);
                            /**
                             * @quirk should be ObjectoryComponentType.OBJCTYPTYPE
                             */
                            $release->set_field_value("OBJRRELTYPE", PATTERN_TYPE);
                            //
                            if ($release->insertRow($datasource) == false) {
                                //
                                $this->m_errorSet["RELEASE_ERROR"] = true;
                                //
                                return (false);
                            }
                            unset($release);
                            //
                            $this->m_publishedCount++;
                        } else {
                            // File creation failed
                            // Log garbage
                            //
                            //	Insert new resource release  garbage
                            $garbage = new ObjectoryResourceReleaseGarbage();
                            $garbage->set_field_value("OBJCOMPPKID", $component["OBJCOMPPKID"]);
                            $garbage->set_field_value("OBJRESOPKID", $resource["OBJRESOPKID"]);
                            $garbage->set_field_value("OBJCRELPKID", $releaseSet->get_field_value("OBJCRELPKID"));
                            $garbage->set_field_value("OBJRRELMILE", $this->m_publicationId);
                            $garbage->set_field_value("OBJRRELCERR", $this->m_publisher->errorNumber());
                            $garbage->set_field_value("OBJRRELPATH", $nodeName);
                            $garbage->set_field_value("OBJRRGATYPE", $component["OBJCTYPTYPE"]);
                            //
                            if ($garbage->insertRow($datasource) == false) {
                                //
                                $this->m_errorSet["GARBAGE_ERROR"] = true;
                                //
                                return (false);
                            }
                            unset($garbage);
                            //
                            $this->m_discardedCount++;
                        }
                    }
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function CreateOrUpdateFile(&$datasource, &$component, &$resource, $locator) {
            //
            if ($component["OBJMAKEMODE"] != STATIC_TYPE) return (false);
            //
            $componentTemplate = new ObjectoryComponentTemplate($component["OBJCOMPPKID"]);
            //
            if ($componentTemplate->selectRow($datasource) == true) {
                //
                $typeName = "file";
                $todoMask = array();
                $todoMask[FILEMAN_MAKE] = FILECMD_CREATE;
                $todoMask[FILEMAN_SAVE] = true;
                $todoMask[FILEMAN_MODE] = "01777";
                //
                $nodeName = $locator . "/" . $component["OBJCOMPPATH"];
                $nodeName = $this->expandDirectoryPattern($datasource, $component, $nodeName);
                if ($this->expandFileLocator($datasource, $component, $resource, $nodeName) == true) {
                    //
                    $todoMask[FILEMAN_DATA] = $componentTemplate->get_field_value("OBJFILETEXT");
                    $todoMask[FILEMAN_MIME] = $componentTemplate->get_field_value("OBJFILEMIME");
                    //
                    if ($this->m_publisher->postResource($nodeName, $typeName, $todoMask) == true) {
                        //	Log release
                        $release = new ObjectoryResourceRelease();
                        $release->set_field_value("OBJRESOPKID", $resource["OBJRESOPKID"]);
                        $release->set_field_value("OBJRRELMILE", $this->m_publicationId);
                        $release->set_field_value("OBJRRELPATH", $nodeName);
                        /**
                         * @quirk should be ObjectoryComponentType.OBJCTYPTYPE
                         */
                        $release->set_field_value("OBJRRELTYPE", FILE_TYPE);
                        //
                        if ($release->insertRow($datasource) == false) {
                            //
                            $this->m_errorSet["RELEASE_ERROR"] = true;
                            //
                            return (false);
                        }
                        unset($release);
                        //
                        $this->m_publishedCount++;
                    } else {
                        // File creation failed
                        // Log garbage
                        //
                        //	Insert new resource release  garbage
                        $garbage = new ObjectoryResourceReleaseGarbage();
                        $garbage->set_field_value("OBJCOMPPKID", $component["OBJCOMPPKID"]);
                        $garbage->set_field_value("OBJRESOPKID", $resource["OBJRESOPKID"]);
                        $garbage->set_field_value("OBJRRELMILE", $this->m_publicationId);
                        $garbage->set_field_value("OBJRRELCERR", $this->m_publisher->errorNumber());
                        $garbage->set_field_value("OBJRRELPATH", $nodeName);
                        $garbage->set_field_value("OBJRRGATYPE", $component["OBJCTYPTYPE"]);
                        //
                        if ($garbage->insertRow($datasource) == false) {
                            //
                            $this->m_errorSet["GARBAGE_ERROR"] = true;
                            //
                            return (false);
                        }
                        unset($garbage);
                        //
                        $this->m_discardedCount++;
                    }
                }
            }
            //
            return (true);
        }        
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function createOrUpdateChildResources(&$datasource, &$component, &$resource, $locator) {
            //
            $componentSet = new PublicationComponentSet($component["OBJCOMPPKID"]);
            if ($componentSet->selectSet($datasource, REFLECTKEY) == true) {
                //
                for ($i = 0;$i < $componentSet->rowCount();$i++) {
                    //	select next row
                    if ($componentSet->fetchCursorRow($datasource) == false) return (false);
                    //
                    $nodeType = $componentSet->get_field_value("OBJCTYPTYPE");
                    if (($nodeType == MODEL_ROOT_TYPE) || ($nodeType == MODEL_DIR_TYPE)) {
                        // update forward
                        $pathname = $locator;
                        $location = $componentSet->get_field_value("OBJCOMPPATH");
                        if (!empty($location))
                            $location = $locator . "/" . $location;
                        else
                            $location = $locator;
                        // create or update directory
                        if ($this->CreateOrUpdateDirectory($datasource, $componentSet->fieldSet() , $resource, $pathname) == false) {
                            //
                            return (false);
                        }
                        // parse forward
                        if ($this->createOrUpdateChildResources($datasource, $componentSet->fieldSet() , $resource, $location) == false) {
                            //
                            return (false);
                        }
                    } else {
                        //	MODEL_FILE
                        if ($this->createOrUpdateResource($datasource, $componentSet->fieldSet() , $resource, $locator) == false) {
                            //
                            return (false);
                        }
                    }
                }
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function createOrUpdateResource(&$datasource, &$component, &$resource, &$locator) {
            //
            switch ($component["OBJCTYPTYPE"]) {
                case MODEL_DIR_TYPE:
                case MODEL_ROOT_TYPE:
                    //
                    return ($this->CreateOrUpdateDirectory($datasource, $component, $resource, $locator));
                    //                    
					break;
                case MODEL_FILE_TYPE:
                    //
                    if ($component["OBJMAKEMODE"] == DYNAMIC_TYPE)
                        return ($this->CreateOrUpdateTemplate($datasource, $component, $resource, $locator));
                    elseif ($component["OBJMAKEMODE"] == STATIC_TYPE) 
                        return ($this->CreateOrUpdateFile($datasource, $component, $resource, $locator));
                    //
                    break;
            }
            //
            return (true);
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"
         * @param	array		name="resource"
         * @param	string		name="locator"
         * @return	boolean
         */
        function buildResourcePath(&$datasource, &$component, &$resource, &$locator) {
            //
            $parentComponent = new PublicationComponent($component["OBJCOMPLINK"]);
            if ($parentComponent->selectRow($datasource) == true) {
                //
                $this->buildResourcePath($datasource, $parentComponent->fieldSet() , $resource, $locator);
                //
                $location = @preg_replace("/[.:*?\"<>|]/", "", $parentComponent->get_field_value("OBJCOMPPATH"));
                if (!empty($location))
				    $locator.= ("/" . $location);
            }
        }
        /**
         *
         * @access	public
         *
         * @param	object		name="datasource"
         * @param	array		name="component"	PublicationComponent.fieldset
         * @param	array		name="resource"
         * @param	boolean		name="expand"
         * @return	boolean
         */
        function publish(&$datasource, &$component, &$resource, $expand = FALSE) {
            //
            if (!is_object($datasource))
                return (false);
            //
            if (!is_array($component))
                return (false);
            //
            if (!is_array($resource))
                return (false);
            //
            /*
            * 1- Publication Manager Agent Start
            */
            //
            @include_once (COMPONENTS_PROC . "proc.ComponentPublicationAgent.php");
            //
            $this->m_publisher = new ComponentPublicationAgent($this->m_parameters["saveFlag"]);
            if ($this->m_publisher->startResourceManager($resource) == false)
			    return (false);
            //
            /*
            * 2- Milestone id
            */
            $this->m_publicationId = @crc32(@md5(@uniqid("")));
            //
            /*
            * 3- Resource path creation
            */
            //
            $this->buildResourcePath($datasource, $component, $resource, $this->m_URLocator);
            //
            /*
            * 4- Target Component Publication
            */
            //            
            $this->createOrUpdateResource($datasource, $component, $resource, $this->m_URLocator);            
            //
            /*
            * 5- Child Components Set Publication
            */
            //
            $this->createOrUpdateChildResources($datasource, $component, $resource, $this->m_URLocator);
            //
            /*
            * 6- Publication Manager Agent Stop
            */
            //
            if ($this->m_publisher->closeResourceManager() == false)
			    return (false);
            //
            return (true);
        }
    };
    //	Class
    //-------------------------------------------------------------------------
        
}
//	namespace
//-----------------------------------------------------------------------------
    
?>
Return current item: Entier Studio