private function createRelationship($lhs_module, $rhs_module = null, $relationship_type = 'one-to-many') { $rhs_module = $rhs_module == null ? $lhs_module : $rhs_module; // Adding relation between products and users $this->relationships = new DeployedRelationships($lhs_module); $definition = array('lhs_module' => $lhs_module, 'relationship_type' => $relationship_type, 'rhs_module' => $rhs_module, 'lhs_label' => $lhs_module, 'rhs_label' => $rhs_module, 'rhs_subpanel' => 'default'); $this->relationship = RelationshipFactory::newRelationship($definition); $this->relationships->add($this->relationship); $this->relationships->save(); $this->relationships->build(); LanguageManager::clearLanguageCache($lhs_module); // Updating $dictionary by created relation global $dictionary; $moduleInstaller = new ModuleInstaller(); $moduleInstaller->silent = true; $moduleInstaller->rebuild_tabledictionary(); require 'modules/TableDictionary.php'; // Updating vardefs VardefManager::$linkFields = array(); VardefManager::clearVardef(); VardefManager::refreshVardefs($lhs_module, BeanFactory::getObjectName($lhs_module)); if ($lhs_module != $rhs_module) { VardefManager::refreshVardefs($rhs_module, BeanFactory::getObjectName($rhs_module)); } SugarRelationshipFactory::rebuildCache(); }
/** * 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; }
//// 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']);
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"); }
/** * 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; }
/** * 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']); } }