cvs diff: Diffing uc_order Index: uc_order/uc_order.migrate.inc =================================================================== RCS file: uc_order/uc_order.migrate.inc diff -N uc_order/uc_order.migrate.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ uc_order/uc_order.migrate.inc 8 Nov 2009 00:06:14 -0000 @@ -0,0 +1,306 @@ + t('UberCart order'), + 'uc_order_product' => t('UberCart order product'), + ); +} + +/** + * @defgroup uc_order_migrate_uc_order Migrate UC order records + * @{ + * Functions related to importing records into the {uc_orders} table. + */ + +/** + * Implement hook_migrate_fields_[type](). + * + * This defines what target fields the uc_order migrate type supports. + */ +function uc_order_migrate_fields_uc_order($type) { + return array( + 'order_id' => t('UC Order: Order ID'), + 'uid' => t('UC Order: User ID'), + 'order_status' => t('UC Order: Order status'), + 'order_total' => t('UC Order: Order total'), + 'primary_email' => t('UC Order: Primary email'), + 'delivery_first_name' => t('UC Order: Delivery: First name'), + 'delivery_last_name' => t('UC Order: Delivery: Last name'), + 'delivery_phone' => t('UC Order: Delivery: Phone'), + 'delivery_company' => t('UC Order: Delivery: Company'), + 'delivery_street1' => t('UC Order: Delivery: Street 1'), + 'delivery_street2' => t('UC Order: Delivery: Street 2'), + 'delivery_city' => t('UC Order: Delivery: City'), + 'delivery_zone' => t('UC Order: Delivery: Zone: Numeric ID'), + 'delivery_zone_code' => t('UC Order: Delivery: Zone: Code (only specify one zone field)'), + 'delivery_zone_name' => t('UC Order: Delivery: Zone: Name (only specify one zone field)'), + 'delivery_postal_code' => t('UC Order: Delivery: Postal Code'), + 'delivery_country' => t('UC Order: Delivery: Country: Numeric ID'), + 'delivery_country_name' => t('UC Order: Delivery: Country: Name (only specify one country field)'), + 'delivery_country_iso2' => t('UC Order: Delivery: Country: 2 character ISO code (only specify one country field)'), + 'delivery_country_iso3' => t('UC Order: Delivery: Country: 3 character ISO code (only specify one country field)'), + 'billing_first_name' => t('UC Order: Billing: First name'), + 'billing_last_name' => t('UC Order: Billing: Last name'), + 'billing_phone' => t('UC Order: Billing: Phone'), + 'billing_company' => t('UC Order: Billing: Company'), + 'billing_street1' => t('UC Order: Billing: Street 1'), + 'billing_street2' => t('UC Order: Billing: Street 2'), + 'billing_city' => t('UC Order: Billing: City'), + 'billing_zone' => t('UC Order: Billing: Zone: Numeric ID'), + 'billing_zone_code' => t('UC Order: Billing: Zone: Code (only specify one zone field)'), + 'billing_zone_name' => t('UC Order: Billing: Zone: Name (only specify one zone field)'), + 'billing_postal_code' => t('UC Order: Billing: Postal Code'), + 'billing_country' => t('UC Order: Billing: Country: Numeric ID'), + 'billing_country_name' => t('UC Order: Billing: Country: Name (only specify one country field)'), + 'billing_country_iso2' => t('UC Order: Billing: Country: 2 character ISO code (only specify one country field)'), + 'billing_country_iso3' => t('UC Order: Billing: Country: 3 character ISO code (only specify one country field)'), + 'payment_method' => t('UC Order: Payment method'), + 'data' => t('UC Order: Data (serialized array of extra info)'), + 'created' => t('UC Order: Created timestamp'), + 'modified' => t('UC Order: Last modified timestamp'), + 'host' => t('UC Order: Host IP address'), + ); +} + +/** + * Implement hook_migrate_delete_[type](). + * + * Force the clearing out of imported uc_order records. + */ +function uc_order_migrate_delete_uc_order($order_id) { + db_query("DELETE FROM {uc_orders} WHERE order_id = %d", $order_id); +} + +/** + * Implement hook_migrate_import_[type](). + * + * Import the given data into a new uc_order object and save it to the DB. + * This function invokes hook_migrate_prepare_uc_order() to give modules + * a chance to alter the $order object before it is written to the DB, and + * hook_migrate_complete_uc_order() for additional processing after the + * record is written and has a valid order_id value. + */ +function uc_order_migrate_import_uc_order($tblinfo, $row) { + $sourcekey = $tblinfo->sourcekey; + $order = new stdClass; + foreach ($tblinfo->fields as $destfield => $values) { + if ($values['srcfield'] && isset($row->$values['srcfield'])) { + $value = $row->$values['srcfield']; + } + else { + $value = $values['default_value']; + } + + // Handle special-case fields: + switch ($destfield) { + case 'created': + case 'modified': + $value = _migrate_valid_date($value); + break; + } + + // Handle the special country fields to map into {uc_countries} IDs. + // We'll handle the special zone fields later so that we know we + // already have the right country code. + $matches = array(); + if (!empty($value) && preg_match('@(delivery|billing)_(country_.*)@', $destfield, $matches)) { + $type = $matches[1]; + $field = $matches[2]; + $country_field = $type . '_country'; + if (isset($order->$country_field)) { + $errors[] = migrate_message(t('Order already has a %field defined', array('%field' => $country_field))); + return $errors; + } + switch ($field) { + case 'country_name': + $where = "country_name = '%s'"; + break; + + case 'country_iso2': + $where = "country_iso_code_2 = '%s'"; + break; + + case 'country_iso3': + $where = "country_iso_code_3 = '%s'"; + break; + + } + $value = db_result(db_query("SELECT country_id FROM {uc_countries} WHERE $where", $value)); + $destfield = $country_field; + } + + $order->$destfield = $value; + } + + // Now, walk through all the fields again and handle the special zone fields. + foreach ($tblinfo->fields as $destfield => $values) { + $matches = array(); + if (!empty($order->$destfield) && preg_match('@(delivery|billing)_(zone_.*)@', $destfield, $matches)) { + $type = $matches[1]; + $field = $matches[2]; + $zone_field = $type . '_zone'; + if (isset($order->$zone_field)) { + $errors[] = migrate_message(t('Order already has a %field defined', array('%field' => $zone_field))); + return $errors; + } + $value = $order->$destfield; + switch ($field) { + case 'zone_code': + $where = "zone_code = '%s'"; + break; + + case 'zone_name': + $where = "zone_name = '%s'"; + break; + + } + $arguments = array($value); + $country_field = $type . '_country'; + if (!empty($order->$country_field) && is_numeric($order->$country_field)) { + $where .= " AND zone_country_id = %d"; + $arguments[] = $order->$country_field; + } + $value = db_result(db_query("SELECT zone_id FROM {uc_zones} WHERE $where", $arguments)); + $order->$zone_field = $value; + } + } + + $errors = migrate_destination_invoke_all('prepare_uc_order', $order, $tblinfo, $row); + + $success = TRUE; + foreach ($errors as $error) { + if ($error['level'] != MIGRATE_MESSAGE_INFORMATIONAL) { + $success = FALSE; + break; + } + } + + if ($success) { + drupal_write_record('uc_orders', $order); + $order_id = $order->order_id; + + // Invoke completion hooks for any processing which needs to be done + // after the order exists in the DB and has an order_id. + $errors = array_merge($errors, migrate_destination_invoke_all('complete_uc_order', $order, $tblinfo, $row)); + + if (!empty($order_id)) { + migrate_add_mapping($tblinfo->mcsid, $row->$sourcekey, $order_id); + } + elseif (empty($errors)) { + $errors[] = migrate_message(t('drupal_write_record() failed for the new order: Unknown reason')); + } + } + + return $errors; +} + +/** + * @} End of "defgroup uc_order_migrate_uc_order". + */ + + +/** + * @defgroup uc_order_migrate_uc_order_product Migrate UC order product records + * @{ + * Functions related to importing records into the {uc_order_products} table. + */ + +/** + * Implement hook_migrate_fields_[type](). + * + * This defines what target fields the uc_order_product migrate type supports. + */ +function uc_order_migrate_fields_uc_order_product($type) { + return array( + 'order_product_id' => t('UC Order: Product: Order Product ID'), + 'order_id' => t('UC Order: Product: Order ID'), + 'nid' => t('UC Order: Product: Node ID'), + 'title' => t('UC Order: Product: Title'), + 'manufacturer' => t('UC Order: Product: Manufacturer'), + 'model' => t('UC Order: Product: Module (SKU)'), + 'qty' => t('UC Order: Product: Quantity'), + 'cost' => t('UC Order: Product: Cost'), + 'price' => t('UC Order: Product: Price'), + 'weight' => t('UC Order: Product: Weight'), + 'data' => t('UC Order: Product: Data (serialized array of extra info)'), + ); +} + +/** + * Implement hook_migrate_delete_[type](). + * + * Force the clearing out of imported uc_order_product records. + */ +function uc_order_migrate_delete_uc_order_product($order_product_id) { + db_query("DELETE FROM {uc_order_products} WHERE order_product_id = %d", $order_id); +} + +/** + * Implement hook_migrate_import_[type](). + * + * Import the given data into a new uc_order_product object and save it to + * the DB. This function invokes hook_migrate_prepare_uc_order_product() to + * give modules a chance to alter the $order_product object before it is + * written to the DB, and hook_migrate_complete_uc_order_product() for + * additional processing after the record is written and has a valid + * order_product_id value. + */ +function uc_order_migrate_import_uc_order_product($tblinfo, $row) { + $sourcekey = $tblinfo->sourcekey; + $order_product = new stdClass; + foreach ($tblinfo->fields as $destfield => $values) { + if ($values['srcfield'] && isset($row->$values['srcfield'])) { + $value = $row->$values['srcfield']; + } + else { + $value = $values['default_value']; + } + $order_product->$destfield = $value; + } + + $errors = migrate_destination_invoke_all('prepare_uc_order_product', $order_product, $tblinfo, $row); + + $success = TRUE; + foreach ($errors as $error) { + if ($error['level'] != MIGRATE_MESSAGE_INFORMATIONAL) { + $success = FALSE; + break; + } + } + + if ($success) { + drupal_write_record('uc_order_products', $order_product); + $order_product_id = $order_product->order_product_id; + + // Invoke completion hooks for any processing which needs to be done + // after the order exists in the DB and has an order_id. + $errors = array_merge($errors, migrate_destination_invoke_all('complete_uc_order_product', $order_product, $tblinfo, $row)); + + if (!empty($order_product_id)) { + migrate_add_mapping($tblinfo->mcsid, $row->$sourcekey, $order_product_id); + } + elseif (empty($errors)) { + $errors[] = migrate_message(t('drupal_write_record() failed for the new order product: Unknown reason')); + } + } + + return $errors; +} + +/** + * @} End of "defgroup uc_order_migrate_uc_order_product". + */ Index: uc_order/uc_order.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/uc_order/uc_order.module,v retrieving revision 1.12.2.33 diff -u -p -r1.12.2.33 uc_order.module --- uc_order/uc_order.module 29 Oct 2009 20:35:58 -0000 1.12.2.33 +++ uc_order/uc_order.module 8 Nov 2009 00:06:15 -0000 @@ -319,6 +319,10 @@ function uc_order_init() { ), 'setting'); drupal_add_js(drupal_get_path('module', 'uc_order') .'/uc_order.js'); } + + if (module_exists('migrate')) { + module_load_include('inc', 'uc_order', 'uc_order.migrate'); + } } /** cvs diff: Diffing uc_order/images cvs diff: Diffing uc_order/templates cvs diff: Diffing uc_order/templates/translations cvs diff: Diffing uc_order/translations