<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); 
/**
* @version $Id: shop.browse.php,v 1.24 2005/01/27 19:34:03 soeren_nb Exp $
* @package mambo-phpShop
* @subpackage HTML
* Contains code from PHPShop(tm):
* 	@copyright (C) 2000 - 2004 Edikon Corporation (www.edikon.com)
*	Community: www.phpshop.org, forums.phpshop.org
* Conversion to Mambo and the rest:
* 	@copyright (C) 2004-2005 Soeren Eberhardt
*
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* mambo-phpShop is Free Software.
* mambo-phpShop comes with absolute no warranty.
*
* www.mambo-phpshop.net
*
* This is the Main Product Listing File!
*/
mm_showMyFileName( __FILE__ );

echo "<h3>".$PHPSHOP_LANG->_PHPSHOP_BROWSE_LBL."</h3>";

/* load important class files */
require_once (CLASSPATH."ps_product.php");
$ps_product = new ps_product;

require_once (CLASSPATH."ps_product_category.php");
$ps_product_category = new ps_product_category;

require_once (CLASSPATH."ps_reviews.php");

/* get all the paramters */
$offset = mosGetParam( $_REQUEST, 'limitstart', 0 );
$category_id = mosGetParam( $_REQUEST, 'category_id', null );
$manufacturer_id = mosGetParam( $_REQUEST, 'manufacturer_id', null );
$keyword = urldecode(mosGetParam( $_REQUEST, 'keyword', null ));
$keyword1 = urldecode(mosGetParam( $_REQUEST, 'keyword1', null ));
$keyword2 = urldecode(mosGetParam( $_REQUEST, 'keyword2', null ));
$search_category= mosGetParam( $_REQUEST, 'search_category', null );
$search_limiter= mosGetParam( $_REQUEST, 'search_limiter', null );
$search_op= mosGetParam( $_REQUEST, 'search_op', null );
$orderby = mosGetParam( $_REQUEST, 'orderby', 'pshop_product.product_name' );
$Itemid = mosgetparam($_REQUEST, "Itemid", null);
if (empty($orderby)) $orderby = "#__pshop_product.product_name";
if (empty($category_id)) $category_id = $search_category;

// Check to see if this is a search or a browse by category
// Default is to show all products
if (!empty($category_id) && @$_REQUEST['output'] != "pdf") { ?>
   
   <table width="100%" align="center" cellspacing="0" cellpadding="0" border="0">
       <tr valign="top">
          <td align="left"><strong><?php
              $ps_product_category->navigation_list($category_id);
              ?></strong><br />
              <?php
              $ps_product_category->print_child_list($category_id);
              ?><br />
          </td>
       </tr>   
       <tr><td>&nbsp;</td></tr>
       <tr>
          <td><?php echo $ps_product_category->get_description($category_id); ?>
          <br /><br />
          </td>
       </tr>
  </table><?php
}
if (empty($manufacturer_id)) {

 /*** GET ALL PUBLISHED PRODUCTS ***/
 $list  = "SELECT DISTINCT product_name,products_per_row,category_browsepage,#__pshop_product.product_id,#__pshop_category.category_id, ";
 $list .= "product_thumb_image,product_s_desc ";
 $list .= "FROM #__pshop_product, #__pshop_category, #__pshop_product_category_xref,#__pshop_shopper_group ";
 $count  = "SELECT DISTINCT #__pshop_product.product_id as num_rows FROM #__pshop_product, #__pshop_category, #__pshop_product_category_xref,#__pshop_shopper_group ";
 $q  = "LEFT JOIN #__pshop_product_price ON #__pshop_product.product_id = #__pshop_product_price.product_id ";
 $q .= "WHERE #__pshop_product_category_xref.category_id=#__pshop_category.category_id ";
if( $category_id ) {
 $q .= "AND #__pshop_product_category_xref.category_id='".$category_id."' ";
}
 $q .= "AND #__pshop_product.product_id=#__pshop_product_category_xref.product_id ";
 $q .= "AND #__pshop_product.product_parent_id='0' ";
 $q .= "AND #__pshop_product.product_publish='Y' ";
 $q .= "AND ((";
 if ($auth["shopper_group_id"] > 0) {
    $q .= "#__pshop_shopper_group.shopper_group_id=#__pshop_product_price.shopper_group_id ";
    //$q .= "AND #__pshop_shopper_group.shopper_group_id='".$auth["shopper_group_id"]."'";
 }
 else {
    $q .= "#__pshop_shopper_group.shopper_group_id=#__pshop_product_price.shopper_group_id ";
    //$q .= "AND #__pshop_shopper_group.default = '1' ";
 }
 $q .= ") OR (#__pshop_product_price.product_id IS NULL)) ";
 if( $keyword1 ) {
  $q .= "AND (";
  if ($search_limiter=="name") {
      $q .= "#__pshop_product.product_name LIKE '%$keyword1%' ";
  }
  elseif ($search_limiter=="cp") {
      $q .= "#__pshop_product.product_url LIKE '%$keyword1%' ";
  }
  elseif ($search_limiter=="desc") {
      $q .= "#__pshop_product.product_s_desc LIKE '%$keyword1%' OR ";
      $q .= "#__pshop_product.product_desc LIKE '%$keyword1%'";
  }
  else {
      $q .= "#__pshop_product.product_name LIKE '%$keyword1%' OR ";
      $q .= "#__pshop_product.product_url LIKE '%$keyword1%' OR ";
      $q .= "#__pshop_category.category_name LIKE '%$keyword1%' OR ";
      $q .= "#__pshop_product.product_sku LIKE '%$keyword1%' OR ";
      $q .= "#__pshop_product.product_s_desc LIKE '%$keyword1%' OR ";
      $q .= "#__pshop_product.product_desc LIKE '%$keyword1%'";
  }
  $q .= ") ";
  /*** KEYWORD 2 TO REFINE THE SEARCH ***/
  if ( !empty($keyword2) ) {
      $q .= "$search_op (";
      if ($search_limiter=="name") {
          $q .= "#__pshop_product.product_name LIKE '%$keyword2%' ";
      }
      elseif ($search_limiter=="cp") {
          $q .= "#__pshop_product.product_url LIKE '%$keyword2%' ";
      }
      elseif ($search_limiter=="desc") {
          $q .= "#__pshop_product.product_s_desc LIKE '%$keyword2%' OR ";
          $q .= "#__pshop_product.product_desc LIKE '%$keyword2%'";
      }
      else {
          $q .= "#__pshop_product.product_name LIKE '%$keyword2%' OR ";
          $q .= "#__pshop_product.product_url LIKE '%$keyword2%' OR ";
          $q .= "#__pshop_category.category_name LIKE '%$keyword2%' OR ";
          $q .= "#__pshop_product.product_sku LIKE '%$keyword2%' OR ";
          $q .= "#__pshop_product.product_s_desc LIKE '%$keyword2%' OR ";
          $q .= "#__pshop_product.product_desc LIKE '%$keyword2%'";
      }
      $q .= ") ";
  }
 }
 elseif( $keyword ) {
     $q .= "AND (#__pshop_product.product_name LIKE '%$keyword%' OR ";
     $q .= "#__pshop_product.product_sku LIKE '%$keyword%' OR ";
     $q .= "#__pshop_product.product_s_desc LIKE '%$keyword%' OR ";
     $q .= "#__pshop_product.product_desc LIKE '%$keyword%') ";
 }
 $q .= "GROUP BY #__pshop_product.product_sku ";
 $q .= "ORDER BY #__$orderby ASC";
 $list .= $q . " LIMIT $offset, " . SEARCH_ROWS;
 $count .= $q;
}
  
/*** GET ALL PUBLISHED PRODUCTS FROM THAT MANUFACTURER ***/
elseif (!empty($manufacturer_id)) {
 $list  = "SELECT DISTINCT * FROM #__pshop_product, #__pshop_product_mf_xref, #__pshop_product_price,#__pshop_shopper_group ";
 $count  = "SELECT DISTINCT product_name as num_rows FROM #__pshop_product, #__pshop_product_mf_xref, #__pshop_product_price,#__pshop_shopper_group ";
 $q  = " manufacturer_id='".$manufacturer_id."' "; 
 $q .= "AND #__pshop_product.product_id=#__pshop_product_mf_xref.product_id ";
 $q .= "AND #__pshop_product.product_id=#__pshop_product_price.product_id ";
 if ($perm->is_registered_customer($my->id)) {
    $list .= ",#__pshop_shopper_vendor_xref WHERE";
    $count .= ",#__pshop_shopper_vendor_xref WHERE";
    $q .= "AND #__pshop_shopper_vendor_xref.user_id = '".$my->id."' ";
    $q .= "AND #__pshop_shopper_vendor_xref.shopper_group_id=#__pshop_product_price.shopper_group_id ";
    $q .= "AND #__pshop_shopper_vendor_xref.shopper_group_id=#__pshop_shopper_group.shopper_group_id ";
 }
 else {
    $list .= " WHERE ";
    $count .= " WHERE ";
    $q .= "AND #__pshop_shopper_group.default = '1' ";
    $q .= "AND #__pshop_shopper_group.shopper_group_id=#__pshop_product_price.shopper_group_id ";
 }
 $q .= "AND ((product_parent_id='0') OR (product_parent_id='')) ";
 $q .= "AND product_publish='Y' ";
 $q .= "GROUP BY mos_pshop_product.product_sku ";
 $q .= "ORDER BY #__$orderby ASC";
 $list .= $q . " LIMIT $offset, " . SEARCH_ROWS;
 $count .= $q;  
}

/*** DEFAULT, LIST ALL PUBLISHED PRODUCTS ***/
else { 
 $list  = "SELECT DISTINCT product_name,#__pshop_product.product_id, product_thumb_image,product_s_desc ";
 $list .= "FROM #__pshop_product, #__pshop_shopper_group ";
 $count  = "SELECT DISTINCT #__pshop_product.product_id as num_rows FROM #__pshop_product, #__pshop_shopper_group ";
 $q  = "LEFT JOIN #__pshop_product_price ON #__pshop_product.product_id = #__pshop_product_price.product_id ";
 $q .= "WHERE #__pshop_product.product_parent_id='0' ";
 $q .= "AND #__pshop_product.product_publish='Y' ";
 $q .= "AND ((";
 if ($auth["shopper_group_id"] > 0) {
    $q .= "#__pshop_shopper_group.shopper_group_id=#__pshop_product_price.shopper_group_id ";
    //$q .= "AND #__pshop_shopper_group.shopper_group_id='".$auth["shopper_group_id"]."'";
 }
 else {
    $q .= "#__pshop_shopper_group.shopper_group_id=#__pshop_product_price.shopper_group_id ";
    //$q .= "AND #__pshop_shopper_group.default = '1' ";
 }
 $q .= ") OR (#__pshop_product_price.product_id IS NULL)) ";
 $q .= "ORDER BY #__$orderby ASC";
 $list .= $q . " LIMIT $offset, " . SEARCH_ROWS;
 $count .= $q;
}
  
$db->setQuery($count);
$db->query();
$db->next_record();
$num_rows = $db->num_rows();

/*** when nothing has been found 
* we tell this here and say goodbye */

if ($num_rows == 0) {
 echo $PHPSHOP_LANG->_PHPSHOP_NO_SEARCH_RESULT;
}

/*** NOW START THE PRODUCT LIST ***/
else {

$db->setQuery($list);
$db->query();

$products_per_row = (!empty($category_id)) ? $db->f("products_per_row") : PRODUCTS_PER_ROW; 

if ( $num_rows > 1 && @$_REQUEST['output'] != "pdf") { ?>
    <!-- ORDER BY .... FORM -->
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get" name="order">
    <?php echo $PHPSHOP_LANG->_PHPSHOP_ORDERBY ?>: 
      <select class="inputbox" name="orderby" onchange="order.submit()">
        <option value="pshop_product.product_name" >
         <?php echo $PHPSHOP_LANG->_PHPSHOP_SELECT ?></option>
        <option value="pshop_product.product_name" <?php echo $orderby=="pshop_product.product_name" ? "selected=\"selected\"" : "";?>>
        <?php echo $PHPSHOP_LANG->_PHPSHOP_PRODUCT_NAME_TITLE ?></option>
        <?php
          if (_SHOW_PRICES == '1') { ?>
              <option value="pshop_product_price.product_price" <?php echo $orderby=="pshop_product_price.product_price" ? "selected=\"selected\"" : "";?>>
              <?php echo $PHPSHOP_LANG->_PHPSHOP_PRODUCT_PRICE_TITLE ?></option><?php 
          } ?>
      </select>
      <input type="hidden" name="Itemid" value="<?php echo @$_REQUEST['Itemid'] ?>" />
      <input type="hidden" name="option" value="com_phpshop" />
      <input type="hidden" name="page" value="shop.browse" />
      <input type="hidden" name="category_id" value="<?php echo $category_id ?>" />
      <input type="hidden" name="manufacturer_id" value="<?php echo $manufacturer_id ?>" />
      <input type="hidden" name="keyword" value="<?php echo urlencode( $keyword ) ?>" />
      <input type="hidden" name="keyword1" value="<?php echo urlencode( $keyword1 ) ?>" />
      <input type="hidden" name="keyword2" value="<?php echo urlencode( $keyword2 ) ?>" />
    </form>
<?php 
} ?>
<table width="100%" border="0" cellspacing="0" cellpadding="5" >
  <tr>
<?php
  $i = 0;
  /** 
  *   Read the template file into a String variable.
  *   Then replace the placeholders with HTML formatted product details
  *
  * function read_file( $file, $defaultfile='') ***/
  if(@$_REQUEST['output'] != "pdf")
    $templatefile = (!empty($category_id)) ? $db->f("category_browsepage") : CATEGORY_TEMPLATE;
  else
    $templatefile = "browse_lite_pdf";
  $template = read_file( PAGEPATH."templates/$templatefile.php", 
                                      PAGEPATH."templates/".CATEGORY_TEMPLATE.".php");
  
  /*** Start printing out all products (in that category) ***/
  while ($db->next_record()) {

      // Set the flypage for this product based on the category.
      // If no flypage is set then use the default as set in phpshop.cfg.php
      $flypage = $ps_product->get_flypage($db->f("product_id"));
      
      $url = $sess->url(URL . "index.php?page=shop.product_details&flypage=$flypage&product_id=" . $db->f("product_id") . "&category_id=" . $db->f("category_id"));
      
      if( $db->f("product_thumb_image") ) {
        if( substr( $db->f("product_thumb_image"), 0, 4) == "http" )
            $product_thumb_image = $db->f("product_thumb_image");
        else {
            if(PSHOP_IMG_RESIZE_ENABLE == '1')
                $product_thumb_image = $mosConfig_live_site."/components/com_phpshop/show_image_in_imgtag.php?filename=".urlencode($db->f("product_thumb_image"))."&newxsize=".PSHOP_IMG_WIDTH."&newysize=".PSHOP_IMG_HEIGHT."&fileout=";
            else
                $product_thumb_image = IMAGEURL."product/".$db->f("product_thumb_image");
        }
      } 
      else {
        $product_thumb_image = IMAGEURL.NO_IMAGE;
      }
      $product_name = $db->f("product_name");
      
      $product_s_desc = $db->f("product_s_desc");
      
      $product_details = $PHPSHOP_LANG->_PHPSHOP_FLYPAGE_LBL;
      
      if (PSHOP_ALLOW_REVIEWS == '1' && @$_REQUEST['output'] != "pdf") {
        /** 
        *   Average customer rating: xxxxx
        *   Total votes: x
        */
        $product_rating = $PHPSHOP_LANG->_PHPSHOP_CUSTOMER_RATING .": <br />";
        $product_rating .= ps_reviews::allvotes( $db->f("product_id") ); 
      }
      else 
        $product_rating = "";

      
      /** Price: xx.xx EUR ***/
      if (_SHOW_PRICES == '1') {
        $product_price = $ps_product->show_price( $db->f("product_id") ); 
      } 
      else {
        $product_price = "";
      }
      
      /*** Add-to-Cart Button ***/
      if (USE_AS_CATALOGUE != '1') { 
          $form_addtocart = "<form action=\"". $_SERVER['PHP_SELF'] ."\" method=\"post\" name=\"addtocart\" id=\"addtocart".$i."\">\n
                <input class=\"inputbox\" type=\"text\" size=\"3\" name=\"quantity\" value=\"1\" />&nbsp;\n
                <input class=\"button\" type=\"submit\" name=\"submit\" value=\"".$PHPSHOP_LANG->_PHPSHOP_CART_ADD_TO."\" />\n
                <input type=\"hidden\" name=\"category_id\" value=\"". @$_REQUEST['category_id'] ."\" />\n
                <input type=\"hidden\" name=\"product_id\" value=\"". $db->f("product_id") ."\" />\n
                <input type=\"hidden\" name=\"page\" value=\"shop.cart\" />\n
                <input type=\"hidden\" name=\"func\" value=\"cartadd\" />\n
                <input type=\"hidden\" name=\"Itemid\" value=\"$Itemid\" />\n
                <input type=\"hidden\" name=\"option\" value=\"com_phpshop\" />\n
              </form>\n";
      }
      else
        $form_addtocart = "";
      
      /*** Now fill the template 
      * Customizing:
      *   a. Define your own placeholders(e.g. {product_weight} )
      *   b. Add a line below like this (must be below first str_replace call!):
              $product_cell = str_replace( "{product_weight}", $db->f("product_weight"), $product_cell );
      *   c. put the placeholder {product_weight} somewhere in the template (/html/templates)
              <tr><td>Product Weight: {product_weight}</td></tr>
      *   d. save the template file under a new name (e.g. browse_weight.php )
      *   e. Assign the browse page "browse_weight" to the categories,
      *       you want to have using that template file (do that in the category form!) 
      **/
      $product_cell = str_replace( "{product_flypage}", $url, $template );
      $product_cell = str_replace( "{product_thumb_image}", $product_thumb_image, $product_cell );
      
      if( PSHOP_IMG_RESIZE_ENABLE=='1' ) {
          $product_cell = str_replace( "{image_width}", "", $product_cell );
          $product_cell = str_replace( "{image_height}", "", $product_cell );
      }
      else {
          $product_cell = str_replace( "{image_width}", "width=\"".PSHOP_IMG_WIDTH."\"", $product_cell );
          $product_cell = str_replace( "{image_height}", "", $product_cell );
      }
      $product_cell = str_replace( "{product_name}", $product_name, $product_cell );
      $product_cell = str_replace( "{product_s_desc}", $product_s_desc, $product_cell );
      $product_cell = str_replace( "{product_details...}", $product_details, $product_cell );
      $product_cell = str_replace( "{product_rating}", $product_rating, $product_cell );
      $product_cell = str_replace( "{product_price}", $product_price, $product_cell );
      $product_cell = str_replace( "{form_addtocart}", $form_addtocart, $product_cell );
      
      /*** Now echo the filled cell ***/
      if(@$_REQUEST['output'] != "pdf")
        echo "<td valign=\"top\" width=\"". intval(round(100/$products_per_row)) ."%\" >";
      else 
        echo "<td>";
      echo $product_cell; 
      
?>
</td>
<?php
    $i++;
    /*** START NEXT ROW ??? ***/
    if ( ($i) % $products_per_row == 0)
        /** if yes, close the current row and print out a horizontal bar ***/
        echo "\n</tr>\n<tr><td colspan=\"$products_per_row\"><hr /></td></tr>\n<tr>";

    
  } /*** END OF while loop ***/
?>
</table>
<!-- BEGIN PAGE NAVIGATION -->
<div align="center">

<?php
if ( $num_rows > SEARCH_ROWS ) {
    include_once( "includes/pageNavigation.php" );
    $pagenav = new mosPageNav( $num_rows, $offset, SEARCH_ROWS);
    
    $keyword = urlencode( $keyword );
    
    $search_string = "index.php?option=com_phpshop&page=$modulename.browse&category_id=$category_id&keyword=$keyword&manufacturer_id=$manufacturer_id&Itemid=$Itemid";
    $search_string .= !empty($orderby) ? "&orderby=".urlencode($orderby) : "";
    
    if (!empty($keyword1)) {
        $search_string.="&keyword1=$keyword1";
        $search_string.="&search_category=$search_category";
        $search_string.="&search_limiter=$search_limiter";
        if (!empty($keyword2)) {
          $search_string.="&keyword2=$keyword2";
          $search_string.="&search_op=$search_op";
        }
        urlencode( $search_string );
    }

    echo sefReltoAbs($pagenav->writePagesLinks( $search_string ));
}
?>

</div>
<!-- END PAGE NAVIGATION -->

<?php 
}
?>
