You are here

public function SqlGroupGraphStorage::removeEdge in Subgroup (Graph) 1.0.x

Removes the relationship between the parent group and the child group.

The child group will no longer be a child of the parent group. Inferred relationships based on existing relationships to the parent group will also be removed.

Parameters

int $parent_group_id: The ID of the parent group.

int $child_group_id: The ID of the child group.

Overrides GroupGraphStorageInterface::removeEdge

File

src/Graph/SqlGroupGraphStorage.php, line 446

Class

SqlGroupGraphStorage
SQL based storage of the group relationship graph.

Namespace

Drupal\ggroup\Graph

Code

public function removeEdge($parent_group_id, $child_group_id) {
  $edge_id = $this
    ->getEdgeId($parent_group_id, $child_group_id);
  if (empty($edge_id)) {
    return;
  }
  $edges_to_delete = [];
  $query = $this->connection
    ->select('group_graph', 'gg')
    ->fields('gg', [
    'id',
  ]);
  $query
    ->condition('direct_edge_id', $edge_id);
  $results = $query
    ->execute();
  while ($id = $results
    ->fetchField()) {
    $edges_to_delete[] = $id;
  }
  if (empty($edges_to_delete)) {
    return;
  }
  do {
    $total_edges = count($edges_to_delete);
    $query = $this->connection
      ->select('group_graph', 'gg')
      ->fields('gg', [
      'id',
    ]);
    $query
      ->condition('hops', 0);
    $query
      ->condition('id', $edges_to_delete, 'NOT IN');
    $query_or_conditions = new Condition('OR');
    $query_or_conditions
      ->condition('entry_edge_id', $edges_to_delete, 'IN');
    $query_or_conditions
      ->condition('exit_edge_id', $edges_to_delete, 'IN');
    $query
      ->condition($query_or_conditions);
    $results = $query
      ->execute();
    while ($id = $results
      ->fetchField()) {
      $edges_to_delete[] = $id;
    }
  } while (count($edges_to_delete) > $total_edges);
  $this->connection
    ->delete('group_graph')
    ->condition('id', $edges_to_delete, 'IN')
    ->execute();
  $this
    ->invalidate([
    $parent_group_id,
    $child_group_id,
  ]);
}