You are here

function amazon_item_insert in Amazon Product Advertisement API 7

Same name and namespace in other branches
  1. 6 amazon.module \amazon_item_insert()
  2. 7.2 amazon.module \amazon_item_insert()

Insert 'cleaned' amazon item into database.

Parameters

$item: 'Cleaned' amazon structure.

Return value

No return value.

4 calls to amazon_item_insert()
amazon_cron in ./amazon.module
amazon_test_form_submit in ./amazon.admin.inc
_amazon_item_batch_lookup_from_web in ./amazon.module
Get 10 or less items from the AWS web service. AWS allows ONLY 10 items, See http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.ht....
_asin_devel_generate in asin/asin.module
Utility function that actually provides the values for asin_devel_generate().

File

./amazon.module, line 619

Code

function amazon_item_insert($item) {
  static $item_keys = NULL;
  if (empty($item_keys)) {
    require_once 'amazon.install';
    $schema = amazon_schema();
    $item_keys = $schema['amazon_item']['fields'];
  }

  // We have boatloads of data to insert in here, so we're going to
  // cheat and blow away the old entries first.
  amazon_item_delete($item['asin']);
  $metadata = amazon_data_cache();
  $item['timestamp'] = REQUEST_TIME;

  // The db_insert needs fields that match exactly to the database,
  // so we'll intersect with what the actual schema says.
  $db_item = array_intersect_key($item, $item_keys);

  // Remove urlencoding for select characters before storing item in the database
  // This is purely for the sake of prettying up the resulting URLs, they work
  // either way.
  $replacements = array(
    '%7E' => '~',
    '%3D' => '=',
    '%3A' => ':',
    '%2C' => ',',
    '%2F' => '/',
    '%26' => '&',
    '%3F' => '?',
  );
  if (isset($db_item['detailpageurl'])) {
    $db_item['detailpageurl'] = str_replace(array_keys($replacements), $replacements, $db_item['detailpageurl']);
  }
  if (isset($db_item['customerreviews_iframe'])) {
    $db_item['customerreviews_iframe'] = str_replace(array_keys($replacements), $replacements, $db_item['customerreviews_iframe']);
  }
  try {
    db_insert('amazon_item')
      ->fields($db_item)
      ->execute();
  } catch (Exception $e) {
    amazon_db_error_watchdog("Failed to insert item into amazon_item table", $e, $db_item);
  }

  // Handle the various credits for a product, including Artist, Author,
  // Actor, etc. We map these to a separate table.
  if (in_array('creators', variable_get('amazon_core_data', array(
    'creators',
    'images',
  )))) {
    $participant_types = preg_split('/,/', AMAZON_PARTICIPANT_TYPES);
    foreach ($participant_types as $type) {
      if (isset($item[strtolower($type)])) {
        foreach ((array) $item[strtolower($type)] as $participant) {
          $item_participant = array(
            'asin' => $item['asin'],
            'type' => strtolower($type),
            'participant' => $participant,
          );
          try {
            db_insert('amazon_item_participant')
              ->fields($item_participant)
              ->execute();
          } catch (Exception $e) {
            amazon_db_error_watchdog("Failed to insert item into amazon_item_participant table", $e, $item_participant);
          }
        }
      }
    }
  }

  // Save the product images if they exist.
  if (in_array('images', variable_get('amazon_core_data', array(
    'creators',
    'images',
  )))) {
    if (isset($item['imagesets'])) {
      foreach ($item['imagesets'] as $size => $data) {
        $image = array(
          'asin' => $item['asin'],
          'size' => $size,
          'height' => $data['height'],
          'width' => $data['width'],
          'url' => $data['url'],
        );
        try {
          db_insert('amazon_item_image')
            ->fields($image)
            ->execute();
        } catch (Exception $e) {
          amazon_db_error_watchdog("Failed to insert item into amazon_item_image table", $e, $image);
        }
      }
    }
  }

  // Save the editorial reviews if they exist.
  if (in_array('editorial_reviews', variable_get('amazon_core_data', array(
    'creators',
    'images',
    'editorial_reviews',
  )))) {
    if (isset($item['editorialreviews'])) {
      foreach ($item['editorialreviews'] as $data) {
        $review = array(
          'asin' => $item['asin'],
          'source' => $data['source'],
          'content' => $data['content'],
        );
        try {
          db_insert('amazon_item_editorial_review')
            ->fields($review)
            ->execute();
        } catch (Exception $e) {
          amazon_db_error_watchdog("Failed to insert item into amazon_item_editorial_review table", $e, $review);
        }
      }
    }
  }
  module_invoke_all('amazon_item_insert', $item);
}