Index: uc_attribute_stock_filter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_attribute_stock_filter/uc_attribute_stock_filter.module,v
retrieving revision 1.12
diff -u -p -r1.12 uc_attribute_stock_filter.module
--- uc_attribute_stock_filter.module	5 Apr 2009 22:58:25 -0000	1.12
+++ uc_attribute_stock_filter.module	2 Aug 2009 15:16:37 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: uc_attribute_stock_filter.module,v 1.12 2009/04/05 22:58:25 henrrrik Exp $
+// $Id: uc_attribute_stock_filter.module,v 1.1.2.2 2009/04/06 00:32:03 henrrrik Exp $
 
 /**
  * @file
@@ -15,40 +15,42 @@
  * Implementation of hook_nodeapi().
  */
 function uc_attribute_stock_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  switch ($op) {
-    case 'view':
 
-      // Get attribute count for this product
-      $attrib = db_fetch_object(db_query("SELECT COUNT(aid) AS count FROM {uc_product_attributes} WHERE nid = %d", $node->nid));
+  static $available = array();
 
-      // Only proceed if we have more than one attribute (otherwise we do the filtering with hook_form_alter())
-      if ($attrib->count > 1) {
-        // Grab serialized options and stock levels
-        $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", $node->nid);
-
-        // This builds an array where each option is a key containing its available combinations as elements
-        $available = array();
-        while ($data = db_fetch_object($result)) {
-          $combo = unserialize($data->combination);
-          if ($data->stock > 0) { // Stock is available
-            foreach ($combo as $key => $option) { // Grab possible combinations
-              foreach ($combo as $avail_key => $avail_opt) {
-                if ($avail_key != $key) {
-                  $available[$option][] = $avail_opt;
-                }
+  if (($op == 'view') ||
+      (($op == 'load') && (preg_match('%^/catalog/%', request_uri())) &&
+       variable_get('uc_catalog_grid_display_attributes', TRUE))) {
+
+    // Get attribute count for this product
+    $attrib = db_fetch_object(db_query("SELECT COUNT(aid) AS count FROM {uc_product_attributes} WHERE nid = %d", $node->nid));
+
+    // Only proceed if we have more than one attribute (otherwise we do the filtering with hook_form_alter())
+    if ($attrib->count > 1) {
+      // Grab serialized options and stock levels
+      $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", $node->nid);
+
+      // This builds an array where each option is a key containing its available combinations as elements
+      while ($data = db_fetch_object($result)) {
+        $combo = unserialize($data->combination);
+        if ($data->stock > 0) { // Stock is available
+          foreach ($combo as $key => $option) { // Grab possible combinations
+            foreach ($combo as $avail_key => $avail_opt) {
+              if ($avail_key != $key) {
+                $available[$node->nid][$option][] = $avail_opt;
               }
             }
           }
         }
-        if (!empty($available)) { // If there are no stock levels at all, we assume stocks aren't used and skip filtering.
+      }
+      if (!empty($available)) { // If there are no stock levels at all, we assume stocks aren't used and skip filtering.
 
-          // Encode the array as JSON and add the JavaScript
-          $js_array = "var uc_asf_AvailableOptions = ". json_encode($available);
-          drupal_add_js($js_array, 'inline');
-          drupal_add_js(drupal_get_path('module', 'uc_attribute_stock_filter') .'/uc_attribute_stock_filter.js', 'module');
-        }
+        // Encode the array as JSON and add the JavaScript
+        $js_array = "var uc_asf_AvailableOptions = ". json_encode($available);
+        drupal_add_js($js_array, 'inline');
+        drupal_add_js(drupal_get_path('module', 'uc_attribute_stock_filter') .'/uc_attribute_stock_filter.js', 'module');
       }
-      break;
+    }
   }
 }
 
@@ -59,20 +61,18 @@ function uc_attribute_stock_filter_form_
 
   if (ereg('^uc_product_add_to_cart_form_', $form_id)) {
 
-    $stock = db_fetch_object( db_query("SELECT COUNT(s.stock) AS count FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1 AND stock > 0", $form['nid']['#value']));
-
     // Does the product have attributes?
     $product_attributes = db_fetch_object( db_query("SELECT COUNT(nid) as count FROM {uc_product_adjustments} WHERE nid = %d", $form['nid']['#value']));
 
     if ($product_attributes->count < 1) {
       // No attributes
-      $stock = db_fetch_object( db_query("SELECT s.stock as count FROM {uc_product_stock} s WHERE s.nid = %d", $form['nid']['#value']));
+      $stock = db_fetch_object( db_query("SELECT s.stock AS count, s.active AS active FROM {uc_product_stock} s WHERE s.nid = %d", $form['nid']['#value']));
     }
     else {
-      $stock = db_fetch_object( db_query("SELECT COUNT(s.stock) AS count FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1 AND stock > 0", $form['nid']['#value']));
+      $stock = db_fetch_object( db_query("SELECT COUNT(s.stock) AS count, 1 AS active FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1 AND stock > 0", $form['nid']['#value']));
     }
 
-    if ($stock->count < 0) {
+    if ($stock->active && $stock->count < 1) {
        // We're all out, kill the 'Add to cart' submit button and disable any attribute select boxes
       $form['submit']['#attributes']['disabled'] = 'disabled';
       $form['submit']['#value'] = t('Out of stock');
@@ -84,8 +84,8 @@ function uc_attribute_stock_filter_form_
         }
       }
     }
-    else if ($product_attributes->count > 0) { // The product has at least one attribute
-      $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", arg(1));
+    else if ($stock->active && $product_attributes->count > 0) { // The product has at least one attribute
+      $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", $form['nid']['#value']);
 
       // This builds an array containing the available options
       $available = array();
@@ -110,4 +110,4 @@ function uc_attribute_stock_filter_form_
     }
 
   }
-}
\ No newline at end of file
+}
