You are here

amazon_filter.module in Amazon Product Advertisement API 6

File

amazon_filter/amazon_filter.module
View source
<?php

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

/**
 * Implementation of hook_filter_tips().
 */
function amazon_filter_filter_tips($delta, $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.
    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>.');
  }
}

/**
 * Implementation of hook_filter().
 */
function amazon_filter_filter($op, $delta = 0, $format = -1, $text = '') {
  if ($op == 'list') {
    return array(
      0 => t('Amazon filter'),
    );
  }
  switch ($delta) {
    case 0:
      switch ($op) {
        case 'description':
          return t('Lets writers use the [amazon] tag to embed Amazon product information in text.');
        case 'prepare':
          return $text;
        case 'process':
          return _amazon_filter_process_text($text);
      }
      break;
  }
}
function _amazon_filter_process_text($text) {
  $pattern = "/\\[amazon +(.*?)(?: +(.*?))?\\]/";
  $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', $item);
            break;
          case 'thumbnail':

          // Handle themeable cases, like thumbnail, full.
          case 'full':
            $replace[] = theme('amazon_item', $item, $action);
            break;
          default:

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

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

Functions