diff --git aat_legacy/aat_legacy.d5-migrate.inc aat_legacy/aat_legacy.d5-migrate.inc new file mode 100644 index 0000000..2b11b52 --- /dev/null +++ aat_legacy/aat_legacy.d5-migrate.inc @@ -0,0 +1,66 @@ +nid] = $node->title; + + $context['sandbox']['progress']++; + $context['sandbox']['current_node'] = $node->nid; + $context['message'] = t('Processing nid %nid, %title', array('!url' => url("node/$node->nid"), '%nid' => $node->nid, '%title' => $node->title)); + } + } +} +/** + * Batch 'finished' callback + */ +function _aat_legacy_migrate_fields_finished($success, $results, $operations) { + if ($success) { + $message = t('%count legacy fields (amazonnodes and links) were imported.', array('%count' => count($results))); + $message .= theme('item_list', $results); + } + else { + // An error occurred. + // $operations contains the operations that remained unprocessed. + $error_operation = reset($operations); + $message = t('An error occurred while processing %op. with arguments = %args.', array('%op' => $error_operation[0], '%args' => print_r($error_operation[0], TRUE))); + } + drupal_set_message($message); +} + +/** + * Process one node. + * @param $nid + * The nid to which we are to add values. + * @param $type + * The ntype field from amazonnode table: the content type we're working with. + * @return + * the updated node. + */ +function _aat_legacy_migrate_from_d5($nid, $type) { + $results = db_query("SELECT asin, ntype FROM {amazonnode} an WHERE an.nid = %d and an.ntype = '%s'", $nid, $type); + $dirty = FALSE; + $node = node_load($nid); + + while ($asin = db_fetch_object($results)) { + // if (!in_array($asin->asin, array_keys(array_values($node->field_legacy_asin)))) { + $node->field_legacy_asin[]['asin'] = $asin->asin; + // } + } + node_save($node); + $sql = "DELETE FROM {amazonnode} WHERE nid = %d AND ntype = '%s'"; + db_query($sql, $nid, $type); + + return $node; +} \ No newline at end of file diff --git aat_legacy/aat_legacy.info aat_legacy/aat_legacy.info index 0cb5faa..ea7f0eb 100644 --- aat_legacy/aat_legacy.info +++ aat_legacy/aat_legacy.info @@ -1,7 +1,8 @@ ; $Id$ -name = Amazon legacy support -description = Provides an upgrade path for users of the Amazon Associates Tools module. +name = Amazon legacy importer +description = This module is used only to import legacy Drupal 5 Amazontools data. After upgrade it should be disabled. package = Amazon dependencies[] = amazon +dependencies[] = asin core = 6.x php = 5.2 diff --git aat_legacy/aat_legacy.install aat_legacy/aat_legacy.install index 90484fa..a76649e 100644 --- aat_legacy/aat_legacy.install +++ aat_legacy/aat_legacy.install @@ -1,20 +1,10 @@ nid, -1, $asin->asin, 'aat_legacy'); - $i++; - } - drupal_set_message(t("Imported %num records from legacy amazon_node table into amazon_item_node table", array('%num' => $i))); - } -} +} \ No newline at end of file diff --git aat_legacy/aat_legacy.module aat_legacy/aat_legacy.module index 9e17200..0ffb08b 100644 --- aat_legacy/aat_legacy.module +++ aat_legacy/aat_legacy.module @@ -1,46 +1,169 @@ array( - 'name' => t('Amazon review'), - 'module' => 'aat_legacy', - 'description' => t('A review of an Amazon.com product.'), - 'locked' => TRUE, - ), - 'amazon_node' => array( - 'name' => t('Amazon data'), - 'module' => 'aat_legacy', - 'description' => t('Legacy Amazon data created by the Amazon Associates Tools module.'), - 'has_body' => FALSE, - 'locked' => TRUE, - ) +/** + * Implementation of hook_menu. + */ +function aat_legacy_menu() { + $items['admin/settings/aat_legacy'] = array( + 'title' => "Amazontools Legacy Import", + 'page callback' => 'drupal_get_form', + 'access arguments' => array('administer site configuration'), + 'page arguments' => array('aat_legacy_import_form') ); + return $items; +} +/** + * Form to start the import process. + */ +function aat_legacy_import_form() { + $form = array(); + $form['explanation'] = array( + '#type' => 'markup', + '#prefix' => "
", + '#value' => t("Here you can import legacy Drupal 5 items from Amazon Tools. This may take a long time. It may be safely restarted."), + '#suffix' => "
", + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t("Start import"), + ); + return $form; } -function aat_legacy_load($node) { - $asins = array(); - $sql = "SELECT * FROM {amazon_item_node} ain WHERE ain.nid = %d AND ain.module = 'aat_legacy'"; - $results = db_query($sql, $node->nid); - while ($asin = db_fetch_array($results)) { - $asins[$asin['asin']] = $asin; - } - $node->aat_legacy_items = $asins; +function aat_legacy_import_form_submit($form_state, &$form_value) { + aat_legacy_import_data(); } -function aat_legacy_view(&$node, $teaser = FALSE, $page = FALSE) { - $asins = amazon_item_lookup(array_keys($node->aat_legacy_items)); - foreach ($asins as $asin) { - $node->content['aat_legacy_items'][] = array( - '#type' => 'markup', - '#value' => theme('amazon_item', $asin) + +/** + * Create a CCK field for each node type that had Amazon link in D5. + * Then populate it. + */ +function aat_legacy_import_data() { + $ret = array(); + $result = db_query("SELECT DISTINCT type FROM node_type nt, amazonnode an where nt.type = an.ntype"); + // Build a list of fields that need data updating. + module_load_install('content'); + module_load_include('inc', 'content', 'includes/content.admin'); + module_load_include('inc', 'content', 'includes/content.crud'); + + $field = aat_legacy_legacy_field_description(); + + // First attach the new field to the content type. + $types = array(); + while ($type_ary = db_fetch_array($result)) { + $ntype = $type_ary['type']; + $types[] = $ntype; + $instance = content_field_instance_read(array('type_name' => $ntype, 'field_name' => 'field_legacy_asin')); + if (empty($instance)) { + drupal_set_message(t("Adding field_legacy_asin to node type %ntype", array('%ntype' => $ntype))); + $field['type_name'] = $ntype; + content_field_instance_create($field); + } + else { + drupal_set_message(t("field_legacy_asin already exists in node type %ntype; not creating it.", array('%ntype' => $ntype))); + } + } + + // Now batch-import the data from those items. + if (!empty($types)) { + $operations = array(); + $sql = "SELECT DISTINCT(nid) nid, ntype FROM {amazonnode} WHERE ntype IN (" . db_placeholders($types, 'varchar') . ") ORDER BY nid ASC"; + $result = db_query($sql, $types); + while ($item = db_fetch_object($result)) { + $operations[] = array('_aat_legacy_migrate_field_from_d5', array(array('nid' => $item->nid, 'ntype' => $item->ntype))); + } + $batch = array( + 'title' => t('Migrating amazonnode field values into field_legacy_asin CCK fields'), + 'operations' => $operations, + 'finished' => '_aat_legacy_migrate_fields_finished', + 'init_message' => t('Beginning migration of legacy_asin fields.'), + 'progress_message' => t('Processed @current out of @total.'), + 'error_message' => t('AAT Legacy field update encountered an error.'), + 'file' => drupal_get_path('module', 'aat_legacy') .'/aat_legacy.d5-migrate.inc', ); + batch_set($batch); } - return $node; + if (empty($operations)) { + drupal_set_message(t("You have no items remaining to import. You may now disable the Amazon Legacy Importer module (aat_legacy).")); + } + } -function aat_legacy_delete(&$node) { - amazon_item_node_delete(NULL, $node->nid, 'aat_legacy'); +/** + * Just returns field description for field_legacy_asin. + */ +function aat_legacy_legacy_field_description() { + return array ( + 'label' => 'Legacy ASIN', + 'field_name' => 'field_legacy_asin', + 'type' => 'asin', + 'widget_type' => 'asin_text', + 'change' => 'Change basic information', + 'weight' => '31', + 'description' => '', + 'default_value' => + array ( + 0 => + array ( + 'asin' => '', + ), + ), + 'default_value_php' => '', + 'default_value_widget' => NULL, + 'required' => 0, + 'multiple' => '1', + 'op' => 'Save field settings', + 'module' => 'asin', + 'widget_module' => 'asin', + 'columns' => + array ( + 'asin' => + array ( + 'type' => 'varchar', + 'length' => 32, + 'not null' => false, + ), + ), + 'display_settings' => + array ( + 'label' => + array ( + 'format' => 'above', + 'exclude' => 0, + ), + 5 => + array ( + 'format' => 'default', + 'exclude' => 0, + ), + 'teaser' => + array ( + 'format' => 'default', + 'exclude' => 0, + ), + 'full' => + array ( + 'format' => 'default', + 'exclude' => 0, + ), + 4 => + array ( + 'format' => 'default', + 'exclude' => 0, + ), + 2 => + array ( + 'format' => 'default', + 'exclude' => 0, + ), + 3 => + array ( + 'format' => 'default', + 'exclude' => 0, + ), + ), + ); } diff --git amazon.css amazon.css index 140af32..760fa84 100644 --- amazon.css +++ amazon.css @@ -5,7 +5,9 @@ div.field-type-asin { border-top: 2px solid #DDD; padding-top: 3px; } - +div.amazon-item { + clear: both; +} div.amazon-item img { float: left; padding-left: 3px;