You are here

amazon_media.module in Amazon Product Advertisement API 7

Provides additional behaviors and data type for amazon items which are DVDs, software, video games, music, etc.

File

amazon_media/amazon_media.module
View source
<?php

/**
 * @file
 *   Provides additional behaviors and data type for amazon items which are
 *   DVDs, software, video games, music, etc.
 */

/**
 * Implements hook_theme().
 */
function amazon_media_theme() {
  $templates = array(
    'amazon_inline_item__dvd' => array(
      'variables' => array(
        'item' => array(),
      ),
    ),
    'amazon_inline_item__software' => array(
      'variables' => array(
        'item' => array(),
      ),
    ),
    'amazon_inline_item__games' => array(
      'variables' => array(
        'item' => array(),
      ),
    ),
    'amazon_inline_item__console_video_games' => array(
      'variables' => array(
        'item' => array(),
      ),
      'function' => 'theme_amazon_inline_item__games',
    ),
    'amazon_item__book__details' => array(
      'variables' => array(
        'item' => array(),
      ),
      'template' => 'amazon-item--book--details',
    ),
    'amazon_item__dvd__details' => array(
      'variables' => array(
        'item' => array(),
      ),
      'template' => 'amazon-item--dvd--details',
    ),
    'amazon_item__software__details' => array(
      'variables' => array(
        'item' => array(),
      ),
      'template' => 'amazon-item--software--details',
    ),
    'amazon_item__games__details' => array(
      'variables' => array(
        'item' => array(),
      ),
      'template' => 'amazon-item--games--details',
    ),
  );
  return $templates;
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Because we aren't defining our own stuff here, just providing some
 * high-specificity templates for theme_amazon_item, we'll add this
 * module's directory to the list of paths to search for when finding
 * amazon_item tpl's.
 */
function amazon_media_theme_registry_alter(&$theme_registry) {
  $theme_registry['amazon_item']['theme path'] = drupal_get_path('module', 'amazon_media');
  if (!empty($theme_registry['amazon_item'])) {

    // We'll try our best to avoid stepping on theme template files.
    if (!empty($theme_registry['amazon_item']['theme_paths']) && count($theme_registry['amazon_item']['theme paths']) > 1) {
      $theme = array_pop($theme_registry['amazon_item']['theme paths']);
    }
    $theme_registry['amazon_item']['theme paths'][] = drupal_get_path('module', 'amazon_media');
    if (isset($theme)) {
      $theme_registry['amazon_item']['theme paths'][] = $theme;
    }
  }
}

/**
 * Implements hook_preprocess.
 *
 * @param $variables
 */
function amazon_media_preprocess_amazon_item(&$variables) {
  if (!empty($variables['publicationdate'])) {
    $date = explode('-', $variables['publicationdate']);
    $variables['publicationyear'] = $date[0];
  }

  // Various bits we want to ensure have defaults.
  $variables += array(
    'director' => '',
    'actor' => '',
    'audiencerating' => t('NR - Not Rated'),
    'publicationyear' => '',
    'runningtime' => '',
  );
}

/**
 * Load and return additional information for an Amazon item.
 * @param $item
 *   The item requiring additions.
 * @return
 *   The added array elements, as an array.
 */
function amazon_media_amazon_item_load($item) {
  $table = amazon_media_get_table_name($item);
  if (!empty($table)) {
    $additions = db_select($table)
      ->condition('asin', $item['asin'])
      ->fields($table)
      ->execute()
      ->fetchAssoc();
    return $additions;
  }
}

/**
 * Given an item, return the name of the table it's associated with.
 * @param $item
 *   populated amazon_item with $item['producttypename'] populated.
 * @return
 *   the name of the associated table.
 */
function amazon_media_get_table_name($item) {
  static $names = array(
    'ABIS_BOOK' => 'amazon_book',
    'BOOKS_1973_AND_LATER' => 'amazon_book',
    'VIDEO_DVD' => 'amazon_dvd',
    'ABIS_DVD' => 'amazon_dvd',
    'ABIS_MUSIC' => 'amazon_music',
    'CONSOLE_VIDEO_GAMES' => 'amazon_software',
    'VIDEO_GAMES' => 'amazon_software',
    'SOFTWARE' => 'amazon_software',
  );
  if (!empty($names[$item['producttypename']])) {
    return $names[$item['producttypename']];
  }
}

/**
 * Insert the associated information into the related table.
 * @param $item
 *   Populated amazon item record.
 */
function amazon_media_amazon_item_insert($item) {
  static $item_keys = NULL;
  if (empty($item_keys)) {
    require_once 'amazon_media.install';
    $schema = amazon_media_schema();
    $item_keys = $schema;
  }
  $table = amazon_media_get_table_name($item);
  if (!empty($table)) {

    // We need to present a record that only has the items in it used by
    // this table.
    $db_item = array_intersect_key($item, $item_keys[$table]['fields']);
    try {
      db_insert($table)
        ->fields($db_item)
        ->execute();
    } catch (Exception $e) {
      amazon_db_error_watchdog("Failed to insert item into amazon table", $e, $db_item);
    }
  }
}

/**
 * Delete a record using the asin as key.
 * @param $asin
 *   the asin to delete.
 */
function amazon_media_amazon_item_delete($asin) {

  // This is pretty inefficient; we're going to thrash these tables
  // every time a product gets deleted or updated. We'll solve it later.
  db_delete('amazon_book')
    ->condition('asin', $asin)
    ->execute();
  db_delete('amazon_dvd')
    ->condition('asin', $asin)
    ->execute();
  db_delete('amazon_music')
    ->condition('asin', $asin)
    ->execute();
  db_delete('amazon_software')
    ->condition('asin', $asin)
    ->execute();
}

/**
 * Implements hook_views_api.
 */
function amazon_media_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implements theme_ with specialty dvd.
 *
 * @param $item
 *   fully populated amazon item.
 * @param $style
 *   'full', 'thumbnail', or whatever.
 */
function theme_amazon_item__dvd($item, $style = 'default') {
  drupal_set_message('Theming with theme_amazon_item__dvd');
  $output = '';
  $type = $item['type'];
  switch ($style) {
    case 'full':
      $output .= '<div class="' . _amazon_item_classes($type) . ' amazon-item-full">';
      $output .= theme('image', array(
        'path' => $item['imagesets']['mediumimage']['url'],
        'alt' => t('Cover image'),
        'title' => check_markup($item['title']),
        'attributes' => array(),
        'getsize' => FALSE,
      ));
      $output .= '<h3>' . l(check_plain($item['title']), $item['detailpageurl'], array(
        'attributes' => array(
          'rel' => 'nofollow',
        ),
      )) . '</h3>';
      $output .= '</div>';
      break;
    case 'thumbnail':
      $output .= '<div class="' . _amazon_item_classes($type) . ' amazon-item-thumbnail">';
      $output .= theme('image', array(
        'path' => $item['imagesets']['mediumimage']['url'],
        'alt' => t('Cover image'),
        'title' => check_markup($item['title']),
        'attributes' => array(),
        'getsize' => FALSE,
      ));
      $output .= '</div>';
      break;
    default:
      $output .= '<div class="' . _amazon_item_classes($type) . '">';
      $output .= theme('image', array(
        'path' => $item['imagesets']['smallimage']['url'],
        'alt' => t('Cover image'),
        'title' => check_markup($item['title']),
        'attributes' => array(),
        'getsize' => FALSE,
      ));
      $output .= '<h3>' . l(check_markup($item['title']), $item['detailpageurl'], array(
        'attributes' => array(
          'rel' => 'nofollow',
        ),
      )) . '</h3>';
      $output .= '<div><strong>' . t('Director') . ':</strong> ' . implode(', ', $item['director']) . '</div>';
      $output .= '<div><strong>' . t('Rating') . ':</strong> ' . $item['audiencerating'] . '</div>';
      $output .= '<div><strong>' . t('Running time') . ':</strong> ' . $item['runningtime'] . ' ' . t('minutes') . '</div>';
      $output .= '</div>';
      break;
  }
  return $output;
}

/**
 * Theme with inline item dvd.
 * @param $item
 * @return
 *   rendered output.
 */
function theme_amazon_inline_item__dvd($item) {
  $type = $item['type'];
  $output = '';
  $output .= '<span class="' . _amazon_item_classes($type) . ' amazon-item-inline">';
  $output .= l($item['title'], $item['detailpageurl'], array(
    'attributes' => array(
      'rel' => 'nofollow',
    ),
  ));
  $output .= '</span>';
  return $output;
}
function theme_amazon_inline_item__software($item) {
  $output = '';
  $output .= '<span class="' . _amazon_item_classes($item) . ' amazon-item-inline">';
  $output .= l($item['title'] . ' (' . $item['operatingsystem'] . ')', $item['detailpageurl'], array(
    'attributes' => array(
      'rel' => 'nofollow',
    ),
  ));
  $output .= '</span>';
  return $output;
}
function theme_amazon_inline_item__games($item) {
  $output = '';
  $output .= '<span class="' . _amazon_item_classes($item) . ' amazon-item-inline">';
  $output .= l($item['title'] . ' (' . $item['hardwareplatform'] . ')', $item['detailpageurl'], array(
    'attributes' => array(
      'rel' => 'nofollow',
    ),
  ));
  $output .= '</span>';
  return $output;
}

Functions

Namesort descending Description
amazon_media_amazon_item_delete Delete a record using the asin as key.
amazon_media_amazon_item_insert Insert the associated information into the related table.
amazon_media_amazon_item_load Load and return additional information for an Amazon item.
amazon_media_get_table_name Given an item, return the name of the table it's associated with.
amazon_media_preprocess_amazon_item Implements hook_preprocess.
amazon_media_theme Implements hook_theme().
amazon_media_theme_registry_alter Implements hook_theme_registry_alter().
amazon_media_views_api Implements hook_views_api.
theme_amazon_inline_item__dvd Theme with inline item dvd.
theme_amazon_inline_item__games
theme_amazon_inline_item__software
theme_amazon_item__dvd Implements theme_ with specialty dvd.