You are here

function xmlsitemap_write_record in XML sitemap 6.2

Special implementation of drupal_write_record() to allow NULL values.

@todo Remove when http://drupal.org/node/227677 is fixed.

1 call to xmlsitemap_write_record()
xmlsitemap_link_save in ./xmlsitemap.module
Saves or updates a sitemap link.

File

./xmlsitemap.inc, line 66
Miscellaneous functions for the xmlsitemap module.

Code

function xmlsitemap_write_record($table, &$object, $update = array()) {

  // Standardize $update to an array.
  if (is_string($update)) {
    $update = array(
      $update,
    );
  }
  $schema = drupal_get_schema($table);
  if (empty($schema)) {
    return FALSE;
  }

  // Convert to an object if needed.
  if (is_array($object)) {
    $object = (object) $object;
    $array = TRUE;
  }
  else {
    $array = FALSE;
  }
  $fields = $defs = $values = $serials = $placeholders = array();
  $object_fields = get_object_vars($object);

  // Go through our schema, build SQL, and when inserting, fill in defaults for
  // fields that are not set.
  foreach ($schema['fields'] as $field => $info) {

    // Special case -- skip serial types if we are updating.
    if ($info['type'] == 'serial') {
      if (empty($update)) {

        // Track serial fields so we can helpfully populate them after the query.
        $serials[] = $field;
      }
      continue;
    }

    // For inserts, populate defaults from Schema if not already provided
    if (!isset($object->{$field}) && !count($update) && isset($info['default']) && !array_key_exists($field, $object_fields)) {
      $object->{$field} = $info['default'];
    }

    // Build arrays for the fields, placeholders, and values in our query.
    if (isset($object->{$field}) || array_key_exists($field, $object_fields) && empty($info['not null'])) {
      $fields[] = $field;
      if (isset($object->{$field})) {
        $placeholders[] = db_type_placeholder($info['type']);
        $values[] = empty($info['serialize']) ? $object->{$field} : serialize($object->{$field});
      }
      else {
        $placeholders[] = '%s';
        $values[] = 'NULL';
      }
    }
  }

  // Build the SQL.
  $query = '';
  if (!count($update)) {
    $query = "INSERT INTO {" . $table . "} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ')';
    $return = SAVED_NEW;
  }
  else {
    $query = '';
    foreach ($fields as $id => $field) {
      if ($query) {
        $query .= ', ';
      }
      $query .= $field . ' = ' . $placeholders[$id];
    }
    foreach ($update as $key) {
      $conditions[] = "{$key} = " . db_type_placeholder($schema['fields'][$key]['type']);
      $values[] = $object->{$key};
    }
    $query = "UPDATE {" . $table . "} SET {$query} WHERE " . implode(' AND ', $conditions);
    $return = SAVED_UPDATED;
  }

  // Execute the SQL.
  if (db_query($query, $values)) {
    if ($serials) {

      // Get last insert ids and fill them in.
      foreach ($serials as $field) {
        $object->{$field} = db_last_insert_id($table, $field);
      }
    }
  }
  else {
    $return = FALSE;
  }

  // If we began with an array, convert back so we don't surprise the caller.
  if ($array) {
    $object = (array) $object;
  }
  return $return;
}