You are here

function uuid_taxonomy in Universally Unique IDentifier 6

Implementation of hook_taxonomy().

File

./uuid.module, line 260
Main module functions for the uuid module.

Code

function uuid_taxonomy($op, $type, $array = NULL) {
  if (!isset($array['vid']) || !in_array($array['vid'], variable_get('uuid_automatic_for_taxonomy', array())) && !isset($array['uuid'])) {

    // Nothing to do.
    return;
  }
  switch ($type) {
    case 'term':
      $keyfield = 'tid';
      $table = 'uuid_term_data';
      $key = $array['tid'];
      break;
    case 'vocabulary':
      $keyfield = 'vid';
      $table = 'uuid_vocabulary';
      $key = $array['vid'];
      break;
    default:

      // Nothing to do.
      return;
      break;
  }
  switch ($op) {
    case 'insert':
      if (empty($array['uuid']) || !uuid_is_valid($array['uuid'])) {
        $array['uuid'] = uuid_uuid();
      }
      db_query("INSERT INTO {$table} ({$keyfield}, uuid) VALUES (%d, '%s')", $key, $array['uuid']);
      break;
    case 'update':
      $existing_uuid = db_result(db_query("SELECT uuid FROM {$table} WHERE {$keyfield} = %d", $key));
      if ($existing_uuid) {

        // If there is an existing uuid, but no new one, remove the existing one.
        if (!isset($array['uuid']) || empty($array['uuid'])) {
          uuid_taxonomy('delete', $type, $array);
        }
        elseif ($array['uuid'] != $existing_uuid) {
          db_query("UPDATE {$table} SET uuid = '%s' WHERE {$keyfield} = %d", $array['uuid'], $key);
        }

        // Otherwise, don't do anything. No update necessary.
      }
      else {
        uuid_taxonomy('insert', $type, $array);
      }
      break;
    case 'delete':
      db_query("DELETE FROM {$table} WHERE {$keyfield} = %d", $keyfield, $key);
      break;
  }
}