You are here

function template_preprocess_amazon_item in Amazon Product Advertisement API 7.2

Same name and namespace in other branches
  1. 7 amazon.module \template_preprocess_amazon_item()

template_preprocess: amazon_item.

1 string reference to 'template_preprocess_amazon_item'
amazon_theme in ./amazon.module
Implementation of hook_theme().

File

./amazon.module, line 180

Code

function template_preprocess_amazon_item(&$variables, $theme_type) {
  $item = $variables['item'];

  // Do a quick cycle through the simple keys on the item, filter_xss() them,
  // and stick them in the variables collection.
  foreach ($item as $key => $value) {
    if (is_string($value)) {
      $variables[$key] = filter_xss($value);
    }
  }
  $variables['type'] = _amazon_clean_type($item['producttypename']);
  $variables['detailpageurl'] = check_url($item['detailpageurl']);

  // Since we use l() on templates to print links, we need to pass the decoded url to l().
  $variables['detailpageurl'] = htmlspecialchars_decode($variables['detailpageurl']);
  $variables['editorialreview'] = !empty($item['editorialreviews']) ? filter_xss_admin($item['editorialreviews'][0]['content']) : '';
  $variables['customerreviews_iframe'] = !empty($item['customerreviews_iframe']) ? check_url($item['customerreviews_iframe']) : '';
  $variables['invalid_asin'] = !empty($item['invalid_asin']) ? 1 : 0;
  if (!empty($variables['customerreviews'])) {
    foreach ($variables['customerreviews'] as $key => $review) {
      $variables['customerreviews'][$key] = filter_xss($variables['customerreviews'][$key]);
    }
  }
  $variables['invalid_asin'] = !empty($variables['invalid_asin']) ? 1 : 0;
  if (!empty($variables['publicationdate'])) {
    $date = explode('-', $variables['publicationdate']);
    $variables['publicationyear'] = check_plain($date[0]);
  }
  else {
    $variables['publicationyear'] = '';
  }

  // Handle participants and types.
  if (isset($item['participants'])) {
    $variables['participants'] = filter_xss(filter_xss(implode(', ', $item['participants'])));
    $participant_types = preg_split('/,/', AMAZON_PARTICIPANT_TYPES);
    foreach ($participant_types as $participant_type) {
      $participant_type = strtolower($participant_type);
      if (!empty($item[$participant_type])) {
        if (is_string($item[$participant_type])) {
          $variables[$participant_type] = filter_xss($item[$participant_type]);
        }
        else {
          $variables[$participant_type] = filter_xss(implode(', ', $item[$participant_type]));
        }
      }
    }
  }
  $variables += array(
    'participants' => '',
    'director' => '',
    'actor' => '',
    'artist' => '',
    'author' => '',
  );

  // Handle supported image resolutions.
  if (isset($item['imagesets'])) {
    foreach ($item['imagesets'] as $key => $image) {
      $variables[$key] = theme('image', array(
        'path' => $image['url'],
        'alt' => t('Image of') . ' ' . strip_tags($item['title']),
        'title' => strip_tags($item['title']),
        'attributes' => array(
          'height' => $image['height'],
          'width' => $image['width'],
        ),
        'getsize' => FALSE,
      ));
      $variables["{$key}url"] = check_url($image['url']);
      $variables["{$key}height"] = check_plain($image['height']);
      $variables["{$key}width"] = check_plain($image['width']);
    }
  }
  $variables['image'] = !empty($variables['mediumimage']) ? $variables['mediumimage'] : '';

  // Product Image Galleries
  if (isset($item['imagesets_gallery'])) {
    foreach ($item['imagesets_gallery'] as $key => $imageset_gallery) {
      foreach ($imageset_gallery as $imageset_gallery_key => $image) {

        // If :: Check to see if image is the "primary" product image.
        // 1 - Add image to the beginning of the array.
        // 0 - Add image the the end of the array.
        if ($image['category'] === 'primary') {

          // Primary Image :: Core Image Theme
          $primary_image = theme('image', array(
            'path' => $image['url'],
            'alt' => t('Image of') . ' ' . strip_tags($item['title']),
            'title' => strip_tags($item['title']),
            'attributes' => array(
              'height' => $image['height'],
              'width' => $image['width'],
              'data-category' => $image['category'],
            ),
            'getsize' => FALSE,
          ));

          // Add Primary Image to beginning of Product Image Gallery array
          array_unshift($variables[$key], $primary_image);
        }
        else {

          // Variant Image :: Core Image Theme
          $variables[$key][$imageset_gallery_key] = theme('image', array(
            'path' => $image['url'],
            'alt' => t('Image of') . ' ' . strip_tags($item['title']),
            'title' => strip_tags($item['title']),
            'attributes' => array(
              'height' => $image['height'],
              'width' => $image['width'],
              'data-category' => $image['category'],
            ),
            'getsize' => FALSE,
          ));
        }
      }
    }
  }
  $variables['classes_array'] = array(
    'amazon-product--container',
  );
  $variables['attributes_array'] = array();
  $variables['title_attributes_array'] = array();
  $variables['content_attributes_array'] = array();
  if (!empty($variables['detail']) && $theme_type == 'amazon_detail') {
    $variables['classes_array'] = array(
      "amazon-item",
      "amazon-item-detail-{$variables['detail']}",
    );
  }
  if (!empty($variables['style']) && $variables['style'] == 'inline' && !empty($variables['producttypename'])) {
    $variables['theme_hook_suggestions'][] = 'amazon_inline_item__' . _amazon_clean_type($item['producttypename']);
  }
  if (!empty($variables['style'])) {
    $variables['classes'] = _amazon_item_classes($item) . ' amazon-item-' . check_plain($variables['style']);
    $variables['classes_array'][] = _amazon_item_classes($item);
    $variables['classes_array'][] = 'amazon-item-' . check_plain($variables['style']);
    $variables['classes_array'][] = 'locale-' . check_plain(strtolower($variables['locale']));

    // A set of more specific templates to use when displaying items.
    $variables['theme_hook_suggestions'][] = 'amazon_item__' . $variables['style'];
    $variables['theme_hook_suggestions'][] = 'amazon_item__' . strtolower($variables['type']);
    $variables['theme_hook_suggestions'][] = 'amazon_item__' . strtolower($variables['type']) . '__' . $variables['style'];
    if (!empty($item['view']) && !empty($item['view']->name)) {
      $variables['theme_hook_suggestions'][] = 'amazon_item_view__' . $item['view']->name;
      $variables['theme_hook_suggestions'][] = 'amazon_item__' . strtolower($variables['type']) . '__view__' . $item['view']->name;
    }
  }
}