Index: uc_upsell.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_upsell/uc_upsell.module,v
retrieving revision 1.7.2.1
diff -u -r1.7.2.1 uc_upsell.module
--- uc_upsell.module	27 Mar 2009 00:29:22 -0000	1.7.2.1
+++ uc_upsell.module	7 Apr 2009 19:05:40 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: uc_upsell.module,v 1.7.2.1 2009/03/27 00:29:22 torgospizza Exp $
+// $Id: uc_upsell.module,v 1.7.2.7 2009/04/06 05:10:54 torgospizza Exp $
 
 /**
  * @file
@@ -176,6 +176,7 @@
 
   // Create an array of selected related products and add the current product to the related products lists for each of them
   if (in_array($node_type, $product_types)) {
+
     foreach ($form_state['values']['relatedProducts'] as $product) {
       $products[] = $product;
 
@@ -740,10 +741,9 @@
 /**
  * Get parts for cart pane
  */
-function uc_upsell_cart_pane_content() {
-  $msg = '';
-  $related = uc_upsell_resolve_associates($msg); // get related cart items (sets $msg too)
-  return theme('pane_upsell', $related, $msg);
+function uc_upsell_cart_pane_content($data = '') {
+  $related = uc_upsell_resolve_associates($data); // get related cart items (sets $msg too)
+  return theme('pane_upsell', $related, $data);
 }
 
 
@@ -751,7 +751,7 @@
  * Renders upsell block
  */
 function uc_upsell_block_contents($id = 0) {
-  $msg = '';
+
   // Get related cart items (sets $msg too)
   if ($nodes = uc_upsell_resolve_associates($msg)) {
 
@@ -759,26 +759,26 @@
     foreach ($nodes as $node) {
       $output = '';
 
-      if ($node->field_image_cache) {
-
-        $output .= l(
-          theme('imagecache', uc_upsell_get_imagecache_preset(), $node->image['filepath'] . $node->field_image_cache[0]['filepath'], $node->title, $node->title, array('class' => 'buy-related-item')),
-          drupal_get_path_alias("node/". $node->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
-        $output .= '<br/>';
+      if ($node->field_image_cache && file_exists($node->image['filepath'] . $node->field_image_cache[0]['filepath'])) {
+        $output .= l(theme('imagecache', uc_upsell_get_imagecache_preset(), $node->image['filepath'] . $node->field_image_cache[0]['filepath'], $node->title, $node->title, array('class' => 'buy-related-item')), drupal_get_path_alias("node/". $node->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
+      } else {
+        //use the default image
+        $output .= l(theme('imagecache', uc_upsell_get_imagecache_preset(), 'files/default_product.jpg', $node->title, $node->title, array('class' => 'buy-related-item')), drupal_get_path_alias("node/". $node->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
       }
 
       // Linked title
+      $output .= '<div class="upsell-block-product-title">';
       $output .= l($node->title, drupal_get_path_alias("node/". $node->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
+      $output .= '</div>';
 
       // Only show an "add to cart" link if there is a value for Sell Price
       if ($node->sell_price) {
-        $output .= '<br />';
-        $output .= l(t('Add to Cart ') . uc_currency_format($node->sell_price), "cart/add/i{$node->nid}Checkout-p{$node->nid}_q1", array('attributes' => array('class' => 'buy-related-item-buynow'), 'query' => 'destination=cart'));
+        $output .= l(t('Add to Cart ') . uc_currency_format($node->sell_price), "cart/add/p{$node->nid}_q1-i{$node->nid}Checkout", array('attributes' => array('class' => 'buy-related-item-buynow'), 'query' => 'destination=cart'));
       }
       $content[] = $output;
 
     }
-    return array('subject' => $msg, 'content' => '<div class="upsell-block-product">'. implode('</div><div class="upsell-block-product">', $content) .'</div>', 'related' => $nodes);
+    return array('subject' => $msg, 'content' => '<div id="upsell-block-'.$id.'"><div class="upsell-block-product">'. implode('</div><div class="upsell-block-product">', $content) .'</div></div>', 'related' => $nodes);
   }
   else {
     return array();
@@ -802,27 +802,23 @@
   }
   else {
     $is_block = TRUE;
-    $matches = array(); // Matches should we need the node id
-
-    preg_match('/^node\/(\d+)$/', $dpath, $matches);
-    //dsm($matches[1]);
-
+    $node = node_load(arg(1));
     // If this is a product node...
-    if ($matches && uc_product_is_product($matches[1] * 1)) {
-      $product_nid = $matches[1] * 1;
+    if (uc_product_is_product($node) == TRUE) {
+      $product_nid = $node->nid;
       $context = 'product';
     }
     else {
       $context = 'node';
     }
   }
-  //dsm("context: '$context' dpath: $dpath nid: {$matches[1]}");
+  //drupal_set_message("context: '$context' dpath: $dpath nid: {$matches[1]}");
   // If can't be enabled for this context, exit function
   if ($context != 'pane' && (($config['block']['enable'] == 1 && $context == 'product') || ($config['block']['enable'] == 0 && $context == 'node'))) {
     return array();
   }
 
-  //dsm($config['block'][$context]);
+  //drupal_set_message('Context' .$config['block'][$context]);
 
   // set vars to use when getting related items, based on the type of page being shown
   $vars = array(
@@ -835,7 +831,7 @@
   $user_cart = uc_cart_get_contents();
 
   foreach ($user_cart as $cart) {
-    $cart_nids[] = $cart->nid;
+    $cart_nids[$cart->nid] = $cart->nid;
   }
 
   $related = array(); // List of related nodes to return
@@ -846,7 +842,7 @@
   // If source is a product node, begin with nodes related to this product that are not in cart or itself
   if (isset($product_nid)) {
     // Start list with nids related to the product node
-    $related = uc_upsell_get_associate_nids($product_nid, $vars['max'], $cart_nids);
+    $related = uc_upsell_get_associate_nids($cart_nids, intval($vars['max']), $cart_nids);
 
     // If related is not empty...
     if (!empty($related)) {
@@ -857,7 +853,6 @@
   else {
     // We are on the cart, so we need to use $cart_nids as the source, as well as filter.
     $related = uc_upsell_get_associate_nids($cart_nids, $vars['max'], $cart_nids);
-
     if (!empty($related)) {
       $primary = 1;
       $sources += 1;
@@ -876,7 +871,7 @@
     $rel_count = count($related);
 
     // Get/add nids related to the cart
-    $related = array_merge($related, uc_upsell_get_associate_nids($cart_nids, $vars['max'] - count($related)));
+    //$related = array_merge($related, uc_upsell_get_associate_nids($cart_nids, $vars['max'] - count($related)));
 
     // if related count is greater than original...
     if (count($related) > $rel_count) {
@@ -885,6 +880,7 @@
     }
   }
 
+  //drupal_set_message('number of related '.count($related));
   // If count of related nodes doesn't meet threshold and can include random nodes, add them.
   if (count($related) < $vars['max'] && $vars['use_random']) {
     $product_types = uc_upsell_get_config_types();
@@ -930,6 +926,7 @@
  */
 function uc_upsell_get_associate_nids($source, $max = 0, $filter = array()) {
   // Process args (a bit more robust here, since developer's will likely use it)
+
   // Ensure source an array
   if (!is_array($source) && (is_numeric($source) || (is_object($source) && $source->nid))) $source = array((is_numeric($source)) ? $source : $source->nid);
   // Ensure filter an array
@@ -937,13 +934,17 @@
   // Ensure max is an integer
   if (!is_int($max) && is_numeric($max)) $max = abs(round($max));
   // If source or filter is not an array, or max is not an integer, exit function
-  if (!is_array($source) || !is_array($filter) || is_int($max)) return array();
+  if (!is_array($source) || !is_array($filter) || !is_int($max)) {
+    //drupal_set_message('returning');
+    return array();
+  }
 
   $ary = array(); // Array to return
   $sought = array(); // Capture found nid
 
   foreach ($source as $nid) {
     if ((is_object($nid) && $nid->nid) || (is_array($nid) && $nid['nid'])) $nid = (is_object($nid)) ? $nid->nid : $nid['nid'];
+
     if (is_numeric($nid)) {
       $sought[] = $nid;
 
@@ -955,13 +956,13 @@
 
       if (is_array(unserialize($related))) {
         $ary = array_merge($ary, unserialize($related));
-        $ary = uc_upsell_get_types_array($ary);
-        $ary = uc_upsell_filter_types($ary);
       }
     }
   }
 
   // Remove found nids with filter and already sought nids, make them unique
+  $ary = uc_upsell_get_types_array($ary);
+  $ary = uc_upsell_filter_types($ary);
   $ary = array_diff(array_keys($ary), $filter, $sought);
   $ary = array_unique($ary);
 
@@ -1001,22 +1002,21 @@
   foreach ($products as $related) {
     $output .= '<div class="cart-related-item">';
 
-    // If ImageCache has been enabled, find the 'thumbnail' preset and filepath, and display a linked themed thumbnail image.
-    if ($related->field_image_cache) {
-      $output .= l(
-        theme('imagecache', uc_upsell_get_imagecache_preset(), $related->image['filepath'] . $related->field_image_cache[0]['filepath'],
-          $related->title, $related->title, array('class' => 'buy-related-item')),
-        drupal_get_path_alias("node/". $rel_product->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
-      $output .= '<br/>';
+    if ($related->field_image_cache && file_exists($related->image['filepath'] . $related->field_image_cache[0]['filepath'])) {
+      $output .= l(theme('imagecache', uc_upsell_get_imagecache_preset(), $related->image['filepath'] . $related->field_image_cache[0]['filepath'], $related->title, $related->title, array('class' => 'buy-related-item')), drupal_get_path_alias("node/". $related->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
+    } else {
+      //use the default image
+      $output .= l(theme('imagecache', uc_upsell_get_imagecache_preset(), 'files/default_product.jpg', $related->title, $related->title, array('class' => 'buy-related-item')), drupal_get_path_alias("node/". $related->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
     }
 
     // Linked title
-    $output .= l($rel_product->title, drupal_get_path_alias("node/". $rel_product->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
+    $output .= '<div class="upsell-block-product-title">';
+    $output .= l($related->title, drupal_get_path_alias("node/". $related->nid), array('attributes' => array('class' => 'buy-related-item'), 'html' => TRUE));
+    $output .= '</div>';
 
     // Only show an "add to cart" link if there is a value for Sell Price
     if ($related->sell_price) {
-      $output .= '<br />';
-      $output .= l(t('Buy now ') . uc_currency_format($rel_product->sell_price), "cart/add/i{$rel_product->nid}Checkout-p{$rel_product->nid}_q1", array('attributes' => array('class' => 'buy-related-item-buynow'), 'query' => 'destination=cart'));
+      $output .= l(t('Buy now ') . uc_currency_format($related->sell_price), "cart/add/p{$related->nid}_q1-i{$related->nid}Checkout", array('attributes' => array('class' => 'buy-related-item-buynow'), 'query' => 'destination=cart'));
     }
 
     $output .= '</div>';
@@ -1095,3 +1095,21 @@
   $preset = isset($config['global']['thumbnail_preset']) ? $config['global']['thumbnail_preset'][0] : 'uc_thumbnail';
   return $preset;
 }
+
+
+/**
+ * Implementation of hook_nodeapi
+ */
+function uc_upsell_nodeapi(&$node, $op) {
+  if ($op == 'view') {
+    $block = module_invoke('uc_upsell', 'block', 'view', 0);
+    if ($block['content']) {
+      $output .= '<p class="product-related">'.$block['subject'].':</p><br/>';
+      $output .= '<div class="content">'.$block['content'].'</div>';
+    }
+    $node->content['upsell'] = array(
+      '#value' => $output,
+      '#weight' => 10,
+    );
+  }
+}

