You are here

function rdfx_save_terms in RDF Extensions 7.2

Saves vocabulary terms.

1 call to rdfx_save_terms()
evoc_import_vocabulary in evoc/evoc.module

File

./rdfx.terms.inc, line 166
Functions for managing RDF Terms.

Code

function rdfx_save_terms($vocabulary_uri, $prefix, $vocabulary) {
  $nsids = _rdfx_save_vocabulary($vocabulary_uri, $prefix, $vocabulary);
  foreach ($vocabulary['terms'] as $term_type => $terms) {
    foreach ($terms as $term_uri => $term_description) {
      list($term_ns, $term_local_name) = rdfx_split_uri($term_uri);
      if (isset($nsids[$term_ns])) {
        $nsid = $nsids[$term_ns];
      }
      else {

        // If the namespace wasn't mapped to a prefix in the source graph, we
        // didn't save it to the namespaces table, so we need to add an entry.
        // @todo For the prefix value, we save the URI... should this be changed?
        $gid = rdfx_get_gid($vocabulary_uri);
        $nsid = db_insert('rdfx_namespaces')
          ->fields(array(
          'uri' => $term_ns,
          'prefix' => $term_ns,
          'gid' => $gid,
        ))
          ->execute();
        $nsids[$term_ns] = $nsid;
      }

      // Get the tid of this term, saving to {rdfx_terms} if not already there.
      $tid = db_query("SELECT tid FROM {rdfx_terms} WHERE nsid = :nsid AND local_name = :localname", array(
        ':nsid' => $nsid,
        ':localname' => $term_local_name,
      ))
        ->fetchField();
      if ($tid == NULL) {
        $tid = db_insert('rdfx_terms')
          ->fields(array(
          'nsid',
          'local_name',
        ))
          ->values(array(
          'nsid' => $nsid,
          'local_name' => $term_local_name,
        ))
          ->execute();
      }

      // Add the current type to this term in {rdfx_term_types}.
      db_merge('rdfx_term_types')
        ->key(array(
        'tid' => $tid,
        'type' => $term_type,
      ))
        ->fields(array(
        'tid' => $tid,
        'type' => $term_type,
      ))
        ->execute();

      // Add label and comment to {rdfx_term_details}.
      $term_details = array();
      if (isset($term_description['label'])) {
        foreach ($term_description['label'] as $lang => $text) {
          $term_details[$lang]['label'] = $text;
        }
      }
      if (isset($term_description['comment'])) {
        foreach ($term_description['comment'] as $lang => $text) {
          $term_details[$lang]['comment'] = $text;
        }
      }
      if (!empty($term_details)) {
        foreach ($term_details as $lang => $details) {
          db_merge('rdfx_term_details')
            ->key(array(
            'tid' => $tid,
            'language' => $lang,
          ))
            ->fields(array(
            'tid' => $tid,
            'language' => $lang,
            'label' => isset($details['label']) ? $details['label'] : NULL,
            'comment' => isset($details['comment']) ? $details['comment'] : NULL,
          ))
            ->execute();
        }
      }

      // Add relationships to their respective tables. This is handled as a
      // complicated set of loops to reduce code duplication. To add a new
      // relationship, just add the array key that is used in
      // $types['properties']['description'] to define the relationship, and
      // then add the name of the table that stores the relationship.
      $relationships = array(
        'domain' => 'rdfx_term_domains',
        'range' => 'rdfx_term_ranges',
      );
      foreach ($relationships as $relationship => $table_name) {
        if (isset($term_description[$relationship])) {
          foreach ($term_description[$relationship] as $related_term) {
            $related_term_tid = rdfx_get_tid($related_term, $vocabulary_uri);
            if ($related_term_tid) {
              db_merge($table_name)
                ->key(array(
                'tid' => $tid,
                $relationship . '_tid' => $related_term_tid,
              ))
                ->fields(array(
                'tid' => $tid,
                $relationship . '_tid' => $related_term_tid,
              ))
                ->execute();
            }
          }
        }
      }
    }
  }

  // @todo Add a hook that passes the $vocabulary and the $model.
}