<?php
/*
  index.php
  作成日：2004/12/2
　最終更新日：2005/8/19

　作成者：有限会社セカンドブレーン
　　　　　　青木隆平
　　　　　　ryuhei@secondbrain.co.jp

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

// the following cPath references come from application_top.php
  $category_depth = 'top';
  if (isset($cPath) && tep_not_null($cPath)) {
    $categories_products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$current_category_id . "'");
    $cateqories_products = tep_db_fetch_array($categories_products_query);
    if ($cateqories_products['total'] > 0) {
      $category_depth = 'products'; // display products
    } else {
      $category_parent_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$current_category_id . "'");
      $category_parent = tep_db_fetch_array($category_parent_query);
      if ($category_parent['total'] > 0) {
        $category_depth = 'nested'; // navigate through the categories
      } else {
        $category_depth = 'products'; // category has no products, but display the 'no products' message
      }
    }
  }

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_DEFAULT);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>" /> 
<meta http-equiv="content-style-type" content="text/css" />
<meta name="description" content="<?php echo KN_CFG_META_DESCRIPTION; ?>" />
<meta name="keywords" content="<?php echo KN_CFG_META_KEYWORDS; ?>" />
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>" />
<link rel="stylesheet" type="text/css" href="general.css" media="screen,print" />
</head>
<body>
<div id="container">
<!-- header -->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- /header -->
<hr />
<!-- contents -->
<div id="contents">
<!-- left_contents -->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- /left_contents -->
<hr />
<!-- right_contents -->
<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- /right_contents -->
<hr />
<!-- main_contents -->
<div id="mainContents">
<p class="hide"><a id="skipped" name="skipped" title="<?php echo TEXT_NAVISKIPPED; ?>"><?php echo TEXT_NAVISKIPPED; ?></a></p> 
<?php
  if ($category_depth == 'nested') {
// カテゴリー一覧ページ
    $category_query = tep_db_query("select cd.categories_name, cd.categories_description, c.categories_image from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$current_category_id . "' and cd.categories_id = '" . (int)$current_category_id . "' and cd.language_id = '" . (int)$languages_id . "'");
    $category = tep_db_fetch_array($category_query);
?>
<h2 class="pageHeading"><?php echo $category['categories_name']; ?></h2>
<div id="common">
<?php
    if ($category['categories_image']) {
      echo '<div class="headingImage">' .  tep_image(DIR_WS_IMAGES . $category['categories_image'], $category['categories_name'], HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT) . "</div>\n";
    }
    if ($category['categories_description']) {
      echo '<div class="headingDescription">' . nl2br($category['categories_description']) . "</div>\n";
    }
    // カテゴリー一覧
    if (isset($cPath) && preg_match('/_/', $cPath)) {
      $display_category_id = array_reverse($cPath_array);
      include(DIR_WS_MODULES . FILENAME_CATEGORIES);
    } else {
      $display_category_id = $current_category_id;
      include(DIR_WS_MODULES . FILENAME_CATEGORIES);
    }
    // 当店の特選品
    $recommended_products_category_id = $current_category_id;
    include(DIR_WS_MODULES . FILENAME_RECOMMENDATION);
    // 新着商品
    $new_products_category_id = $current_category_id;
    include(DIR_WS_MODULES . FILENAME_NEW_PRODUCTS);

  } elseif ($category_depth == 'products' || $_GET['manufacturers_id']) {
// 商品一覧ページ
    $define_list = array();
    $define_list['PRODUCT_LIST_MODEL'] = KN_CFG_PRODUCT_LIST_MODEL;
    $define_list['PRODUCT_LIST_NAME'] = KN_CFG_PRODUCT_LIST_NAME;
    if (KN_CFG_USE_SUBTITLE == 'true') {
      $define_list['PRODUCT_LIST_SUBTITLE'] = KN_CFG_PRODUCT_LIST_SUBTITLE;
    }
    if (KN_CFG_USE_SERIES_TITLE == 'true') {
      $define_list['PRODUCT_LIST_SERIES_TITLE'] = KN_CFG_PRODUCT_LIST_SERIES_TITLE;
    }
    if (KN_CFG_USE_AUTHOR == 'true') {
      $define_list['PRODUCT_LIST_AUTHOR'] = KN_CFG_PRODUCT_LIST_AUTHOR;
    }
    if (KN_CFG_USE_PUBLISHED == 'true') {
      $define_list['PRODUCT_LIST_PUBLISHED'] = KN_CFG_PRODUCT_LIST_PUBLISHED;
    }
    if (KN_CFG_USE_VOLUME == 'true') {
      $define_list['PRODUCT_LIST_VOLUME'] = KN_CFG_PRODUCT_LIST_VOLUME;
    }
    if (KN_CFG_USE_ISBN == 'true') {
      $define_list['PRODUCT_LIST_ISBN'] = KN_CFG_PRODUCT_LIST_ISBN;
    }
    $define_list['PRODUCT_LIST_PRICE'] = KN_CFG_PRODUCT_LIST_PRICE;
    if (KN_CFG_USE_PUBLISHERS == 'true') {
      $define_list['PRODUCT_LIST_PUBLISHERS'] = KN_CFG_PRODUCT_LIST_PUBLISHERS;
    }
    $define_list['PRODUCT_LIST_BUY_NOW'] = KN_CFG_PRODUCT_LIST_BUY_NOW;
    $define_list['PRODUCT_LIST_IMAGE'] = KN_CFG_PRODUCT_LIST_IMAGE;
    $define_list['PRODUCT_LIST_QUANTITY'] = KN_CFG_PRODUCT_LIST_QUANTITY;
    $define_list['PRODUCT_LIST_WEIGHT'] = KN_CFG_PRODUCT_LIST_WEIGHT;
    $define_list['PRODUCT_LIST_DESCRIPTION'] = KN_CFG_PRODUCT_LIST_DESCRIPTION;

    asort($define_list);

    $column_list = array();
    reset($define_list);
    while (list($column, $value) = each($define_list)) {
      if ($value) $column_list[] = $column;
    }

    $select_column_list = '';
    $flg_subtitle = 0;
    $flg_series_title = 0;
    $flg_volume = 0;
    $flg_published = 0;
    $flg_publishers = 0;
    for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
      if ( ($column_list[$col] == 'PRODUCT_LIST_BUY_NOW') || ($column_list[$col] == 'PRODUCT_LIST_PRICE') ) {
        continue;
      }

      if (tep_not_null($select_column_list)) {
        $select_column_list .= ', ';
      }

      switch ($column_list[$col]) {
        case 'PRODUCT_LIST_MODEL':
          $select_column_list .= 'p.products_model';
          break;
        case 'PRODUCT_LIST_NAME':
          $select_column_list .= 'pd.products_name';
          break;
        case 'PRODUCT_LIST_SUBTITLE':
          if ($flg_subtitle) {
            $select_column_list = rtrim($select_column_list, ', ');
            break;
          }
          $select_column_list .= 'pd.products_subtitle';
          $flg_subtitle = 1;
          break;
        case 'PRODUCT_LIST_SERIES_TITLE':
          if ($flg_series_title) {
            $select_column_list = rtrim($select_column_list, ', ');
            break;
          }
          $select_column_list .= 'pd.products_series_title';
          $flg_series_title = 1;
          break;
        case 'PRODUCT_LIST_AUTHOR':
          $select_column_list .= 'pd.products_author';
          break;
        case 'PRODUCT_LIST_PUBLISHED':
          if ($flg_published) {
            $select_column_list = rtrim($select_column_list, ', ');
            break;
          }
          $select_column_list .= 'pd.products_published';
          $flg_published = 1;
          break;
        case 'PRODUCT_LIST_VOLUME':
          if ($flg_volume) {
            $select_column_list = rtrim($select_column_list, ', ');
            break;
          }
          $select_column_list .= 'pd.products_volume';
          $flg_volume = 1;
          break;
        case 'PRODUCT_LIST_ISBN':
          $select_column_list .= 'p.products_isbn';
          break;
        case 'PRODUCT_LIST_PUBLISHERS':
          if ($flg_publishers) {
            $select_column_list = rtrim($select_column_list, ', ');
            break;
          }
          $select_column_list .= 'pd.products_publishers';
          $flg_publishers = 1;
          break;
        case 'PRODUCT_LIST_QUANTITY':
          $select_column_list .= 'p.products_quantity';
          break;
        case 'PRODUCT_LIST_IMAGE':
          $select_column_list .= 'p.products_image';
          break;
        case 'PRODUCT_LIST_WEIGHT':
          $select_column_list .= 'p.products_weight';
          break;
        case 'PRODUCT_LIST_DESCRIPTION':
          if (!$flg_subtitle) {
            $select_column_list .= 'pd.products_subtitle, ';
            $flg_subtitle = 1;
          }
          if (!$flg_series_title) {
            $select_column_list .= 'pd.products_series_title, ';
            $flg_series_title = 1;
          }
          if (!$flg_published) {
            $select_column_list .= 'pd.products_published, ';
            $flg_published = 1;
          }
          if (!$flg_volume) {
            $select_column_list .= 'pd.products_volume, ';
            $flg_volume = 1;
          }
          if (!$flg_publishers) {
            $select_column_list .= 'pd.products_publishers, ';
            $flg_publishers = 1;
          }
          $select_column_list .= 'pd.products_description';
          break;
      }
    }

    if (tep_not_null($select_column_list)) {
      $select_column_list .= ', ';
    }

// show the products of a specified manufacturer
    if (isset($_GET['manufacturers_id'])) {
      if (isset($_GET['filter_id'])) {
// We are asked to show only a specific category
        $listing_sql = "select " . $select_column_list . " p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from (" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$_GET['filter_id'] . "'";
      } else {
// We show them all
        $listing_sql = "select " . $select_column_list . " p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from (" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where p.products_status = '1' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'";
      }
// We build the categories-dropdown
      $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)$languages_id . "' and p.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "' order by cd.categories_name";
    } else {
// show the products in a given categorie
      if (isset($_GET['filter_id'])) {
// We are asked to show only specific catgeory
        $listing_sql = "select " . $select_column_list . " p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from (" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where p.products_status = '1' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$_GET['filter_id'] . "' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";
      } else {
// We show them all
        $listing_sql = "select " . $select_column_list . " p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from ((" . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p) left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c) left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";
      }
// We build the manufacturers Dropdown
      $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.sort_order";
    }

    if ( (!$_GET['sort']) || (!preg_match('/[1-8][ad]/', $_GET['sort'])) || (substr($_GET['sort'],0,1) > sizeof($column_list)) ) {
      $sort_order_check = false;
      for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
        if ($column_list[$col] == 'PRODUCT_LIST_MODEL') {
          $_GET['sort'] = $col+1 . 'a';
          $listing_sql .= " order by p.sort_order, p.products_model";
          $sort_order_check = true;
          break;
        }
      }
      if ($sort_order_check == false) {
        $listing_sql .= " order by p.sort_order";
      }
    } else {
      $sort_col = substr($_GET['sort'], 0 , 1);
      $sort_order = substr($_GET['sort'], 1);
      $listing_sql .= ' order by ';
      switch ($column_list[$sort_col-1]) {
        case 'PRODUCT_LIST_MODEL':
          $listing_sql .= "p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_NAME':
          $listing_sql .= "pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_SUBTITLE':
          $listing_sql .= "pd.products_subtitle " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_SERIES_TITLE':
          $listing_sql .= "pd.products_series_title " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_AUTHOR':
          $listing_sql .= "pd.products_author " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_PUBLISHED':
          $listing_sql .= "pd.products_published " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_VOLUME':
          $listing_sql .= "pd.products_volume " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_ISBN':
          $listing_sql .= "p.products_isbn " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_PUBLISHERS':
          $listing_sql .= "pd.products_publishers " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_QUANTITY':
          $listing_sql .= "p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_IMAGE':
          $listing_sql .= "pd.products_name";
          break;
        case 'PRODUCT_LIST_WEIGHT':
          $listing_sql .= "p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_PRICE':
          $listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
      }
    }

    if (isset($_GET['manufacturers_id'])) {
      $manufacturer_query = tep_db_query("select manufacturers_name, manufacturers_image from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'");
      $manufacturer = tep_db_fetch_array($manufacturer_query);
      $title_name = $manufacturer['manufacturers_name'];
      $title_image = $manufacturer['manufacturers_image'];
      $title_description = '';
    } elseif ($current_category_id) {
      $category_query = tep_db_query("select cd.categories_name, c.categories_image, cd.categories_description from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$current_category_id . "' and cd.categories_id = '" . (int)$current_category_id . "' and cd.language_id = '" . (int)$languages_id . "'");
      $category = tep_db_fetch_array($category_query);
      $title_name = $category['categories_name'];
      $title_image = $category['categories_image'];
      $title_description = $category['categories_description'];
    }
?>
<h2 class="pageHeading"><?php echo $title_name; ?></h2>
<div id="common">
<?php
    if ($title_image) {
      echo '<div class="headingImage">' . tep_image(DIR_WS_IMAGES . $title_image, $title_name, HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT) . "</div>\n";
    }
    if ($title_description) {
      echo '<div class="headingDescription">' . nl2br($title_description) . "</div>\n";
    }
      
    $display_category_id = $current_category_id;
    if (!isset($_GET['manufacturers_id'])) {
      include(DIR_WS_MODULES . FILENAME_CATEGORIES);
    }

    if (PRODUCT_LIST_FILTER > 0) {
      $filterlist_query = tep_db_query($filterlist_sql);
      if (tep_db_num_rows($filterlist_query) > 1) {
        echo '<div id="filterSelect"><form>' . TEXT_SHOW . '<select size="1" onChange="if(options[selectedIndex].value) window.location.href=(options[selectedIndex].value)">';
        if (isset($_GET['manufacturers_id'])) {
          $arguments = 'manufacturers_id=' . $_GET['manufacturers_id'];
        } else {
          $arguments = 'cPath=' . $cPath;
        }
        $arguments .= '&sort=' . $_GET['sort'];

        $option_url = tep_href_link(FILENAME_DEFAULT, $arguments);

        if (!isset($_GET['filter_id'])) {
          echo '<option value="' . $option_url . '" SELECTED>' . TEXT_ALL . '</option>';
        } else {
          echo '<option value="' . $option_url . '">' . TEXT_ALL . '</option>';
        }

        echo '<option value="">---------------</option>';
        while ($filterlist = tep_db_fetch_array($filterlist_query)) {
          $option_url = tep_href_link(FILENAME_DEFAULT, $arguments . '&filter_id=' . $filterlist['id']);
          if (isset($_GET['filter_id']) && ($_GET['filter_id'] == $filterlist['id'])) {
            echo '<option value="' . $option_url . '" SELECTED>' . $filterlist['name'] . '</option>';
          } else {
            echo '<option value="' . $option_url . '">' . $filterlist['name'] . '</option>';
          }
        }
        echo '</select></form></div>' . "\n";
      }
    }

    // 商品一覧
    include(DIR_WS_MODULES . FILENAME_PRODUCT_LISTING);
    
  } else {
// トップページ
?>
<h2 class="pageHeading"><?php echo HEADING_TITLE; ?></h2>
<div id="common">
<?php
    // あいさつ文
    if (!defined("KN_CFG_ALLOW_CHECKOUT") || (KN_CFG_ALLOW_CHECKOUT == "true")) {
      if (KN_CFG_DISPLAY_CUSTOMER_GREETING == "true") {
        echo '<div class="greeting">' . tep_customer_greeting() . '</div>';
      }
    }
    // 新着情報
    $shop_info_query = tep_db_query("select shop_info_title, shop_info_value, allow_edit_title, allow_tags from " . TABLE_SHOP_INFO . " where shop_info_key = 'SHOP_INFO_WHATS_NEW'");
    $shop_info = tep_db_fetch_array($shop_info_query);
    if (isset($shop_info['shop_info_value']) && tep_not_null($shop_info['shop_info_value'])) {
      if ($shop_info['allow_edit_title']) {
        echo '<h3>' . $shop_info['shop_info_title'] . "</h3>\n";
      }
      echo '<div id="whatsNew">';
      if ($shop_info['allow_tags']) {
        echo $shop_info['shop_info_value'];
      } else {
        echo nl2br(htmlspecialchars($shop_info['shop_info_value']));
      }
      echo "</div>\n";
    }
    // カテゴリー一覧
    $display_category_id = $current_category_id;
    include(DIR_WS_MODULES . FILENAME_CATEGORIES);
    // 当店の特選品
    include(DIR_WS_MODULES . FILENAME_RECOMMENDATION);
    // 新着商品
    include(DIR_WS_MODULES . FILENAME_NEW_PRODUCTS);
  }
?>
</div>
</div>
<!-- /main_contents -->
</div>
<!-- /contents -->
<hr />
<!-- footer -->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- /footer -->
</div>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
