2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
40 * A mechanism to dynamically define new Relationships between modules
41 * This differs from the classes in modules/Relationships and data/Link in that they contain the implementation for pre-defined Relationships
42 * Those classes use the metadata in the dictionary and layout definitions to implement the relationships; this class allows you to manage and manipulate that metadata
44 class AbstractRelationship
47 protected $definition ; // enough information to rebuild this relationship
51 * These are the elements that fully define any Relationship
52 * Any subclass of AbstractRelationship uses an array with a subset of the following keys as metadata to describe the Relationship it will implement
53 * The base set of keys are those used in the Relationships table
54 * Defined as Public as MBRelationship uses these to read the _POST data
56 public static $definitionKeys = array (
57 // atttributes of this relationship - here in the definition so they are preserved across saves and loads
61 'readonly' , // a readonly relationship cannot be Built by subclasses of AbstractRelationships
62 'deleted' , // a deleted relationship will not be built, and if it had been built previously the built relationship will be removed
63 'relationship_only' , // means that we won't build any UI components for this relationship - required while the Subpanel code is restricted to one subpanel only from any module, and probably useful afterwards also for developers to build relationships for new code - it's a feature!
64 // keys not found in Relationships table
66 'rhs_label', // optional
67 'lhs_label', // optional
68 'lhs_subpanel' , // subpanel FROM the lhs_module to display on the rhs_module detail view
69 'rhs_subpanel' , // subpanel FROM the rhs_module to display on the lhs_module detail view
70 // keys from Relationships table
82 'relationship_role_column' ,
83 'relationship_role_column_value' ,
87 * Relationship_role_column and relationship_role_column_value:
88 * These two values define an additional condition on the relationship. If present, the value in relationship_role_column in the relationship table must equal relationship_role_column_value
89 * Any update to the relationship made using a link field tied to the relationship (as is proper) will automatically (in Link.php) add in the relationship_role_column_value
90 * The relationship table must of course contain a column with the name given in relationship_role_column
92 * relationship_role_column and relationship_role_column_value are here implemented in a slightly less optimized form than in the standard OOB application
93 * In the OOB application, multiple relationships can, and do, share the same relationship table. Therefore, each variant of the relationship does not require its own table
94 * Here for simplicity in implementation each relationship has its own unique table. Therefore, the relationship_role_column in these tables will only contain the value relationship_role_column_value
95 * In the OOB relationships, the relationship_role_column will contain any of the relationship_role_column_values from the relationships that share the table
96 * TODO: implement this optimization
102 * @param string $definition Definition array for this relationship. Parameters are given in self::keys
104 function __construct ($definition)
106 // set any undefined attributes to the default value
107 foreach ( array ( 'readonly' , 'deleted' , 'relationship_only', 'for_activities', 'is_custom', 'from_studio' ) as $key )
108 if (! isset ( $definition [ $key ] ))
109 $definition [ $key ] = false ;
111 foreach ( self::$definitionKeys as $key )
113 $this->$key = isset ( $definition [ $key ] ) ? $definition [ $key ] : '' ;
115 $this->definition = $definition ;
119 * Get the unique name of this relationship
120 * @return string The unique name (actually just that given to the constructor)
122 public function getName ()
124 return isset ( $this->definition [ 'relationship_name' ] ) ? $this->definition [ 'relationship_name' ] : null ;
127 public function setName ($relationshipName)
129 $this->relationship_name = $this->definition [ 'relationship_name' ] = $relationshipName ;
133 * Is this relationship readonly or not?
134 * @return boolean True if cannot be changed; false otherwise
136 public function readonly ()
138 return $this->definition [ 'readonly' ] ;
141 public function setReadonly ($set = true)
143 $this->readonly = $this->definition [ 'readonly' ] = $set ;
146 public function setFromStudio ()
148 $this->from_studio = $this->definition [ 'from_studio' ] = true ;
152 * Has this relationship been deleted? A deleted relationship does not get built, and is no longer visible in the list of relationships
153 * @return boolean True if it has been deleted; false otherwise
155 public function deleted ()
157 return $this->definition [ 'deleted' ] ;
160 public function delete ()
162 $this->deleted = $this->definition [ 'deleted' ] = true ;
165 public function getFromStudio()
167 return $this->from_studio;
170 public function getLhsModule()
172 return $this->lhs_module;
175 public function getRhsModule()
177 return $this->rhs_module;
180 public function getType ()
182 return $this->relationship_type ;
185 public function relationship_only ()
187 return $this->definition [ 'relationship_only' ] ;
190 public function setRelationship_only ()
192 $this->relationship_only = $this->definition [ 'relationship_only' ] = true ;
196 * Get a complete description of this relationship, sufficient to pass back to a constructor to reestablish the relationship
197 * Each subclass must provide enough information in $this->definition for its constructor
198 * Used by UndeployedRelationships to save out a set of AbstractRelationship descriptions
199 * The format is the same as the schema for the Relationships table for convenience, and is defined in self::keys. That is,
200 * `relationship_name`, `lhs_module`, `lhs_table`, `lhs_key`, `rhs_module`, `rhs_table`,`rhs_key`, `join_table`, `join_key_lhs`, `join_key_rhs`, `relationship_type`, `relationship_role_column`, `relationship_role_column_value`, `reverse`,
201 * @return array Set of parameters to pass to an AbstractRelationship constructor - must contain at least ['relationship_type']='OneToOne' or 'OneToMany' or 'ManyToMany'
203 function getDefinition ()
205 return $this->definition ;
209 * BUILD methods called during the build
213 * Define the labels to be added to the module for the new relationships
214 * @return array An array of system value => display value
216 function buildLabels ($update=false)
218 $labelDefinitions = array ( ) ;
219 if (!$this->relationship_only)
221 if(!$this->is_custom && $update && file_exists("modules/{$this->rhs_module}/metadata/subpaneldefs.php")){
222 include("modules/{$this->rhs_module}/metadata/subpaneldefs.php");
223 if(isset($layout_defs[$this->rhs_module]['subpanel_setup'][strtolower($this->lhs_module)]['title_key'])){
224 $rightSysLabel = $layout_defs[$this->rhs_module]['subpanel_setup'][strtolower($this->lhs_module)]['title_key'];
226 $layout_defs = array();
228 if(!$this->is_custom && $update && file_exists("modules/{$this->lhs_module}/metadata/subpaneldefs.php")){
229 include("modules/{$this->lhs_module}/metadata/subpaneldefs.php");
230 if(isset($layout_defs[$this->lhs_module]['subpanel_setup'][strtolower($this->rhs_module)]['title_key'])){
231 $leftSysLabel = $layout_defs[$this->lhs_module]['subpanel_setup'][strtolower($this->rhs_module)]['title_key'];
233 $layout_defs = array();
235 $labelDefinitions [] = array (
236 'module' => $this->rhs_module ,
237 'system_label' => isset($rightSysLabel)?$rightSysLabel : 'LBL_' . strtoupper ( $this->relationship_name . '_FROM_' . $this->getLeftModuleSystemLabel() ) . '_TITLE' ,
238 'display_label' => ($update && !empty($_REQUEST [ 'lhs_label' ] ))?$_REQUEST [ 'lhs_label' ] :(empty($this->lhs_label) ? translate ( $this->lhs_module ) : $this->lhs_label),
240 $labelDefinitions [] = array (
241 'module' => $this->lhs_module ,
242 'system_label' => isset($leftSysLabel)?$leftSysLabel :'LBL_' . strtoupper ( $this->relationship_name . '_FROM_' . $this->getRightModuleSystemLabel() ) . '_TITLE' ,
243 'display_label' => ($update && !empty($_REQUEST [ 'rhs_label' ] ))?$_REQUEST [ 'rhs_label' ] :(empty($this->rhs_label) ? translate ( $this->rhs_module ) : $this->rhs_label),
246 return $labelDefinitions ;
249 function getLeftModuleSystemLabel()
251 if($this->lhs_module == $this->rhs_module){
252 return $this->lhs_module.'_L';
254 return $this->lhs_module;
257 function getRightModuleSystemLabel()
259 if($this->lhs_module == $this->rhs_module){
260 return $this->rhs_module.'_R';
262 return $this->rhs_module;
266 * Returns a key=>value set of labels used in this relationship for use when desplaying the relationship in MB
267 * @return array labels used in this relationship
269 public function getLabels() {
271 $labelDefinitions = $this->buildLabels();
272 foreach($labelDefinitions as $def){
273 $labels[$def['module']][$def['system_label']] = $def['display_label'];
280 * GET methods called by the BUILD methods of the subclasses to construct the relationship metadata
284 * Build a description of a Subpanel that can be turned into an actual Subpanel by saveSubpanelDefinition in the implementation
285 * Note that we assume that the subpanel name we are given is valid - that is, a subpanel definition by that name exists, and that a module won't have attempt to define multiple subpanels with the same name
286 * Among the elements we construct is get_subpanel_data which is used as follows in SugarBean:
287 * $related_field_name = $this_subpanel->get_data_source_name();
288 * $parentbean->load_relationship($related_field_name);
289 * ...where $related_field_name must be the name of a link field that references the Relationship used to obtain the subpanel data
290 * @param string $sourceModule Name of the source module for this field
291 * @param string $relationshipName Name of the relationship
292 * @param string $subpanelName Name of the subpanel provided by the sourceModule
293 * @param string $titleKeyName Name of the subpanel title , if none, we will use the module name as the subpanel title.
295 protected function getSubpanelDefinition ($relationshipName , $sourceModule , $subpanelName, $titleKeyName = '', $source = "")
298 $source = $this->getValidDBName($relationshipName);
299 $subpanelDefinition = array ( ) ;
300 $subpanelDefinition [ 'order' ] = 100 ;
301 $subpanelDefinition [ 'module' ] = $sourceModule ;
302 $subpanelDefinition [ 'subpanel_name' ] = $subpanelName ;
303 // following two lines are required for the subpanel pagination code in ListView.php->processUnionBeans() to correctly determine the relevant field for sorting
304 $subpanelDefinition [ 'sort_order' ] = 'asc' ;
305 $subpanelDefinition [ 'sort_by' ] = 'id' ;
306 if(!empty($titleKeyName)){
307 $subpanelDefinition [ 'title_key' ] = 'LBL_' . strtoupper ( $relationshipName . '_FROM_' . $titleKeyName ) . '_TITLE' ;
309 $subpanelDefinition [ 'title_key' ] = 'LBL_' . strtoupper ( $relationshipName . '_FROM_' . $sourceModule ) . '_TITLE' ;
311 $subpanelDefinition [ 'get_subpanel_data' ] = $source ;
312 $subpanelDefinition [ 'top_buttons' ] = array(
313 array('widget_class' => "SubPanelTopButtonQuickCreate"),
314 array('widget_class' => 'SubPanelTopSelectButton', 'mode'=>'MultiSelect')
317 return array ( $subpanelDefinition );
323 * Construct a first link id field for the relationship for use in Views
324 * It is used during the save from an edit view in SugarBean->save_relationship_changes(): for each relate field, $this->$linkfieldname->add( $this->$def['id_name'] )
325 * @param string $sourceModule Name of the source module for this field
326 * @param string $relationshipName Name of the relationship
328 protected function getLinkFieldDefinition ($sourceModule , $relationshipName, $right_side = false, $vname = "", $id_name = false)
330 $vardef = array ( ) ;
332 $vardef [ 'name' ] = $this->getValidDBName($relationshipName) ;
333 $vardef [ 'type' ] = 'link' ;
334 $vardef [ 'relationship' ] = $relationshipName ;
335 $vardef [ 'source' ] = 'non-db' ;
337 $vardef [ 'side' ] = 'right' ;
339 $vardef [ 'vname' ] = $vname;
340 if (!empty($id_name))
341 $vardef['id_name'] = $id_name;
347 * Construct a second link id field for the relationship for use in Views
348 * It is used in two places:
349 * - the editview.tpl for Relate fields requires that a field with the same name as the relate field's id_name exists
350 * - it is loaded in SugarBean->fill_in_link_field while SugarBean processes the relate fields in fill_in_relationship_fields
351 * @param string $sourceModule Name of the source module for this field
352 * @param string $relationshipName Name of the relationship
354 protected function getLink2FieldDefinition ($sourceModule , $relationshipName, $right_side = false, $vname = "")
356 $vardef = array ( ) ;
358 $vardef [ 'name' ] = $this->getIDName( $sourceModule ) ; // must match the id_name field value in the relate field definition
359 $vardef [ 'type' ] = 'link' ;
360 $vardef [ 'relationship' ] = $relationshipName ;
361 $vardef [ 'source' ] = 'non-db' ;
362 $vardef ['reportable'] = false;
364 $vardef [ 'side' ] = 'right' ;
366 $vardef [ 'side' ] = 'left' ;
368 $vardef [ 'vname' ] = $vname;
374 * Construct a relate field for the vardefs
375 * The relate field is the element that is shown in the UI
376 * @param string $sourceModule Name of the source module for this field
377 * @param string $relationshipName Name of the relationship
378 * @param string $moduleType Optional - "Types" of the module - array of SugarObject types such as "file" or "basic"
380 protected function getRelateFieldDefinition ($sourceModule , $relationshipName , $vnameLabel='')
382 $vardef = array ( ) ;
383 $vardef [ 'name' ] = $this->getValidDBName($relationshipName . "_name") ; // must end in _name for the QuickSearch code in TemplateHandler->createQuickSearchCode
384 $vardef [ 'type' ] = 'relate' ;
386 $vardef [ 'source' ] = 'non-db' ;
387 if(!empty($vnameLabel)){
388 $vardef [ 'vname' ] = 'LBL_' . strtoupper ( $relationshipName . '_FROM_' . $vnameLabel ) . '_TITLE' ;
390 $vardef [ 'vname' ] = 'LBL_' . strtoupper ( $relationshipName . '_FROM_' . $sourceModule ) . '_TITLE' ;
393 $vardef [ 'save' ] = true; // the magic value to tell SugarBean to save this relate field even though it is not listed in the $relationship_fields array
395 // id_name matches the join_key_ column in the relationship table for the sourceModule - that is, the column in the relationship table containing the id of the corresponding field in the source module's table (vardef['table'])
396 $vardef [ 'id_name' ] = $this->getIDName( $sourceModule ) ;
398 // link cannot match id_name otherwise the $bean->$id_name value set from the POST is overwritten by the Link object created by this 'link' entry
399 $vardef [ 'link' ] = $this->getValidDBName($relationshipName) ; // the name of the link field that points to the relationship - required for the save to function
400 $vardef [ 'table' ] = $this->getTablename( $sourceModule ) ;
401 $vardef [ 'module' ] = $sourceModule ;
403 require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php' ;
404 $parsedModuleName = AbstractRelationships::parseDeployedModuleName( $sourceModule ) ;
406 // now determine the appropriate 'rname' field for this relate
407 // the 'rname' points to the field in source module that contains the displayable name for the record
408 // usually this is 'name' but sometimes it is not...
410 $vardef [ 'rname' ] = 'name' ;
411 if ( isset( $parsedModuleName['packageName'] ) )
413 require_once 'modules/ModuleBuilder/MB/ModuleBuilder.php' ;
414 $mb = new ModuleBuilder ( ) ;
415 $module = $mb->getPackageModule ( $parsedModuleName['packageName'] , $parsedModuleName['moduleName'] ) ;
416 if (in_array( 'file' , array_keys ( $module->config [ 'templates' ] ) ) ){
417 $vardef [ 'rname' ] = 'document_name' ;
418 }elseif(in_array ( 'person' , array_keys ( $module->config [ 'templates' ] ) ) ){
419 $vardef [ 'db_concat_fields' ] = array( 0 =>'first_name', 1 =>'last_name') ;
424 switch ( strtolower( $sourceModule ) )
427 $vardef [ 'rname' ] = 'account_name' ;
430 $vardef [ 'rname' ] = 'document_name' ;
433 $vardef [ 'rname' ] = 'kbdocument_name' ;
437 // special handling as these modules lack a name column in the database; instead 'name' refers to a non-db field that concatenates first_name and last_name
438 // luckily, the relate field mechanism can handle this with an equivalent additional db_concat_fields entry
439 $vardef [ 'rname' ] = 'name' ;
440 $vardef [ 'db_concat_fields' ] = array( 0 =>'first_name', 1 =>'last_name') ;
443 // now see if we have any module inheriting from the 'file' template - records in file-type modules are named by the document_name field, not the usual 'name' field
444 $object = $GLOBALS ['beanList'] [ $sourceModule ];
445 require_once ( $GLOBALS ['beanFiles'] [ $object ] );
446 $bean = new $object();
447 if ( isset ( $GLOBALS [ 'dictionary' ] [ $object ] [ 'templates'] )){
448 if(in_array ( 'file' , $GLOBALS [ 'dictionary' ] [ $object ] [ 'templates'] )){
449 $vardef [ 'rname' ] = 'document_name' ;
450 }elseif(in_array ( 'person' , $GLOBALS [ 'dictionary' ] [ $object ] [ 'templates'] )){
451 $vardef [ 'db_concat_fields' ] = array( 0 =>'first_name', 1 =>'last_name') ;
463 * Construct the contents of the Relationships MetaData entry in the dictionary for a generic relationship
464 * The entry we build will have three sections:
465 * 1. relationships: the relationship definition
466 * 2. table: name of the join table for this many-to-many relationship
467 * 3. fields: fields within the join table
468 * 4. indicies: indicies on the join table
469 * @param string $relationshipType Cardinality of the relationship, for example, MB_ONETOONE or MB_ONETOMANY or MB_MANYTOMANY
470 * @param bool $checkExisting check if a realtionship with the given name is already depolyed in this instance. If so, we will clones its table and column names to preserve existing data.
472 function getRelationshipMetaData ($relationshipType, $checkExisting = true)
475 $relationshipName = $this->definition [ 'relationship_name' ] ;
476 $lhs_module = $this->lhs_module ;
477 $rhs_module = $this->rhs_module ;
479 $lhs_table = $this->getTablename ( $lhs_module ) ;
480 $rhs_table = $this->getTablename ( $rhs_module ) ;
482 $properties = array ( ) ;
485 if ($checkExisting && !empty($dictionary[$relationshipName])
486 && !empty($dictionary[$relationshipName][ 'true_relationship_type' ])
487 && $dictionary[$relationshipName][ 'true_relationship_type' ] == $relationshipType
488 && !empty($dictionary[$relationshipName]['relationships'][$relationshipName]))
491 $properties [ 'true_relationship_type' ] = $relationshipType ;
492 $rel_properties = $dictionary[$relationshipName]['relationships'][$relationshipName];
495 // first define section 1, the relationship element of the metadata entry
497 $rel_properties = array ( ) ;
498 $rel_properties [ 'lhs_module' ] = $lhs_module ;
499 $rel_properties [ 'lhs_table' ] = $lhs_table ;
500 $rel_properties [ 'lhs_key' ] = 'id' ;
501 $rel_properties [ 'rhs_module' ] = $rhs_module ;
502 $rel_properties [ 'rhs_table' ] = $rhs_table ;
503 $rel_properties [ 'rhs_key' ] = 'id' ;
505 // because the implementation of one-to-many relationships within SugarBean does not use a join table and so requires schema changes to add a foreign key for each new relationship,
506 // we currently implement all new relationships as many-to-many regardless of the real type and enforce cardinality through the relate fields and subpanels
507 $rel_properties [ 'relationship_type' ] = MB_MANYTOMANY ;
508 // but as we need to display the true cardinality in Studio and ModuleBuilder we also record the actual relationship type
509 // this property is only used by Studio/MB
510 $properties [ 'true_relationship_type' ] = $relationshipType ;
511 if ($this->from_studio)
512 $properties [ 'from_studio' ] = true;
514 $rel_properties [ 'join_table' ] = $this->getValidDBName ( $relationshipName."_c" ) ;
515 // a and b are in case the module relates to itself
516 $rel_properties [ 'join_key_lhs' ] = $this->getJoinKeyLHS() ;
517 $rel_properties [ 'join_key_rhs' ] = $this->getJoinKeyRHS() ;
520 // set the extended properties if they exist = for now, many-to-many definitions do not have to contain a role_column even if role_column_value is set; we'll just create a likely name if missing
521 if (isset ( $this->definition [ 'relationship_role_column_value' ] ))
523 if (! isset ( $this->definition [ 'relationship_role_column' ] ))
524 $this->definition [ 'relationship_role_column' ] = 'relationship_role_column' ;
525 $rel_properties [ 'relationship_role_column' ] = $this->definition [ 'relationship_role_column' ] ;
526 $rel_properties [ 'relationship_role_column_value' ] = $this->definition [ 'relationship_role_column_value' ] ;
529 $properties [ 'relationships' ] [ $relationshipName ] = $rel_properties ;
531 // construct section 2, the name of the join table
533 $properties [ 'table' ] = $rel_properties [ 'join_table' ] ;
535 // now construct section 3, the fields in the join table
537 $properties [ 'fields' ] [] = array ( 'name' => 'id' , 'type' => 'varchar' , 'len' => 36 ) ;
538 $properties [ 'fields' ] [] = array ( 'name' => 'date_modified' , 'type' => 'datetime' ) ;
539 $properties [ 'fields' ] [] = array ( 'name' => 'deleted' , 'type' => 'bool' , 'len' => '1' , 'default' => '0' , 'required' => true ) ;
540 $properties [ 'fields' ] [] = array ( 'name' => $rel_properties [ 'join_key_lhs' ] , 'type' => 'varchar' , 'len' => 36 ) ;
541 $properties [ 'fields' ] [] = array ( 'name' => $rel_properties [ 'join_key_rhs' ] , 'type' => 'varchar' , 'len' => 36 ) ;
542 if (strtolower ( $lhs_module ) == 'documents' || strtolower ( $rhs_module ) == 'documents' )
544 $properties [ 'fields' ] [] = array ( 'name' => 'document_revision_id' , 'type' => 'varchar' , 'len' => '36' ) ;
546 // if we have an extended relationship condition, then add in the corresponding relationship_role_column to the relationship (join) table
547 // for now this is restricted to extended relationships that can be specified by a varchar
548 if (isset ( $this->definition [ 'relationship_role_column_value' ] ))
550 $properties [ 'fields' ] [] = array ( 'name' => $this->definition [ 'relationship_role_column' ] , 'type' => 'varchar' ) ;
553 // finally, wrap up with section 4, the indices on the join table
555 $indexBase = $this->getValidDBName ( $relationshipName ) ;
556 $properties [ 'indices' ] [] = array ( 'name' => $indexBase . 'spk' , 'type' => 'primary' , 'fields' => array ( 'id' ) ) ;
558 switch ($relationshipType)
561 $alternateKeys = array () ;
562 $properties [ 'indices' ] [] = array ( 'name' => $indexBase . '_ida1' , 'type' => 'index' , 'fields' => array ( $rel_properties [ 'join_key_lhs' ] ) ) ;
563 $properties [ 'indices' ] [] = array ( 'name' => $indexBase . '_idb2' , 'type' => 'index' , 'fields' => array ( $rel_properties [ 'join_key_rhs' ] ) ) ;
566 $alternateKeys = array ( $rel_properties [ 'join_key_rhs' ] ) ;
567 $properties [ 'indices' ] [] = array ( 'name' => $indexBase . '_ida1' , 'type' => 'index' , 'fields' => array ( $rel_properties [ 'join_key_lhs' ] ) ) ;
570 $alternateKeys = array ( $rel_properties [ 'join_key_lhs' ] , $rel_properties [ 'join_key_rhs' ] ) ;
573 if (count($alternateKeys)>0)
574 $properties [ 'indices' ] [] = array ( 'name' => $indexBase . '_alt' , 'type' => 'alternate_key' , 'fields' => $alternateKeys ) ; // type must be set to alternate_key for Link.php to correctly update an existing record rather than inserting a copy - it uses the fields in this array as the keys to check if a duplicate record already exists
585 * Method to build a name for a relationship between a module and an Activities submodule
586 * Used primarily in UndeployedRelationships to ensure that the subpanels we construct for Activities get their data from the correct relationships
587 * @param string $activitiesSubModuleName Name of the activities submodule, such as Tasks
589 function getActivitiesSubModuleRelationshipName ( $activitiesSubModuleName )
591 return $this->lhs_module . "_" . strtolower ( $activitiesSubModuleName ) ;
595 * Return a version of $proposed that can be used as a column name in any of our supported databases
596 * Practically this means no longer than 25 characters as the smallest identifier length for our supported DBs is 30 chars for Oracle plus we add on at least four characters in some places (for indicies for example)
597 * TODO: Ideally this should reside in DBHelper as it is such a common db function...
598 * @param string $name Proposed name for the column
599 * @param string $ensureUnique
600 * @return string Valid column name trimmed to right length and with invalid characters removed
602 static function getValidDBName ($name, $ensureUnique = true)
605 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
606 return getValidDBName($name, $ensureUnique, MB_MAXDBIDENTIFIERLENGTH);
610 * Tidy up any provided relationship type - convert all the variants of a name to the canonical type - for example, One To Many = MB_ONETOMANY
611 * @param string $type Relationship type
612 * @return string Canonical type
614 static function parseRelationshipType ($type)
616 $type = strtolower ( $type ) ;
617 $type = preg_replace ( '/[^\w]+/i', '', strtolower ( $type ) ) ;
618 $canonicalTypes = array ( ) ;
619 foreach ( array ( MB_ONETOONE , MB_ONETOMANY , MB_MANYTOMANY , MB_MANYTOONE) as $canonicalType )
621 if ($type == preg_replace ( '/[^\w]+/i', '', strtolower ( $canonicalType ) ))
622 return $canonicalType ;
625 return MB_MANYTOMANY ;
629 function getJoinKeyLHS()
631 if (!isset($this->joinKeyLHS))
632 $this->joinKeyLHS = $this->getValidDBName ( $this->relationship_name . $this->lhs_module . "_ida" , true) ;
634 return $this->joinKeyLHS;
637 function getJoinKeyRHS()
639 if (!isset($this->joinKeyRHS))
640 $this->joinKeyRHS = $this->getValidDBName ( $this->relationship_name . $this->rhs_module . "_idb" , true) ;
642 return $this->joinKeyRHS;
646 * Return the name of the ID field that will be used to link the subpanel, the link field and the relationship metadata
647 * @param string $sourceModule The name of the primary module in the relationship
648 * @return string Name of the id field
650 function getIDName( $sourceModule )
652 return ($sourceModule == $this->lhs_module ) ? $this->getJoinKeyLHS() : $this->getJoinKeyRHS() ;
656 * Return the name of a module's standard (non-cstm) table in the database
657 * @param string $moduleName Name of the module for which we are to find the table
658 * @return string Tablename
660 protected function getTablename ($moduleName)
662 // Check the moduleName exists in the beanList before calling get_module_info - Activities is the main culprit here
663 if (isset ( $GLOBALS [ 'beanList' ] [ $moduleName ] ))
665 $module = get_module_info ( $moduleName ) ;
666 return $module->table_name ;
668 return strtolower ( $moduleName ) ;
671 public function getTitleKey($left=false){
672 if(!$this->is_custom && !$left && file_exists("modules/{$this->rhs_module}/metadata/subpaneldefs.php")){
673 include("modules/{$this->rhs_module}/metadata/subpaneldefs.php");
674 if(isset($layout_defs[$this->rhs_module]['subpanel_setup'][strtolower($this->lhs_module)]['title_key'])){
675 return $layout_defs[$this->rhs_module]['subpanel_setup'][strtolower($this->lhs_module)]['title_key'];
677 }else if(!$this->is_custom && file_exists("modules/{$this->lhs_module}/metadata/subpaneldefs.php")){
678 include("modules/{$this->lhs_module}/metadata/subpaneldefs.php");
679 if(isset($layout_defs[$this->lhs_module]['subpanel_setup'][strtolower($this->rhs_module)]['title_key'])){
680 return $layout_defs[$this->lhs_module]['subpanel_setup'][strtolower($this->rhs_module)]['title_key'];
685 $titleKeyName = $this->getRightModuleSystemLabel();
686 $sourceModule = $this->rhs_module;
688 $titleKeyName = $this->getLeftModuleSystemLabel();
689 $sourceModule = $this->lhs_module;
692 if(!empty($titleKeyName)){
693 $title_key = 'LBL_' . strtoupper ( $this->relationship_name . '_FROM_' . $titleKeyName ) . '_TITLE' ;
695 $title_key = 'LBL_' . strtoupper ( $this->relationship_name . '_FROM_' . $sourceModule ) . '_TITLE' ;