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

    function bm_add_reviews() {
      $this->title = MODULE_BOXES_MULTY_REVIEWS_TITLE;
      $this->description = MODULE_BOXES_MULTY_REVIEWS_DESCRIPTION;

      if ( defined('MODULE_BOXES_MULTY_REVIEWS_STATUS') ) {
        $this->sort_order = MODULE_BOXES_MULTY_REVIEWS_SORT_ORDER;
        $this->enabled = (MODULE_BOXES_MULTY_REVIEWS_STATUS == 'True');
        $this->pages = MODULE_BOXES_MULTY_REVIEWS_DISPLAY_PAGES;
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Left Column')	{
					$this->group = 'boxes_column_left';
				}
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Right Column')	{
					$this->group = 'boxes_column_right';
				}
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Under Header Block')	{
					$this->group = 'boxes_under_header';
				}
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Above Footer Block')	{
					$this->group = 'boxes_above_footer';
				}	
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Content Block Top')	{
					$this->group = 'boxes_content_top';
				}
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Content Block Bottom')	{
					$this->group = 'boxes_content_bottom';
				}
				if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT == 'Tab Product Info')	{
					$this->group = 'box_toogle_pos_22';
				}	
					
      }
    }

    function execute() {
      global $languages_id, $HTTP_GET_VARS, $currencies, $oscTemplate;
      if (isset($HTTP_GET_VARS['products_id'])) {
        $random_select_id = " and p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'";
      }
		$random_select_rid = " ORDER BY RAND() limit " . (REVIEWS_BOX_MULTY_MAX_DISPLAY);
		$random_select = tep_db_query("select r.reviews_id, r.reviews_rating, r.date_added, p.products_id, p.products_image, p.products_price, p.products_tax_class_id, pd.products_name, s.specials_new_products_price, r.customers_name from " . TABLE_REVIEWS . " r, " . TABLE_REVIEWS_DESCRIPTION . " rd, " . 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_id = r.products_id and r.reviews_id = rd.reviews_id and rd.languages_id = '" . (int)$languages_id . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and r.reviews_status = 1". $random_select_id . $random_select_pid . $random_select_rid);
      
		$random_product = tep_db_fetch_array($random_select);
		$text_no_product = false;
		$row = 0;
		$col = 0;
		$image_width = REVIEWS_BOX_MULTY_IMAGE_WIDTH;
		$image_height = REVIEWS_BOX_MULTY_IMAGE_HEIGHT;
		$equal_height_products_name = 'equal-height_products_name_03';
		$col_items = (REVIEWS_BOX_MULTY_PER_ROW);
		$products_block_width = REVIEWS_BOX_MULTY_WIDTH.'px';
		
		$row_new_products_name = (REVIEWS_BOX_MULTY_SHOW_NAME == 'Show') ? 'row_products_name_03' : '';
		
      	$reviews_box_contents = '';
			if	($random_product)	{
				
				$reviews_box_contents .= '<div class="content '.$view.'">';	
			while ($random_product = tep_db_fetch_array($random_select)) {
  				
				$reviews_box_contents .= (($col === 0 && $row != 0) ? '<ul class="row_separator"><li class="prods_hseparator">'.tep_draw_separator('spacer.gif', '1', '1').'</li></ul>' : '');
				$reviews_box_contents .= (($col === 0) ? '<ul class="box_reviwes '.$row_new_products_name.'" id="box_03-'.$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)$random_product['products_id'] . "'");
				$featured_product = tep_db_fetch_array($featured_query);
				
				$product_query = tep_db_query("select reviews_text from " . TABLE_REVIEWS_DESCRIPTION . " where reviews_id = '" . (int)$random_product['reviews_id'] . "' and languages_id = '" . (int)$languages_id . "'");
				$product = tep_db_fetch_array($product_query);
				
				$reviews_box_contents .= '';
				$p_id = $random_product['products_id'];		
/* ********************************* */
				$p_name =	((REVIEWS_BOX_MULTY_SHOW_NAME == 'Show') ? '<h3 class="product_name_wrapper name '.$equal_height_products_name.'"><span><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id='.$p_id) . '">' . $random_product['products_name'] . '</a></span></h3>' : '');
				
				  if (tep_not_null($random_product['specials_new_products_price'])) {
					if	(REVIEWS_BOX_MULTY_SHOW_PRICE == 'Show')	{
						$p_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><del>' . $currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])) . '</del> <span class="productSpecialPrice">' . $currencies->display_price($random_product['specials_new_products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])) . '</span></div>';
					}else{
					$p_price = '';	
					}
					$extra = 'extra';
					$persent = ($currencies->display_sale_percent($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id']), $random_product['specials_new_products_price']) * -1);
					$sale_percent =	((REVIEWS_BOX_MULTY_SHOW_SPECIALS_LABEL == 'Show') ? '<div class="sale_percent '.$color.'"><strong>'.$persent.'%</strong></div>' : '');
				 } else {
					if	(REVIEWS_BOX_MULTY_SHOW_PRICE == 'Show')	{
					$p_price = '<div class="product_price_wrapper price '.$extra.'"><b>'.PRICE. '</b><span class="productSpecialPrice">'.$currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])).'</span></div>';
					}else{
					$p_price = '';	
					}
					$extra = '';
					$sale_percent	= '';
				  }
 $text_no_product = true;
				$p_pic =	((REVIEWS_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_REVIEWS_INFO, 'products_id='.$p_id) . '" style="width:'.$image_width.'px;">' . tep_image(DIR_WS_IMAGES . $random_product['products_image'], $random_product['products_name'], $image_width, $image_height, ' style="width:'.$image_width.'px;height:'.$image_height.'px;"').'</a>'.$sale_percent.'</div>' : '');
				$p_desc =	((REVIEWS_BOX_MULTY_MAX_DESCR != 0) ? '<div class="product_desc_wrapper desc"><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id='.$p_id) . '">'.mb_substr(strip_tags($product['reviews_text']), 0, REVIEWS_BOX_MULTY_MAX_DESCR , 'UTF-8').'</a>...</div>' : '');
				$p_featured =	((REVIEWS_BOX_MULTY_SHOW_FEATURED_LABEL == 'Show' && ($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 = ((REVIEWS_BOX_MULTY_SHOW_BUTTONS == 'Show') ?  '<div class="product_button_wrapper">'.$p_buy_now_text.' '.$p_details_text.'</div>'  : '');
				
				$button_block = ((REVIEWS_BOX_MULTY_SHOW_BUTTONS == 'Show') ?  '<div class="stars">' . tep_image(DIR_WS_IMAGES . 'icons/stars_' . $random_product['reviews_rating'] . '.png' , sprintf(MODULE_BOXES_REVIEWS_BOX_TEXT_OF_5_STARS, $random_product['reviews_rating'])) . '</div>'  : '');
				
				$p_data_add = '<div class="data"><b>' . sprintf(TEXT_REVIEW_BY, tep_output_string_protected($random_product['customers_name'])) . '</b><br />' . sprintf(TEXT_REVIEW_DATE_ADDED, tep_date_long($random_product['date_added'])).'</div>';
				
/* ********************************* */	
if (REVIEWS_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 (REVIEWS_BOX_MULTY_SHOW_PRICE == 'Show' || REVIEWS_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 = '';
		}
   $reviews_box_contents .= ''. "\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_data_add.''. "\n".
					'						'.$p_desc.''. "\n".									  
	//				'						'.$p_manufacturers.''. "\n".	
					'						'.$p_price.''. "\n".
					'						'.$button_block.''. "\n".	
	//			  '				'.$wrapper_row_01_bottom.''."\n".			  
		//		  '				'.$wrapper_row_02_top.''."\n".			  
		//		  '				'.$wrapper_row_02_bottom.''."\n".			  
				  '		'.$wrapper_bottom.''."\n".			  
				  '		</div></div>'. "\n";	
		$col ++;		  
    if ($col > $col_items-1) {
       $reviews_box_contents .= '</li></ul>';
	   	 $row ++;
       $col = 0;
    }else{
	   $reviews_box_contents .= '</li>';	
  	}
	}
	$reviews_box_contents .= '</div><br />' .tep_draw_button3_top() . '<a href="'.tep_href_link(FILENAME_REVIEWS).'" role="button"><span class="ui-button-text">'.  MODULE_BOXES_MULTY_REVIEWS_BOX_SEE_ALL .'</span></a>' .tep_draw_button3_bottom();
    $title = ''. "\n";
		if	(MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT != 'Tab Product Info')	{
			$title = '  		<div class="ui-widget-header infoBoxHeading"><a href="' . tep_href_link(FILENAME_REVIEWS_PRODUCTS) . '">' . MODULE_BOXES_MULTY_REVIEWS_BOX_TITLE . '</a></div>'. "\n";
		}
	} elseif (isset($HTTP_GET_VARS['products_id'])) {
// display 'write a review' box
        $reviews_box_contents .= '<div><table class="ui-widget-content infoBoxContents"><tr><td><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, 'products_id=' . $HTTP_GET_VARS['products_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icons/box_write_review.png', IMAGE_BUTTON_WRITE_REVIEW) . '</a></td><td><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, 'products_id=' . $HTTP_GET_VARS['products_id']) . '">' . MODULE_BOX_MULTY_REVIEWS_BOX_WRITE_REVIEW .'</a></td></tr></table></div>';
		$title = ''. "\n";
      } else {
// display 'no reviews' box
        $reviews_box_contents .= '<div class="ui-widget-content infoBoxContents">' . MODULE_BOXES_MULTY_REVIEWS_BOX_NO_REVIEWS . '</div>';
		$title = ''. "\n";
      }

/*      $data = '<div class="ui-widget infoBoxContainer">' .
              '  <div class="ui-widget-header infoBoxHeading"><a href="' . tep_href_link(FILENAME_REVIEWS) . '">' . MODULE_BOX_MULTY_REVIEWS_BOX_TITLE . '</a></div>' .
              '  ' . $reviews_box_contents .
              '</div>';

      $oscTemplate->addBlock($data, $this->group);
    }
*/
	
	$data = '' . "\n" .
			'<div class="ui-widget infoBoxContainer additional_box box_reviwes">' . "\n" .
			'  		'.$title. "\n".			
			' 		<div class="infoBoxContents">' . "\n" .
			'					' . ($text_no_product ? $reviews_box_contents : '<div class="contentBlock no_product">'.MODULE_BOXES_MULTY_REVIEWS_BOX_NO_REVIEWS.'</div>'). "\n" .
			'					'. "\n".
			' 		</div>'.
			'</div>';
 echo ((REVIEWS_BOX_MULTY_SHOW_NAME == 'Show') ? ''."\n".
'<script type="text/javascript">'."\n".
'$(document).ready(function(){' ."\n".				
'				var row_list_products_name_03 = $(\'.row_products_name_03\');'."\n".
'						row_list_products_name_03.each(function(){'."\n".
'						new equalHeights_products_name_03($(\'#\' + $(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_REVIEWS_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 Reviews Module', 'MODULE_BOXES_MULTY_REVIEWS_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_REVIEWS_CONTENT_PLACEMENT', 'Tab Product Info', '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 Product Info?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\',\'Content Block Top\', \'Content Block Bottom\', \'Under Header Block\', \'Above Footer Block\', \'Tab Product Info\'), ', 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_REVIEWS_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_REVIEWS_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_REVIEWS_STATUS', 'MODULE_BOXES_MULTY_REVIEWS_CONTENT_PLACEMENT', 'MODULE_BOXES_MULTY_REVIEWS_SORT_ORDER', 'MODULE_BOXES_MULTY_REVIEWS_DISPLAY_PAGES');
    }
  }
?>
