From c70e2b4fa8d624b94b436392c6187f3f16523c7a Mon Sep 17 00:00:00 2001
From: Andy Fowlston <andy@220112.no-reply.drupal.org>
Date: Tue, 15 Dec 2015 13:35:58 +0000
Subject: [PATCH] Issue #2635548 by AndyF: Update to use Feeds new generic
 bundle handling.

---
 commerce_feeds.install                             | 58 ++++++++++++++++++++++
 ...mmerce_feeds_example.feeds_importer_default.inc |  2 +-
 plugins/FeedsCommerceCouponProcessor.inc           | 16 ++----
 plugins/FeedsCommerceCustomerProfileProcessor.inc  | 17 +++----
 plugins/FeedsCommerceProductProcessor.inc          | 15 ++----
 5 files changed, 75 insertions(+), 33 deletions(-)

diff --git a/commerce_feeds.install b/commerce_feeds.install
index b0ddc17..7163639 100644
--- a/commerce_feeds.install
+++ b/commerce_feeds.install
@@ -12,3 +12,61 @@ function commerce_feeds_enable() {
   // Clear the cache to display in Feeds as available plugin.
   cache_clear_all('plugins:feeds:plugins', 'cache');
 }
+
+/**
+ * Update to use generic bundle handling.
+ *
+ * @see feeds_update_7208()
+ * @see https://www.drupal.org/node/2426449
+ */
+function commerce_feeds_update_7100(&$sandbox) {
+
+  if (!isset($sandbox['importers'])) {
+    // Get all importers.
+    $sandbox['importers'] = db_query("SELECT id FROM {feeds_importer}")->fetchCol();
+    $sandbox['total'] = count($sandbox['importers']);
+  }
+
+  $importer = array_pop($sandbox['importers']);
+  $config = db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $importer))->fetchField();
+
+  if ($config) {
+    $config = unserialize($config);
+
+    switch ($config['processor']['plugin_key']) {
+      case 'FeedsCommerceCouponProcessor':
+        $config_key = 'coupon_type';
+        break;
+
+      case 'FeedsCommerceCustomerProfileProcessor':
+        $config_key = 'customer_profile_type';
+        break;
+
+      case 'FeedsCommerceProductProcessor':
+        $config_key = 'product_type';
+        break;
+
+      default:
+        $config_key = FALSE;
+        break;
+    }
+
+    if ($config_key && isset($config['processor']['config'][$config_key])) {
+      $config['processor']['config']['bundle'] = $config['processor']['config'][$config_key];
+      unset($config['processor']['config'][$config_key]);
+
+      // Update database.
+      db_update('feeds_importer')
+        ->fields(array(
+          'config' => serialize($config),
+        ))
+        ->condition('id', $importer)
+        ->execute();
+    }
+
+    $sandbox['#finished'] = 1 - count($sandbox['importers']) / $sandbox['total'];
+  }
+  else {
+    $sandbox['#finished'] = 1;
+  }
+}
diff --git a/commerce_feeds_example/commerce_feeds_example.feeds_importer_default.inc b/commerce_feeds_example/commerce_feeds_example.feeds_importer_default.inc
index 5c9351b..70ddeac 100644
--- a/commerce_feeds_example/commerce_feeds_example.feeds_importer_default.inc
+++ b/commerce_feeds_example/commerce_feeds_example.feeds_importer_default.inc
@@ -34,7 +34,7 @@ function commerce_feeds_example_feeds_importer_default() {
     'processor' => array(
       'plugin_key' => 'FeedsCommerceProductProcessor',
       'config' => array(
-        'product_type' => 'product',
+        'bundle' => 'product',
         'author' => '1',
         'mappings' => array(
           0 => array(
diff --git a/plugins/FeedsCommerceCouponProcessor.inc b/plugins/FeedsCommerceCouponProcessor.inc
index 465a25a..cb324a2 100644
--- a/plugins/FeedsCommerceCouponProcessor.inc
+++ b/plugins/FeedsCommerceCouponProcessor.inc
@@ -30,7 +30,7 @@ class FeedsCommerceCouponProcessor extends FeedsProcessor {
    * Creates a new coupon and returns it.
    */
   protected function newEntity(FeedsSource $source) {
-    $type = $this->config['coupon_type'];
+    $type = $this->config['bundle'];
     $coupon = commerce_coupon_create($type);
     $coupon->log = 'Created by FeedsCommerceCouponProcessor';
     return $coupon;
@@ -70,7 +70,7 @@ class FeedsCommerceCouponProcessor extends FeedsProcessor {
   public function configDefaults() {
     $types = commerce_coupon_type_get_name();
     return array(
-      'coupon_type' => key($types),
+      'bundle' => key($types),
       'is_active' => TRUE,
     ) + parent::configDefaults();
   }
@@ -81,14 +81,8 @@ class FeedsCommerceCouponProcessor extends FeedsProcessor {
   public function configForm(&$form_state) {
     $form = parent::configForm($form_state);
 
-    $types = commerce_coupon_type_get_name();
-    $form['coupon_type'] = array(
-      '#type' => 'select',
-      '#title' => t('Coupon type'),
-      '#description' => t('Select the coupon type for the coupon to be created.'),
-      '#options' => $types,
-      '#default_value' => $this->config['coupon_type'],
-    );
+    $form['bundle']['#title'] = t('Coupon type');
+    $form['bundle']['#description'] = t('Select the coupon type for the coupon to be created.');
     $form['is_active'] = array(
       '#type' => 'radios',
       '#title' => t('Status'),
@@ -109,7 +103,7 @@ class FeedsCommerceCouponProcessor extends FeedsProcessor {
     // Let other modules expose mapping targets.
     self::loadMappers();
     $entity_type = $this->entityType();
-    $bundle = $this->config['coupon_type'];
+    $bundle = $this->config['bundle'];
     drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
 
     return $targets;
diff --git a/plugins/FeedsCommerceCustomerProfileProcessor.inc b/plugins/FeedsCommerceCustomerProfileProcessor.inc
index 210c43c..9a6bbb7 100644
--- a/plugins/FeedsCommerceCustomerProfileProcessor.inc
+++ b/plugins/FeedsCommerceCustomerProfileProcessor.inc
@@ -30,7 +30,7 @@ class FeedsCommerceCustomerProfileProcessor extends FeedsProcessor {
    * Creates a new profile in memory and returns it.
    */
   protected function newEntity(FeedsSource $source) {
-    $type = $this->config['customer_profile_type'];
+    $type = $this->config['bundle'];
     $profile = commerce_customer_profile_new($type, 0);
     $profile->log = 'Created by FeedsCommerceProfileProcessor';
     return $profile;
@@ -71,7 +71,7 @@ class FeedsCommerceCustomerProfileProcessor extends FeedsProcessor {
     $types = commerce_customer_profile_type_get_name();
     $type = isset($types['billing']) ? 'billing' : key($types);
     return array(
-      'customer_profile_type' => $type,
+      'bundle' => $type,
     ) + parent::configDefaults();
   }
 
@@ -83,13 +83,8 @@ class FeedsCommerceCustomerProfileProcessor extends FeedsProcessor {
     array_walk($types, 'check_plain');
     $form = parent::configForm($form_state);
     unset($form['input_format']);
-    $form['customer_profile_type'] = array(
-      '#type' => 'select',
-      '#title' => t('Customer profile type'),
-      '#description' => t('Select the customer profile type for the profiles to be created. <strong>Note:</strong> Users with "import !feed_id feeds" permissions will be able to <strong>import</strong> profiles of the customer type selected here regardless of the customer profile level permissions. Further, users with "clear !feed_id permissions" will be able to <strong>delete</strong> imported profiles regardless of their customer profile level permissions.', array('!feed_id' => $this->id)),
-      '#options' => $types,
-      '#default_value' => $this->config['customer_profile_type'],
-    );
+    $form['bundle']['#title'] = t('Customer profile type');
+    $form['bundle']['#description'] = t('Select the customer profile type for the profiles to be created. <strong>Note:</strong> Users with "import !feed_id feeds" permissions will be able to <strong>import</strong> profiles of the customer type selected here regardless of the customer profile level permissions. Further, users with "clear !feed_id permissions" will be able to <strong>delete</strong> imported profiles regardless of their customer profile level permissions.', array('!feed_id' => $this->id));
     return $form;
   }
 
@@ -116,7 +111,7 @@ class FeedsCommerceCustomerProfileProcessor extends FeedsProcessor {
     // Let other modules expose mapping targets.
     self::loadMappers();
     $entity_type = $this->entityType();
-    $bundle = $this->config['customer_profile_type'];
+    $bundle = $this->config['bundle'];
     drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
 
     return $targets;
@@ -131,7 +126,7 @@ class FeedsCommerceCustomerProfileProcessor extends FeedsProcessor {
         $profile_types = commerce_customer_profile_type_get_name();
         $target_entity->type = $value;
         if (!array_key_exists($value, $profile_types)) {
-          $target_entity->type = $this->config['customer_profile_type'];
+          $target_entity->type = $this->config['bundle'];
         }
         break;
       case 'uid':
diff --git a/plugins/FeedsCommerceProductProcessor.inc b/plugins/FeedsCommerceProductProcessor.inc
index 9e0ab50..8d56ed9 100644
--- a/plugins/FeedsCommerceProductProcessor.inc
+++ b/plugins/FeedsCommerceProductProcessor.inc
@@ -30,7 +30,7 @@ class FeedsCommerceProductProcessor extends FeedsProcessor {
    * Creates a new product in memory and returns it.
    */
   protected function newEntity(FeedsSource $source) {
-    $type = $this->config['product_type'];
+    $type = $this->config['bundle'];
     $product = commerce_product_new($type);
     $product->uid = $this->config['author'];
     $product->log = 'Created by FeedsCommerceProductProcessor';
@@ -72,7 +72,7 @@ class FeedsCommerceProductProcessor extends FeedsProcessor {
     $types = commerce_product_type_get_name();
     $type = isset($types['product']) ? 'product' : key($types);
     return array(
-      'product_type' => $type,
+      'bundle' => $type,
       'author' => 0,
       'tax_rate' => '',
     ) + parent::configDefaults();
@@ -85,13 +85,8 @@ class FeedsCommerceProductProcessor extends FeedsProcessor {
     $types = commerce_product_type_get_name();
     array_walk($types, 'check_plain');
     $form = parent::configForm($form_state);
-    $form['product_type'] = array(
-      '#type' => 'select',
-      '#title' => t('Product type'),
-      '#description' => t('Select the product type for the products to be created. <strong>Note:</strong> Users with "import !feed_id feeds" permissions will be able to <strong>import</strong> products of the type selected here regardless of the product level permissions. Further, users with "clear !feed_id permissions" will be able to <strong>delete</strong> imported products regardless of their product level permissions.', array('!feed_id' => $this->id)),
-      '#options' => $types,
-      '#default_value' => $this->config['product_type'],
-    );
+    $form['bundle']['#title'] = t('Product type');
+    $form['bundle']['#description'] = t('Select the product type for the products to be created. <strong>Note:</strong> Users with "import !feed_id feeds" permissions will be able to <strong>import</strong> products of the type selected here regardless of the product level permissions. Further, users with "clear !feed_id permissions" will be able to <strong>delete</strong> imported products regardless of their product level permissions.', array('!feed_id' => $this->id));
     $author = user_load($this->config['author']);
     $form['author'] = array(
       '#type' => 'textfield',
@@ -174,7 +169,7 @@ class FeedsCommerceProductProcessor extends FeedsProcessor {
     // Let other modules expose mapping targets.
     self::loadMappers();
     $entity_type = $this->entityType();
-    $bundle = $this->config['product_type'];
+    $bundle = $this->config['bundle'];
     drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
 
     return $targets;
-- 
2.6.3.windows.1

