function chado_featuremap_insert

2.x tripal_featuremap.chado_node.inc chado_featuremap_insert($node)
3.x tripal_featuremap.chado_node.inc chado_featuremap_insert($node)
1.x tripal_featuremap.module chado_featuremap_insert($node)

When a new chado_featuremap node is created we also need to add information to our chado_featuremap table. This function is called on insert of a new node of type 'chado_featuremap' and inserts the necessary information.

Related topics

File

tripal_featuremap/tripal_featuremap.module, line 341

Code

function chado_featuremap_insert($node) {

  // if the featuremap_id already exists then we got to the insert via 
  // a syncing operation.  We do not need to add the feature map
  if ($node->featuremap_id) {
    $featuremap['featuremap_id'] = $node->featuremap_id;
  }
  else {
    $values = array(
      'name' => $node->title,
      'description' => $node->description,
      'unittype_id' => $node->unittype_id
    );
    $featuremap = tripal_core_chado_insert('featuremap', $values);
    if (!$featuremap) {
      drupal_set_message(t('Unable to add featuremap.', 'warning'));
      watchdog('tripal_featuremap', 'Unable to create feature map where values: %values', 
      array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
      return;
    }

    // now add the properties
    $properties = array(); // stores all of the properties we need to add
    $cross_refs = array(); // stores any cross references for this featuremap

    // get the list of properties for easy lookup (without doing lots of database queries
    $properties_list = array();
    $sql = "
      SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
      FROM  {cvterm} CVT
        INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
      WHERE 
        CV.name = 'featuremap_property' AND 
        NOT CVT.is_obsolete = 1
      ORDER BY CVT.name ASC 
    ";
    $prop_types = chado_query($sql);
    while ($prop = db_fetch_object($prop_types)) {
      $properties_list[$prop->cvterm_id] = $prop->name;
    }

    // get the properties that should be added. Properties are in one of two forms:
    //  1) prop_value-[type id]-[index]
    //  2) new_value-[type id]-[index]
    //  3) new_id, new_value
    foreach ($node as $name => $value) {
      if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
        $type_id = $matches[1];
        $index = $matches[2];
        $name = $properties_list[$type_id];
        $properties[$name][$index] = trim($value);
      }
    }
    if ($node->new_id and $node->new_value) {
      $type_id = $node->new_id;
      $index = count($properties[$name]);
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($node->new_value);
    }

    // iterate through all of the properties to see if the Map dbxref is set, 
    // if so, add it to the $cross_refs array
    foreach ($properties as $name => $element) {
      foreach ($element as $index => $value) {
        if ($name == "Map Dbxref") {
          // we will add the cross-references to the featuremap_dbxref table
          // but we also want to keep the property in the featuremapprop table so don't unset it
          $cross_refs[] = $value;
        }
      }
    }

    // now add in the properties
    foreach ($properties as $property => $elements) {
      foreach ($elements as $rank => $value) {

        $status = tripal_featuremap_insert_property($featuremap['featuremap_id'], $property, $value, FALSE);
        if (!$status) {
          drupal_set_message("Error cannot add property: $property", "error");
          watchdog('t_featuremap', "Error cannot add property: %prop", 
          array('%property' => $property), WATCHDOG_ERROR);
        }
      }
    }

    // add in any database cross-references
    foreach ($cross_refs as $index => $ref) {
      $featuremap_dbxref = tripal_featuremap_add_featuremap_dbxref($featuremap['featuremap_id'], trim($ref));
      if (!$featuremap_dbxref) {
        drupal_set_message("Error cannot add map cross reference: $ref", "error");
        watchdog('t_featuremap', "Error cannot add map cross reference: %ref", 
        array('%ref' => $ref), WATCHDOG_ERROR);
      }
    }
  }

  // add the record to the chado_featuremap table in Drupal
  if ($featuremap) {
    // make sure the entry for this feature doesn't already exist in the chado_featuremap table
    // if it doesn't exist then we want to add it.
    $featuremap_id = chado_get_id_for_node('featuremap', $node);
    if (!$featuremap_id) {
      // next add the item to the drupal table
      $sql = "INSERT INTO {chado_featuremap} (nid, vid, featuremap_id) VALUES (%d, %d, %d)";
      db_query($sql, $node->nid, $node->vid, $featuremap['featuremap_id']);
    }
  }




}