You are here

amazon.install in Amazon Product Advertisement API 6

Same filename and directory in other branches
  1. 7.2 amazon.install
  2. 7 amazon.install

File

amazon.install
View source
<?php

/**
 * Implements hook_install.
 */
function amazon_install() {
  drupal_install_schema('amazon');
}

/**
 * Implements hook_uninstall().
 */
function amazon_uninstall() {
  drupal_uninstall_schema('amazon');
  $variables = preg_split('/\\s+/', "amazon_media_data\n    amazon_default_image\n    amazon_default_image_large_preset\n    amazon_default_image_medium_preset\n    amazon_default_image_small_preset\n    amazon_locale\n    amazon_associate_setting\n    amazon_custom_associate_id\n    amazon_aws_access_key\n    amazon_aws_secret_access_key\n    amazon_refresh_schedule\n    amazon_core_data");
  foreach ($variables as $var) {
    variable_del($var);
  }
}

/**
 * implementation of hook_requirements to make sure we let them know about
 * the requirement for amazon_aws_secret_access_key
 * @param $phase
 * @return A $requirement[] array, or NULL if there are no issues.
 */
function amazon_requirements($phase) {
  if ($phase != 'runtime') {
    return;
  }
  $secret_access_key = variable_get('amazon_aws_secret_access_key', "");
  $api_key = variable_get('amazon_aws_access_key', "");
  $associate_tag = amazon_get_associate_id();
  if (empty($secret_access_key) || empty($api_key) || empty($associate_tag)) {
    drupal_set_message(t("The Amazon API must be configured with an Associate Tag, an Access Key ID and an Amazon AWS Secret Access Key to function. Go to !settings.", array(
      "!settings" => l(t("Amazon API Setttings"), 'admin/settings/amazon'),
    )));
    $requirement['title'] = t("Amazon AWS Settings");
    $requirement['severity'] = REQUIREMENT_WARNING;
    $requirement['value'] = t("Not Set");
    $requirement['description'] = t("The Amazon API must be configured with an Associate Tag, an Access Key ID and an Amazon AWS Secret Access Key to function. Go to !settings.", array(
      "!settings" => l(t("Amazon API Setttings"), 'admin/settings/amazon'),
    ));
    return array(
      'amazon_aws_secret_access_key' => $requirement,
    );
  }
  return NULL;
}
function amazon_schema() {
  $schema['amazon_item'] = array(
    'fields' => array(
      'asin' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'detailpageurl' => array(
        'type' => 'text',
      ),
      'salesrank' => array(
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'brand' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'publisher' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'manufacturer' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'mpn' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
      'studio' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'binding' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'releasedate' => array(
        'type' => 'varchar',
        'length' => 64,
      ),
      'listpriceamount' => array(
        'type' => 'numeric',
        'not null' => FALSE,
        'precision' => '10',
        'scale' => '0',
      ),
      'listpricecurrencycode' => array(
        'type' => 'varchar',
        'length' => 32,
      ),
      'listpriceformattedprice' => array(
        'type' => 'varchar',
        'length' => 32,
      ),
      'lowestpriceamount' => array(
        'type' => 'numeric',
        'not null' => FALSE,
        'precision' => '10',
        'scale' => '0',
      ),
      'lowestpricecurrencycode' => array(
        'type' => 'varchar',
        'length' => 32,
      ),
      'lowestpriceformattedprice' => array(
        'type' => 'varchar',
        'length' => 32,
      ),
      'amazonpriceamount' => array(
        'type' => 'numeric',
        'not null' => FALSE,
        'precision' => '10',
        'scale' => '0',
      ),
      'amazonpricecurrencycode' => array(
        'type' => 'varchar',
        'length' => 32,
      ),
      'amazonpriceformattedprice' => array(
        'type' => 'varchar',
        'length' => 32,
      ),
      'productgroup' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
      'producttypename' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
      'customerreviews_iframe' => array(
        'type' => 'varchar',
        'length' => 255,
      ),
      'invalid_asin' => array(
        'type' => 'int',
        'default' => 0,
      ),
      'timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'asin',
    ),
  );
  $schema['amazon_item_participant'] = array(
    'fields' => array(
      'asin' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'participant' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'asin' => array(
        'asin',
      ),
    ),
  );
  $schema['amazon_item_image'] = array(
    'fields' => array(
      'asin' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'size' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'url' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'height' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'width' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'asin',
      'size',
    ),
    'indexes' => array(
      'asin' => array(
        'asin',
      ),
    ),
  );
  $schema['amazon_item_editorial_review'] = array(
    'fields' => array(
      'asin' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'source' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'content' => array(
        'type' => 'text',
      ),
    ),
    'indexes' => array(
      'asin' => array(
        'asin',
      ),
    ),
  );
  $schema['amazon_item_node'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'asin' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'notes' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => 'amazon',
      ),
      'delta' => array(
        'type' => 'int',
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'asin',
      'module',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
      'vid' => array(
        'vid',
      ),
      'vid_module' => array(
        'vid',
        'module',
      ),
      'asin' => array(
        'asin',
      ),
    ),
  );
  return $schema;
}

/**
 * Update variables from legacy AAT module where possible. Add mpn field.
 */
function amazon_update_6001() {
  $ret = array();
  db_add_column($ret, 'amazon_item', 'mpn', 'varchar(128)');

  // If the access key is not set, and there is a D5 amazontools access key, use it.
  $d6_aws_access_key = variable_get('amazon_aws_access_key', "");
  if (empty($d6_aws_access_key)) {
    $old_access_key = variable_get('amazon_awsaccess_key', "");
    if (!empty($old_access_key)) {
      variable_set('amazon_aws_access_key', $old_access_key);
      variable_del('amazon_awsaccess_key');
      $ret[] = array(
        'success' => TRUE,
        'query' => 'Updated Amazon AWS Access Key from D5 AAT',
      );
    }
  }

  // Bring over the d5 associate id if possible.
  $d6_associate_id = variable_get("amazon_custom_associate_id", "");
  if (empty($d6_associate_id)) {
    $associate_id = variable_get("amazon_associate_id", "");
    if (!empty($associate_id)) {
      variable_set('amazon_custom_associate_id', $associate_id);
      variable_set('amazon_associate_setting', 'custom');
      variable_del("amazon_associate_id");
      $ret[] = array(
        'success' => TRUE,
        'query' => 'Updated Amazon Associate ID from D5 AAT',
      );
    }
  }
  if (is_numeric(variable_get('amazon_locale', "US"))) {
    variable_del('amazon_locale');
    drupal_set_message(t("The Amazon locale setting was invalid. Please visit the <a href='!url'>Amazon settings page</a> to correct it.", array(
      '!url' => url('admin/settings/amazon'),
    )));
  }
  return $ret;
}
function amazon_update_6002() {
  $ret = array();

  // Make sure that primary keys are correct
  db_drop_primary_key($ret, 'amazon_item_image');
  db_add_primary_key($ret, 'amazon_item_image', array(
    'asin',
    'size',
  ));
  return $ret;
}

/**
 * http://drupal.org/node/426786 - Allow longer image url fields.
 */
function amazon_update_6003() {
  $ret = array();
  db_change_field($ret, 'amazon_item_image', 'url', 'url', array(
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
  ));
  return $ret;
}

/**
 * Ugly hack for those updating from amazon module in D5.
 */
function amazon_update_6004() {
  $ret = array();
  if (!db_table_exists('amazon_item')) {
    drupal_install_schema('amazon');
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Force-installed Amazon module tables after botched D5 upgrade.',
    );
  }
  return $ret;
}
function amazon_update_6005() {
  $ret = array();
  db_change_field($ret, 'amazon_item', 'detailpageurl', 'detailpageurl', array(
    'type' => 'text',
  ));
  db_change_field($ret, 'amazon_item', 'brand', 'brand', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'publisher', 'publisher', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'manufacturer', 'manufacturer', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'studio', 'studio', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'label', 'label', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'binding', 'binding', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'releasedate', 'releasedate', array(
    'type' => 'varchar',
    'length' => 64,
  ));
  db_change_field($ret, 'amazon_item', 'productgroup', 'productgroup', array(
    'type' => 'varchar',
    'length' => 255,
  ));
  db_change_field($ret, 'amazon_item', 'producttypename', 'producttypename', array(
    'type' => 'varchar',
    'length' => 255,
  ));
  return $ret;
}
function amazon_update_6006() {
  $ret = array();
  db_add_field($ret, 'amazon_item', 'invalid_asin', array(
    'type' => 'int',
    'default' => 0,
  ));
  return $ret;
}
function amazon_update_6007() {
  $ret = array();
  if (variable_get('amazon_associate_setting', 'associate') == 'author') {
    variable_del('amazon_associate_setting');

    // go with the default.
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Replaced "author" Amazon associate setting with the default.'),
    );
  }
  return $ret;
}

/**
 * Change listpriceamount field to numeric. It's not floating anyway.
 */
function amazon_update_6008() {
  $ret = array();
  db_change_field($ret, 'amazon_item', 'listpriceamount', 'listpriceamount', array(
    'type' => 'numeric',
  ));
  return $ret;
}

/**
 * Change key on amazon_item_node (again).
 *
 * It looks like nid should be the correct portion of the key, since there
 * is no vid coming in. In fact... There should be no vid.
 *
 */
function amazon_update_6009() {
  $ret = array();
  db_drop_primary_key($ret, 'amazon_item_node');
  db_add_primary_key($ret, 'amazon_item_node', array(
    'nid',
    'asin',
    'module',
  ));
  return $ret;
}

/**
 * Change key on editorial reviews, since it shouldn't be primary.
 */
function amazon_update_6010() {
  $ret = array();
  db_drop_primary_key($ret, 'amazon_item_editorial_review');
  db_add_index($ret, 'amazon_item_editorial_review', 'amazon_item_editorial_review', array(
    'asin',
  ));
  return $ret;
}

/**
 * Add Customer Reviews table so we can handle that.
 * @return unknown_type
 */
function amazon_update_6011() {
  $schema = amazon_schema();
  if (!db_table_exists('amazon_item_customer_review')) {
    db_create_table($ret, 'amazon_item_customer_review', $schema['amazon_item_customer_review']);
  }
  return $ret;
}

/**
 * Add new elements to the amazon_item table: Lowest price, etc.
 * @return unknown_type
 */
function amazon_update_6012() {
  db_add_field($ret, 'amazon_item', 'lowestpriceamount', array(
    'type' => 'numeric',
  ));
  db_add_field($ret, 'amazon_item', 'lowestpricecurrencycode', array(
    'type' => 'numeric',
  ));
  db_add_field($ret, 'amazon_item', 'lowestpriceformattedprice', array(
    'type' => 'varchar',
    'length' => 32,
  ));
  db_add_field($ret, 'amazon_item', 'amazonpriceamount', array(
    'type' => 'numeric',
  ));
  db_add_field($ret, 'amazon_item', 'amazonpricecurrencycode', array(
    'type' => 'numeric',
  ));
  db_add_field($ret, 'amazon_item', 'amazonpriceformattedprice', array(
    'type' => 'varchar',
    'length' => 32,
  ));
  return $ret;
}

/**
 * Fix messed up type for currency code in 6012.
 * @return unknown_type
 */
function amazon_update_6013() {
  db_change_field($ret, 'amazon_item', 'lowestpricecurrencycode', 'lowestpricecurrencycode', array(
    'type' => 'varchar',
    'length' => 32,
  ));
  db_change_field($ret, 'amazon_item', 'amazonpricecurrencycode', 'amazonpricecurrencycode', array(
    'type' => 'varchar',
    'length' => 32,
  ));
  return $ret;
}

/**
 * Implementation of a default image requires that all the images be updated.
 * Here we'll just set the timestamp to 0 and then let cron finish the job.
 */
function amazon_update_6014() {

  // Force all data to be updated from Amazon so that images can be properly updated.
  $ret[] = update_sql('UPDATE {amazon_item} SET timestamp = 0;');
  drupal_set_message(t('If you have enabled a default image, you may need to wait for a few cron runs (or run cron a few times) before all images are properly set up.'));
  return $ret;
}

/**
 * Amazon has stopped providing customer review data; now we only get an
 * iframe link where Amazon will provide the data, so we need to drop the table
 * that was used for customer reviews and add the new iframe link.
 */
function amazon_update_6015() {
  db_add_field($ret, 'amazon_item', 'customerreviews_iframe', array(
    'type' => 'varchar',
    'length' => 255,
  ));
  db_drop_table($ret, 'amazon_item_customer_review');
  $ret[] = update_sql('UPDATE {amazon_item} SET timestamp = 0;');
  return $ret;
}

/**
 * Increase the length of the mpn field.
 */
function amazon_update_6016() {
  $ret = array();
  db_change_field($ret, 'amazon_item', 'mpn', 'mpn', array(
    'type' => 'varchar',
    'length' => 255,
  ));
  return $ret;
}

Functions

Namesort descending Description
amazon_install Implements hook_install.
amazon_requirements implementation of hook_requirements to make sure we let them know about the requirement for amazon_aws_secret_access_key
amazon_schema
amazon_uninstall Implements hook_uninstall().
amazon_update_6001 Update variables from legacy AAT module where possible. Add mpn field.
amazon_update_6002
amazon_update_6003 http://drupal.org/node/426786 - Allow longer image url fields.
amazon_update_6004 Ugly hack for those updating from amazon module in D5.
amazon_update_6005
amazon_update_6006
amazon_update_6007
amazon_update_6008 Change listpriceamount field to numeric. It's not floating anyway.
amazon_update_6009 Change key on amazon_item_node (again).
amazon_update_6010 Change key on editorial reviews, since it shouldn't be primary.
amazon_update_6011 Add Customer Reviews table so we can handle that.
amazon_update_6012 Add new elements to the amazon_item table: Lowest price, etc.
amazon_update_6013 Fix messed up type for currency code in 6012.
amazon_update_6014 Implementation of a default image requires that all the images be updated. Here we'll just set the timestamp to 0 and then let cron finish the job.
amazon_update_6015 Amazon has stopped providing customer review data; now we only get an iframe link where Amazon will provide the data, so we need to drop the table that was used for customer reviews and add the new iframe link.
amazon_update_6016 Increase the length of the mpn field.