From 3aa12f0f16b98cf36880770fda26eb4a092e6234 Mon Sep 17 00:00:00 2001
From: Peter Lieverdink <me@cafuego.net>
Date: Mon, 20 Jun 2011 18:04:12 +1000
Subject: [PATCH 1/4] #873398. Add views integration to uc_product_kit

---
 uc_product_kit/uc_product_kit.module               |   10 +
 uc_product_kit/views/uc_product_kit.views.inc      |  205 ++++++++++++++++++++
 .../uc_product_kit_handler_filter_product_kit.inc  |   16 ++
 ...product_kit_handler_filter_product_kit_item.inc |   16 ++
 4 files changed, 247 insertions(+), 0 deletions(-)
 create mode 100644 uc_product_kit/views/uc_product_kit.views.inc
 create mode 100644 uc_product_kit/views/uc_product_kit_handler_filter_product_kit.inc
 create mode 100644 uc_product_kit/views/uc_product_kit_handler_filter_product_kit_item.inc

diff --git a/uc_product_kit/uc_product_kit.module b/uc_product_kit/uc_product_kit.module
index f924e3c..07cdb13 100644
--- a/uc_product_kit/uc_product_kit.module
+++ b/uc_product_kit/uc_product_kit.module
@@ -1132,3 +1132,13 @@ function _uc_product_kit_sort_products($a, $b) {
     return ($a->ordering < $b->ordering) ? -1 : 1;
   }
 }
+
+/**
+ * Implements hook_views_api().
+ */
+function uc_product_kit_views_api() {
+  return array(
+    'api' => 2,
+    'path' => drupal_get_path('module', 'uc_product_kit') .'/views',
+  );
+}
diff --git a/uc_product_kit/views/uc_product_kit.views.inc b/uc_product_kit/views/uc_product_kit.views.inc
new file mode 100644
index 0000000..8aa1e46
--- /dev/null
+++ b/uc_product_kit/views/uc_product_kit.views.inc
@@ -0,0 +1,205 @@
+<?php
+/**
+ * Implements hook_views_data()
+ */
+function uc_product_kit_views_data() {
+  $data = array();
+
+  $data['uc_product_kits']['table'] = array(
+    'group' => t('Product Kit'),
+    'join' => array(
+      'uc_products' => array(
+        'left_field' => 'nid',
+        'field' => 'product_id',
+      ),
+      'node' => array(
+        'left_field' => 'nid',
+        'field' => 'product_id',
+      ),
+    ),
+  );
+
+  $data['uc_product_kits']['nid'] = array(
+    'title' => t('Node ID'),
+    'help' => t('The {node}.nid of the product kit.'),
+    'field' => array(
+      'handler' => 'views_handler_field_node',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+      'allow empty' => TRUE,
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_node_nid',
+      'numeric' => TRUE,
+      'validate type' => 'nid',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'relationship' => array(
+      'handler' => 'views_handler_relationship',
+      'base' => 'node',
+      'base field' => 'nid',
+      'label' => t('kit'),
+    ),
+  );
+
+  $data['uc_product_kits']['product_id'] = array(
+    'title' => t('Product'),
+    'help' => t('The {uc_products}.nid of a product contained in the kit.'),
+    'field' => array(
+      'handler' => 'views_handler_field_node',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+      'allow empty' => TRUE,
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_node_nid',
+      'numeric' => TRUE,
+      'validate type' => 'nid',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'relationship' => array(
+      'handler' => 'views_handler_relationship',
+      'base' => 'uc_products',
+      'base field' => 'nid',
+      'label' => t('product'),
+    ),
+  );
+
+  $data['uc_product_kits']['mutable'] = array(
+    'title' => t('Mutable'),
+    'help' => t('A flag indicating whether the contents of the kit can be changed by the customer.'),
+    'field' => array(
+      'handler' => 'views_handler_field_boolean',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_boolean_operator',
+      'type' => 'yes-no',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  $data['uc_product_kits']['qty'] = array(
+    'title' => t('Quantity'),
+    'help' => t('The number of this product contained in the kit.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+      'allow empty' => TRUE,
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_numeric',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  $data['uc_product_kits']['discount'] = array(
+    'title' => t('Discount'),
+    'help' => t('The adjustment to the {uc_products}.sell_price of the product.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+      'allow empty' => TRUE,
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_numeric',
+    ),
+      'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  $data['uc_product_kits']['ordering'] = array(
+    'title' => t('Weight'),
+    'help' => ('The weight of this product in relation to other products in the kit.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+      'allow empty' => TRUE,
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_numeric',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  $data['uc_product_kits']['synchronized'] = array(
+    'title' => t('Synchronized'),
+    'help' => t('A flag indicating that changes to the sell price of this product will change the total price of the kit. 1 => Yes. 0 => No.'),
+    'field' => array(
+      'handler' => 'views_handler_field_boolean',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_boolean_operator',
+      'type' => 'yes-no',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  $data['uc_product_kits']['is_kit_item'] = array(
+    'title' => t('Is part of a product kit'),
+    'help' => t('Check for the fields provided by the Ubercart product_kit module.'),
+    'group' => t('Node'),
+    'filter' => array(
+      'handler' => 'uc_product_kit_handler_filter_product_kit_item',
+      'label' => t('Is part of a product kit'),
+    ),
+  );
+
+  $data['uc_product_kits']['is_kit'] = array(
+    'title' => t('Is a product kit'),
+    'help' => t('Check for the fields provided by the Ubercart product_kit module.'),
+    'group' => t('Node'),
+    'filter' => array(
+      'handler' => 'uc_product_kit_handler_filter_product_kit',
+      'label' => t('Is a product kit'),
+    ),
+  );
+
+  return $data;
+}
+
+/**
+ * Implements hook_views_handlers().
+ */
+function uc_product_kit_views_handlers() {
+  return array(
+    'info' => array(
+      'path' => drupal_get_path('module', 'uc_product_kit') .'/views',
+    ),
+    'handlers' => array(
+      'uc_product_kit_handler_filter_product_kit' => array(
+        'parent' => 'views_handler_filter_boolean_operator',
+      ),
+      'uc_product_kit_handler_filter_product_kit_item' => array(
+        'parent' => 'views_handler_filter_boolean_operator',
+      ),
+    ),
+  );
+}
diff --git a/uc_product_kit/views/uc_product_kit_handler_filter_product_kit.inc b/uc_product_kit/views/uc_product_kit_handler_filter_product_kit.inc
new file mode 100644
index 0000000..8a12a76
--- /dev/null
+++ b/uc_product_kit/views/uc_product_kit_handler_filter_product_kit.inc
@@ -0,0 +1,16 @@
+<?php
+/**
+ * @file
+ * Views handler: Node filter on "product-kit-ness".
+ */
+
+/**
+ * Filter nodes based on whether they have an entry in the uc_product_kit table.
+ */
+class uc_product_kit_handler_filter_product_kit extends views_handler_filter_boolean_operator {
+  function query() {
+    $this->query->add_field('node', 'type');
+    $this->query->add_where($this->options['group'], "node.type ". (empty($this->value) ? "NOT " : "") ."IN ('product_kit')");
+  }
+}
+
diff --git a/uc_product_kit/views/uc_product_kit_handler_filter_product_kit_item.inc b/uc_product_kit/views/uc_product_kit_handler_filter_product_kit_item.inc
new file mode 100644
index 0000000..1122bb6
--- /dev/null
+++ b/uc_product_kit/views/uc_product_kit_handler_filter_product_kit_item.inc
@@ -0,0 +1,16 @@
+<?php
+/**
+ * @file
+ * Views handler: Node filter on "product-kit-part-ness".
+ */
+
+/**
+ * Filter nodes based on whether they are part of a product kit.
+ */
+class uc_product_kit_handler_filter_product_kit_item extends views_handler_filter_boolean_operator {
+  function query() {
+    $this->query->add_field('node', 'nid');
+    $this->query->add_where($this->options['group'], "node.nid ". (empty($this->value) ? "NOT " : "") ."IN (SELECT product_id FROM {uc_product_kits})");
+  }
+}
+
-- 
1.7.4.1


From ee0b5b03527004721bdb886c3355496ce42b91c8 Mon Sep 17 00:00:00 2001
From: Lyle Mantooth <lyle@ubercart.org>
Date: Thu, 25 Aug 2011 16:44:19 -0400
Subject: [PATCH 2/4] Port product kit Views integration to Drupal 7.

---
 uc_product_kit/uc_product_kit.info            |    2 ++
 uc_product_kit/uc_product_kit.module          |    2 +-
 uc_product_kit/views/uc_product_kit.views.inc |   19 -------------------
 3 files changed, 3 insertions(+), 20 deletions(-)

diff --git a/uc_product_kit/uc_product_kit.info b/uc_product_kit/uc_product_kit.info
index 94d59cc..c465aa4 100644
--- a/uc_product_kit/uc_product_kit.info
+++ b/uc_product_kit/uc_product_kit.info
@@ -3,3 +3,5 @@ description = This node module represents two or more products that have been li
 dependencies[] = uc_product
 package = Ubercart - extra
 core = 7.x
+files[] = views/uc_product_kit_handler_filter_product_kit.inc
+files[] = views/uc_product_kit_handler_filter_product_kit_item.inc
diff --git a/uc_product_kit/uc_product_kit.module b/uc_product_kit/uc_product_kit.module
index 07cdb13..9338ce7 100644
--- a/uc_product_kit/uc_product_kit.module
+++ b/uc_product_kit/uc_product_kit.module
@@ -1138,7 +1138,7 @@ function _uc_product_kit_sort_products($a, $b) {
  */
 function uc_product_kit_views_api() {
   return array(
-    'api' => 2,
+    'api' => 3,
     'path' => drupal_get_path('module', 'uc_product_kit') .'/views',
   );
 }
diff --git a/uc_product_kit/views/uc_product_kit.views.inc b/uc_product_kit/views/uc_product_kit.views.inc
index 8aa1e46..585663e 100644
--- a/uc_product_kit/views/uc_product_kit.views.inc
+++ b/uc_product_kit/views/uc_product_kit.views.inc
@@ -184,22 +184,3 @@ function uc_product_kit_views_data() {
 
   return $data;
 }
-
-/**
- * Implements hook_views_handlers().
- */
-function uc_product_kit_views_handlers() {
-  return array(
-    'info' => array(
-      'path' => drupal_get_path('module', 'uc_product_kit') .'/views',
-    ),
-    'handlers' => array(
-      'uc_product_kit_handler_filter_product_kit' => array(
-        'parent' => 'views_handler_filter_boolean_operator',
-      ),
-      'uc_product_kit_handler_filter_product_kit_item' => array(
-        'parent' => 'views_handler_filter_boolean_operator',
-      ),
-    ),
-  );
-}
-- 
1.7.4.1


From 46875e2c0f10b70ee11fc537fc0a39cccf5858dc Mon Sep 17 00:00:00 2001
From: Lyle Mantooth <lyle@ubercart.org>
Date: Thu, 25 Aug 2011 16:44:44 -0400
Subject: [PATCH 3/4] Join {uc_product_kits} to {uc_products} as the kit node, not the components.

---
 uc_product_kit/views/uc_product_kit.views.inc |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/uc_product_kit/views/uc_product_kit.views.inc b/uc_product_kit/views/uc_product_kit.views.inc
index 585663e..986c934 100644
--- a/uc_product_kit/views/uc_product_kit.views.inc
+++ b/uc_product_kit/views/uc_product_kit.views.inc
@@ -10,11 +10,11 @@ function uc_product_kit_views_data() {
     'join' => array(
       'uc_products' => array(
         'left_field' => 'nid',
-        'field' => 'product_id',
+        'field' => 'nid',
       ),
       'node' => array(
         'left_field' => 'nid',
-        'field' => 'product_id',
+        'field' => 'nid',
       ),
     ),
   );
@@ -67,7 +67,7 @@ function uc_product_kit_views_data() {
     ),
     'relationship' => array(
       'handler' => 'views_handler_relationship',
-      'base' => 'uc_products',
+      'base' => 'node',
       'base field' => 'nid',
       'label' => t('product'),
     ),
-- 
1.7.4.1


From bb25dcc6afc7af844a91b9171355b24fd55280cc Mon Sep 17 00:00:00 2001
From: Lyle Mantooth <lyle@ubercart.org>
Date: Thu, 25 Aug 2011 16:56:24 -0400
Subject: [PATCH 4/4] Join on vid to prevent duplicate rows.

---
 uc_product_kit/views/uc_product_kit.views.inc |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/uc_product_kit/views/uc_product_kit.views.inc b/uc_product_kit/views/uc_product_kit.views.inc
index 986c934..872d6d2 100644
--- a/uc_product_kit/views/uc_product_kit.views.inc
+++ b/uc_product_kit/views/uc_product_kit.views.inc
@@ -9,12 +9,12 @@ function uc_product_kit_views_data() {
     'group' => t('Product Kit'),
     'join' => array(
       'uc_products' => array(
-        'left_field' => 'nid',
-        'field' => 'nid',
+        'left_field' => 'vid',
+        'field' => 'vid',
       ),
       'node' => array(
-        'left_field' => 'nid',
-        'field' => 'nid',
+        'left_field' => 'vid',
+        'field' => 'vid',
       ),
     ),
   );
-- 
1.7.4.1

