/**
  * @static
  * @return SugarRelationshipFactory
  */
 public static function getInstance()
 {
     if (is_null(self::$rfInstance)) {
         self::$rfInstance = new SugarRelationshipFactory();
     }
     return self::$rfInstance;
 }
Example #2
0
 function action_DeleteRelationship()
 {
     if (isset($_REQUEST['relationship_name'])) {
         if (empty($_REQUEST['view_package'])) {
             require_once 'modules/ModuleBuilder/parsers/relationships/DeployedRelationships.php';
             if (!empty($_REQUEST['remove_tables'])) {
                 $GLOBALS['mi_remove_tables'] = $_REQUEST['remove_tables'];
             }
             $relationships = new DeployedRelationships($_REQUEST['view_module']);
         } else {
             $mb = new ModuleBuilder();
             $module =& $mb->getPackageModule($_REQUEST['view_package'], $_REQUEST['view_module']);
             require_once 'modules/ModuleBuilder/parsers/relationships/UndeployedRelationships.php';
             $relationships = new UndeployedRelationships($module->getModuleDir());
         }
         $relationships->delete($_REQUEST['relationship_name']);
         $relationships->save();
         require_once "data/Relationships/RelationshipFactory.php";
         SugarRelationshipFactory::deleteCache();
     }
     $this->view = 'relationships';
 }
Example #3
0
 /**
  * Updates relationships based on changes to fields of type 'parent' which
  * may or may not have links associated with them
  *
  * @param array $exclude
  */
 protected function update_parent_relationships($exclude = array())
 {
     foreach ($this->field_defs as $def) {
         if (!empty($def['type']) && $def['type'] == "parent") {
             if (empty($def['type_name']) || empty($def['id_name'])) {
                 continue;
             }
             $typeField = $def['type_name'];
             $idField = $def['id_name'];
             if (in_array($idField, $exclude)) {
                 continue;
             }
             //Determine if the parent field has changed.
             if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && (empty($this->{$typeField}) || empty($this->{$idField})) || !empty($this->{$typeField}) && !empty($this->{$idField}) && (empty($this->fetched_row[$typeField]) || empty($this->fetched_row[$idField]) || $this->fetched_row[$idField] != $this->{$idField}) || $this->deleted == 1) {
                 $parentLinks = array();
                 //Correlate links to parent field module types
                 foreach ($this->field_defs as $ldef) {
                     if (!empty($ldef['type']) && $ldef['type'] == "link" && !empty($ldef['relationship'])) {
                         $relDef = SugarRelationshipFactory::getInstance()->getRelationshipDef($ldef['relationship']);
                         if (!empty($relDef['relationship_role_column']) && $relDef['relationship_role_column'] == $typeField) {
                             $parentLinks[$relDef['lhs_module']] = $ldef;
                         }
                     }
                 }
                 // Save $this->$idField, because it can be resetted in case of link->delete() call
                 $idFieldVal = $this->{$idField};
                 //If we used to have a parent, call remove on that relationship
                 if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && !empty($parentLinks[$this->fetched_row[$typeField]]) && $this->fetched_row[$idField] != $this->{$idField}) {
                     $oldParentLink = $parentLinks[$this->fetched_row[$typeField]]['name'];
                     //Load the relationship
                     if ($this->load_relationship($oldParentLink)) {
                         $this->{$oldParentLink}->delete($this->fetched_row[$idField]);
                         // Should resave the old parent
                         SugarRelationship::addToResaveList(BeanFactory::getBean($this->fetched_row[$typeField], $this->fetched_row[$idField]));
                     }
                 }
                 // If both parent type and parent id are set, save it unless the bean is being deleted
                 if (!empty($this->{$typeField}) && !empty($idFieldVal) && !empty($parentLinks[$this->{$typeField}]['name']) && $this->deleted != 1) {
                     //Now add the new parent
                     $parentLink = $parentLinks[$this->{$typeField}]['name'];
                     if ($this->load_relationship($parentLink)) {
                         $this->{$parentLink}->add($idFieldVal);
                     }
                 }
             }
         }
     }
 }
Example #4
0
 /**
  * The collector method for relationships.
  *
  * @return array An array of relationships, indexed by the relationship name
  */
 public function getRelationshipData()
 {
     $relFactory = SugarRelationshipFactory::getInstance();
     // Request fresh relationship metadata always
     $data = $relFactory->getRelationshipDefs(true);
     // Sanity check the rel defs, just in case they came back empty
     if (is_array($data)) {
         // Certain elements of the relationship defs need to be pruned
         $unsets = array('table', 'fields', 'indices', 'relationships');
         foreach ($data as $relKey => $relData) {
             // Prune the relationship defs as needed
             foreach ($unsets as $unset) {
                 unset($relData[$unset]);
             }
             // Sort each def array for consistency to ensure sameness between
             // metadata cache refreshes
             ksort($relData);
             // Reset the defs for this key
             $data[$relKey] = $relData;
         }
     }
     // To maintain hashes between requests, make sure this array is always
     // in the same order. Otherwise, the serialized value of this data will
     // potentially be different from one request to another.
     ksort($data);
     $data["_hash"] = $this->hashChunk($data);
     return $data;
 }
Example #5
0
 private function deleteRelationship($lhs_module, $rhs_module = null, $rel_name = null)
 {
     $rhs_module = $rhs_module == null ? $lhs_module : $rhs_module;
     $this->relationships = new DeployedRelationships($lhs_module);
     $this->relationships->delete($rel_name !== null ? $rel_name : $this->relationship->getName());
     $this->relationships->save();
     SugarRelationshipFactory::deleteCache();
     LanguageManager::clearLanguageCache($lhs_module);
     if ($lhs_module != $rhs_module) {
         LanguageManager::clearLanguageCache($rhs_module);
     }
 }
Example #6
0
 /**
  * @param  $linkName String name of a link field in the module's vardefs
  * @param  $bean SugarBean focus bean for this link (one half of a relationship)
  * @param  $linkDef Array Optional vardef for the link in case it can't be found in the passed in bean for the global dictionary
  * @return void
  *
  */
 function __construct($linkName, $bean, $linkDef = false)
 {
     $this->focus = $bean;
     //Try to load the link vardef from the beans field defs. Otherwise start searching
     if (empty($bean->field_defs) || empty($bean->field_defs[$linkName]) || empty($bean->field_defs[$linkName]['relationship'])) {
         if (empty($linkDef)) {
             //Assume $linkName is really relationship_name, and find the link name with the vardef manager
             $this->def = VardefManager::getLinkFieldForRelationship($bean->module_dir, $bean->object_name, $linkName);
         } else {
             $this->def = $linkDef;
         }
         //Check if multiple links were found for a given relationship
         if (is_array($this->def) && !isset($this->def['name'])) {
             //More than one link found, we need to figure out if we are currently on the LHS or RHS
             //default to lhs for now
             if (isset($this->def[0]['side']) && $this->def[0]['side'] == 'left') {
                 $this->def = $this->def[0];
             } else {
                 if (isset($this->def[1]['side']) && $this->def[1]['side'] == 'left') {
                     $this->def = $this->def[1];
                 } else {
                     $this->def = $this->def[0];
                 }
             }
         }
         if (empty($this->def['name'])) {
             $GLOBALS['log']->fatal("failed to find link for {$linkName}");
             return false;
         }
         $this->name = $this->def['name'];
     } else {
         //Linkdef was found in the bean (this is the normal expectation)
         $this->def = $bean->field_defs[$linkName];
         $this->name = $linkName;
     }
     //Instantiate the relationship for this link.
     $this->relationship = SugarRelationshipFactory::getInstance()->getRelationship($this->def['relationship']);
     // Fix to restore functionality from Link.php that needs to be rewritten but for now this will do.
     $this->relationship_fields = !empty($this->def['rel_fields']) ? $this->def['rel_fields'] : array();
     if (!$this->loadedSuccesfully()) {
         global $app_strings;
         $GLOBALS['log']->error(string_format($app_strings['ERR_DATABSE_RELATIONSHIP_QUERY'], array($this->name, $this->def['relationship'])));
     }
     //Following behavior is tied to a property(ignore_role) value in the vardef. It alters the values of 2 properties, ignore_role_filter and add_distinct.
     //the property values can be altered again before any requests are made.
     if (!empty($this->def) && is_array($this->def)) {
         if (isset($this->def['ignore_role'])) {
             if ($this->def['ignore_role']) {
                 $this->ignore_role_filter = true;
                 $this->add_distinct = true;
             }
         }
         if (!empty($this->def['primary_only'])) {
             $this->relationship->primaryOnly = true;
         }
     }
 }
 /**
  * Remove all created relationships
  *
  * @static
  */
 public static function removeAllCreatedRelationships()
 {
     foreach (self::$_relsAdded as $rel) {
         $relationships = new DeployedRelationships($rel['lhs_module']);
         $relationships->delete($rel['relationship_name']);
         $relationships->save();
         $relationships->build();
         LanguageManager::clearLanguageCache($rel['lhs_module']);
         require_once "data/Relationships/RelationshipFactory.php";
         SugarRelationshipFactory::deleteCache();
         SugarRelationshipFactory::rebuildCache();
     }
     // since we are creating a relationship we need to unset this global var
     if (isset($GLOBALS['reload_vardefs'])) {
         unset($GLOBALS['reload_vardefs']);
     }
 }
Example #8
0
////    START RELATIONSHIP CREATION
ksort($rel_dictionary);
foreach ($rel_dictionary as $rel_name => $rel_data) {
    $table = $rel_data['table'];
    if ($setup_db_drop_tables) {
        if ($db->tableExists($table)) {
            $db->dropTableName($table);
        }
    }
    if (!$db->tableExists($table)) {
        $db->createTableParams($table, $rel_data['fields'], isset($rel_data['indices']) ? $rel_data['indices'] : array());
    }
    SugarBean::createRelationshipMeta($rel_name, $db, $table, $rel_dictionary, '');
}
// Setup the relationship cache and build out the initial vardef cache
SugarRelationshipFactory::rebuildCache();
///////////////////////////////////////////////////////////////////////////////
////    START CREATE DEFAULTS
echo "<br>";
echo "<b>{$mod_strings['LBL_PERFORM_CREATE_DEFAULT']}</b><br>";
echo "<br>";
installLog("Begin creating Defaults");
installerHook('pre_createDefaultSettings');
if ($new_config) {
    installLog("insert defaults into config table");
    insert_default_settings();
}
installerHook('post_createDefaultSettings');
echo $line_entry_format . $mod_strings['LBL_PERFORM_LICENSE_SETTINGS'] . $line_exit_format;
installLog($mod_strings['LBL_PERFORM_LICENSE_SETTINGS']);
update_license_settings($_SESSION['setup_license_key_users'], $_SESSION['setup_license_key_expire_date'], $_SESSION['setup_license_key'], $_SESSION['setup_num_lic_oc']);
Example #9
0
 /**
  * Updates relationships based on changes to fields of type 'parent' which
  * may or may not have links associated with them
  *
  * @param array $exclude
  */
 protected function update_parent_relationships($exclude = array())
 {
     foreach ($this->field_defs as $def) {
         if (!empty($def['type']) && $def['type'] == "parent") {
             if (empty($def['type_name']) || empty($def['id_name'])) {
                 continue;
             }
             $typeField = $def['type_name'];
             $idField = $def['id_name'];
             // save the new id
             $newIdValue = $this->{$idField};
             if (in_array($idField, $exclude)) {
                 continue;
             }
             //Determine if the parent field has changed.
             if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && (empty($this->{$typeField}) || empty($this->{$idField})) || !empty($this->{$typeField}) && !empty($this->{$idField}) && (empty($this->fetched_row[$typeField]) || empty($this->fetched_row[$idField]) || $this->fetched_row[$idField] != $this->{$idField}) || $this->deleted == 1) {
                 $parentLinks = array();
                 //Correlate links to parent field module types
                 foreach ($this->field_defs as $ldef) {
                     if (!empty($ldef['type']) && $ldef['type'] == "link" && !empty($ldef['relationship'])) {
                         $rel = SugarRelationshipFactory::getInstance()->getRelationship($ldef['relationship']);
                         $relColumns = $rel->getRelationshipRoleColumns();
                         if (isset($relColumns[$typeField])) {
                             $parentLinks[$rel->getLHSModule()] = $ldef;
                         }
                     }
                 }
                 //If we used to have a parent, call remove on that relationship
                 if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && !empty($parentLinks[$this->fetched_row[$typeField]]) && $this->fetched_row[$idField] != $this->{$idField}) {
                     $oldParentLink = $parentLinks[$this->fetched_row[$typeField]]['name'];
                     //Load the relationship
                     if ($this->load_relationship($oldParentLink)) {
                         $this->{$oldParentLink}->delete($this->id, $this->fetched_row[$idField]);
                         // Should resave the old parent, if the current user has access to it and can save it
                         $beanToSave = BeanFactory::getBean($this->fetched_row[$typeField], $this->fetched_row[$idField]);
                         if (!empty($beanToSave->id)) {
                             SugarRelationship::addToResaveList($beanToSave);
                         }
                     }
                 }
                 // If both parent type and parent id are set, save it unless the bean is being deleted
                 if (!empty($this->{$typeField}) && !empty($newIdValue) && !empty($parentLinks[$this->{$typeField}]['name']) && $this->deleted != 1) {
                     //Now add the new parent
                     $parentLink = $parentLinks[$this->{$typeField}]['name'];
                     if ($this->load_relationship($parentLink)) {
                         $this->{$parentLink}->add($newIdValue);
                     }
                 }
             }
         }
     }
 }
 function build()
 {
     $modulesToBuild = array();
     if (!isset($this->relationships[$this->newRelationshipName])) {
         $GLOBALS['log']->fatal("Could not find a relationship by the name of {$this->newRelationshipName}, you will have to quick repair and rebuild to get the new relationship added.");
     } else {
         $newRel = $this->relationships[$this->newRelationshipName];
         $newRelDef = $newRel->getDefinition();
         $modulesToBuild[$newRelDef['rhs_module']] = true;
         $modulesToBuild[$newRelDef['lhs_module']] = true;
     }
     $basepath = "custom/Extension/modules";
     $this->activitiesToAdd = false;
     // and mark all as built so that the next time we come through we'll know and won't build again
     foreach ($this->relationships as $name => $relationship) {
         if ($relationship->readonly()) {
             continue;
         }
         $definition = $relationship->getDefinition();
         // activities will always appear on the rhs only - lhs will be always be this module in MB
         if (strtolower($definition['rhs_module']) == 'activities') {
             $this->activitiesToAdd = true;
             $relationshipName = $definition['relationship_name'];
             foreach (self::$activities as $activitiesSubModuleLower => $activitiesSubModuleName) {
                 $definition['rhs_module'] = $activitiesSubModuleName;
                 $definition['for_activities'] = true;
                 $definition['relationship_name'] = $relationshipName . '_' . $activitiesSubModuleLower;
                 $this->relationships[$definition['relationship_name']] = RelationshipFactory::newRelationship($definition);
             }
             unset($this->relationships[$name]);
         }
     }
     $GLOBALS['log']->info(get_class($this) . "->build(): installing relationships");
     $MBModStrings = $GLOBALS['mod_strings'];
     $adminModStrings = return_module_language('', 'Administration');
     // required by ModuleInstaller
     foreach ($this->relationships as $name => $relationship) {
         if ($relationship->readonly()) {
             continue;
         }
         $relationship->setFromStudio();
         $GLOBALS['mod_strings'] = $MBModStrings;
         $installDefs = parent::build($basepath, "<basepath>", array($name => $relationship));
         // and mark as built so that the next time we come through we'll know and won't build again
         $relationship->setReadonly();
         $this->relationships[$name] = $relationship;
         // now install the relationship - ModuleInstaller normally only does this as part of a package load where it installs the relationships defined in the manifest. However, we don't have a manifest or a package, so...
         // If we were to chose to just use the Extension mechanism, without using the ModuleInstaller install_...() methods, we must :
         // 1)   place the information for each side of the relationship in the appropriate Ext directory for the module, which means specific $this->save...() methods for DeployedRelationships, and
         // 2)   we must also manually add the relationship into the custom/application/Ext/TableDictionary/tabledictionary.ext.php file as install_relationship doesn't handle that (install_relationships which requires the manifest however does)
         //      Relationships must be in tabledictionary.ext.php for the Admin command Rebuild Relationships to reliably work:
         //      Rebuild Relationships looks for relationships in the modules vardefs.php, in custom/modules/<modulename>/Ext/vardefs/vardefs.ext.php, and in modules/TableDictionary.php and custom/application/Ext/TableDictionary/tabledictionary.ext.php
         //      if the relationship is not defined in one of those four places it could be deleted during a rebuilt, or during a module installation (when RebuildRelationships.php deletes all entries in the Relationships table)
         // So instead of doing this, we use common save...() methods between DeployedRelationships and UndeployedRelationships that will produce installDefs,
         // and rather than building a full manifest file to carry them, we manually add these installDefs to the ModuleInstaller, and then
         // individually call the appropriate ModuleInstaller->install_...() methods to take our relationship out of our staging area and expand it out to the individual module Ext areas
         $GLOBALS['mod_strings'] = $adminModStrings;
         require_once 'ModuleInstall/ModuleInstaller.php';
         $mi = new ModuleInstaller();
         $mi->id_name = 'custom' . $name;
         // provide the moduleinstaller with a unique name for this relationship - normally this value is set to the package key...
         $mi->installdefs = $installDefs;
         $mi->base_dir = $basepath;
         $mi->silent = true;
         VardefManager::clearVardef();
         $mi->install_relationships();
         $mi->install_languages();
         $mi->install_vardefs();
         $mi->install_layoutdefs();
         $mi->install_extensions();
         $mi->install_client_files();
     }
     $GLOBALS['mod_strings'] = $MBModStrings;
     // finally, restore the ModuleBuilder mod_strings
     // Anything that runs in-process needs to reload their vardefs
     $GLOBALS['reload_vardefs'] = true;
     // save out the updated definitions so that we keep track of the change in built status
     // sending false so we don't rebuild relationshsips for a third time.
     $this->save(false);
     $mi = new ModuleInstaller();
     $mi->silent = true;
     $mi->rebuild_relationships($modulesToBuild);
     // now clear all caches so that our changes are visible
     require_once 'modules/Administration/QuickRepairAndRebuild.php';
     $rac = new RepairAndClear();
     $rac->module_list = $modulesToBuild;
     $rac->clearJsFiles();
     $rac->clearVardefs();
     $rac->clearJsLangFiles();
     $rac->clearLanguageCache();
     $rac->rebuildExtensions(array_keys($modulesToBuild));
     $rac->clearVardefs();
     foreach ($rac->module_list as $moduleName => $ignore) {
         // Now rebuild the vardefs in memory
         $bean = BeanFactory::newBean($moduleName);
         VardefManager::loadVardef($bean->module_dir, $bean->object_name, true, array('bean' => $bean));
     }
     foreach (array_keys($modulesToBuild) as $module) {
         unset($GLOBALS['dictionary'][BeanFactory::getObjectName($module)]);
     }
     SugarRelationshipFactory::rebuildCache();
     MetaDataManager::refreshLanguagesCache(array($GLOBALS['current_language']));
     MetaDataManager::refreshSectionCache(array(MetaDataManager::MM_RELATIONSHIPS));
     MetaDataManager::refreshModulesCache(array_keys($modulesToBuild));
     $GLOBALS['log']->info(get_class($this) . "->build(): finished relationship installation");
 }
Example #11
0
 /**
  * @param  $linkName String name of a link field in the module's vardefs
  * @param  $bean SugarBean focus bean for this link (one half of a relationship)
  * @param  $linkDef Array Optional vardef for the link in case it can't be found in the passed in bean for the global dictionary
  * @return void
  *
  */
 function __construct($linkName, $bean, $linkDef = false)
 {
     $this->focus = $bean;
     //Try to load the link vardef from the beans field defs. Otherwise start searching
     if (empty($bean->field_defs) || empty($bean->field_defs[$linkName]) || empty($bean->field_defs[$linkName]['relationship'])) {
         if (empty($linkDef)) {
             //Assume $linkName is really relationship_name, and find the link name with the vardef manager
             $this->def = VardefManager::getLinkFieldForRelationship($bean->module_dir, $bean->object_name, $linkName);
         } else {
             $this->def = $linkDef;
         }
         //Check if multiple links were found for a given relationship
         if (is_array($this->def) && !isset($this->def['name'])) {
             //More than one link found, we need to figure out if we are currently on the LHS or RHS
             //default to lhs for now
             if (isset($this->def[0]['side']) && $this->def[0]['side'] == 'left') {
                 $this->def = $this->def[0];
             } else {
                 if (isset($this->def[1]['side']) && $this->def[1]['side'] == 'left') {
                     $this->def = $this->def[1];
                 } else {
                     $this->def = $this->def[0];
                 }
             }
         }
         if (empty($this->def['name'])) {
             $GLOBALS['log']->fatal("failed to find link for {$linkName}");
             return false;
         }
         $this->name = $this->def['name'];
     } else {
         //Linkdef was found in the bean (this is the normal expectation)
         $this->def = $bean->field_defs[$linkName];
         $this->name = $linkName;
     }
     //Instantiate the relationship for this link.
     $this->relationship = SugarRelationshipFactory::getInstance()->getRelationship($this->def['relationship']);
     if (!$this->loadedSuccesfully()) {
         $GLOBALS['log']->fatal("{$this->name} for {$this->def['relationship']} failed to load\n");
     }
     //Following behavior is tied to a property(ignore_role) value in the vardef. It alters the values of 2 properties, ignore_role_filter and add_distinct.
     //the property values can be altered again before any requests are made.
     if (!empty($this->def) && is_array($this->def)) {
         if (array_key_exists('ignore_role', $this->def)) {
             if ($this->def['ignore_role']) {
                 $this->ignore_role_filter = true;
                 $this->add_distinct = true;
             }
         }
     }
 }
 /**
  * Used internally to determine if a field def is a valid link for use in formulas
  * @static
  * @param  $def array, Link field definition.
  * @return bool true if field is valid.
  */
 protected static function validLinkField($def)
 {
     global $dictionary;
     $invalidModules = array("Emails" => true, "Teams" => true);
     if (empty($def['relationship'])) {
         return false;
         //Not a good link field
     }
     $rel = SugarRelationshipFactory::getInstance()->getRelationship($def['relationship']);
     if ($rel === false) {
         return false;
         //Unable to find a relationship definition
     }
     if (!empty($invalidModules[$rel->lhs_module]) || !empty($invalidModules[$rel->rhs_module])) {
         return false;
         //Invalid module
     }
     //Otherwise this link looks ok
     return true;
 }
Example #13
0
 public static function delete_cache()
 {
     $filename = Relationship::cache_file_dir() . '/' . Relationship::cache_file_name_only();
     if (file_exists($filename)) {
         unlink($filename);
     }
     SugarRelationshipFactory::deleteCache();
     // Delete the internal cache as well
     self::$relCacheInternal = array();
 }
Example #14
0
 /**
  * Find links with wrong relationship.
  * @param DefinitionObject $fieldDefs
  * @param SugarBean $seed
  * @return array Wrong relationships.
  */
 protected function checkFieldsRelationships($fieldDefs, $seed)
 {
     $wrongRelations = array();
     foreach ($fieldDefs as $fieldnm => $field) {
         // Check for bad links
         if ($field['type'] == 'link') {
             $seed->load_relationship($field['name']);
             $wRel = false;
             if (empty($seed->{$field}['name'])) {
                 $wRel = true;
             } else {
                 if ($this->checkRelationshipDef($field['name'], $seed)) {
                     // Need to delete cache of TableDictionary to avoid inclusion of deleted files.
                     if (file_exists('custom/application/Ext/TableDictionary/tabledictionary.ext.php')) {
                         SugarAutoLoader::unlink('custom/application/Ext/TableDictionary/tabledictionary.ext.php');
                     }
                     SugarRelationshipFactory::deleteCache();
                     SugarRelationshipFactory::rebuildCache();
                     unset($seed->{$field}['name']);
                     $seed->load_relationship($field['name']);
                 }
                 $relModule = $seed->{$field}['name']->getRelatedModuleName();
                 $relBean = $this->getBean($relModule);
                 if (empty($relBean)) {
                     $wRel = true;
                 }
             }
             if ($wRel) {
                 if (!empty($field['relationship'])) {
                     $wrongRelations[] = $field['relationship'];
                 }
                 $fieldDefs->setWrongDef($fieldnm);
             }
         }
     }
     return $wrongRelations;
 }
Example #15
0
 public static function delete_cache()
 {
     $filename = Relationship::cache_file_dir() . '/' . Relationship::cache_file_name_only();
     if (file_exists($filename)) {
         unlink($filename);
     }
     require_once "data/Relationships/RelationshipFactory.php";
     SugarRelationshipFactory::deleteCache();
 }
Example #16
0
 public function action_DeleteRelationship()
 {
     if (isset($_REQUEST['relationship_name'])) {
         if (empty($_REQUEST['view_package'])) {
             if (!empty($_REQUEST['remove_tables'])) {
                 $GLOBALS['mi_remove_tables'] = $_REQUEST['remove_tables'];
             }
             $relationships = new DeployedRelationships($_REQUEST['view_module']);
         } else {
             $mb = new ModuleBuilder();
             $module =& $mb->getPackageModule($_REQUEST['view_package'], $_REQUEST['view_module']);
             $relationships = new UndeployedRelationships($module->getModuleDir());
         }
         $relationships->delete($_REQUEST['relationship_name']);
         $relationships->save();
         SugarRelationshipFactory::deleteCache();
     }
     $this->view = 'relationships';
 }
Example #17
0
 function install_relationships()
 {
     if (isset($this->installdefs['relationships'])) {
         $this->log(translate('LBL_MI_IN_RELATIONSHIPS'));
         $str = "<?php \n //WARNING: The contents of this file are auto-generated\n";
         $save_table_dictionary = false;
         if (!file_exists("custom/Extension/application/Ext/TableDictionary")) {
             mkdir_recursive("custom/Extension/application/Ext/TableDictionary", true);
         }
         foreach ($this->installdefs['relationships'] as $key => $relationship) {
             $filename = basename($relationship['meta_data']);
             $this->copy_path($relationship['meta_data'], 'custom/metadata/' . $filename);
             $this->install_relationship('custom/metadata/' . $filename);
             $save_table_dictionary = true;
             if (!empty($relationship['module_vardefs'])) {
                 $relationship['module_vardefs'] = str_replace('<basepath>', $this->base_dir, $relationship['module_vardefs']);
                 $this->install_vardef($relationship['module_vardefs'], $relationship['module']);
             }
             if (!empty($relationship['module_layoutdefs'])) {
                 $relationship['module_layoutdefs'] = str_replace('<basepath>', $this->base_dir, $relationship['module_layoutdefs']);
                 $this->install_layoutdef($relationship['module_layoutdefs'], $relationship['module']);
             }
             $relName = strpos($filename, "MetaData") !== false ? substr($filename, 0, strlen($filename) - 12) : $filename;
             $out = sugar_fopen("custom/Extension/application/Ext/TableDictionary/{$relName}.php", 'w');
             fwrite($out, $str . "include('custom/metadata/{$filename}');\n\n?>");
             fclose($out);
         }
         Relationship::delete_cache();
         $this->rebuild_vardefs();
         $this->rebuild_layoutdefs();
         if ($save_table_dictionary) {
             $this->rebuild_tabledictionary();
         }
         require_once "data/Relationships/RelationshipFactory.php";
         SugarRelationshipFactory::deleteCache();
     }
 }
Example #18
0
 /**
  * Doing the same things like setUp but for initialized list of modules
  *
  * @static
  * @return bool are caches refreshed or not
  */
 protected static function tearDown_relation()
 {
     SugarRelationshipFactory::deleteCache();
     $modules = array_unique(self::$cleanModules);
     foreach ($modules as $module) {
         LanguageManager::clearLanguageCache($module);
     }
     self::tearDown('dictionary');
     VardefManager::$linkFields = array();
     VardefManager::clearVardef();
     foreach ($modules as $module) {
         VardefManager::refreshVardefs($module, BeanFactory::getBeanName($module));
     }
     SugarRelationshipFactory::rebuildCache();
     self::$cleanModules = array();
     return true;
 }
Example #19
0
 /**
  * @group 45339
  */
 public function testGetExtensionsList()
 {
     // Create new relationship between Leads and Accounts
     $_REQUEST['view_module'] = "Leads";
     $_REQUEST['lhs_module'] = "Leads";
     $_REQUEST['rhs_module'] = "Accounts";
     $_REQUEST['lhs_label'] = "Leads";
     $_REQUEST['rhs_label'] = "Accounts";
     $deployedRelation = new DeployedRelationships($_REQUEST['view_module']);
     $relationLeadAccount = $deployedRelation->addFromPost();
     $deployedRelation->save();
     $deployedRelation->build();
     //create expected file paths from custom extensions
     $accountContactRelInAccountVardefExtensions = sprintf('custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationAccountContact->getName() . '_Accounts.php', DIRECTORY_SEPARATOR);
     $contactAccountRelInAccountVardefExtensions = sprintf('custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $this->relationContactAccount->getName() . '_Accounts.php', DIRECTORY_SEPARATOR);
     $leadAccountRelInAccountVardefExtensions = sprintf('custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . $relationLeadAccount->getName() . '_Accounts.php', DIRECTORY_SEPARATOR);
     $sugarfieldAccountVardefExtensions = sprintf('custom%1$sExtension%1$smodules%1$sAccounts%1$sExt%1$sVardefs%1$s' . 'sugarfield_' . $this->field->name . '.php', DIRECTORY_SEPARATOR);
     //call mbPackage to retrieve arrays of Files to be exported using different test parameters
     $accountAllExtensions = $this->mbPackage->getExtensionsListTest('Accounts', array('Accounts', 'Contacts', 'Leads'));
     $accountExtContacts = $this->mbPackage->getExtensionsListTest('Accounts', array('Accounts', 'Contacts'));
     $accountExtOnly = $this->mbPackage->getExtensionsListTest('Accounts', array('Accounts'));
     $contactExtWithWrongRelationship = $this->mbPackage->getExtensionsListTest('Contacts', array(''));
     $wrongModuleName = $this->mbPackage->getExtensionsListTest('Wrong_module_name');
     // Remove relationship
     $deployedRelation->delete($relationLeadAccount->getName());
     $deployedRelation->save();
     SugarRelationshipFactory::deleteCache();
     //assert that contact rels are exported when all rels were defined
     $this->assertContains($accountContactRelInAccountVardefExtensions, $accountAllExtensions, 'Contact Relationship should have been exported when accounts and contacts modules are exported');
     //assert that contact rels are not exported when contact is not defined
     $this->assertNotContains($accountContactRelInAccountVardefExtensions, $accountExtOnly, 'Contact Relationship should NOT have been exported when exporting accounts only');
     //assert that non relationship change is exported when no related module is defined
     $this->assertContains($sugarfieldAccountVardefExtensions, $accountExtOnly, 'Sugarfield change should have been exported when exporting Accounts only');
     //assert only contact and Account modules are present when both contact and Accounts are defined
     $this->assertContains($accountContactRelInAccountVardefExtensions, $accountExtContacts, 'Accounts rels should be present when exporting Contacts and Accounts');
     $this->assertContains($contactAccountRelInAccountVardefExtensions, $accountExtContacts, 'Contacts rels should be present when exporting Contacts and Accounts');
     $this->assertNotContains($leadAccountRelInAccountVardefExtensions, $accountExtContacts, 'Leads rels should NOT be present when exporting Contacts and Accounts');
     //assert that requesting a wrong relationship returns an empty array
     $this->assertInternalType('array', $contactExtWithWrongRelationship, 'array type should be returned when no relationships are exported, and no other changes exist');
     $this->assertEmpty($contactExtWithWrongRelationship, 'An empty array should be returned when no relationships are exported, and no other changes exist');
     //assert that requesting a wrong module name returns an empty array
     $this->assertInternalType('array', $wrongModuleName, 'An array type should be returned when a bad module is requested for export');
     $this->assertEmpty($wrongModuleName, 'An empty array should be returned when a bad module is requested for export');
 }
Example #20
0
 /**
  * @static
  * @param  $module String name of module.
  * @param  $object String name of module Bean.
  * Updates a list of link fields which have relationships to modules with calculated fields
  * that use this module. Needed to cause an update to those modules when this module is updated.
  * @return bool
  */
 protected static function updateRelCFModules($module, $object)
 {
     global $dictionary, $beanList;
     if (empty($dictionary[$object]) || empty($dictionary[$object]['fields'])) {
         return false;
     }
     $linkFields = self::getLinkFieldsForModule($module, $object);
     if (empty($linkFields)) {
         $dictionary[$object]['related_calc_fields'] = array();
         return false;
     }
     $linksWithCFs = array();
     foreach ($linkFields as $name => $def) {
         $relName = $def['relationship'];
         //Start by getting the relation
         $relDef = false;
         if (!empty($def['module'])) {
             $relMod = $def['module'];
         } else {
             if (!empty($dictionary[$relName]['relationships'][$relName])) {
                 $relDef = $dictionary[$relName]['relationships'][$relName];
             } else {
                 if (!empty($dictionary[$object][$relName])) {
                     $relDef = $dictionary[$object][$relName];
                 } else {
                     $relDef = SugarRelationshipFactory::getInstance()->getRelationshipDef($relName);
                     if (!$relDef) {
                         continue;
                     }
                 }
             }
             if (empty($relDef['lhs_module'])) {
                 continue;
             }
             $relMod = $relDef['lhs_module'] == $module ? $relDef['rhs_module'] : $relDef['lhs_module'];
         }
         if (empty($beanList[$relMod])) {
             continue;
         }
         $relObject = BeanFactory::getObjectName($relMod);
         $relLinkFields = self::getLinkFieldsForModule($relMod, $relObject);
         if (!empty($relLinkFields)) {
             foreach ($relLinkFields as $rfName => $rfDef) {
                 if ($rfDef['relationship'] == $relName && self::modHasCalcFieldsWithLink($relMod, $relObject, $rfName)) {
                     $linksWithCFs[$name] = true;
                 }
             }
         }
     }
     $dictionary[$object]['related_calc_fields'] = array_keys($linksWithCFs);
 }