/**
  * @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;
 }
 /**
  * @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;
 }
 /**
  * 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;
 }
Esempio n. 6
0
 /**
  * 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;
 }