<?php
/*
  $Id$

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

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  class bm_add_bestsellers {
    var $code = 'bm_add_bestsellers';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;
    var $pages;	

    function bm_add_bestsellers() {
      $this->title = MODULE_BOXES_MULTY_BEST_SELLERS_TITLE;
      $this->description = MODULE_BOXES_MULTY_BEST_SELLERS_DESCRIPTION;

      if ( defined('MODULE_BOXES_MULTY_BEST_SELLERS_STATUS') ) {
        $this->sort_order = MODULE_BOXES_MULTY_BEST_SELLERS_SORT_ORDER;
        $this->enabled = (MODULE_BOXES_MULTY_BEST_SELLERS_STATUS == 'True');
        $this->pages = MODULE_BOXES_MULTY_BEST_SELLERS_DISPLAY_PAGES;
				if	(MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Left Column')	{
					$this->group = 'boxes_column_left';
				}
				if	(MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Right Column')	{
					$this->group = 'boxes_column_right';
				}
				if	(MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Under Header Block')	{
					$this->group = 'boxes_under_header';
				}
				if	(MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Above Footer Block')	{
					$this->group = 'boxes_above_footer';
				}	
				if	(MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Content Block Top')	{
					$this->group = 'boxes_content_top';
				}
				if	(MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Content Block Bottom')	{
					$this->group = 'boxes_content_bottom';
				}	
      }
    }

    function execute() {
      global $HTTP_GET_VARS, $current_category_id, $languages_id, $oscTemplate, $currencies;
 
       if (!isset($HTTP_GET_VARS['products_id'])) {
        if (isset($current_category_id) && ($current_category_id > 0)) {
          $best_sellers_query = tep_db_query("select distinct p.products_id, pd.products_description, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_status = '1' and p.products_ordered > 0 and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and '" . (int)$current_category_id . "' in (c.categories_id, c.parent_id) order by p.products_ordered desc, pd.products_name limit " . BESTSELLERS_BOX_MULTY_MAX_DISPLAY);
        } else {
          $best_sellers_query = tep_db_query("select distinct p.products_id, pd.products_description, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_ordered > 0 and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' order by p.products_ordered desc, pd.products_name limit " . BESTSELLERS_BOX_MULTY_MAX_DISPLAY);
        }
			  if (tep_db_num_rows($best_sellers_query) >= BESTSELLERS_BOX_MULTY_MIN_DISPLAY) {
		$text_no_product = false;					
	  $row = 0;
	  $col = 0;
	$image_width = BESTSELLERS_BOX_MULTY_IMAGE_WIDTH;
	$image_height = BESTSELLERS_BOX_MULTY_IMAGE_HEIGHT;
	$equal_height_products_name = 'equal-height_products_name_01';
	$col_items = (BESTSELLERS_BOX_MULTY_PER_ROW);
	$products_block_width = BESTSELLERS_BOX_MULTY_WIDTH.'px';
	
	$row_new_products_name = (BESTSELLERS_BOX_MULTY_SHOW_NAME == 'Show') ? 'row_products_name_01' : '';
		
  	$bestsellers_list .= '<div class="content '.$view.'">';
				while ($best_sellers = tep_db_fetch_array($best_sellers_query)) {
				$bestsellers_list .= (($col === 0 && $row != 0) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '');
				$bestsellers_list .= (($col === 0) ? '<ul class="best_sellers '.$row_new_products_name.'" id="box_01-'.$row.'">' : '<li class="prods_vseparator"><div>'.tep_draw_separator('spacer.gif', '1', '1').'</div>');
				$featured_query = tep_db_query("select status, products_id from " . TABLE_FEATURED . " where products_id = '" . (int)$best_sellers['products_id'] . "'");
				$featured_product = tep_db_fetch_array($featured_query);
				$product_query = tep_db_query("select products_description, products_id from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$best_sellers['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
				$product = tep_db_fetch_array($product_query);
				$bestsellers_list .= '';
				$p_id = $product['products_id'];		
/* ********************************* */	
				$p_name =	((BESTSELLERS_BOX_MULTY_SHOW_NAME == 'Show') ? '<h3 class="product_name_wrapper name '.$equal_height_products_name.'"><span><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id='.$p_id) . '">' . $best_sellers['products_name'] . '</a></span></h3>' : '');
				if	(BESTSELLERS_BOX_MULTY_SHOW_PRICE == 'Show')	{
				  if (tep_not_null($best_sellers['specials_new_products_price'])) {
						$p_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><del>' . $currencies->display_price($best_sellers['products_price'], tep_get_tax_rate($best_sellers['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($best_sellers['specials_new_products_price'], tep_get_tax_rate($best_sellers['products_tax_class_id'])) . '</span></div>';
						$extra = 'extra';
						$persent = ($currencies->display_sale_percent($best_sellers['products_price'], tep_get_tax_rate($best_sellers['products_tax_class_id']), $best_sellers['specials_new_products_price']) * -1);
						$sale_percent =	((BESTSELLERS_BOX_MULTY_SHOW_SPECIALS_LABEL == 'Show') ? '<div class="sale_percent '.$color.'"><strong>'.$persent.'%</strong></div>' : '');
				  } else {
						$p_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><span class="productSpecialPrice">'.$currencies->display_price($best_sellers['products_price'], tep_get_tax_rate($best_sellers['products_tax_class_id'])).'</span></div>';
						$extra = '';
						$sale_percent	= '';
				  }
				}else{
					$p_price = '';	
				} $text_no_product = true;
				$p_pic =	((BESTSELLERS_BOX_MULTY_SHOW_PIC == 'Show') ? '<div class="product_pic_wrapper" style="width:'.$image_width.'px;"><a class="product_img" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id='.$p_id) . '" style="width:'.$image_width.'px;">' . tep_image(DIR_WS_IMAGES . $best_sellers['products_image'], $best_sellers['products_name'], $image_width, $image_height, ' style="width:'.$image_width.'px;height:'.$image_height.'px;"').'</a>'.$sale_percent.'</div>' : '');
				$p_desc =	((BESTSELLERS_BOX_MULTY_MAX_DESCR != 0) ? '<div class="product_desc_wrapper desc">'.mb_substr(strip_tags($product['products_description']), 0, BESTSELLERS_BOX_MULTY_MAX_DESCR, 'UTF-8').'...</div>' : '');
				$p_featured = (($featured_product['status']) ? '<div class="featured_label '.$color.'"><strong></strong></div>' : '');
				$p_details_text = '' .tep_draw_button2_top() . '<a href="' . tep_href_link('product_info.php?products_id='.$p_id) . '"><span class="ui-button-text">'.  IMAGE_BUTTON_DETAILS .'</span></a>' . tep_draw_button2_bottom().''. "\n";
				$p_buy_now_text = '' .tep_draw_button1_top() . '<a href="'.tep_href_link("products_new.php","action=buy_now&products_id=".$p_id).'"><span class="ui-button-text">'.  IMAGE_BUTTON_IN_CART .'</span></a>' . tep_draw_button1_bottom().'';
				$button_block = ((BESTSELLERS_BOX_MULTY_SHOW_BUTTONS == 'Show') ?  '<div class="product_button_wrapper">'.$p_buy_now_text.' '.$p_details_text.'</div>'  : '');
/* ********************************* */	
if (BESTSELLERS_BOX_MULTY_SHOW_PIC == 'Show') {
			$wrapper_row_00_top = '<div class="row_00">';
			$wrapper_row_00_bottom = '</div>';
		}else {
			$wrapper_row_00_top = '';
			$wrapper_row_00_bottom = '';
		}         				
if (BESTSELLERS_BOX_MULTY_SHOW_PRICE == 'Show' || BESTSELLERS_BOX_MULTY_SHOW_BUTTONS == 'Show') {
			$wrapper_row_01_top = '<div class="row_01">';
			$wrapper_row_01_bottom = '</div>';
		}else {
			$wrapper_row_01_top_ = '';
			$wrapper_row_01_bottom = '';
		}         				
if ($p_desc !='') {
			$wrapper_row_02_top = '<div class="row_02">';
			$wrapper_row_02_bottom = '</div>';
		}else {
			$wrapper_row_02_top = '';
			$wrapper_row_02_bottom = '';
		}
   $bestsellers_list .= ''. "\n".
						'<li class="responsive_block" style="width:'.$products_block_width.'">'. "\n".
						'		<div class="product_block '.$equal_height_products_block.'"><div>'. "\n".
						'				'.$p_featured.''. "\n". 
						'				'.$p_name.''. "\n".
						'				'.$wrapper_row_00_top.''."\n".			  
						'					'.$p_pic.''. "\n". 
						'				'.$wrapper_row_00_bottom.''."\n".			  
						'				'. "\n".	
						'				'.$wrapper_top.''."\n".
						'   				'.$p_data. ''. "\n".	
						'				'.$wrapper_row_01_top.''."\n".			  
						'						'.$p_manufacturers.''. "\n".	
						'						'.$p_price.''. "\n".
						'						'.$button_block.''. "\n".	
						'				'.$wrapper_row_01_bottom.''."\n".			  
						'				'.$wrapper_row_02_top.''."\n".			  
						'						'.$p_desc.''. "\n".	
						'						'.$wrapper_row_02_bottom.''."\n".			  
						'		'.$wrapper_bottom.''."\n".			  
						'		</div></div>'. "\n";	
		$col ++;		  
    if ($col > $col_items-1) {
       $bestsellers_list .= '</li></ul>';
	   	 $row ++;
       $col = 0;
    }else{
	   $bestsellers_list .= '</li>';	
  	}
	}
  $bestsellers_list .= '</div>';
	if	((MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Tab section 1') || (MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Tab section 2') || (MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Tab section 3') || (MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT == 'Tab section 4')) {
		$title = '' . "\n";
	}else	{
		$title = '  		<div class="ui-widget-header infoBoxHeading"><a href="' . tep_href_link(FILENAME_TOPSELLERS_PRODUCTS) . '">' . MODULE_BOXES_MULTY_BEST_SELLERS_BOX_TITLE . '</a></div>'. "\n";
	}
	$data = 
			'<div class="ui-widget infoBoxContainer additional_box">' . "\n" .
			'  		'.$title. "\n".			
			' 		<div class="infoBoxContents">' . "\n" .
			'					' . ($text_no_product ? $bestsellers_list : '<div class="contentBlock no_product"><p>'.MODULE_BOXES_MULTY_BEST_SELLERS_NO_PRODUCT.'</p></div>'). "\n" .
			'					' .tep_draw_button3_top() . '<a href="'.tep_href_link(FILENAME_TOPSELLERS_PRODUCTS).'" role="button"><span class="ui-button-text">'.  MODULE_BOXES_MULTY_BEST_SELLERS_BOX_SEE_ALL .'</span></a>' .tep_draw_button3_bottom()  . "\n".
			' 		</div>'.
			'</div>
<script type="text/javascript">'."\n".
'$(document).ready(function(){' ."\n".				
'				var row_list_products_name_01 = $(\'.row_products_name_01\');'."\n".
'						row_list_products_name_01.each(function(){'."\n".
'						new equalHeights_products_name_01($(\'#\' + $(this).attr("id")));'."\n".
'				});	'."\n".	
'})'."\n".
'</script>' . "\n";

		$oscTemplate->addBlock($data, $this->group);
        }
    }
}
    function isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined('MODULE_BOXES_MULTY_BEST_SELLERS_STATUS');
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Best Sellers Module', 'MODULE_BOXES_MULTY_BEST_SELLERS_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_MULTY_BEST_SELLERS_CONTENT_PLACEMENT', 'Tab section 1', 'Should the module be loaded in the Left Column, Right Column, Content Block Top, Content Block Bottom, Under Header Block, Above Footer Block or Tab section 1?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\',\'Content Block Top\', \'Content Block Bottom\', \'Under Header Block\', \'Above Footer Block\', \'Tab section 1\'), ', now(e configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_BOXES_MULTY_FEATURED_STATUS', 'MODULE_BOXES_MULTY_FEATURED_CONTENT_PLACEMENT', 'MODULE_BOXES_MULTY_FEATURED_SORT_ORDER','MODULE_BOXES_MULTY_FEATURED_DISPLAY_PAGES');
    }
  }
?>
