public function testGetRelatedListFunctionWithLink2Class() { $focusModule = 'Accounts'; $linkedModules = array('Bugs', 'Contacts'); $focus = BeanFactory::newBean($focusModule); $focus->name = "bug49505"; $focus->save(); $this->_createdBeans[] = $focus; foreach ($linkedModules as $v) { $linkedBean = BeanFactory::newBean($v); $linkedBean->name = "bug49505"; $linkedBean->save(); $this->_createdBeans[] = $linkedBean; $link = new Link2(strtolower($v), $focus); $link->add(array($linkedBean)); // get relation from 'Link2' class $link2List = $focus->get_related_list($linkedBean, strtolower($v)); // get relation for 'get_related_list' function from Link class $focus->field_defs[strtolower($v)]['link_class'] = 'Link'; $focus->field_defs[strtolower($v)]['link_file'] = 'data/Link.php'; $linkList = $focus->get_related_list($linkedBean, strtolower($v)); unset($focus->field_defs[strtolower($v)]['link_class']); unset($focus->field_defs[strtolower($v)]['link_file']); $this->assertEquals($linkedBean->id, $linkList['list'][0]->id); $this->assertEquals($linkedBean->id, $link2List['list'][0]->id); } }
/** * 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 getSubpanelQuery($params = array(), $return_array = false) { $db = DBManagerFactory::getInstance(); $result = parent::getSubpanelQuery($params, $return_array); if ($return_array) { $result['join'] .= ' LEFT JOIN quotes ON products.quote_id = quotes.id '; $result['where'] .= ' AND (quotes.quote_stage IS NULL OR quotes.quote_stage NOT IN (' . $db->quoted('Closed Lost') . ',' . $db->quoted('Closed Dead') . ')) AND ( quotes.deleted = 0 OR quotes.deleted IS NULL )'; array_push($result['join_tables'], 'quotes'); } return $result; }
/** * This method changes join of any item to campaign through target list * if you want to use this join method you should add code below to your vardef.php * 'link_class' => 'ProspectLink', * 'link_file' => 'modules/Campaigns/ProspectLink.php' * * @see Link::getJoin method */ public function getJoin($params, $return_array = false) { $join_type = ' INNER JOIN '; if (isset($params['join_type'])) { $join_type = $params['join_type']; } $join = ''; $bean_is_lhs = $this->_get_bean_position(); if ($this->_relationship->relationship_type == 'one-to-many' && $bean_is_lhs) { $table_with_alias = $table = $this->_relationship->rhs_table; $key = $this->_relationship->rhs_key; $module = $this->_relationship->rhs_module; $other_table = empty($params['left_join_table_alias']) ? $this->_relationship->lhs_table : $params['left_join_table_alias']; $other_key = $this->_relationship->lhs_key; $alias_prefix = $table; if (!empty($params['join_table_alias'])) { $table_with_alias = $table . " " . $params['join_table_alias']; $table = $params['join_table_alias']; $alias_prefix = $params['join_table_alias']; } $join .= ' ' . $join_type . ' prospect_list_campaigns ' . $alias_prefix . '_plc ON'; $join .= ' ' . $alias_prefix . '_plc.' . $key . ' = ' . $other_table . '.' . $other_key . "\n"; // join list targets $join .= ' ' . $join_type . ' prospect_lists_prospects ' . $alias_prefix . '_plp ON'; $join .= ' ' . $alias_prefix . '_plp.prospect_list_id = ' . $alias_prefix . '_plc.prospect_list_id AND'; $join .= ' ' . $alias_prefix . '_plp.related_type = ' . $GLOBALS['db']->quoted($module) . "\n"; // join target $join .= ' ' . $join_type . ' ' . $table_with_alias . ' ON'; $join .= ' ' . $table . '.id = ' . $alias_prefix . '_plp.related_id AND'; $join .= ' ' . $table . '.deleted=0' . "\n"; if ($return_array) { $ret_arr = array(); $ret_arr['join'] = $join; $ret_arr['type'] = $this->_relationship->relationship_type; if ($bean_is_lhs) { $ret_arr['rel_key'] = $this->_relationship->join_key_rhs; } else { $ret_arr['rel_key'] = $this->_relationship->join_key_lhs; } return $ret_arr; } return $join; } else { return parent::getJoin($params, $return_array); } }
/** * @param Link2 $link * @param array $params * @param bool $return_array * * @return array|string */ public function getJoin($link, $params = [], $return_array = false) { $linkIsLHS = $link->getSide() == REL_LHS; $startingTable = $link->getFocus()->table_name; $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key']; $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table']; $targetTableWithAlias = $targetTable; $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key']; $join_type = isset($params['join_type']) ? $params['join_type'] : ' INNER JOIN '; $join = ''; //Set up any table aliases required if (!empty($params['join_table_alias'])) { $targetTableWithAlias = $targetTable . " " . $params['join_table_alias']; $targetTable = $params['join_table_alias']; } $deleted = !empty($params['deleted']) ? 1 : 0; //join the related module's table $join .= "{$join_type} {$targetTableWithAlias} ON {$targetTable}.{$targetKey}={$startingTable}.{$startingKey}" . " AND {$targetTable}.deleted={$deleted}\n" . $this->getRoleWhere(); if ($return_array) { return ['join' => $join, 'type' => $this->type, 'rel_key' => $targetKey, 'join_tables' => [$targetTable], 'where' => "", 'select' => "{$targetTable}.id"]; } return $join; }
/** * Method returns part of where in style table_alias.id IN (...) because we can't join of relation * * @param array $layout_def definition of a field * @param bool $rename_columns unused * @return string SQL where part */ public function queryFilterStarts_With($layout_def, $rename_columns = true) { $ids = array(); $relation = new Relationship(); $relation->retrieve_by_name($layout_def['link']); global $beanList; $beanClass = $beanList[$relation->lhs_module]; $seed = new $beanClass(); $seed->retrieve($layout_def['input_name0']); $link = new Link2($layout_def['link'], $seed); $sql = $link->getQuery(); $result = $this->reporter->db->query($sql); while ($row = $this->reporter->db->fetchByAssoc($result)) { $ids[] = $row['id']; } $layout_def['name'] = 'id'; return $this->_get_column_select($layout_def) . " IN ('" . implode("', '", $ids) . "')"; }
/** * @param Link2 $link * @param array $params * @param bool|false $return_array * * @return array|string */ public function getSubpanelQuery($link, $params = [], $return_array = false) { $linkIsLHS = $link->getSide() == REL_RHS; $startingTable = empty($params['left_join_table_alias']) ? $this->def['lhs_table'] : $params['left_join_table_alias']; if (!$linkIsLHS) { $startingTable = empty($params['right_join_table_alias']) ? $this->def['rhs_table'] : $params['right_join_table_alias']; } $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key']; $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table']; $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key']; $join_type = isset($params['join_type']) ? $params['join_type'] : ' INNER JOIN '; $query = ''; $alias = empty($params['join_table_alias']) ? "{$link->name}_rel" : $params['join_table_alias']; $alias = $GLOBALS['db']->getValidDBName($alias, false, 'alias'); $tableInRoleFilter = ""; if (($startingTable == "meetings" || $startingTable == "notes" || $startingTable == "tasks" || $startingTable == "calls" || $startingTable == "emails") && ($targetTable == "meetings" || $targetTable == "notes" || $targetTable == "tasks" || $targetTable == "calls") && substr($alias, 0, 12 + strlen($targetTable)) == $targetTable . "_activities_") { $tableInRoleFilter = $linkIsLHS ? $alias : $startingTable; } //Set up any table aliases required $targetTableWithAlias = "{$targetTable} {$alias}"; $targetTable = $alias; $query .= "{$join_type} {$targetTableWithAlias} ON {$startingTable}.{$startingKey}={$targetTable}.{$targetKey} AND {$targetTable}.deleted=0\n" . $this->getRoleWhere($tableInRoleFilter) . "\n"; if (!empty($params['return_as_array'])) { $return_array = true; } if ($return_array) { return ['join' => $query, 'type' => $this->type, 'rel_key' => $targetKey, 'join_tables' => [$targetTable], 'where' => "WHERE {$startingTable}.{$startingKey}='{$link->focus->id}'", 'select' => " "]; } return $query; }
/** * Method returns part of where in style table_alias.id IN (...) because we can't join of relation * * @param array $layout_def definition of a field * @param bool $rename_columns unused * @return string SQL where part */ public function queryFilterone_of($layout_def, $rename_columns = true) { $ids = array(); $relation = BeanFactory::getBean('Relationships'); $relation->retrieve_by_name($layout_def['link']); $seed = BeanFactory::getBean($relation->lhs_module); foreach ($layout_def['input_name0'] as $beanId) { $seed->retrieve($beanId); $link = new Link2($layout_def['link'], $seed); $sql = $link->getQuery(); $result = $this->reporter->db->query($sql); while ($row = $this->reporter->db->fetchByAssoc($result)) { $ids[] = $row['id']; } } $ids = array_unique($ids); $layout_def['name'] = 'id'; return $this->_get_column_select($layout_def) . " IN ('" . implode("', '", $ids) . "')"; }
/** * Build a Join Query with a SugarQuery Object * * @param Link2 $link * @param SugarQuery $sugar_query * @param Array $options array of additional paramters. Possible parameters include * - 'myAlias' String name of starting table alias * - 'joinTableAlias' String alias to use for the related table in the final result * - 'reverse' Boolean true if this join should be built in reverse for subpanel style queries where the select is * on the related table * - 'ignoreRole' Boolean true if the role column of the relationship should be ignored for this join . * * @return SugarQuery */ public function buildJoinSugarQuery(Link2 $link, $sugar_query, $options) { $linkIsLHS = $link->getSide() == REL_LHS; if (!empty($options['reverse'])) { $linkIsLHS = !$linkIsLHS; } $startingTable = $linkIsLHS ? $this->def['lhs_table'] : $this->def['rhs_table']; $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key']; $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table']; $targetModule = $linkIsLHS ? $this->def['rhs_module'] : $this->def['lhs_module']; $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key']; $join_type = isset($options['joinType']) ? $options['joinType'] : 'INNER'; $joinParams = array('joinType' => $join_type, 'bean' => BeanFactory::newBean($targetModule)); $jta = $targetTable; if (!empty($options['joinTableAlias'])) { $jta = $joinParams['alias'] = $options['joinTableAlias']; } $joinTable = $sugar_query->joinTable($targetTable, $joinParams); $joinTable->on()->equalsField("{$startingTable}.{$startingKey}", "{$jta}.{$targetKey}")->equals("{$jta}.deleted", "0"); $relTable = $linkIsLHS ? $jta : $startingTable; if (empty($options['ignoreRole'])) { foreach ($this->getRelationshipRoleColumns() as $column => $value) { $sugar_query->where()->equals("{$relTable}.{$column}", $value); } } $this->addCustomToSugarQuery($sugar_query, $options, $linkIsLHS, $jta); return $sugar_query->join[$jta]; }
/** * Similar to getQuery or Get join, except this time we are starting from the related table and * searching for items with id's matching the $link->focus->id * * @param Link2 $link * @param array $params * @param bool $return_array * * @return String|Array */ public function getSubpanelQuery($link, $params = [], $return_array = false) { $targetIsLHS = $link->getSide() == REL_RHS; $startingTable = $targetIsLHS ? $this->def['lhs_table'] : $this->def['rhs_table']; $startingKey = $targetIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key']; $startingJoinKey = $targetIsLHS ? $this->def['join_key_lhs'] : $this->def['join_key_rhs']; $joinTable = $this->getRelationshipTable(); $joinTableWithAlias = $joinTable; $joinKey = $targetIsLHS ? $this->def['join_key_rhs'] : $this->def['join_key_lhs']; $targetKey = $targetIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key']; $join_type = isset($params['join_type']) ? $params['join_type'] : ' INNER JOIN '; $query = ''; //Set up any table aliases required if (!empty($params['join_table_link_alias'])) { $joinTableWithAlias = $joinTable . " " . $params['join_table_link_alias']; $joinTable = $params['join_table_link_alias']; } $where = "{$startingTable}.{$startingKey}={$joinTable}.{$startingJoinKey} AND {$joinTable}.{$joinKey}='{$link->getFocus()->{$targetKey}}'"; //Check if we should ignore the role filter. $ignoreRole = !empty($params['ignore_role']); //First join the relationship table $query .= "{$join_type} {$joinTableWithAlias} ON {$where} AND {$joinTable}.deleted=0\n" . $this->getRoleWhere($joinTable, $ignoreRole) . "\n"; if (!empty($params['return_as_array'])) { $return_array = true; } if ($return_array) { return ['join' => $query, 'type' => $this->type, 'rel_key' => $joinKey, 'join_tables' => [$joinTable], 'where' => "", 'select' => " "]; } return $query; }
/** * @param Link2 $link * * @return bool */ protected function linkIsLHS($link) { return $link->getSide() == REL_LHS && !$this->selfReferencing || $link->getSide() == REL_RHS && $this->selfReferencing; }
/** * Build a Join using an existing SugarQuery Object * @param Link2 $link * @param SugarQuery $sugar_query * @param Array $options array of additional paramters. Possible parameters include * - 'myAlias' String name of starting table alias * - 'joinTableAlias' String alias to use for the related table in the final result * - 'reverse' Boolean true if this join should be built in reverse for subpanel style queries where the select is * on the related table * - 'ignoreRole' Boolean true if the role column of the relationship should be ignored for this join . * @return SugarQuery */ public function buildJoinSugarQuery(Link2 $link, $sugar_query, $options) { $linkIsLHS = $link->getSide() == REL_LHS; if (!empty($options['reverse'])) { $linkIsLHS = !$linkIsLHS; } $startingTable = $linkIsLHS ? $this->def['lhs_table'] : $this->def['rhs_table']; if (!empty($options['myAlias'])) { $startingTable = $options['myAlias']; } $startingKey = $linkIsLHS ? $this->def['lhs_key'] : $this->def['rhs_key']; $startingJoinKey = $linkIsLHS ? $this->def['join_key_lhs'] : $this->def['join_key_rhs']; $joinTable = $this->getRelationshipTable(); $joinKey = $linkIsLHS ? $this->def['join_key_rhs'] : $this->def['join_key_lhs']; $targetTable = $linkIsLHS ? $this->def['rhs_table'] : $this->def['lhs_table']; $targetKey = $linkIsLHS ? $this->def['rhs_key'] : $this->def['lhs_key']; $targetModule = $linkIsLHS ? $this->def['rhs_module'] : $this->def['lhs_module']; $join_type = isset($options['joinType']) ? $options['joinType'] : 'INNER'; $joinTable_alias = $sugar_query->getJoinTableAlias($joinTable, false, false); $targetTable_alias = !empty($options['joinTableAlias']) ? $options['joinTableAlias'] : $targetTable; $relTableJoin = $sugar_query->joinTable($joinTable, array('alias' => $joinTable_alias, 'joinType' => $join_type, 'linkingTable' => true))->on()->equalsField("{$startingTable}.{$startingKey}", "{$joinTable_alias}.{$startingJoinKey}")->equals("{$joinTable_alias}.deleted", "0"); $targetTableJoin = $sugar_query->joinTable($targetTable, array('alias' => $targetTable_alias, 'joinType' => $join_type, 'bean' => BeanFactory::newBean($targetModule)))->on()->equalsField("{$targetTable_alias}.{$targetKey}", "{$joinTable_alias}.{$joinKey}")->equals("{$targetTable_alias}.deleted", "0"); $sugar_query->join[$targetTable_alias]->relationshipTableAlias = $joinTable_alias; if (empty($options['ignoreRole'])) { $this->buildSugarQueryRoleWhere($sugar_query, $joinTable_alias); } $this->addCustomToSugarQuery($sugar_query, $options, $linkIsLHS, $targetTable_alias); return array($joinTable_alias => $relTableJoin, $targetTable_alias => $targetTableJoin); }
/** * @param Link2 $link removes all the beans associated with this link from the relationship * * @return boolean true if all beans were successfully removed or there * were not related beans, false otherwise */ public function removeAll($link) { $focus = $link->getFocus(); $related = $link->getBeans(); $result = true; foreach ($related as $relBean) { if (empty($relBean->id)) { continue; } if ($link->getSide() == REL_LHS) { $sub_result = $this->remove($focus, $relBean); } else { $sub_result = $this->remove($relBean, $focus); } $result = $result && $sub_result; } return $result; }