<?php
/*
  $Id: geta_products.php,v 1.4 2007/04/16 05:40:39 ryu Exp $

　Koshoten.net2
　http://www.koshoten.net/

  Released under the GNU General Public License

  //----------------------------------------------------------------------------//
  事前に必要な環境
  //----------------------------------------------------------------------------//
  このモジュールはGETAの使用を前提としているため、makewam.phpに記載された
  手順およびmakewam.phpでのWAMの生成が必要です。
  //----------------------------------------------------------------------------//
*/
?>
<!-- getaProducts -->
<?php
  $results_count = 0;
  $original_products_array = array();
  $original_products_count = 0;
  if ((KN_CFG_GETA_ENABLED == 'true') && (KN_CFG_GETA_PRODUCTS_ENABLED == 'true')) {
    if (!empty($g_products_id)) {
      $filename_g_products_id = @tempnam(DIR_FS_GETA_WORK, "GP_");
      $tmpfile = fopen($filename_g_products_id, "w");
      if (is_array($g_products_id)) {
        for ($i=0; $i<count($g_products_id); $i++) {
          fwrite($tmpfile, $g_products_id[$i] . "\n");
          $original_products_array[$i] = trim($g_products_id[$i]);
          $original_products_count++;
        }
      } else {
        fwrite($tmpfile, $g_products_id);
        $original_products_array[0] = trim($g_products_id);
        $original_products_count++;
      }
      fclose($tmpfile);
      // 商品WAMの文書連想検索
      $max = KN_CFG_GETA_PRODUCTS_MAX * 2 + $original_products_count;		// 商品が売切れになっていることも考慮して２倍分検索する（＋元の商品数）
      $geta_products_text = array();
      $s_result = popen("perl " . FILENAME_S_GETA_PRODUCTS . " " . $filename_g_products_id . " " . TEXT_P_HANDLE_ID . " " . $geta_row_max . " " . $max . " 2>&1", "r");
      while ( !feof($s_result) ) {
        $s_result_data = fgets($s_result, 1000);
        $search_pID = trim($s_result_data);
        if ($search_pID) {
          $original_products = false;
          foreach ($original_products_array as $val) {
            if ($search_pID == $val) {
              $original_products = true;	// 元の商品は除外
            }
          }
          if (!$original_products) {
            $geta_products_query = tep_db_query("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 from " . TABLE_PRODUCTS . " p left join " . TABLE_PRODUCTS_DESCRIPTION . " pd on p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where p.products_id = '" . (int)$search_pID . "' and p.products_status = '1'");
            $num_products = tep_db_num_rows($geta_products_query);
            if ($num_products > 0) {
              $geta_products = tep_db_fetch_array($geta_products_query);
              if ($results_count < KN_CFG_GETA_PRODUCTS_MAX) {
              	  if ((KN_CFG_CONTACT_PRICE != 0) && ($geta_products['products_price'] == KN_CFG_CONTACT_PRICE)) {
              	    $geta_products_price = '-';
              	  } else {
                  if ($geta_products_special_price = tep_get_products_special_price($geta_products['products_id'])) {
                    $geta_products_price = '<del>' . $currencies->display_price($geta_products['products_price'], tep_get_tax_rate($geta_products['products_tax_class_id'])) . '</del><br /><span class="specialPrice">' . $currencies->display_price($geta_products_special_price, tep_get_tax_rate($geta_products['products_tax_class_id']));
                  } else {
              	      $geta_products_price = $currencies->display_price($geta_products['products_price'], tep_get_tax_rate($geta_products['products_tax_class_id']));
                  }
              	  }
                $geta_products_text[] = '<div><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $geta_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $geta_products['products_image'], $geta_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . "</a></div>\n" .
                                                '<p><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $geta_products['products_id']) . '">' . $geta_products['products_name'] . "</a></p>\n" .
                                                '<p class="pPrice">' . $geta_products_price . "</p>\n";
              }
              $results_count++;
            }
          }
        }
      }
      pclose($s_result);
      @unlink($filename_g_products_id);
    }
  }

    
  if ($results_count > 0) {
    $cssid = 'getaProductsBox';
    $contentbox_header = TABLE_HEADING_GETA_PRODUCTS;

    $row = 0;
    $col = 0;
    $contentbox_contents = array();
    for ($i=0; $i<KN_CFG_GETA_PRODUCTS_MAX; $i++) {
      $contentbox_contents[$row][$col] = array('text' => $geta_products_text[$i]);
      $col ++;
      if ($col > 2) {
        $col = 0;
        $row ++;
      }
    }

    new contentBox($cssid, $contentbox_contents, $contentbox_header);
  }
?>
<!-- /getaProducts -->
