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

Copyright (c) 2003 osCommerce 

osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License
*/

class template {
var $products_template_array;

	function replace_template(){
		$fp=@fopen(DIR_WS_TEMPLATES . TEMPLATE_NAME .'/content/products_info.tpl.php',"r");
		if (!$fp) return;
		$content=fread($fp,filesize(DIR_WS_TEMPLATES . TEMPLATE_NAME .'/content/products_info.tpl.php'));
		if ($content=="") return false;
		while(list($key,$value)=each($this->assign_vars)){
			$content=eregi_replace("{" . $key . "}",$value . "",$content);
		}
	return $content;
	}
	
	function load_file($filename){
		$filename=DIR_WS_TEMPLATES .  "/content/" . $filename;
		$fp=@fopen($filename,"r");
		if (!$fp) return false;
		$this->files=$filename;
		return true;
	}
	
	
	function set_value($url='',$date_available='',$date_added='',$products_id=''){
		$string="";
			$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$products_id . "'");
			$reviews = tep_db_fetch_array($reviews_query);
		if ($reviews['count'] > 0) {
			$string.=" <tr>
					<td class='main'>". TEXT_CURRENT_REVIEWS . ' ' . $reviews['count'] ."</td>
					</tr>
					<tr>
					<td>". tep_draw_separator('pixel_trans.gif', '100%', '10')."</td>
					</tr>";
		 }
		if (tep_not_null($url)) {
			 $string.=" <tr>
					 <td class='main'>". sprintf(TEXT_MORE_INFORMATION, tep_href_link(FILENAME_REDIRECT, 'action=url&goto=' . urlencode($url), 'NONSSL', true, false)). "</td>
					 </tr>
					 <tr>
					 <td>" .tep_draw_separator('pixel_trans.gif', '100%', '10'). "</td>
					 </tr>";
		}
		if ($date_available > date('Y-m-d H:i:s')) {
			 $string.=" <tr>
					  <td  colspan='2' align='center' class='smallText'>". sprintf(TEXT_DATE_AVAILABLE, tep_date_long($date_available))."</td>
					  </tr>";
		} else {
			 $string.=" <tr>
						<td  colspan='2' align='center' class='smallText'>".sprintf(TEXT_DATE_ADDED, tep_date_long($date_added))."</td>
						</tr>";
		}
		return $string;
	}
	
	function set_image($products_array=array()){
		$product_info=$products_array;
		$images=array();
		$img_string="";
		$title_string="";
		for ($icnt=1;$icnt<=5;$icnt++){
			if ($product_info['products_image_' . $icnt]!=""){
				$images[]=array("image"=>$product_info["products_image_" .$icnt],'title'=>$product_info["products_title_" . $icnt]);
				if (file_exists(DIR_FS_CATALOG . DIR_WS_IMAGES . "big/" .$product_info['products_image_' . $icnt])){
					$img_string.="'" . DIR_WS_IMAGES . "big/" . $product_info["products_image_" . $icnt] . "',";
				} else {
					$img_string.="'" . DIR_WS_IMAGES . $product_info["products_image_" . $icnt] . "',";
				}
				$title_string.="'" . addslashes($product_info["products_title_" . $icnt]) . "',";
			}
		}
		$img_string=substr($img_string,0,-1);
		$title_string=substr($title_string,0,-1);
		if ($img_string!=""){
			$string.='<script language="javascript">load_images(' . $img_string  .');</script>';
		}
		if ($title_string!=""){
			$string.='<script language="javascript">load_titles(' . $title_string  .');</script>';
		}
		 if (count($images)>0) { 
			$string.='<td valign="top">
						<table border="0" width="0" cellpadding="2" cellspacing="0" align="center">
							<tr height="280">
								<td valign="top" align="center" class="main">';
									if (file_exists(DIR_FS_CATALOG . DIR_WS_IMAGES . "big/" .$images[0]["image"])){
										$string.= '<image src="' . DIR_WS_IMAGES . "big/" . $images[0]["image"] . '" id="imgContainer">';
									} else {
										$string.= '<image src="' . DIR_WS_IMAGES . $images[0]["image"] . '" id="imgContainer">';
									}	
									$string.='<br /><br />
									<b><span id="titleContainer">'. stripslashes(tep_output_string($product_info['products_title_1'])).'</span></b>
								</td>
							</tr>
							<tr>
								<td valign="top" align="center" class="main">
								</td>
							</tr>';
			if (count($images)>1) {
				$string.='<tr>
							<td valign="top">
								<table border="0" width="0" cellpadding="0" cellspacing="2"  bgcolor="#FFFFFF">
									<tr>';
										  for ($icnt=0;$icnt<count($images);$icnt++) {$image_width +=SMALL_IMAGE_WIDTH;  if($image_width>300){ $string.= '</tr><tr>';$image_width=0;}
											$string.='<td class="smallText" align="center" valign="bottom" style="border:1px solid #E5E4E4">
														<table border="0" width="0" cellpadding="0" cellspacing="2">
														<tr height="100">
														<td valign="middle" align="center">'.
														tep_product_small_image($images[$icnt]["image"],$images[$icnt]["title"],'onClick="javascript:set_image(' . $icnt .');" style="cursor:pointer;cursor:hand;"').
														'</td>
														 </tr>
														 <tr>
														 <td class="smallText" align="center">'.
														 stripslashes(tep_output_string($images[$icnt]["title"])).
														'</td>
													</tr>
												</table>
											</td>';
										 } 
							$string.='</tr>
						</table>
					</td>
				</tr>';
				 } // images>1
			$string.='</table>
					</td>';
			}
		return $string;
	}
	
	function set_attributes($products_id='',$is_attributes=''){
		global $current_category_id,$languages_id,$currencies;
		$string="";
		$products_attributes_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$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);
		if($is_attributes=='Y'){
			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)$products_id . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "'" . " order by popt.products_options_sort_order");
			  $string.='<table width="100%" border="0" cellpading="0" cellspacing"0">';
			  $string.= '<tr height="25"><td class="availability" colspan="2" width="100%"><b>&nbsp;' . TEXT_PRODUCT_OPTIONS . '</b></td></tr><tr height="10"><td colspan="2"></td></tr>';
			  while ($products_options_name_values = tep_db_fetch_array($products_options_name)) {
					$selected = 0;
					$products_options_array = array();
					$products_options_array[] = array('id'=>0,'text'=>TEXT_SELECT_ATTRIBUTE);
					//$attributes_array[]=array("id"=>$products_options_name_values['products_options_id']);
					$string.= '<tr height="10"><td class="main">' . $products_options_name_values['products_options_name'] . '</td><td>' . "\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, pa.options_id, 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 = '" . $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, pa.options_id, pa.options_values_id, 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 = '" . $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");
				  }
		// 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) .') ';
					  }
				  }
				 
				$string.= tep_draw_pull_down_menu('id[' . $products_options_name_values['products_options_id'] . ']', $products_options_array, $cart->contents[$products_id]['attributes'][$products_options_name_values['products_options_id']],' id="id[' . $products_options_name_values['products_options_id'] . ']' . '" onchange="javascript:out_stock();"');
				$string.= '</td></tr>';		
			  }//while
			}
		}
		$string.= '<table width="100%" align="left" border="0" cellpadding="0" cellspacing="0">';
		$string.= '<tr height="30"><td colspan="2" valign="top"></td></tr>';
		$selected_products_name = tep_db_query("select pd.products_id,pd.products_name,p.products_price from " . TABLE_PRODUCTS . " p," . TABLE_PRODUCTS_DESCRIPTION . " pd," . TABLE_PRODUCTS_TO_CATEGORIES . " pc where pc.categories_id=" . $current_category_id . " and pc.products_id=p.products_id and pd.products_id=p.products_id and pd.language_id=" . $languages_id . " order by rand() limit 0,3");
		$check_selected_products_name = $selected_products_name;
		$check_count = tep_db_num_rows($check_selected_products_name);
		if($check_count>1){
			$string.='<tr height="25"><td class="availability" colspan="2"><b>&nbsp;' . TEXT_BUY . '</b></td></tr><tr height="10"><td colspan="2"></td></tr>';
			$count=0;
			while($fetch_row = tep_db_fetch_array($selected_products_name)){	  		
				if(($fetch_row['products_id']!=(int)$products_id) && ($count < 2)){
					$string.='<tr height="30" valign="middle"><td class="main"><a class="brown" href="' . tep_href_link(FILENAME_PRODUCT_INFO,'cPath='.$cPath.'&products_id='.$fetch_row['products_id']) . '">' . $fetch_row['products_name'] . '</a></td><td class="main" width="30%" align="right">' . $currencies->format($fetch_row['products_price']) . '</td></tr>';
					$count++;
				} 
			}
	   }
	   $string.='</table>';
	   return $string;
	}
}
?>
