From e60f46eac27932932d65772d0980f7cf2961f9e4 Mon Sep 17 00:00:00 2001
From: Peter Lieverdink <me@cafuego.net>
Date: Mon, 20 Jun 2011 18:04:12 +1000
Subject: [PATCH] #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 c086170..f81b5af 100644
--- a/uc_product_kit/uc_product_kit.module
+++ b/uc_product_kit/uc_product_kit.module
@@ -1169,3 +1169,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.1

