You are here

function feeds_imagegrabber_feeds_set_target in Feeds Image Grabber 6

Same name and namespace in other branches
  1. 7 feeds_imagegrabber.module \feeds_imagegrabber_feeds_set_target()

Implementation of hook_feeds_set_target().

Parameters

$node: The target node.

$target: The name of field on the target node to map to.

$page_url: This should contain a valid URL for the feed item, from which the target image is to be grabbed.

1 string reference to 'feeds_imagegrabber_feeds_set_target'
feeds_imagegrabber_feeds_node_processor_targets_alter in ./feeds_imagegrabber.module
Implementation of hook_feeds_node_processor_targets_alter().

File

./feeds_imagegrabber.module, line 162
Grabs image for each feed-item from their respective web pages and stores it in an image field. Requires Feeds module.

Code

function feeds_imagegrabber_feeds_set_target($node, $target, $page_url) {
  $feed_nid = $node->feeds_node_item->feed_nid;
  $settings = feeds_imagegrabber_get_settings($feed_nid);
  if (!$settings || !$settings['enabled']) {
    return;
  }
  if (!feeds_imagegrabber_include_library('url_to_absolute.php', 'feeds_imagegrabber')) {
    drupal_set_message(t('Feeds Image Grabber: The URL conversion script is missing. Go to <a href="!admin-reports-status">Status Report page</a>', array(
      '!admin-reports-status' => url('admin/reports/status'),
    )), 'error');
    return FALSE;
  }
  list($field_name) = split(':', $target);
  $field = content_fields($field_name, $node->type);
  $max_filesize = parse_size(file_upload_max_size());
  if (!empty($field['widget']['max_filesize_per_file']) && parse_size($field['widget']['max_filesize_per_file']) < $max_filesize) {
    $max_filesize = parse_size($field['widget']['max_filesize_per_file']);
  }
  $max_exec_time = ini_get('max_execution_time');
  $timeout = $max_exec_time == 0 ? 10 : $settings['exec_time'] * $max_exec_time / 100;
  $page_time = timer_read('page') / 1000;
  if (function_exists('encode_url')) {
    $page_url = encode_url($page_url);
  }
  if (valid_url($page_url)) {
    $xml = feeds_imagegrabber_webpage_scraper($page_url, $settings['id_class'], $settings['id_class_desc'], $timeout);
    if ($xml == FALSE) {
      return;
    }
    $timeout = $timeout - timer_read('page') / 1000 + $page_time;
    $options = array(
      'max_imagesize' => $max_filesize,
      'timeout' => $timeout,
      'feeling_lucky' => $settings['feeling_lucky'],
    );
    $images = feeds_imagegrabber_scrape_images($xml, $page_url, $options);
    if ($images == FALSE || count($images) <= 0) {
      return;
    }
    asort($images);
    $images = array_reverse($images, TRUE);

    // Map enclosures.
    $items = isset($node->{$field_name}) ? $node->{$field_name} : array();
    foreach ($images as $url => $size) {
      $enclosure = new FeedsEnclosure($url, 'application/octet-stream');
      if (($file = $enclosure
        ->getFile()) && ($file = feeds_imagegrabber_is_image($file))) {
        $target_dir = filefield_widget_file_path($field, user_load($node->uid));
        $file_validators = filefield_widget_upload_validators($field);
        $image_validators = imagefield_widget_upload_validators($field);
        $validators = array_merge($file_validators, $image_validators);
        if (array_key_exists('filefield_validate_is_image', $validators)) {
          unset($validators['filefield_validate_is_image']);
        }
        $info = field_file_save_file($file, $validators, $target_dir, user_load($node->uid));
        if ($info) {
          $info['list'] = array();
          $info['data'] = array(
            'description' => '',
          );
          if ($field['list_field']) {
            $info['list'] = $field['list_default'];
          }
          $items[] = $info;
          break;
        }
      }
    }
    $node->{$field_name} = $items;
  }
}