<?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_featured {
    var $code = 'bm_add_featured';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;
    var $pages;	

    function bm_add_featured() {
      $this->title = MODULE_BOXES_MULTY_FEATURED_TITLE;
      $this->description = MODULE_BOXES_MULTY_FEATURED_DESCRIPTION;

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

    function execute() {
      global $HTTP_GET_VARS, $languages_id, $currencies, $oscTemplate;

      if (!isset($HTTP_GET_VARS['products_id'])) {
	$featured_products_array = array();
	$featured_products_query_raw = "select p.products_id, pd.products_name, p.products_image, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, p.products_date_added, m.manufacturers_name
   from " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id
   left join " . TABLE_PRODUCTS_DESCRIPTION . " pd on p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "'
   left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id
   left join " . TABLE_FEATURED . " f on p.products_id = f.products_id
   where p.products_status = '1' and f.status = '1' order by f.featured_last_modified DESC, f.featured_date_added DESC, f.date_status_change DESC";
		$title_on = 0;
	$row = 0;
	$col = 0;
	$image_width = FEATURED_BOX_MULTY_IMAGE_WIDTH;
	$image_height = FEATURED_BOX_MULTY_IMAGE_HEIGHT;
	$equal_height_products_name = 'equal-height_products_name_04';		
	$col_items = (FEATURED_BOX_MULTY_PER_ROW);
	$products_block_width = FEATURED_BOX_MULTY_WIDTH.'px';
	
	$row_new_products_name = (FEATURED_BOX_MULTY_SHOW_NAME == 'Show') ? 'row_products_name_04' : '';
		
  	$featured_list .= '<div class="content '.$view.'">';
		$featured_products_split = new splitPageResults($featured_products_query_raw, FEATURED_BOX_MULTY_MAX_DISPLAY);
		$featured_products_query = tep_db_query($featured_products_split->sql_query);
			while ($featured = tep_db_fetch_array($featured_products_query)) {
				$title_on = 1;	
					$featured_list .= (($col === 0 && $row != 0) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '');
					$featured_list .= (($col === 0) ? '<ul class="featured '.$row_new_products_name.'" id="box_04-'.$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)$featured['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)$featured['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
    $product = tep_db_fetch_array($product_query);
				$featured_list .= '';
				$p_id = $product['products_id'];		
/* ********************************* */	
				$p_name =	((FEATURED_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) . '">' . $featured['products_name'] . '</a></span></h3>' : '');
				if	(FEATURED_BOX_MULTY_SHOW_PRICE == 'Show')	{
				  if (tep_not_null($featured['specials_new_products_price'])) {
						$p_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><del>' . $currencies->display_price($featured['products_price'], tep_get_tax_rate($featured['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($featured['specials_new_products_price'], tep_get_tax_rate($featured['products_tax_class_id'])) . '</span></div>';
						$extra = 'extra';
						$persent = ($currencies->display_sale_percent($featured['products_price'], tep_get_tax_rate($featured['products_tax_class_id']), $featured['specials_new_products_price']) * -1);
						$sale_percent =	((FEATURED_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($featured['products_price'], tep_get_tax_rate($featured['products_tax_class_id'])).'</span></div>';
						$extra = '';
						$sale_percent	= '';
				  }
				}else{
					$p_price = '';	
				}
				$p_pic =	((FEATURED_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 . $featured['products_image'], $featured['products_name'], $image_width, $image_height, ' style="width:'.$image_width.'px;height:'.$image_height.'px;"').'</a>'.$sale_percent.'</div>' : '');				
				$p_desc =	((FEATURED_BOX_MULTY_MAX_DESCR != 0) ? '<div class="product_desc_wrapper desc">'.mb_substr(strip_tags($product['products_description']), 0, FEATURED_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 = ((FEATURED_BOX_MULTY_SHOW_BUTTONS == 'Show') ?  '<div class="product_button_wrapper">'.$p_buy_now_text.' '.$p_details_text.'</div>'  : '');
/* ********************************* */	
if (FEATURED_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 (FEATURED_BOX_MULTY_SHOW_PRICE == 'Show' || FEATURED_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 = '';
		}
   $featured_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) {
       $featured_list .= '</li></ul>';
	   	 $row ++;
       $col = 0;
    }else{
	   $featured_list .= '</li>';	
  }
}
  $featured_list .= '</div>';
	if	((MODULE_BOXES_MULTY_FEATURED_CONTENT_PLACEMENT == 'Tab section 1') || (MODULE_BOXES_MULTY_FEATURED_CONTENT_PLACEMENT == 'Tab section 2') || (MODULE_BOXES_MULTY_FEATURED_CONTENT_PLACEMENT == 'Tab section 3') || (MODULE_BOXES_MULTY_FEATURED_CONTENT_PLACEMENT == 'Tab section 4'))	{
		$title = '' . "\n";
	}else	{
		$title = '  		<div class="ui-widget-header infoBoxHeading"><a href="' . tep_href_link(FILENAME_FEATURED_PRODUCTS) . '">' . MODULE_BOXES_MULTY_FEATURED_BOX_TITLE . '</a></div>'. "\n";
	}	
	$data = '' . "\n" .
			'<div class="ui-widget infoBoxContainer additional_box">' . "\n" .
			'  		'.$title. "\n".
			' 		<div class="infoBoxContents">' . "\n" .
'					' . ($title_on ? $featured_list : '<div class="contentBlock">'.MODULE_BOXES_MULTY_FEATURED_BOX_NO_PRODUCT.'</div><br />'). "\n" .
'					' .tep_draw_button3_top() . '<a href="'.tep_href_link(FILENAME_FEATURED_PRODUCTS).'" role="button"><span class="ui-button-text">'.  MODULE_BOXES_MULTY_FEATURED_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_04 = $(\'.row_products_name_04\');'."\n".
'						row_list_products_name_04.each(function(){'."\n".
'						new equalHeights_products_name_04($(\'#\' + $(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_FEATURED_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 What\'s New Module', 'MODULE_BOXES_MULTY_FEATURED_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_FEATURED_CONTENT_PLACEMENT', 'Right Column', 'Should the module be loaded in the Left Column, Right Column, Content Block Top, Content Block Bottom, Under Header Block, Above Footer Block, Tab section 4?', '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 4\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_MULTY_FEATURED_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', 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 ('Display in pages.', 'MODULE_BOXES_MULTY_FEATURED_DISPLAY_PAGES', 'all', 'select pages where this box should be displayed. ', '6', '0','tep_cfg_select_pages(' , now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where 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');
    }
  }
?>
