View source  
  <?php
module_load_include('inc', 'amazon_component', 'amazon_component.preprocess');
function amazon_component_permission() {
  return array(
    'administer amazon component' => array(
      'title' => t('Administer Amazon Component module'),
      'description' => t('Manage and arrange Amazon Components.'),
    ),
  );
}
function amazon_component_block_info() {
  $blocks['amazon_component_gallery']['info'] = t('Amazon Component :: Gallery');
  $blocks['amazon_component_cart']['info'] = t('Amazon Component :: Cart');
  $blocks['amazon_component_add_to_cart']['info'] = t('Amazon Component :: Add To Cart');
  $blocks['amazon_component_reviews']['info'] = t('Amazon Component :: Reviews');
  return $blocks;
}
function amazon_component_block_view($delta) {
  $block = array();
  switch ($delta) {
    case 'amazon_component_gallery':
      $block['subject'] = t("Gallery");
      $block['content'] = theme('amazon_gallery');
      break;
    case 'amazon_component_cart':
      $block['subject'] = t("Cart");
      $block['content'] = theme('amazon_component_cart');
      break;
    case 'amazon_component_add_to_cart':
      $block['subject'] = t("Add To Cart");
      $block['content'] = theme('amazon_component_add_to_cart');
      break;
    case 'amazon_component_reviews':
      $block['subject'] = t("Reviews");
      $block['content'] = theme('amazon_component_reviews');
      break;
      return $block;
  }
  return $block;
}
function amazon_component_block_configure($delta) {
  switch ($delta) {
    case 'amazon_gallery':
      $form = array();
      $form['amazon_store_search_block_keywords_width'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum Amount of Gallery Images'),
        '#size' => 5,
        '#default_value' => variable_get("amazon_component['gallery']", 5),
      );
      return $form;
    case 'amazon_component_cart':
      $form = array();
      $form['amazon_component_cart_style'] = array(
        '#type' => 'textfield',
        '#title' => t('Style'),
        '#size' => 5,
        '#default_value' => variable_get("amazon_component['cart']", ''),
      );
      return $form;
    case 'amazon_component_reviews':
      $form = array();
      $form['amazon_component_reviews_style'] = array(
        '#type' => 'textfield',
        '#title' => t('Style'),
        '#size' => 15,
        '#default_value' => variable_get("amazon_component['reviews_style']", ''),
      );
      return $form;
  }
}
function amazon_component_block_save($delta, $edit) {
  switch ($delta) {
    case 'amazon_gallery':
      variable_set("amazon_component_gallery", $edit["amazon_component_gallery"]);
      break;
    case 'amazon_component_cart':
      variable_set("amazon_component_cart", $edit["amazon_component_cart"]);
      break;
    case 'amazon_component_reviews':
      variable_set("amazon_component_reviews_style", $edit["amazon_component_reviews_style"]);
      break;
  }
  return;
}
function amazon_component_theme() {
  return array(
    'amazon_gallery' => array(
      'variables' => array(
        'results' => NULL,
      ),
      'template' => 'amazon_component_gallery',
      
      'path' => drupal_get_path('module', 'amazon_component') . '/templates',
    ),
    'amazon_component_cart' => array(
      'variables' => array(
        'cart' => NULL,
      ),
      'template' => 'amazon_component_cart',
      'path' => drupal_get_path('module', 'amazon_component') . '/templates',
    ),
    'amazon_component_add_to_cart' => array(
      'variables' => array(
        'cart' => NULL,
      ),
      'template' => 'amazon_component_add_to_cart',
      'path' => drupal_get_path('module', 'amazon_component') . '/templates',
    ),
    'amazon_component_reviews' => array(
      'variables' => array(
        'cart' => NULL,
      ),
      'template' => 'amazon_component_reviews',
      'path' => drupal_get_path('module', 'amazon_component') . '/templates',
    ),
  );
}
function amazon_component_gallery_lookup($item_ids = array(), $force_lookup = FALSE, $locale = NULL) {
  
  $items = array();
  $items_to_fetch = array();
  
  if (empty($item_ids)) {
    return array();
  }
  
  if (is_string($item_ids)) {
    $item_ids = array(
      $item_ids,
    );
  }
  
  if (!$force_lookup) {
    $items = amazon_component_lookup_gallery_from_db($item_ids, $size = 'mediumimage');
  }
  
  foreach ($item_ids as $item_id) {
    if (!isset($items[$item_id])) {
      $items_to_fetch[] = $item_id;
    }
  }
  
  $items_from_web = amazon_component_gallery_lookup_from_web($items_to_fetch, $locale);
  
  $full_set = $items + $items_from_web;
  
  return $full_set;
}
function amazon_component_lookup_gallery_from_db($item_ids = array(), $size = NULL) {
  if (!empty($item_ids)) {
    
    $items = array();
    $i = 0;
    
    if (!$size) {
      
      $query = 'SELECT * FROM {amazon_item_image_gallery} WHERE asin IN (:asins)';
      $args = array(
        ':asins' => $item_ids,
      );
      $options = array(
        'fetch' => PDO::FETCH_ASSOC,
      );
      
      $result = db_query($query, $args, $options);
      
    }
    else {
      
      $query = 'SELECT * FROM {amazon_item_image_gallery} WHERE asin IN (:asins) AND size IN (:size)';
      $args = array(
        ':asins' => $item_ids,
        ':size' => $size,
      );
      $options = array(
        'fetch' => PDO::FETCH_ASSOC,
      );
      
      $result = db_query($query, $args, $options);
    }
    
    foreach ($result as $image) {
      $items[$image['asin']]['imagesets_gallery'][$image['size']][$image['image_order']] = $image;
    }
    
    return $items;
  }
  return array();
}
function amazon_component_gallery_lookup_from_web($item_ids = array(), $locale = NULL) {
  
  $amazon_limit = 10;
  $asins = array();
  $results = array();
  
  $item_ids = array_filter($item_ids);
  
  foreach ($item_ids as $asin) {
    if (!empty($asin)) {
      $asins[] = $asin;
      if (count($asins) >= $amazon_limit || count($asins) == count($item_ids)) {
        $results += _amazon_component_batch_lookup_from_web($asins, $locale);
        $asins = array();
      }
    }
  }
  
  return $results;
}
function _amazon_component_batch_lookup_from_web($item_ids = array(), $locale = NULL) {
  
  $items = array();
  
  if (!empty($item_ids)) {
    $params = array(
      'ItemId' => implode(',', $item_ids),
      'ResponseGroup' => amazon_get_response_groups(),
    );
    
    $results = amazon_http_request('ItemLookup', $params, $locale);
    
    if (!empty($results->Items->Request->Errors)) {
      _amazon_item_batch_lookup_from_web_errors($results->Items->Request->Errors);
    }
    
    if (!empty($results->Items->Item)) {
      foreach ($results->Items->Item as $xml) {
        $item = amazon_component_gallery_clean_xml($xml);
        
        
        $items[(string) $xml->ASIN] = $item;
      }
    }
    return $items;
  }
  return array();
}
function amazon_component_gallery_clean_xml($xml) {
  
  $item = array();
  $metadata = amazon_data_cache();
  $supported_sizes = preg_split('/,/', AMAZON_IMAGE_SIZES);
  
  
  
  if (isset($xml->ImageSets)) {
    $i = 0;
    foreach ((array) $xml->ImageSets as $ImageSetKey => $ImageSets) {
      foreach ((array) $ImageSets as $ImageSetKey => $ImageSet) {
        $i++;
        $k = 0;
        $ImageSet = (array) $ImageSet;
        foreach ($ImageSet as $imageSize => $imageData) {
          $k++;
          if (in_array($imageSize, $supported_sizes)) {
            $item['imagesets_gallery'][strtolower($imageSize)][$i] = array(
              'asin' => (string) $xml->ASIN,
              'url' => (string) $imageData->URL,
              'category' => (string) $ImageSet['@attributes']['Category'],
              'height' => intval($imageData->Height),
              'width' => intval($imageData->Width),
              'size' => (string) strtolower($imageSize),
              'image_order' => $i,
            );
          }
        }
      }
    }
  }
  return $item;
}
function amazon_component_get_cart_creds() {
  if (!empty($_SESSION['cart_id']) && !empty($_SESSION['HMAC'])) {
    return array(
      'CartId' => $_SESSION['cart_id'],
      'HMAC' => $_SESSION['HMAC'],
    );
  }
  return FALSE;
}