--- feeds_imagegrabber.module-revBASE.svn000.tmp.module	Wed Nov 10 12:18:18 2010
+++ feeds_imagegrabber.module	Wed Nov 10 12:21:26 2010
@@ -87,11 +87,12 @@
         '#type' => 'radios',
         '#title' => t('Feeling lucky, huh?'),
         '#options' => array(
-          t('No, select the largest image between the tag.'),
+          t('No, select the largest image(s) between the tag.'),
           t('Yes, select the first image between the tag. (Recommended)'),
         ),
         '#default_value' => $settings['feeling_lucky'],
       );
+      
       $form['feeds_imagegrabber']['exec_time'] = array(
         '#type' => 'select',
         '#title' => t('Execution time[%]'),
@@ -197,11 +198,12 @@
  *   The target node.
  * @param $target
  *   The name of field on the target node to map to.
- * @param $page_url
- *   This should contain a valid URL for the feed item, from
- *   which the target image is to be grabbed.
+ * @param $source
+ *   This should contain a either valid URL for the feed item, from
+ *   which the target image is to be grabbed, or the direct URL to an
+ *   single image, or an array containing the URLs of multiple images.
  */
-function feeds_imagegrabber_feeds_set_target($node, $target, $page_url) {
+function feeds_imagegrabber_feeds_set_target($node, $target, $source) {
   $feed_nid     = $node->feeds_node_item->feed_nid;
   $settings     = feeds_imagegrabber_get_settings($feed_nid);
   if(!$settings || !$settings['enabled']) {
@@ -213,6 +215,14 @@
     return FALSE;
   }
 
+  $image_array = NULL;
+  $page_url = NULL;  
+  if (is_array($source)) {
+    $image_array = $source;
+  } else {
+    $page_url = $source;
+  }
+  
   list($field_name) = split(':', $target);
   $field        = content_fields($field_name, $node->type);
   $max_filesize = parse_size(file_upload_max_size());
@@ -223,13 +233,27 @@
   $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 ($image_array) {
+      foreach ($image_array as &$image_url) {
+        $image_url = encode_url($image_url);
+      }
+    }
+    else {
+      $page_url = encode_url($page_url);
+    }
   }
   
-  if (valid_url($page_url)) {
+  if (valid_url($page_url) || feeds_imagegrabber_valid_url_array($image_array)) {
     if ($settings['id_class'] == FIG_SCRAPER_METHOD_URL) {
       // No scraping, just get the image directly.
-      $images = array($page_url => 0);
+      if ($image_array) {
+        foreach ($image_array as $url) {
+          $images[$url] = 0;
+        }
+      }
+      else {
+        $images = array($page_url => 0);
+      }
     }
     else {
       $xml = feeds_imagegrabber_webpage_scraper($page_url, $settings['id_class'], $settings['id_class_desc'], $timeout);
@@ -245,19 +269,19 @@
       );
       $images = feeds_imagegrabber_scrape_images($xml, $page_url, $options);
       // Done scraping.
+      
+      // Sort by size, largest first.
+      arsort($images);
     }
-    
+
     if (empty($images)) {
       watchdog('feeds_imagegrabber', 'No images to import from @url', array('@url' => $page_url), WATCHDOG_NOTICE);
       return;
     }
 
-    asort($images);
-    $images = array_reverse($images, TRUE);
-
     // Map enclosures.
     $items = isset($node->$field_name) ? $node->$field_name : array();
-
+    $image_count = 1;
     foreach ($images as $url => $size) {
       $enclosure = new FeedsEnclosure($url, 'application/octet-stream');
       if ($file = $enclosure->getFile()) {
@@ -265,7 +289,7 @@
         $file_validators  = filefield_widget_upload_validators($field);
         $image_validators = imagefield_widget_upload_validators($field);
         $validators       = array_merge($file_validators, $image_validators);
-        $info             = field_file_save_file($enclosure->getFile(), $validators, $target_dir, user_load($node->uid));
+        $info             = field_file_save_file($file, $validators, $target_dir, user_load($node->uid));
         if ($info) {
           $info['list'] = array();
           $info['data'] = array('description' => '');
@@ -273,7 +297,12 @@
             $info['list'] = $field['list_default'];
           }
           $items[] = $info;
-          break;
+          // Stop processing when the maximum number of field values has been reached.
+          // 0 = single value, 1 = unlimited, other integers = limited multiple values.
+          if ($field['multiple'] == 0 || ($image_count == $field['multiple'] && $field['multiple'] != 1)) {
+            break;
+          }
+          $image_count++;
         }
       }
       else {
@@ -829,3 +858,16 @@
   return FALSE;
 }
 
+function feeds_imagegrabber_valid_url_array(&$url_array) {
+  if (is_array($url_array)) {
+    foreach ($url_array as $url) {
+      if (!valid_url($url)) {
+        unset($url_array[$url]);
+      }
+    }
+    if (count($url_array)) {
+      return true;
+    }
+  }
+  return false;
+}
\ No newline at end of file
