/**
  * @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;
 }
 /**
  * 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
  * @param array $params
  *
  * @return array|string|false
  */
 public function getQuery($link, $params = [])
 {
     //There was an old signature with $return_as_array as the second parameter. We should respect this if $params is true
     if ($params === true) {
         $params = ["return_as_array" => true];
     }
     if ($link->getSide() == REL_RHS) {
         return false;
     } else {
         $lhsKey = $this->def['lhs_key'];
         $rhsTable = $this->def['rhs_table'];
         $rhsTableKey = "{$rhsTable}.{$this->def['rhs_key']}";
         $deleted = !empty($params['deleted']) ? 1 : 0;
         $where = "WHERE {$rhsTableKey} = '{$link->getFocus()->{$lhsKey}}' AND {$rhsTable}.deleted={$deleted}";
         //Check for role column
         if (!empty($this->def["relationship_role_column"]) && !empty($this->def["relationship_role_column_value"])) {
             $roleField = $this->def["relationship_role_column"];
             $roleValue = $this->def["relationship_role_column_value"];
             $where .= " AND {$rhsTable}.{$roleField} = '{$roleValue}'";
         }
         //Add any optional where clause
         if (!empty($params['where'])) {
             $add_where = is_string($params['where']) ? $params['where'] : "{$rhsTable}." . $this->getOptionalWhereClause($params['where']);
             if (!empty($add_where)) {
                 $where .= " AND {$add_where}";
             }
         }
         $from = $this->def['rhs_table'];
         if (empty($params['return_as_array'])) {
             //Limit is not compatible with return_as_array
             $query = "SELECT id FROM {$from} {$where}";
             if (!empty($params['limit']) && $params['limit'] > 0) {
                 $offset = isset($params['offset']) ? $params['offset'] : 0;
                 $query = DBManagerFactory::getInstance()->limitQuery($query, $offset, $params['limit'], false, "", false);
             }
             return $query;
         } else {
             return ['select' => "SELECT {$this->def['rhs_table']}.id", 'from' => "FROM {$this->def['rhs_table']}", 'where' => $where];
         }
     }
 }
 /**
  * @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;
 }