/**
* Create a relationship
*
* Params should be passed in as this:
*
* array(
* 'relationship_type' => 'one-to-many',
* 'lhs_module' => 'Accounts',
* 'rhs_module' => 'Accounts',
* )
*
* @static
* @param array $relationship_def
* @return ActivitiesRelationship|bool|ManyToManyRelationship|ManyToOneRelationship|OneToManyRelationship|OneToOneRelationship
*/
public static function createRelationship(array $relationship_def)
{
if (!self::checkRequiredFields($relationship_def)) {
return false;
}
$relationships = new DeployedRelationships($relationship_def['lhs_module']);
if (!isset($relationship_def['view_module'])) {
$relationship_def['view_module'] = $relationship_def['lhs_module'];
}
$REQUEST_Backup = $_REQUEST;
$_REQUEST = $relationship_def;
$relationship = $relationships->addFromPost();
$relationships->save();
$relationships->build();
LanguageManager::clearLanguageCache($relationship_def['lhs_module']);
SugarRelationshipFactory::rebuildCache();
// rebuild the dictionary to make sure that it has the new relationship in it
SugarTestHelper::setUp('dictionary');
// reset the link fields since we added one
VardefManager::$linkFields = array();
$_REQUEST = $REQUEST_Backup;
unset($REQUEST_Backup);
self::$_relsAdded[] = $relationship->getDefinition();
return $relationship;
}
public function setUp()
{
global $dictionary;
$this->_view = 'editview';
VardefManager::loadVardef('Contacts', 'Contact');
$this->def = $dictionary['Contact']['fields']['email1'];
}
/**
* @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;
}
}
}
/**
* The constructor
* @param string $subpanelName
* @param string $loadedModule - Accounts
* @param string $client - base
*/
public function __construct($subpanelName, $loadedModule, $client = 'base')
{
$GLOBALS['log']->debug(get_class($this) . "->__construct({$subpanelName} , {$loadedModule})");
$this->mdc = new MetaDataConverter();
$this->loadedModule = $loadedModule;
$this->setViewClient($client);
$linkName = $this->linkName = $this->getLinkName($subpanelName, $loadedModule);
// get the link and the related module name as the module we need the subpanel from
$bean = BeanFactory::getBean($loadedModule);
// Get the linkdef, but make sure to tell VardefManager to use name instead by passing true
$linkDef = VardefManager::getLinkFieldForRelationship($bean->module_dir, $bean->object_name, $subpanelName, true);
if ($bean->load_relationship($linkName)) {
$link = $bean->{$linkName};
} else {
$link = new Link2($linkName, $bean);
}
$this->_moduleName = $link->getRelatedModuleName();
$this->bean = BeanFactory::getBean($this->_moduleName);
$subpanelFixed = true;
if (empty($this->bean)) {
$subpanelFixed = $this->fixUpSubpanel();
}
if (empty($linkDef['name']) && (!$subpanelFixed && isModuleBWC($this->loadedModule))) {
$GLOBALS['log']->error("Cannot find a link for {$subpanelName} on {$loadedModule}");
return true;
}
// Handle validation up front that will throw exceptions
if (empty($this->bean) && !$subpanelFixed) {
throw new Exception("No valid parent bean found for {$this->linkName} on {$this->loadedModule}");
}
$this->setUpSubpanelViewDefFileInfo();
include $this->loadedSubpanelFileName;
// Prepare to load the history file. This will be available in cases when
// a layout is restored.
$this->historyPathname = 'custom/history/modules/' . $this->_moduleName . '/clients/' . $this->getViewClient() . '/views/' . $this->sidecarSubpanelName . '/' . self::HISTORYFILENAME;
$this->_history = new History($this->historyPathname);
if (file_exists($this->historyPathname)) {
// load in the subpanelDefOverride from the history file
$GLOBALS['log']->debug(get_class($this) . ": loading from history");
require $this->historyPathname;
}
$this->_viewdefs = !empty($viewdefs) ? $this->getNewViewDefs($viewdefs) : array();
$this->_fielddefs = $this->bean->field_defs;
$this->_mergeFielddefs($this->_fielddefs, $this->_viewdefs);
$this->_language = '';
// don't attempt to access the template_instance property if our subpanel represents a collection, as it won't be there - the sub-sub-panels get this value instead
if (isset($this->_viewdefs['type']) && $this->_viewdefs['type'] != 'collection') {
$this->_language = $this->bean->module_dir;
}
// Make sure the paneldefs are proper if there are any
$this->_paneldefs = isset($this->_viewdefs['panels']) ? $this->_viewdefs['panels'] : array();
}
public function testSaveUsersVardefs()
{
global $dictionary;
$dynamicField = new DynamicField('Users');
VardefManager::loadVardef('Users', 'User');
$dynamicField->saveToVardef('Users', $dictionary['User']['fields']);
//Test that we have refreshed the Employees vardef
$this->assertTrue(file_exists('cache/modules/Employees/Employeevardefs.php'), 'cache/modules/Employees/Emloyeevardefs.php file not created');
//Test that status is not set to be required
$this->assertFalse($dictionary['Employee']['fields']['status']['required'], 'status field set to required');
//Test that the studio attribute is set to false for status field
$this->assertFalse($dictionary['Employee']['fields']['status']['studio'], 'status field studio not set to false');
}
/**
* @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;
}
}
}
}
/**
* Find the link entry for a particular relationship and module.
*
* @param $module
* @return array|bool
*/
public function getLinkedDefForModuleByRelationship($module)
{
$results = VardefManager::getLinkFieldForRelationship($module, BeanFactory::getObjectName($module), $this->name);
//Only a single link was found
if (isset($results['name'])) {
return $results;
} else {
if (is_array($results)) {
$GLOBALS['log']->error("Warning: Multiple links found for relationship {$this->name} within module {$module}");
return $this->getMostAppropriateLinkedDefinition($results);
} else {
return FALSE;
}
}
}
public function tearDown()
{
$GLOBALS['db']->dropTableName($this->_tablename . '_cstm');
$GLOBALS['db']->query("DELETE FROM fields_meta_data WHERE id in ('Accountsbug34993_test_c', 'Accountsbug34993_test2_c')");
if (isset($this->_old_installing)) {
$GLOBALS['installing'] = $this->_old_installing;
} else {
unset($GLOBALS['installing']);
}
if (file_exists('custom/Extension/modules/Accounts/Ext/Vardefs/sugarfield_bug34993_test_c.php')) {
unlink('custom/Extension/modules/Accounts/Ext/Vardefs/sugarfield_bug34993_test_c.php');
}
if (file_exists('custom/Extension/modules/Accounts/Ext/Vardefs/sugarfield_bug34993_test2_c.php')) {
unlink('custom/Extension/modules/Accounts/Ext/Vardefs/sugarfield_bug34993_test2_c.php');
}
VardefManager::clearVardef('Accounts', 'Account');
VardefManager::refreshVardefs('Accounts', 'Account');
}
请发表评论