From 3cc5174dea99fa09dc4daff17ef16fa9e9f0db32 Mon Sep 17 00:00:00 2001
From: Thomas MacLean <thomas@Thomas-MacLeans-MacBook-Pro.local>
Date: Wed, 23 Mar 2011 23:03:08 -0400
Subject: [PATCH] Issue #1103584 by chromix: added multiple fields for enclosures.

---
 itunes.admin.inc |    1 +
 itunes.module    |   74 +++++++++++++++++++++++++++--------------------------
 2 files changed, 39 insertions(+), 36 deletions(-)

diff --git a/itunes.admin.inc b/itunes.admin.inc
index 98692ae..26be436 100644
--- a/itunes.admin.inc
+++ b/itunes.admin.inc
@@ -42,6 +42,7 @@ function itunes_admin_settings() {
       }
       $form['itunes_enclosure_source'][$type->type] = array(
         '#type' => 'select',
+        '#multiple' => TRUE,
         '#options' => $options,
         '#default_value' => empty($fields[$type->type]) ? NULL : $fields[$type->type],
         '#access' => count($options) > 1,
diff --git a/itunes.module b/itunes.module
index 7ae3df4..dfc0f09 100644
--- a/itunes.module
+++ b/itunes.module
@@ -164,51 +164,53 @@ function itunes_nodeapi(&$node, $op, $arg) {
           // This variable won't be set unless content.module is enabled.
           $content_types = content_types();
           $content = $content_types[$node->type];
+          $enclosure_field = array();
           foreach ($content['fields'] as $field_name => $field) {
-            if ($fields[$node->type] == $field_name) {
-              $enclosure_field = $field;
-              break;
+            if (in_array($field_name, $fields[$node->type])) {
+              $enclosure_field[$field_name] = $field;
             }
           }
-          if ($field['type'] == 'filefield') {
-            if (!empty($node->{$fields[$node->type]}[0]['fid'])) {
-              $file = (array) $node->{$fields[$node->type]}[0];
-              $ret[] = array(
-                'key' => 'enclosure',
-                'value' => '',
-                'attributes' => array(
-                  'url' => file_create_url($file['filepath']),
-                  'length' => $file['filesize'],
-                  'type' => $file['filemime'],
-                ),
-              );
-              if (!empty($file['data']['duration'])) {
+          foreach ($enclosure_field as $field_name => $field) {
+            if ($field['type'] == 'filefield') {
+              if (!empty($node->{$field_name}[0]['fid'])) {
+                $file = (array) $node->{$field_name}[0];
                 $ret[] = array(
-                  'namespace' => array('xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd'),
-                  'key' => 'itunes:duration',
-                  'value' => (int) $file['data']['duration'],
+                  'key' => 'enclosure',
+                  'value' => '',
+                  'attributes' => array(
+                    'url' => file_create_url($file['filepath']),
+                    'length' => $file['filesize'],
+                    'type' => $file['filemime'],
+                  ),
                 );
+                if (!empty($file['data']['duration'])) {
+                  $ret[] = array(
+                    'namespace' => array('xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd'),
+                    'key' => 'itunes:duration',
+                    'value' => (int) $file['data']['duration'],
+                  );
+                }
               }
             }
-          }
-          elseif ($field['type'] == 'emaudio') {
-            if (!empty($node->{$fields[$node->type]}[0]['value'])) {
-              $file = $node->{$fields[$node->type]}[0];
-              $ret[] = array(
-                'key' => 'enclosure',
-                'value' => '',
-                'attributes' => array(
-                  'url' => $file['value'],
-                  'length' => !empty($file['data']['size']) ? $file['data']['size'] : 0,
-                  'type' => !empty($file['data']['mime']) ? $file['data']['mime'] : 'audio/mpeg',
-                ),
-              );
-              if (!empty($file['data']['duration'])) {
+            elseif ($field['type'] == 'emaudio') {
+              if (!empty($node->{$field_name}[0]['value'])) {
+                $file = $node->{$field_name}[0];
                 $ret[] = array(
-                  'namespace' => array('xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd'),
-                  'key' => 'itunes:duration',
-                  'value' => (int) $file['data']['duration'],
+                  'key' => 'enclosure',
+                  'value' => '',
+                  'attributes' => array(
+                    'url' => $file['value'],
+                    'length' => !empty($file['data']['size']) ? $file['data']['size'] : 0,
+                    'type' => !empty($file['data']['mime']) ? $file['data']['mime'] : 'audio/mpeg',
+                  ),
                 );
+                if (!empty($file['data']['duration'])) {
+                  $ret[] = array(
+                    'namespace' => array('xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd'),
+                    'key' => 'itunes:duration',
+                    'value' => (int) $file['data']['duration'],
+                  );
+                }
               }
             }
           }
-- 
1.7.4.1

