abstractrelationships.php
来自「SugarCRM5.1 开源PHP客户关系管理系统」· PHP 代码 · 共 526 行 · 第 1/2 页
PHP
526 行
<?phpif (! defined ( 'sugarEntry' ) || ! sugarEntry) die ( 'Not A Valid Entry Point' ) ;/********************************************************************************* * SugarCRM is a customer relationship management program developed by * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation with the addition of the following permission added * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. * * This program 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 * this program; if not, see http://www.gnu.org/licenses or write to the Free * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA. * * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * SugarCRM" logo. If the display of the logo is not reasonably feasible for * technical reasons, the Appropriate Legal Notices must display the words * "Powered by SugarCRM". *********************************************************************************//* * Abstract class for managing a set of Relationships * The Relationships we're managing consist of metadata about relationships, rather than relationship implementations used by the application * Relationships defined here are implemented by the build() method to become a relationship that the application can use * Note that the modules/Relationships/Relationship.php contains some methods that look similar; remember though that the methods in that file are acting on implemented relationships, not the metadata that we deal with here */class AbstractRelationships{ static $methods = array ( 'Labels' => 'language' , 'RelationshipMetaData' => 'relationships' , 'SubpanelDefinitions' => 'layoutdefs' , 'Vardefs' => 'vardefs' , 'FieldsToLayouts' => 'layouts' ) ; static $activities = array ( 'calls' => 'Calls' , 'meetings' => 'Meetings' , 'notes' => 'Notes' , 'tasks' => 'Tasks' , 'emails' => 'Emails' ) ; protected $relationships = array ( ) ; // array containing all the AbstractRelationship objects that are in this set of relationships protected $moduleName ; /* * Find all deployed modules that can participate in a relationship * Return a list of modules with associated subpanels * @param boolean $includeActivitiesSubmodules True if the list should include Calls, Meetings etc; false if they should be replaced by the parent, Activities * @return array Array of [$module][$subpanel] */ static function findRelatableModules ($includeActivitiesSubmodules = true) { $relatableModules = array ( ) ; // add in activities automatically if required// if (! $includeActivitiesSubmodules)// $relatableModules [ 'Activities' ] [ 'default' ] = $GLOBALS [ 'mod_strings' ] [ 'LBL_DEFAULT' ] ; // find all deployed modules $d = dir ( 'modules' ) ; require_once 'modules/ModuleBuilder/Module/StudioModule.php' ; while ( $moduleName = $d->read () ) { // do not include the submodules of Activities as already have the parent... if (! $includeActivitiesSubmodules && in_array ( $moduleName, self::$activities )) continue ; if (file_exists ( 'modules/' . $moduleName . '/metadata/studio.php' )) { $module = new StudioModule( $moduleName ) ; $relatableModules [ $moduleName ] = $module->getProvidedSubpanels() ; } } return $relatableModules ; } static function validSubpanel ($filename) { if (! file_exists ( $filename )) return false ; include $filename ; return (isset ( $subpanel_layout ) && (isset ( $subpanel_layout [ 'top_buttons' ] ) && isset ( $subpanel_layout [ 'list_fields' ] ))) ; } /* * Get a list of all relationships (which have not been deleted) * @return array Array of relationship names, ready for use in get() */ function getRelationshipList () { $list = array ( ) ; foreach ( $this->relationships as $name => $relationship ) { if (! $relationship->deleted ()) $list [ $name ] = $name ; } return $list ; } /* * Get a relationship by name * @param string $relationshipName The unique name for this relationship, as returned by $relationship->getName() * @return AbstractRelationship or false if $relationshipName is not in this set of relationships */ function get ($relationshipName) { if (isset ( $this->relationships [ $relationshipName ] )) { return $this->relationships [ $relationshipName ] ; } return false ; } /* * Construct a relationship from the information in the $_REQUEST array * If a relationship_name is provided, and that relationship is not read only, then modify the existing relationship, overriding the definition with any AbstractRelationship::$definitionkeys entries set in the $_REQUEST * Otherwise, create and add a new relationship with the information in the $_REQUEST * @return AbstractRelationship */ function addFromPost () { $definition = array ( ) ; require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationship.php' ; foreach ( AbstractRelationship::$definitionKeys as $key ) { if (! empty ( $_REQUEST [ $key ] )) { $definition [ $key ] = ($key == 'relationship_type') ? AbstractRelationship::parseRelationshipType ( $_REQUEST [ $key ] ) : $definition [ $key ] = $_REQUEST [ $key ] ; } } $newRelationship = RelationshipFactory::newRelationship ( $definition ) ; if (! empty ( $_REQUEST [ 'relationship_name' ] )) { if ($relationship = $this->get ( $_REQUEST [ 'relationship_name' ] )) if (! $relationship->readonly ()) $this->delete ( $_REQUEST [ 'relationship_name' ] ) ; } // TODO: error handling in case we get a badly formed definition and hence relationship $this->add ( $newRelationship ) ; return $newRelationship ; } /* * Add a relationship to the set * @param AbstractRelationship $relationship The relationship to add */ function add ($relationship) { $name = $this->getUniqueName ( $relationship ) ; $relationship->setName ( $name ) ; $this->relationships [ $name ] = $relationship ; } /* * Load a set of relationships from a file * The saved relationships are stored as AbstractRelationship objects, which isn't the same as the old MBRelationships definition * @param string $basepath Base directory in which to store the relationships information * @return Array of AbstractRelationship objects */ protected function _load ($basepath) { $GLOBALS [ 'log' ]->info ( get_class ( $this ) . ": loading relationships from " . $basepath . '/relationships.php' ) ; $objects = array ( ) ; if (file_exists ( $basepath . '/relationships.php' )) { include ($basepath . '/relationships.php') ; foreach ( $relationships as $name => $definition ) { $objects [ $name ] = RelationshipFactory::newRelationship ( $definition ) ; } } return $objects ; } /* * Save the set of relationships to a file * @param string $basepath Base directory in which to store the relationships information */ protected function _save ($relationships , $basepath) { $GLOBALS [ 'log' ]->info ( get_class ( $this ) . ": saving relationships to " . $basepath . '/relationships.php' ) ; $header = file_get_contents ( 'modules/ModuleBuilder/MB/header.php' ) ; $definitions = array ( ) ; foreach ( $relationships as $relationship ) { // if (! $relationship->readonly ()) $definitions [ $relationship->getName () ] = $relationship->getDefinition () ; } mkdir_recursive ( $basepath ) ; // replace any existing relationships.php write_array_to_file ( 'relationships', $definitions, $basepath . '/relationships.php', 'w', $header ) ; } /* * Return all known deployed relationships * All are set to read-only - the assumption for now is that we can't directly modify a deployed relationship * However, if it was created through this AbstractRelationships class a modifiable version will be held in the relationships working file, * and that one will override the readonly version in load() * * TODO: currently we ignore the value of the 'reverse' field in the relationships definition. This is safe to do as only one * relationship (products-products) uses it (and there it makes not difference from our POV) and we don't use it when creating new ones * @return array Array of $relationshipName => $relationshipDefinition as an array */ protected function getDeployedRelationships () { require_once 'include/database/PearDatabase.php' ; $db = & PearDatabase::getInstance () ; $query = "SELECT * FROM relationships WHERE deleted = 0" ; $result = $db->query ( $query ) ; while ( $row = $db->fetchByAssoc ( $result ) ) { // set this relationship to readonly $row [ 'readonly' ] = true ; $relationships [ $row [ 'relationship_name' ] ] = $row ; } return $relationships ; } /* * Get a name for this relationship that is unique across all of the relationships we are aware of * We make the name unique by simply adding on a suffix until we achieve uniqueness * @param AbstractRelationship The relationship object * @return string A globally unique relationship name */ protected function getUniqueName ($relationship) { $allRelationships = $this->getRelationshipList () ; $basename = $relationship->getName () ; if (empty ( $basename )) { // start off with the proposed name being simply lhs_module_rhs_module $definition = $relationship->getDefinition () ; $basename = strtolower ( $definition [ 'lhs_module' ] . '_' . $definition [ 'rhs_module' ] ) ; } $name = $basename ; $suffix = 1 ; while ( isset ( $allRelationships [ $name ] ) ) { $name = $basename . "_" . ( string ) ($suffix ++) ; } return $name ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?