abstractrelationships.php

来自「SugarCRM5.1 开源PHP客户关系管理系统」· PHP 代码 · 共 526 行 · 第 1/2 页

PHP
526
字号
    }        /*     * Translate the set of relationship objects into files that the Module Loader can work with     * @param string $basepath          Pathname of the directory to contain the build     * @param string $installDefPrefix  Pathname prefix for the installdefs, for example for ModuleBuilder use "<basepath>/SugarModules"     * @param array $relationships      Relationships to implement     * @param string $moduleType        Optional - "Types" of the module - array of SugarObject types such as "file" or "basic"     */    protected function build ($basepath , $installDefPrefix , $relationships , $moduleTypes = null)    {        // keep the relationships data separate from any other build data by ading /relationships to the basepath        $basepath .= '/relationships' ;        //$GLOBALS['log']->setLevel("debug");        //$GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": basepath = $basepath" ) ;        //$GLOBALS['log']->setLevel("fatal");                $installDefs = array ( ) ;        $compositeAdded = false ;        foreach ( self::$methods as $method => $key )        {            $buildMethod = 'build' . $method ;            $saveMethod = 'save' . $method ;                        foreach ( $relationships as $name => $relationship )            {                if (! ($relationship->readonly () || $relationship->deleted ()))                {                                        if (method_exists ( $relationship, $buildMethod ) && method_exists ( $this, $saveMethod ))                    {                                                // handle the special case for Relate Fields where we need to know the moduleType in order to correctly guess the right rname destination for some types of modules                        if ($method == 'Vardefs' && ! is_null ( $moduleTypes ))                            $metadata = $relationship->$buildMethod ( $moduleTypes ) ; else                            $metadata = $relationship->$buildMethod () ;                                                if (count ( $metadata ) > 0) // don't clutter up the filesystem with empty files...                        {                            $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": BUILD is running METHOD $saveMethod" ) ;                            $installDef = $this->$saveMethod ( $basepath, $installDefPrefix, $name, $metadata ) ;                                                        // some save methods (e.g., saveRelateFieldDefinition) handle the installDefs internally and so return null                            // we should only have one set of installDefs for each module in each section - the save methods return installDefs keyed by module, and each file is contains ALL of the entries for that module for this section                                                        if (! is_null ( $installDef ))                                foreach ( $installDef as $moduleName => $def )                                    $installDefs [ $key ] [ $moduleName ] = $def ;                        }                    }                                }            }        }                return $installDefs ;    }    /*     * SAVE methods called during the build to translate the metadata provided by each relationship into files for the module installer     * Note that the installer expects only one file for each module in each section of the manifest - multiple files result in only the last one being implemented!     */        /*     * Add a set of labels to the module     * @param string $basepath              Basepath location for this module     * @param $installDefPrefix             Pathname prefix for the installdefs, for example for ModuleBuilder use "<basepath>/SugarModules"     * @param string $relationshipName      Name of this relationship (for uniqueness)     * @param array $labelDefinitions       Array of System label => Display label pairs     * @return null Nothing to be added to the installdefs for an undeployed module     */    protected function saveLabels ($basepath , $installDefPrefix , $relationshipName , $labelDefinitions)    {        mkdir_recursive ( "$basepath/language" ) ;                // remove any old label files first - can't do this in the later loop as may have multiple labels / module so we need to append        $headerString = "<?php\n" ;        foreach ( $labelDefinitions as $definition )        {            $filename = "{$basepath}/language/{$definition['module']}.php" ;            if (! file_exists ( $filename ))            {                $fh = fopen ( $filename, 'w' ) ;                fputs ( $fh, $headerString, strlen ( $headerString ) ) ;                fclose ( $fh ) ;            }        }                $installDefs = array ( ) ;        foreach ( $labelDefinitions as $definition )        {            $filename = "{$basepath}/language/{$definition['module']}.php" ;            $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->saveLabels(): saving the following to {$filename}" . print_r ( $definition, true ) ) ;                        $labelString = '$mod_strings [ "' . $definition [ 'system_label' ] . '" ] = "' . $definition [ 'display_label' ] . "\" ;\n" ;            $fh = fopen ( $filename, 'a' ) ;            fputs ( $fh, $labelString, strlen ( $labelString ) ) ;            fclose ( $fh ) ;            $installDefs [ $definition [ 'module' ] ] = array ( 'from' => "{$installDefPrefix}/relationships/language/{$definition [ 'module' ]}.php" , 'to_module' => $definition [ 'module' ] , 'language' => 'en_us' ) ;            // do not use the following write_array_to_file method to write the label file - module installer appends each of the label files together (as it does for all files) into a combined label file and so the last $mod_strings is the only one received by the application        // write_array_to_file ( 'mod_strings', array ( $definition [ 'system_label' ] => $definition [ 'display_label' ] ), $filename, "a" ) ;        }                return $installDefs ;    }    /*     * Translate a set of relationship metadata definitions into files for the Module Loader     * @param string $basepath              Basepath location for this module     * @param $installDefPrefix             Pathname prefix for the installdefs, for example for ModuleBuilder use "<basepath>/SugarModules"     * @param string $relationshipName      Name of this relationship (for uniqueness)     * @param array $relationshipMetaData   Set of metadata definitions in the form $relationshipMetaData[$relationshipName]     * @return array $installDefs           Set of new installDefs     */    protected function saveRelationshipMetaData ($basepath , $installDefPrefix , $relationshipName , $relationshipMetaData)    {        mkdir_recursive ( "$basepath/relationships" ) ;                $installDefs = array ( ) ;        list ( $rhs_module, $properties ) = each ( $relationshipMetaData ) ;        $filename = "$basepath/relationships/{$relationshipName}MetaData.php" ;        $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->saveRelationshipMetaData(): saving the following to {$filename}" . print_r ( $properties, true ) ) ;        write_array_to_file ( 'dictionary["' . $relationshipName . '"]', $properties, "{$filename}", 'w' ) ;        $installDefs [ $relationshipName ] = array ( /*'module' => $rhs_module , 'module_vardefs' => "<basepath>/Vardefs/{$relationshipName}.php" ,*/ 'meta_data' => "{$installDefPrefix}/relationships/relationships/{$relationshipName}MetaData.php" ) ;                return $installDefs ;    }    /*     * Translate a set of subpanelDefinitions into files for the Module Loader     * @param string $basepath              Basepath location for this module     * @param $installDefPrefix             Pathname prefix for the installdefs, for example for ModuleBuilder use "<basepath>/SugarModules"     * @param array $subpanelDefinitions    Set of subpanel definitions in the form $subpanelDefinitions[$for_module][]     * @return array $installDefs           Set of new installDefs     */    protected function saveSubpanelDefinitions ($basepath , $installDefPrefix , $relationshipName , $subpanelDefinitions)    {        mkdir_recursive ( "$basepath/layoutdefs/" ) ;                foreach ( $subpanelDefinitions as $moduleName => $definitions )        {            $filename = "$basepath/layoutdefs/{$moduleName}.php" ;                        foreach ( $definitions as $definition )            {                $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->saveSubpanelDefinitions(): saving the following to {$filename}" . print_r ( $definition, true ) ) ;                write_array_to_file ( 'layout_defs["' . $moduleName . '"]["subpanel_setup"]["' . strtolower ( $definition [ 'module' ] ) . '"]', $definition, $filename, "a" ) ;            }                        $installDefs [ $moduleName ] = array ( 'from' => "{$installDefPrefix}/relationships/layoutdefs/{$moduleName}.php" , 'to_module' => $moduleName ) ;        }        return $installDefs ;    }    /*     * Translate a set of linkFieldDefinitions into files for the Module Loader     * @param string $basepath              Basepath location for this module     * @param $installDefPrefix             Pathname prefix for the installdefs, for example for ModuleBuilder use "<basepath>/SugarModules"     * @param string $relationshipName      Name of this relationship (for uniqueness)     * @param array $linkFieldDefinitions   Set of link field definitions in the form $linkFieldDefinitions[$for_module]     * @return array $installDefs           Set of new installDefs     */    protected function saveVardefs ($basepath , $installDefPrefix , $relationshipName , $vardefs)    {        mkdir_recursive ( "$basepath/vardefs/" ) ;                foreach ( $vardefs as $moduleName => $definitions )        {            // find this module's Object name - the object name, not the module name, is used as the key in the vardefs...            if (isset ( $GLOBALS [ 'beanList' ] [ $moduleName ] ))            {                $module = get_module_info ( $moduleName ) ;                $object = $module->object_name ;            } else            {                $object = $moduleName ;            }                        $filename = "$basepath/vardefs/{$moduleName}.php" ;                        foreach ( $definitions as $definition )            {                $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . "->saveVardefs(): saving the following to {$filename}" . print_r ( $definition, true ) ) ;                write_array_to_file ( 'dictionary["' . $object . '"]["fields"]["' . $definition [ 'name' ] . '"]', $definition, $filename, 'a' ) ;            }            $installDefs [ $moduleName ] = array ( 'from' => "{$installDefPrefix}/relationships/vardefs/{$moduleName}.php" , 'to_module' => $moduleName ) ;        }                return $installDefs ;        }        /*     * Add any fields to the DetailView and EditView of the appropriate modules     * Only add into deployed modules, as addFieldsToUndeployedLayouts has done this already for undeployed modules (and the admin might have edited the layouts already)     * @param string $basepath              Basepath location for this module (not used)     * @param string $relationshipName      Name of this relationship (for uniqueness)     * @param array $layoutAdditions  An array of module => fieldname     * return null     */    protected function saveFieldsToLayouts ($basepath , $dummy , $relationshipName , $layoutAdditions)    {        require_once 'modules/ModuleBuilder/parsers/views/GridLayoutMetaDataParser.php' ;        foreach ( $layoutAdditions as $deployedModuleName => $fieldName )        {            if (strtolower ( $deployedModuleName ) != 'emails') // don't attempt to add a field to the emails module! It uses a custom ajax front end...                foreach ( array ( MB_EDITVIEW , MB_DETAILVIEW ) as $view )                {                    $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": adding $fieldName to $view layout for module $deployedModuleName" ) ;                    $parsedName = $this->parseDeployedModuleName ( $deployedModuleName ) ;                    if (! isset ( $parsedName [ 'packageName' ] ))                    {                        $parser = new GridLayoutMetaDataParser ( $view, $parsedName [ 'moduleName' ] ) ;                        $parser->addField ( array ( 'name' => $fieldName ) ) ;                        $parser->handleSave ( false ) ;                    }                }        }    }    /*     * Determine if we're dealing with a deployed or undeployed module based on the name     * Undeployed modules are those known to ModuleBuilder; the twist is that the deployed names of modulebuilder modules are keyname_modulename not packagename_modulename     * and ModuleBuilder doesn't have any accessor methods based around keys, so we must convert keynames to packagenames     * @param $deployedName Name of the module in the deployed form - that is, keyname_modulename or modulename     * @return array ('moduleName'=>name, 'packageName'=>package) if undeployed, ('moduleName'=>name) if deployed     */    protected function parseDeployedModuleName ($deployedName)    {        $names = explode ( '_', $deployedName ) ;        $moduleName = array_pop ( $names ) ;        $keyName = (count ( $names ) > 0) ? array_pop ( $names ) : "" ;                require_once 'modules/ModuleBuilder/MB/ModuleBuilder.php' ;        $mb = new ModuleBuilder ( ) ;        // convert the keyName into a packageName, needed for checking to see if this is really an undeployed module, or just a module with a _ in the name...        $packageName = '' ;        if (! empty ( $keyName ))        {            foreach ( $mb->getPackageList () as $name )            {                $package = $mb->getPackage ( $name ) ; // seem to need to call getPackage twice to get the key correctly... TODO: figure out why...                if ($mb->getPackage ( $name )->key == $keyName)                {                    $packageName = $name ;                    break ;                }            }        }        // test to see if this module is undeployed        if (! empty ( $packageName ) && (isset ( $mb->packages [ $packageName ]->modules [ $moduleName ] )))        {            return array ( 'moduleName' => $moduleName , 'packageName' => $packageName ) ;        } else        {            return array ( 'moduleName' => $moduleName ) ;        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?