You are here

amazon_filter.module in Amazon Product Advertisement API 7

File

amazon_filter/amazon_filter.module
View source
<?php

/**
 * Implementation of hook_help().
 */

/**
 * Implements of hook_filter_tips().
 */
function _amazon_filter_tips($filter, $format, $long = FALSE) {
  if ($long) {
    return t('Get Amazon product data using [amazon:ASIN:selector], for example, [amazon:0399155341:thumbnail],
    [amazon:0399155341:full], or [amazon:0399155341:inline].
    In addition, you can grab various data items from the item description using selectors like
    author, title, asin, isbn, ean, detailpageurl, salesrank, publisher, manufacturer, studio,
    label, binding, listpriceamount, listpricecurrencycode, listpriceformattedprice,
    lowestpriceamount, lowestpricecurrencycode, lowestpriceformattedprice,
    amazonpriceamount, amazonpricecurrencycode, amazonpriceformattedprice,
    productgroup, producttypename, invalid_asin, deweydecimalnumber, edition, numberofpages,
    publicationyear, type, releaseyear, publicationyear, smallimage, smallimageurl, smallimageheight,
    smallimagewidth, mediumimage, mediumimageurl, mediumimageheight, mediumimagewidth,
    largeimage, largeimageurl, largeimageheight, largeimagewidth.

    For example, [amazon:0596515804:title] will provide the title of the item, and
    [amazon:0596515804:largeimage] will be replaced with an img tag giving the large image.

    NOTE: Spaces can also be used instead of colons between the elements of the filter tag, but should be considered a deprecated form of notation.
    A complete description of filters is <a href="http://drupal.org/node/595464#filters">on the Amazon module handbook page</a>.');
  }
  else {
    return t('Link to Amazon products with: [amazon:product_id:inline|full|thumbnail|datadescriptor]. Example: [amazon:1590597559:thumbnail] or [amazon:1590597559:author]. Details are <a href="http://drupal.org/node/595464#filters" target="_blank">on the Amazon module handbook page</a>.');
  }
}

/**
 * Implements hook_filter_info().
 */
function amazon_filter_info() {
  $filters['amazon'] = array(
    'title' => t('Amazon filter'),
    'description' => t('Provides access to many types of Amazon data. Simplest usage: [amazon:ASIN:inline], for example [amazon:0596515804:inline].'),
    'process callback' => '_amazon_filter_process_text',
    'tips callback' => '_amazon_filter_tips',
  );
  return $filters;
}

/**
 * Actual filter processing function - changes [amazon <whatever>] in text.
 * @param $text
 *   Text to be transformed.
 * @return
 *   The transformed text.
 */
function _amazon_filter_process_text($text) {
  $pattern = "@\\[amazon(?: |:|%20)+(.*?)(?:(?: |:|%20)(.*?))?\\]@";
  $matches = array();
  if (preg_match_all($pattern, $text, $matches)) {
    $search = $matches[0];
    $replace = array();
    foreach ($matches[0] as $key => $value) {
      $asin = $matches[1][$key];
      $asin = amazon_convert_to_asin($asin);
      $action = $matches[2][$key];
      $items = amazon_item_lookup($asin);
      $item = "";
      if (!empty($items)) {
        $item = $items[$asin];
      }
      if (!empty($item)) {
        switch ($action) {
          case "":
          case 'inline':
            $replace[] = theme('amazon_inline_item', array(
              'item' => $item,
            ));
            break;

          // Full is a synonym of 'details'.
          case 'full':
          case 'details':
            $replace[] = theme('amazon_item', array(
              'item' => $item,
              'style' => 'details',
            ));
            break;

          // Handle themeable cases, like thumbnail.
          case 'thumbnail':
            $replace[] = theme('amazon_item__thumbnail', array(
              'item' => $item,
            ));
            break;
          default:

            // Allow to use anything found in the item.
            $replace[] = theme('amazon_detail', array(
              'item' => $item,
              'detail' => $action,
            ));
            break;
        }
      }
      else {

        // error case
        $replace[] = "<!-- The Amazon product '{$asin}' could not be found.-->";
      }
    }
    $text = str_replace($search, $replace, $text);
  }
  return $text;
}

Functions

Namesort descending Description
amazon_filter_info Implements hook_filter_info().
_amazon_filter_process_text Actual filter processing function - changes [amazon <whatever>] in text.
_amazon_filter_tips Implements of hook_filter_tips().