function chado_update_node_form_relationships

2.x tripal_core.chado_nodes.relationships.api.inc chado_update_node_form_relationships($node, $details, $retrieved_relationships = FALSE)

This function is used in hook_insert or hook_update and handles inserting of relationships between the current nodetype and other memebers of the same nodetype

Parameters

$node: The node passed into hook_insert & hook_update

$details:

  • relationship_table: the name of the _relationship linking table (ie: feature_relationship)
  • foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)

$retrieved_relationships: An array of relationships from chado_retrieve_node_form_relationships($node). This can be used if you need special handling for some of the relationships.

Related topics

13 calls to chado_update_node_form_relationships()
chado_contact_insert in tripal_contact/includes/tripal_contact.chado_node.inc
Implements of hook_insert().
chado_contact_update in tripal_contact/includes/tripal_contact.chado_node.inc
Implements hook_update
chado_example_insert in tripal_example/includes/tripal_example.chado_node.inc
Implementation of hook_insert(). This function is called after the node is inserted into the database. We need it so that we can insert appropriate fields as provided by the user into the database. And so that we can link the new Drupal node to the…
chado_example_update in tripal_example/includes/tripal_example.chado_node.inc
Implementation of hook_update(). This function runs after the node has been inserted into the Drupal schema and allows us to update the record in Chado.
chado_feature_insert in tripal_feature/includes/tripal_feature.chado_node.inc
Implements hook_insert().

... See full list

1 string reference to 'chado_update_node_form_relationships'

File

tripal_core/api/tripal_core.chado_nodes.relationships.api.inc, line 966
API to manage the chado _relationship table for various Tripal Node Types

Code

function chado_update_node_form_relationships($node, $details, $retrieved_relationships = FALSE) {

  $relationship_table = $details['relationship_table'];
  $current_id = $details['foreignkey_value'];

  if (isset($node->relationship_table) AND ($current_id > 0)) {

    // determine whether there is a rank in this relationship table
    $form_details = unserialize($node->relationship_table['details']);
    $has_rank = $form_details['table_has_rank'];

    // First remove existing relationships links
    chado_delete_record(
    $relationship_table, 
    array($form_details['subject_field_name'] => $current_id)
    );
    chado_delete_record(
    $relationship_table, 
    array($form_details['object_field_name'] => $current_id)
    );

    // Add back in relationships as needed
    if ($retrieved_relationships) {
      $relationships = $retrieved_relationships;
    }
    else {
      $relationships = chado_retrieve_node_form_relationships($node);
    }
    foreach ($relationships as $type_id => $ranks) {
      foreach ($ranks as $rank => $element) {

        $values = array(
          $form_details['subject_field_name'] => $element['subject_id'],
          'type_id' => $type_id,
          $form_details['object_field_name'] => $element['object_id']
        );

        // Set the current id if not already
        // this is usually only necessary in an insert
        if (empty($values[$form_details['subject_field_name']])) {
          $values[$form_details['subject_field_name']] = $current_id;
        }
        if (empty($values[$form_details['object_field_name']])) {
          $values[$form_details['object_field_name']] = $current_id;
        }

        if ($has_rank) {
          // Ensure that the rank is Set & Current
          $rank_select = chado_get_table_max_rank(
          $relationship_table, 
          array(
            $form_details['subject_field_name'] => $values['subject_id'],
            'type_id' => $values['type_id'],
            $form_details['object_field_name'] => $values['object_id'],
          )
          );
          $values['rank'] = $rank_select + 1;
        }

        // add relationship
        $success_link = chado_insert_record(
        $relationship_table, 
        $values
        );

      }
    }
  }
}