<?php
/*
  $Id: products_attributes.php $
  TomatoCart Open Source Shopping Cart Solutions
  http://www.tomatocart.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

  if ( isset($current_category_id) && !empty($current_category_id) ) {
    $display = false;  
  
    if (PRODUCT_LIST_FILTER > 0) {
      if (isset($_GET['manufacturers']) && !empty($_GET['manufacturers'])) {
        $filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and p2c.categories_id = cd.categories_id and cd.language_id = '" . (int)$osC_Language->getID() . "' and p.manufacturers_id = '" . (int)$_GET['manufacturers'] . "' order by cd.categories_name";
      } else {
        $filterlist_sql = "select distinct m.manufacturers_id as id, m.manufacturers_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_MANUFACTURERS . " m where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_category_id . "' order by m.manufacturers_name";
      }
      
	    $Qfilterlist = $osC_Database->query($filterlist_sql);
	    $Qfilterlist->execute();

	    if($Qfilterlist->numberOfRows() > 0){
	      $display = true;
	    }
    }
    
    $Qgroups = $osC_Database->query('select distinct(p.products_attributes_groups_id) from :table_products p, :table_products_to_categories ptc where p.products_id = ptc.products_id and p.products_attributes_groups_id is not null and ptc.categories_id = :categories_id ');
    $Qgroups->bindTable(':table_products', TABLE_PRODUCTS);
    $Qgroups->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
    $Qgroups->bindInt(':categories_id', $current_category_id);
    $Qgroups->execute();

    if($Qgroups->numberOfRows() > 0 || $display) {
      echo '<div class="moduleBox"><h6>'.$osC_Language->get('products_attributes_filter').'</h6>' . "\n";
      echo '  <div id="productAttributes" class="content">' . "\n";
      echo '    <form name="filter" action="' . osc_href_link(FILENAME_DEFAULT) . '" method="get">' . osc_draw_hidden_field('cPath', isset($_GET['cPath'])? $_GET['cPath'] :'') . osc_draw_hidden_session_id_field();
    }
    
    if ($Qgroups->numberOfRows() > 0) {
      while ($Qgroups->next()) {

        $Qentries = $osC_Database->query('select * from :table_products_attributes_values where products_attributes_groups_id = :products_attributes_groups_id and language_id = :language_id  order by sort_order');
        $Qentries->bindTable(':table_products_attributes_values', TABLE_PRODUCTS_ATTRIBUTES_VALUES);
        $Qentries->bindInt(':products_attributes_groups_id', $Qgroups->valueInt('products_attributes_groups_id'));
        $Qentries->bindInt(':language_id', $osC_Language->getID());
        $Qentries->execute();
        
        while ($Qentries->next()) {
          
          $data = array();
          $data[] = array('id'=>'', 'text' => $osC_Language->get('pull_down_default'));
  
          if ($Qentries->value('module') == 'text_field') {
  
            $Qvalues = $osC_Database->query('select distinct value from :table_products_attributes where products_attributes_values_id = :products_attributes_values_id and language_id = :language_id ');
            $Qvalues->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
            $Qvalues->bindInt(':products_attributes_values_id', $Qentries->value('products_attributes_values_id'));
            $Qvalues->bindInt(':language_id', $osC_Language->getID());
            $Qvalues->execute();
  
            while ($Qvalues->next()){
              $fields_value = $Qvalues->value('value');
  
              if(!empty($fields_value))
                $data[] = array('id'=> $fields_value, 'text' => $fields_value);
            }
  
            $Qvalues->freeResult();
          } else {
            $values = explode(',', $Qentries->value('value'));
  
            for ($i = 1; $i <= sizeof($values); $i++) {
              $data[] = array('id' => $i, 'text' => $values[$i - 1]);
            }
          }
  
          $default = '';
          $products_attributes_values_id = $Qentries->value('products_attributes_values_id');
  
          if ( isset($_GET['products_attributes']) && is_array($_GET['products_attributes']) && isset($_GET['products_attributes'][$products_attributes_values_id]) ) {
            $default = $_GET['products_attributes'][$products_attributes_values_id];
          }
          
          echo osc_draw_hidden_field('products_attributes[' . $products_attributes_values_id . ']', $default, 'id=product_attribute_'. $products_attributes_values_id);
            $att_content = '<div style="float: left; width: 100%;" id="product_attribute_'. $products_attributes_values_id. '">'. osc_draw_label($Qentries->value('name') . ' :', 'products_attributes[' . $products_attributes_values_id . ']');
            for($i = 0; $i < sizeof($data); $i ++) {
              $value = ($i == 0 ? '' : $data[$i]['id']);
              if($default == $data[$i]['id'] || (empty($default) && $i == 0)){
                 $att_content .= '<span style="float: left; margin: 2px 5px;" id=product_attribute_'. $products_attributes_values_id. '_'. $i. ' class="visited"><a href="javascript:form_submit(\'product_attribute_'. $products_attributes_values_id. '\', \''. $value. '\');">'. $data[$i]['text']. '</a></span>';             
              }else{
                $att_content .= '<span style="float: left; margin: 2px 5px;" id=product_attribute_'. $products_attributes_values_id. '_'. $i. '><a href="javascript:form_submit(\'product_attribute_'. $products_attributes_values_id. '\', \''. $value. '\');">'. $data[$i]['text']. '</a></span>';
              }
            }
            $att_content .= '</div>';
            echo $att_content;
        }

      }

      $Qgroups->freeResult();


    }
    
    //manufacture移植过来
    if (PRODUCT_LIST_FILTER > 0) {
      if (isset($_GET['manufacturers']) && !empty($_GET['manufacturers'])) {
        $filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where p.products_status = '1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and p2c.categories_id = cd.categories_id and cd.language_id = '" . (int)$osC_Language->getID() . "' and p.manufacturers_id = '" . (int)$_GET['manufacturers'] . "' order by cd.categories_name";
      } else {
        $filterlist_sql = "select distinct m.manufacturers_id as id, m.manufacturers_name as name from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_MANUFACTURERS . " m where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_category_id . "' order by m.manufacturers_name";
      }
    
      $Qfilterlist = $osC_Database->query($filterlist_sql);
      $Qfilterlist->execute();
    
      if ($Qfilterlist->numberOfRows() > 0) {
    
        if (isset($_GET['manufacturers']) && !empty($_GET['manufacturers'])) {
          echo osc_draw_hidden_field('manufacturers', $_GET['manufacturers']);
          $options = array(array('id' => '', 'text' => $osC_Language->get('filter_all_categories')));
        } else {
          echo osc_draw_hidden_field('cPath', $cPath);
          $options = array(array('id' => '', 'text' => $osC_Language->get('filter_all_manufacturers')));
        }
    
        while ($Qfilterlist->next()) {
          $options[] = array('id' => $Qfilterlist->valueInt('id'), 'text' => $Qfilterlist->value('name'));
        }
        echo osc_draw_hidden_field('filter', (isset($_GET['filter']) ? $_GET['filter'] : null), 'id=filter');
        $att_content = '<div style="float: left; width: 100%;" id="filter">'. osc_draw_label('制造商:', 'filter');
        for($i = 0; $i < sizeof($options); $i ++) {
          $value = ($i == 0 ? '' : $options[$i]['id']);
          if( (isset($_GET['filter']) && $_GET['filter'] == $options[$i]['id']) || (empty($_GET['filter']) && $i == 0)){
             $att_content .= '<span style="float: left; margin: 2px 5px;" id=filter_'. $options[$i]['id']. ' class="visited"><a href="javascript:form_submit(\'filter\', \''. $value. '\');">'. $options[$i]['text']. '</a></span>';             
          }else{
            $att_content .= '<span style="float: left; margin: 2px 5px;" id=filter_'. $options[$i]['id']. '><a href="javascript:form_submit(\'filter\', \''. $value. '\');">'. $options[$i]['text']. '</a></span>';
          }
        }
        $att_content .= '</div>';
        echo $att_content;          
        echo osc_draw_hidden_session_id_field() . "\n";
      }
    } 

    if($Qgroups->numberOfRows() > 0 || $display){
      echo '    <div style="clear: both"></div>';
      echo '    </form>' . "\n";
      echo '  </div>
            </div>';
    }  
  }
?>
<script type="text/javascript">
  function form_submit(id, value){
    $(id).value= value;
    $$('form')[1].submit();
  }
</script>
