function uc_importer_import in Ubercart 5
Imports an XML document into the database.
The script checks for objects that have the same names as those in the XML document. If it finds a duplicate, it may replace that object with the XML, create a new object with marker indicating its imported status, or abort the importing of that particular object.
Parameters
$xml: String of XML data. It should conform to the schema located at http://www.ubercart.org/files/store.xsd
3 calls to uc_importer_import()
- uc_importer_directory_parse in uc_importer/
uc_importer.module - Import XML data from a file.
- uc_importer_import_form_submit in uc_importer/
uc_importer.module - Submit function for uc_importer_import_form().
- uc_repeater_import in uc_repeater/
uc_repeater.module
File
- uc_importer/
uc_importer.module, line 777 - XML product importer and exporter.
Code
function uc_importer_import($xml) {
include_once drupal_get_path('module', 'uc_store') . '/includes/simplexml.php';
global $user, $active_db;
$error = '';
/* $data = new DOMDocument();
$data->loadXML($xml);
if (!$data) {
$error = "Error: Could not load XML.";
}
else { */
/* if (!($data->schemaValidate('http://www.ubercart.org/files/store.xsd'))) {
$error = "Error: XML is not validated by schema.";
}
else { */
$id_map = array(
'vocabularies' => array(),
'categories' => array(),
'manufacturers' => array(),
'attributes' => array(),
'options' => array(),
'classes' => array(),
'products' => array(),
);
$store = new JSimpleXML();
$store
->loadString($xml);
if (module_exists('taxonomy')) {
if (isset($store->document->vocabularies)) {
foreach ($store->document->vocabularies[0]->vocabulary as $vocabulary_data) {
$name = (string) $vocabulary_data->name[0]
->data();
$result = db_query("SELECT vid FROM {vocabulary} WHERE name = '%s'", $name);
if (db_num_rows($result) && variable_get('uc_importer_vocabulary_duplicates', UC_IMPORTER_DO_NOTHING) != UC_IMPORTER_INCREMENT) {
$id_map['vocabularies'][(string) $vocabulary_data->id[0]
->data()] = db_result($result);
}
else {
$vocab = array(
'name' => html_entity_decode((string) $vocabulary_data->name[0]
->data(), ENT_QUOTES, "UTF-8"),
'description' => html_entity_decode((string) $vocabulary_data->description[0]
->data(), ENT_QUOTES, "UTF-8"),
'relations' => (int) $vocabulary_data->relations[0]
->data(),
'hierarchy' => (int) $vocabulary_data->hierarchy[0]
->data(),
'multiple' => (int) $vocabulary_data->multiple[0]
->data(),
'required' => (int) $vocabulary_data->required[0]
->data(),
'tags' => (int) $vocabulary_data->tags[0]
->data(),
'weight' => (int) $vocabulary_data->weight[0]
->data(),
);
foreach ($vocabulary_data->nodes as $node_type) {
$vocab['nodes'][(string) $node_type
->data()] = true;
}
taxonomy_save_vocabulary($vocab);
$id_map['vocabularies'][(int) $vocabulary_data->id[0]
->data()] = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{vocabulary}_vid'"));
}
module_invoke_all('xml_importer', 'vocabulary', taxonomy_get_vocabulary($id_map['vocabularies'][(int) $vocabulary_data->id[0]
->data()]), $vocabulary_data, $store, $id_map);
}
}
$categories = array();
if (isset($store->document->categories)) {
$new_categories = array();
foreach ($store->document->categories[0]->category as $category_data) {
$name = (string) $category_data->name[0]
->data();
$result = db_query("SELECT tid FROM {term_data} WHERE name = '%s'", $name);
if (db_num_rows($result) && variable_get('uc_importer_category_duplicates', UC_IMPORTER_DO_NOTHING) != UC_IMPORTER_INCREMENT) {
$id_map['categories'][(string) $category_data->id[0]
->data()] = db_result($result);
}
else {
$tid = db_next_id('{term_data}_tid');
$vid = isset($id_map['vocabularies'][(int) $category_data->vid[0]
->data()]) ? $id_map['vocabularies'][(int) $category_data->vid[0]
->data()] : variable_get('uc_catalog_vid', 0);
$id_map['categories'][(string) $category_data->id[0]
->data()] = $tid;
$new_categories[] = $tid;
db_query("INSERT INTO {term_data} (tid, vid, name, description) VALUES (%d, %d, '%s', '%s')", $tid, $vid, html_entity_decode((string) $category_data->name[0]
->data(), ENT_QUOTES, "UTF-8"), html_entity_decode((string) $category_data->description[0]
->data(), ENT_QUOTES, "UTF-8"));
}
}
foreach ($store->document->categories[0]->category as $category_data) {
if (in_array($id_map['categories'][(string) $category_data->id[0]
->data()], $new_categories)) {
$parent = 0;
if (isset($category_data->parent)) {
$parent = (string) $category_data->parent[0]
->data();
}
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
db_query("INSERT IGNORE INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)", $id_map['categories'][(string) $category_data->id[0]
->data()], $id_map['categories'][$parent]);
break;
case 'pgsql':
db_query("INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)", $id_map['categories'][(string) $category_data->id[0]
->data()], $id_map['categories'][$parent]);
break;
}
}
module_invoke_all('xml_importer', 'category', taxonomy_get_term($id_map['categories'][(string) $category_data->id[0]
->data()]), $category_data, $store, $id_map);
}
}
db_query("DELETE FROM {term_hierarchy} WHERE tid = parent");
}
if (module_exists('uc_manufacturer')) {
if (isset($store->document->manufacturers)) {
foreach ($store->document->manufacturers[0]->manufacturer as $manufacturer_data) {
$manufacturer = new stdClass();
foreach ($manufacturer_data
->children() as $datum) {
$manufacturer->{$datum
->name()} = (string) $datum
->data();
}
$result = db_query("SELECT tid FROM {term_data} WHERE name LIKE '%s\\__' OR name LIKE '%s'", $manufacturer->name, $manufacturer->name);
if ($tid = db_result($result)) {
$manufacturer->tid = $tid;
$id_map['manufacturers'][(int) $manufacturer->id] = $tid;
if (in_array($tid, $new_categories)) {
drupal_execute('taxonomy_form_term', (array) $manufacturer, variable_get('uc_manufacturer_vid', 0), array(
'tid' => $tid,
));
}
else {
switch (variable_get('uc_importer_category_duplicates', UC_IMPORTER_DO_NOTHING)) {
case UC_IMPORTER_REPLACE:
drupal_execute('taxonomy_form_term', (array) $manufacturer, variable_get('uc_manufacturer_vid', 0), array(
'tid' => $tid,
));
break;
case UC_IMPORTER_INCREMENT:
$manufacturer->name .= '_' . db_num_rows($result);
drupal_execute('taxonomy_form_term', (array) $manufacturer, variable_get('uc_manufacturer_vid', 0));
break;
}
}
}
else {
drupal_execute('taxonomy_form_term', (array) $manufacturer, variable_get('uc_manufacturer_vid', 0));
$id_map['manufacturers'][(int) $manufacturer->id] = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{term_data}_tid'"));
}
module_invoke_all('xml_importer', 'manufacturer', uc_manufacturer_load($id_map['manufacturers'][(int) $manufacturer->id]), $manufacturer_data, $store, $id_map);
}
}
}
if (module_exists('uc_attribute')) {
if (isset($store->document->attributes)) {
foreach ($store->document->attributes[0]->attribute as $attribute_data) {
$attribute = new stdClass();
$attribute->name = html_entity_decode((string) $attribute_data->name[0]
->data(), ENT_QUOTES, "UTF-8");
$attribute->ordering = isset($attribute_data->ordering) ? (int) $attribute_data->ordering[0]
->data() : 0;
$result = db_query("SELECT aid FROM {uc_attributes} WHERE name LIKE '%s\\__' OR name LIKE '%s'", $attribute->name, $attribute->name);
if ($aid = db_result($result)) {
if (variable_get('uc_importer_attribute_duplicates', UC_IMPORTER_DO_NOTHING) == UC_IMPORTER_INCREMENT) {
$attribute->name .= '_' . db_num_rows($result);
drupal_execute('uc_attribute_form', (array) $attribute);
}
}
else {
drupal_execute('uc_attribute_form', (array) $attribute);
$aid = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", $attribute->name));
}
if ($aid) {
$id_map['attributes'][(string) $attribute_data->id[0]
->data()] = $aid;
$attribute->options = array();
if (isset($attribute_data->options)) {
foreach ($attribute_data->options[0]->option as $option_data) {
$option = new stdClass();
$option->name = html_entity_decode((string) $option_data->name[0]
->data(), ENT_QUOTES, "UTF-8");
$option->price = isset($option_data->price) ? (double) $option_data->price[0]
->data() : 0;
$option->weight = isset($option_data->weight) ? (double) $option_data->weight[0]
->data() : 0;
$option->ordering = isset($option_data->ordering) ? (int) $option_data->ordering[0]
->data() : 0;
$result = db_query("SELECT oid FROM {uc_attribute_options} WHERE aid = %d AND (name LIKE '%s\\__' OR name LIKE '%s')", $aid, $option->name, $option->name);
if ($oid = db_result($result)) {
switch (variable_get('uc_importer_attribute_duplicates', UC_IMPORTER_DO_NOTHING)) {
case UC_IMPORTER_INCREMENT:
$option->name .= '_' . db_num_rows($result);
drupal_execute('uc_attribute_option_form', (array) $option, $aid);
$id_map['options'][(string) $option_data->id[0]
->data()] = db_result(db_query("SELECT MAX(oid) FROM {uc_attribute_options}"));
break;
case UC_IMPORTER_REPLACE:
drupal_execute('uc_attribute_option_form', (array) $option, $aid, $oid);
case UC_IMPORTER_DO_NOTHING:
$id_map['options'][(string) $option_data->id[0]
->data()] = $oid;
break;
}
}
else {
drupal_execute('uc_attribute_option_form', (array) $option, $aid);
$id_map['options'][(string) $option_data->id[0]
->data()] = db_result(db_query("SELECT MAX(oid) FROM {uc_attribute_options}"));
}
module_invoke_all('xml_importer', 'option', uc_attribute_option_load($id_map['options'][(string) $option_data->id[0]
->data()]), $option_data, $store, $id_map);
}
}
}
module_invoke_all('xml_importer', 'attribute', uc_attribute_load($id_map['attributes'][(string) $attribute_data->id[0]
->data()]), $attribute_data, $store, $id_map);
}
}
}
$class = 'class';
// keyword workaround
if (isset($store->document->classes)) {
$types = module_invoke_all('node_info');
foreach ($store->document->classes[0]->{$class} as $class_data) {
$class = new stdClass();
$class->pcid = (string) $class_data->id[0]
->data();
$class->name = (string) $class_data->name[0]
->data();
if (isset($class_data->description)) {
$class->description = (string) $class_data->description[0]
->data();
}
$result = db_query("SELECT pcid FROM {uc_product_classes} WHERE pcid = '%s'", $class->pcid);
if ($pcid = db_result($result)) {
switch (variable_get('uc_importer_class_duplicates', UC_IMPORTER_DO_NOTHING)) {
case UC_IMPORTER_REPLACE:
drupal_execute('uc_product_class_form', (array) $class, $class->pcid);
break;
case UC_IMPORTER_INCREMENT:
drupal_execute('uc_product_class_form', (array) $class);
break;
}
}
else {
drupal_execute('uc_product_class_form', (array) $class, $class->pcid);
}
module_invoke('xml_importer', 'class', $types[(string) $class_data->id[0]
->data()], $class_data, $store, $id_map);
}
}
if (isset($store->document->products)) {
foreach ($store->document->products[0]->product as $product_data) {
$product = new stdClass();
//watchdog('importer', '<pre>'. print_r($product_data->unique_hash[0]->data(), true) .'</pre>');
if (!isset($product_data->unique_hash)) {
$product_data
->addChild('unique_hash', md5((string) $product_data->model[0]
->data() . (string) $product_data->list_price[0]
->data() . (string) $product_data->cost[0]
->data() . (string) $product_data->sell_price[0]
->data() . (string) $product_data->weight[0]
->data() . (string) $product_data->weight_units[0]
->data() . (string) $product_data->default_qty[0]
->data() . time()), $product_data
->level() + 1);
}
// Try by unique_hash...
if (!($nid = db_result(db_query("SELECT nid FROM {uc_products} WHERE unique_hash LIKE '%s'", (string) $product_data->unique_hash[0]
->data())))) {
// ...else try by product model.
$nid = db_result(db_query("SELECT nid FROM {uc_products} WHERE model LIKE '%s'", (string) $product_data->model[0]
->data()));
}
if ($nid) {
switch (variable_get('uc_importer_product_duplicates', UC_IMPORTER_DO_NOTHING)) {
case UC_IMPORTER_REPLACE:
$product->nid = $nid;
$product->revision = true;
$product->unique_hash = db_result(db_query("SELECT unique_hash FROM {uc_products} WHERE nid = %d", $nid));
$id_map['products'][(string) $product_data->id[0]
->data()] = $nid;
break;
case UC_IMPORTER_INCREMENT:
unset($product_data->unique_hash);
$id_map['products'][(string) $product_data->id[0]
->data()] = $nid;
break;
case UC_IMPORTER_DO_NOTHING:
$product->nid = $nid;
$id_map['products'][(string) $product_data->id[0]
->data()] = $nid;
continue 2;
}
}
else {
$product->unique_hash = (string) $product_data->unique_hash[0]
->data();
}
$product->type = (string) $product_data->type[0]
->data();
$product->uid = $user->uid;
$product->log = t('Imported product from XML.');
$product->name = $user->name;
$product->status = 1;
$product->format = filter_resolve_format(FILTER_FORMAT_DEFAULT);
if (module_exists('uc_catalog')) {
if (isset($product_data->categories) && count($product_data->categories) > 0) {
foreach ($product_data->categories[0]->category as $category_data) {
$product->taxonomy[] = $id_map['categories'][(string) $category_data->id[0]
->data()];
}
}
}
$product->title = html_entity_decode((string) $product_data->name[0]
->data(), ENT_QUOTES, "UTF-8");
$product->body = html_entity_decode((string) $product_data->description[0]
->data(), ENT_QUOTES, "UTF-8");
$product->teaser = node_teaser($product->body, $product->format);
$product->model = html_entity_decode((string) $product_data->model[0]
->data(), ENT_QUOTES, "UTF-8");
/* if (module_exists('uc_manufacturer') && $manufacturer = variable_get('uc_manufacturer_vid', 0)) {
$product->taxonomy['tags'][$manufacturer] = html_entity_decode((string)$product_data->manufacturer[0]->data(), ENT_QUOTES, "UTF-8");
} */
$product->list_price = (double) $product_data->list_price[0]
->data();
$product->cost = (double) $product_data->cost[0]
->data();
$product->sell_price = (double) $product_data->sell_price[0]
->data();
$product->weight = (double) $product_data->weight[0]
->data();
$product->weight_units = isset($product_data->weight_units) ? (string) $product_data->weight_units[0]
->data() : variable_get('uc_weight_unit', 'lb');
if (isset($product_data->length, $product_data->width, $product_data->height)) {
$product->length = (double) $product_data->length[0]
->data();
$product->width = (double) $product_data->width[0]
->data();
$product->height = (double) $product_data->height[0]
->data();
$product->length_units = isset($product_data->length_units) ? (string) $product_data->length_units[0]
->data() : variable_get('uc_length_unit', 'in');
}
if (isset($product_data->pkg_qty)) {
$product->pkg_qty = (int) $product_data->pkg_qty[0]
->data();
}
if (isset($product_data->default_qty)) {
$product->default_qty = (int) $product_data->default_qty[0]
->data();
}
if (isset($product_data->shippable)) {
$product->shippable = (int) $product_data->shippable[0]
->data();
}
$i = 0;
if (module_exists('imagefield')) {
if (isset($product_data->image)) {
foreach ($product_data->image as $image) {
$image_path = (string) $image->path[0]
->data();
$path_info = pathinfo($image_path);
$image_field = content_fields('field_image_cache', $product->type);
$path = $image_field['widget']['image_path'];
if (!($local_path = file_create_path($path))) {
$local_path = file_check_directory($path, FILE_CREATE_DIRECTORY);
}
$local_path .= '/' . rawurldecode(basename($image_path));
$size = 0;
if (!file_exists($local_path)) {
$input = fopen($image_path, 'rb');
$output = fopen($local_path, 'wb');
while ($data = fread($input, 1024)) {
$size += fwrite($output, $data, 1024);
}
fclose($input);
fclose($output);
}
else {
$size = filesize($local_path);
}
$product->field_image_cache[$i] = array(
'fid' => file_exists($local_path) ? db_result(db_query("SELECT fid FROM {files} WHERE nid = %d AND filepath = '%s'", $nid, $local_path)) : 'upload',
'title' => isset($image->title) ? html_entity_decode((string) $image->title[0]
->data(), ENT_QUOTES, "UTF-8") : '',
'alt' => isset($image->alt) ? html_entity_decode((string) $image->alt[0]
->data(), ENT_QUOTES, "UTF-8") : '',
'filename' => basename($image_path),
'filepath' => $local_path,
'filesize' => $size,
'filemime' => 'image/' . $path_info['extension'],
);
$i++;
}
}
}
if (isset($product_data->fields)) {
$fields = array();
foreach ($product_data->fields[0]->field as $field_data) {
foreach ($field_data->delta as $delta) {
$columns = array();
foreach ($delta
->children() as $value_data) {
$columns[$value_data
->name()] = html_entity_decode((string) $value_data
->data(), ENT_QUOTES, "UTF-8");
}
$field_name = html_entity_decode((string) $field_data->name[0]
->data(), ENT_QUOTES, "UTF-8");
if (!is_array($product->{$field_name})) {
$product->{$field_name} = array();
}
array_push($product->{$field_name}, $columns);
}
}
}
//watchdog('importer', '<pre>'. print_r($product_data, true) .'</pre>');
//watchdog('importer', '<pre>'. print_r($product, true) .'</pre>');
node_save($product);
if (!isset($product->nid)) {
$product->nid = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{node}_nid'"));
}
$id_map['products'][(string) $product_data->id[0]
->data()] = $product->nid;
if (module_exists('uc_attribute')) {
$attr_replace = array();
$attr_values = array();
$opt_replace = array();
$opt_values = array();
if (isset($product_data->attributes)) {
foreach ($product_data->attributes[0]->attribute as $attribute_data) {
if (!isset($id_map['attributes'][(string) $attribute_data->id[0]
->data()])) {
$attribute = new stdClass();
$attribute->name = html_entity_decode((string) $attribute_data->name[0]
->data(), ENT_QUOTES, "UTF-8");
$attribute->ordering = isset($attribute_data->ordering) ? (int) $attribute_data->ordering[0]
->data() : 0;
drupal_execute('uc_attribute_form', (array) $attribute);
$id_map['attributes'][(string) $attribute_data->id[0]
->data()] = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", $attribute->name));
}
$attr_replace[] = '%d,%d,%d,%d';
$attr_values[] = $product->nid;
$attr_values[] = $id_map['attributes'][(string) $attribute_data->id[0]
->data()];
$attr_values[] = isset($attribute_data->ordering) ? (int) $attribute_data->ordering[0]
->data() : 0;
if (isset($attribute_data->options)) {
foreach ($attribute_data->options[0]->option as $option_data) {
if (!isset($id_map['options'][(string) $option_data->id[0]
->data()])) {
$option = new stdClass();
$option->name = html_entity_decode((string) $option_data->name[0]
->data(), ENT_QUOTES, "UTF-8");
$option->price = (double) $option_data->price[0]
->data();
$option->weight = (double) $option_data->weight[0]
->data();
$option->ordering = isset($option_data->ordering) ? (int) $option_data->ordering[0]
->data() : 0;
drupal_execute('uc_attribute_option_form', (array) $option, $id_map['attributes'][(string) $attribute_data->id[0]
->data()]);
$id_map['options'][(string) $option_data->id[0]
->data()] = db_result(db_query("SELECT MAX(oid) FROM {uc_attribute_options}"));
}
$opt_replace[] = '%d,%d,%f,%f,%d';
$opt_values[] = $product->nid;
$opt_values[] = $id_map['options'][(string) $option_data->id[0]
->data()];
$opt_values[] = (double) $option_data->price[0]
->data();
$opt_values[] = (double) $option_data->weight[0]
->data();
$opt_values[] = isset($option_data->ordering) ? (int) $option_data->ordering[0]
->data() : 0;
module_invoke_all('xml_importer', 'product-option', $product->nid, $option_data, $store, $id_map);
}
}
$default_option = isset($attribute_data->default_option) ? (string) $attribute_data->default_option[0]
->data() : $attribute_data->options->option[0]->id[0]
->data();
$attr_values[] = $id_map['options'][$default_option];
module_invoke_all('xml_importer', 'product-attribute', $product->nid, $attribute_data, $store, $id_map);
}
}
if (count($attr_values)) {
db_query("DELETE FROM {uc_product_attributes} WHERE nid = %d", $product->nid);
db_query("INSERT INTO {uc_product_attributes} (nid, aid, ordering, default_option) VALUES (" . implode('),(', $attr_replace) . ")", $attr_values);
}
if (count($opt_values)) {
db_query("DELETE FROM {uc_product_options} WHERE nid = %d", $product->nid);
db_query("INSERT INTO {uc_product_options} (nid, oid, price, weight, ordering) VALUES (" . implode('),(', $opt_replace) . ")", $opt_values);
}
$adjustments = array(
'nid' => $product->nid,
'default' => $product->model,
'body' => array(),
);
if (isset($product_data->adjustments)) {
foreach ($product_data->adjustments[0]->adjustment as $adjustment_data) {
$combination = array();
$old_combo = unserialize(html_entity_decode((string) $adjustment_data->combination[0]
->data(), ENT_QUOTES, "UTF-8"));
if (is_array($old_combo)) {
foreach ($old_combo as $aid => $oid) {
$combination[$id_map['attributes'][$aid]] = $id_map['options'][$oid];
}
$adjustment = array(
'combo_array' => serialize($combination),
'model' => html_entity_decode((string) $adjustment_data->model[0]
->data(), ENT_QUOTES, "UTF-8"),
);
$adjustments['body'][] = $adjustment;
module_invoke_all('xml_importer', 'adjustment', $adjustment, $adjustment_data, $store, $id_map);
}
}
}
if (count($adjustments['body'])) {
uc_product_adjustments_form_submit('uc_product_adjustments_form', $adjustments);
}
}
// Product-by-product hook call to try and improve efficency if 'uc_import' hook
module_invoke_all('xml_importer', 'product', $product, $product_data, $store, $id_map);
}
// General hook call if data to import isn't too Product specific
module_invoke_all('xml_importer', 'store', $store, $id_map);
}
//}
//drupal_set_message('<pre>'. print_r($id_map, true) .'</pre>');
cache_clear_all();
//}
if ($error) {
drupal_set_message($error, 'error');
}
}