
<?php
/* 
$Id: xsell_products.php, v1  2002/09/11

osCommerce, Open Source E-Commerce Solutions 
<http://www.oscommerce.com> 

Copyright (c) 2002 osCommerce 

Released under the GNU General Public License 
*/ 

// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();


if ($HTTP_GET_VARS['products_id']) { 
//xsell_products forced//
$products_id_list=$cart->get_product_id_list(true);
if ($products_id_list=='') 	$products_id_list=0;
$xsell_query = tep_db_query("select distinct p.products_id, p.products_image_1,p.products_title_1,pd.products_name, p.products_tax_class_id,xp.xsell_id, products_price from " . TABLE_PRODUCTS_XSELL . " xp, " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where xp.xsell_type='F' and xp.item_type='P' and xp.products_id = '" . tep_db_input(tep_db_prepare_input($HTTP_GET_VARS['products_id'])) . "' and xp.xsell_id = p.products_id and p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "' and p.products_status = '1' and xp.xsell_id not in ('" .$products_id_list . "') order by xp.products_id asc");

$num_products_xsell = tep_db_num_rows($xsell_query); 
if ($num_products_xsell >= MIN_DISPLAY_XSELL) { 

      $info_box_contents = array();
      $info_box_contents[] = array('align' => 'left', 'text' => TEXT_XSELL_FORCED_PRODUCTS);
      new contentBoxHeading($info_box_contents, false, false);

      $info_box_contents = array();
	//$info_box_contents[0][0]=array('align'=>'right','params'=>' colspan=3 class="smallText" valign="middle"','text'=>tep_draw_selection_field('xsell_forced_item','checkbox',$xsell['xsell_id'],'','valign=top') . TEXT_SELECT_FORCED_ITEMS);
	$info_box_contents[0][1]=array('text'=>'');
	$info_box_contents[0][2]=array('text'=>'');

      $row = 1;
      $col = 0;
	  $forced_product_list='';
	  $product_atrribs='';$abcnt=0; 
if(tep_db_num_rows($xsell_query)>0) {
	$GLOBALS["JS_DETAILS"]["FORCED_ITEMS"]="{enabled:true,values:[";	
	
      while ($xsell = tep_db_fetch_array($xsell_query)) {
        $xsell['specials_new_products_price'] = tep_get_products_special_price($xsell['products_id']); 

//
if ($xsell['specials_new_products_price']) { 
      $xsell_price =  '<s>' . $currencies->display_price($xsell['products_price'], tep_get_tax_rate($xsell['products_tax_class_id'])) . '</s><br>'; 
      $xsell_price .= '<span class="productSpecialPrice">' . $currencies->display_price($xsell['specials_new_products_price'], tep_get_tax_rate($xsell['products_tax_class_id'])) . '</span>'; 
    } else { 
      $xsell_price =  $currencies->display_price($xsell['products_price'], tep_get_tax_rate($xsell['products_tax_class_id'])); 
    } 

			$products_attributes_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$xsell['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "'");
			$products_attributes = tep_db_fetch_array($products_attributes_query);
			$options_text="";
			$products_text=""; 
			
			echo '<input type="hidden" name="xsell_forced_id[]" value="'.$xsell['xsell_id'].'">';
			$products_text='<tr><td class=smalltext ><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $xsell['products_id']) . '">' .tep_product_small_image($xsell['products_image_1'],$xsell['products_title_1']) . '</a></td>' .
											   '<td align=left class=smalltext valign="top"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $xsell['products_id']) . '">' . $xsell['products_title_1'] .'</a><br>' . $xsell_price. '</td></tr>';
			

			if ($products_attributes["total"] > 0) {
				$products_options_name = tep_db_query("select distinct popt.products_options_id, popt.products_options_name, popt.products_options_sort_order from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$xsell['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "'" . " order by popt.products_options_sort_order");
				$options_text='<tr><td colspan="2" valign="top" class="smallText">';
				$options_text.= '<b>' . TEXT_PRODUCT_OPTIONS . '</b><br>' .
							 '<table border="0" cellpading="0" cellspacing="1">';
						
				if(tep_db_num_rows($products_options_name)) 	{
					//$forced_products_list.='[' . $xsell['products_id'] . ',' ;
					//$forced_product_list.= $abcnt . ':' . $xsell['products_id']. ',';	 //FORCED
					
					while ($products_options_name_values = tep_db_fetch_array($products_options_name)) {
						 
						$selected = 0;
						$products_options_array = array();
						$options_text.='<tr><td class="main">' . $products_options_name_values['products_options_name'] . '<br>' . "\n";
						// BOF: WebMakers.com Added: Attributes Copy and Sort
						  if ( PRODUCTS_OPTIONS_SORT_BY_PRICE !='1' ) {
								$products_options = tep_db_query("select pa.products_options_sort_order, pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . $xsell['products_id'] . "' and pa.options_id = '" . $products_options_name_values['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . $languages_id . "'" . " order by pa.products_options_sort_order, pov.products_options_values_name");
						  } else {
								$products_options = tep_db_query("select pa.products_options_sort_order, pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . $xsell['products_id'] . "' and pa.options_id = '" . $products_options_name_values['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . $languages_id . "'" . " order by pa.products_options_sort_order, pa.options_values_price");
						  }
						  $products_options_array[]=array("id"=>0,"text"=>TEXT_SELECT_ATTRIBUTE);
							  
								// EOF: WebMakers.com Added: Attributes Copy and Sort
							while ($products_options_values = tep_db_fetch_array($products_options)) {
							  $products_options_array[] = array('id' => $products_options_values['products_options_values_id'], 'text' => $products_options_values['products_options_values_name']);
							  if ($products_options_values['options_values_price'] != '0') {
								$products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options_values['price_prefix'] . $currencies->display_price($products_options_values['options_values_price'], tep_get_tax_rate($product_info_values['products_tax_class_id']),'1',true) .') ';
							  }
							}
							$options_text.=tep_draw_pull_down_menu('xsell_forced_attributes[' . $xsell["products_id"] .'][' . $products_options_name_values['products_options_id'] . ']', $products_options_array, $cart->contents[$xsell["products_id"]]['attributes'][$products_options_name_values['products_options_id']]);
							$options_text.='</td></tr>';
							$forced_product_list.='\'[' . (int)$xsell['products_id'] . '][' . $products_options_name_values['products_options_id'] . ']\',';
							//$forced_products_list.=$products_options_name_values['products_options_id'] . ','	;
							//$product_atrribs.=$xsell["products_id"] . ':' . $products_options_name_values['products_options_id'] .','; //FORCED
							
						 $abcnt++; 		
					 }	 //while products_options_name
					// $forced_product_list.=substr($forced_products_list,0,-1) .']';
				  } //if products_options_name
				  
								

			//  $JS_DETAILS['forced']=
			  $options_text.='</table></td></tr>';
			} //if products_attibutes
        $info_box_contents[$row][$col] = array('align' => 'left',
                                               'params' => 'class="smallText" width="33%" valign="top"',
                                               'text' => '<table cellpadding=3>' .  $products_text . $options_text . '</table>');
	    $xsell_forced_ids .=($xsell_forced_ids!=''?',':'');
		$xsell_forced_ids .=$xsell['xsell_id'];
        $col ++;
        if ($col > 2) {
          $col = 0;
          $row ++;
        }
      } //while xsell_query
	  if($forced_product_list!='') $forced_product_list=substr($forced_product_list,0,-1);
	  $GLOBALS["JS_DETAILS"]["FORCED_ITEMS"].=$forced_product_list . ']}'; 
	 //$GLOBALS["JS_DETAILS"]["FORCED_ITEMS"].=substr($product_atrribs,0,-1) . '},id:{' . substr($forced_product_list,0,-1) . '},count:' . ($abcnt-1) .'}'; // FORCED

} // if xsell query
      new contentBox($info_box_contents); 
if (MAIN_TABLE_BORDER == 'yes'){
$info_box_contents = array();
$info_box_contents = array();
  $info_box_contents[] = array('align' => 'left',
                                'text'  => tep_draw_separator('pixel_trans.gif', '100%', '1')
                              );
  new infoboxFooter($info_box_contents, true, true);
}	
} // if
echo "<br>";
?>
<!-- xsell_products_eof forced//-->
<!-- xsell_products //-->
<?php 

$xsell_query = tep_db_query("select distinct p.products_id, p.products_image_1,p.products_title_1, pd.products_name, p.products_tax_class_id, products_price from " . TABLE_PRODUCTS_XSELL . " xp, " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where xp.xsell_type='G' and xp.item_type='P' and xp.products_id = '" . tep_db_input(tep_db_prepare_input($HTTP_GET_VARS['products_id'])) . "' and xp.xsell_id = p.products_id and p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "' and p.products_status = '1' order by xp.products_id asc limit " . MAX_DISPLAY_ALSO_PURCHASED); 
$num_products_xsell = tep_db_num_rows($xsell_query);
if ($num_products_xsell >= MIN_DISPLAY_XSELL) {
      $info_box_contents = array();
      $info_box_contents[] = array('align' => 'left', 'text' => TEXT_XSELL_PRODUCTS);
      new contentBoxHeading($info_box_contents, false, false);

      $row = 0;
      $col = 0;
      $info_box_contents = array();
      while ($xsell = tep_db_fetch_array($xsell_query)) {
        $xsell['specials_new_products_price'] = tep_get_products_special_price($xsell['products_id']); 

if ($xsell['specials_new_products_price']) { 
      $xsell_price =  '<s>' . $currencies->display_price($xsell['products_price'], tep_get_tax_rate($xsell['products_tax_class_id']),1,true) . '</s><br>'; 
      $xsell_price .= '<span class="productSpecialPrice">' . $currencies->display_price($xsell['specials_new_products_price'], tep_get_tax_rate($xsell['products_tax_class_id'])) . '</span>'; 
    } else { 
      $xsell_price =  $currencies->display_price($xsell['products_price'], tep_get_tax_rate($xsell['products_tax_class_id']),1,true); 
    } 	
        $info_box_contents[$row][$col] = array('align' => 'center',
                                               'params' => 'class="smallText" width="33%" valign="top"',
                                               'text' => '<table cellpadding=3><tr><td width=10></td><td><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $xsell['products_id']) . '">' . tep_product_small_image($xsell['products_image_1'], stripslashes($xsell['products_title_1'])) . '</a></td></tr>' .
											   '<tr><td></td><td class=smalltext><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $xsell['products_id']) . '">' . stripslashes($xsell['products_title_1']) .'</a><br><div align=center>' .  $xsell_price . '</div></td></tr></table>');
        $col ++;
        if ($col > 2) {
          $col = 0;
          $row ++;
        }
      } 
      new contentBox($info_box_contents);
if (MAIN_TABLE_BORDER == 'yes'){
$info_box_contents = array();
$info_box_contents = array();
  $info_box_contents[] = array('align' => 'left',
                                'text'  => tep_draw_separator('pixel_trans.gif', '100%', '1')
                              );
  new infoboxFooter($info_box_contents, true, true);
}
}  
?> 
<!-- xsell_products_eof //-->
<?php
  } 
?>