<?php
/*
  $Id: geta_search_result.php,v 1.7 2009/04/14 23:51:23 ryu Exp $

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

  Released under the GNU General Public License

  //----------------------------------------------------------------------------//
  事前に必要な環境
  //----------------------------------------------------------------------------//
  このモジュールはGETAの使用を前提としているため、makewam.phpに記載された
  手順およびmakewam.phpでのWAMの生成が必要です。
  //----------------------------------------------------------------------------//
*/

  require('includes/application_top.php');

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_GETA_SEARCH_RESULT);

  //GETA用設定ファイル
  require('../geta_work/geta_cfg.php');

  if ((KN_CFG_GETA_ENABLED != 'true') || (KN_CFG_GETA_SEARCH_ENABLED != 'true')) {
    tep_redirect(tep_href_link(FILENAME_ADVANCED_SEARCH));
  }
  
  // 連想検索
  $no_results = true;
  if ($_POST['search_text']) {
    // 検索文ファイルの作成
    $g_search_text = $_POST['search_text'];
    $g_search_text = str_replace("\r\n", "\r", $g_search_text);
    $g_search_text = str_replace("\r", "\n", $g_search_text);
    $g_search_text = str_replace("\n", "", $g_search_text);
    $g_search_text = mb_convert_kana($g_search_text, "AsK");
    $g_search_text = mbereg_replace("[" . $replace_symbol . "]", " ", $g_search_text);
    foreach ($replace_array as $value) {
      if ($value) $g_search_text = mbereg_replace("[" . $value . "]", " ", $g_search_text);
    }
    //$g_search_text = escapeshellcmd($g_search_text);
    $filename_search_text = @tempnam(DIR_FS_GETA_WORK, "GS_");
    $search_data = fopen($filename_search_text, "w");
    fwrite($search_data, $g_search_text);
    fclose($search_data);
    // 商品WAMのフレーズ検索
    $max = KN_CFG_GETA_SEARCH_RESULTS_MAX;
    if ($_POST['search_start']) {
      $start = (int)$_POST['search_start'];
    } else {
      $start = 1;
    }
    $results_count = 0;
    $s_result_products = array();
    $s_result = popen("perl " . FILENAME_S_GETA_SEARCH . " " . $filename_search_text . " " . TEXT_P_HANDLE_ID . " " . FILENAME_SEARCH_JP . " " . $max . " " . $start . " 2>&1", "r");
    while ( !feof($s_result) ) {
      $s_result_data = fgetcsv($s_result, 1000, "\t");
      if (empty($s_result_data[0])) {
        break;
      }
      $search_pID = $s_result_data[0];
      $search_products = array();
      $search_products['pID'] = $search_pID;
      $search_products_array = explode(',', $s_result_data[2]);
      foreach ($search_products_array as $value) {
        if ($value) {
          list($item, $content) = explode('<>', $value);
          $search_products[$item] = $content;
        }
      }
      $list_num = $start + $results_count;
      $search_products['list_num'] = $list_num;
      
      $s_result_products[] = $search_products;
      $no_results = false;
      $results_count++;
    }
    pclose($s_result);
    @unlink($filename_search_text);
  }

  $breadcrumb->add(NAVBAR_TITLE1, tep_href_link(FILENAME_ADVANCED_SEARCH));
  
?>
<!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" />
<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" />
<?php 
  $customCSS = substr(FILENAME_GETA_SEARCH_RESULT, 0, strrpos(FILENAME_GETA_SEARCH_RESULT, '.')). ".css";
  if (file_exists($customCSS)){
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$customCSS\" 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> 
<h2 class="pageHeading"><?php echo HEADING_TITLE; ?></h2>
<div id="getaSearch">
<?php echo tep_draw_form('geta_search', tep_href_link(FILENAME_GETA_SEARCH_RESULT, '', 'NONSSL', false)) . "\n"; ?>
<p><?php echo tep_draw_textarea_field('search_text', 'soft', '', '5'); ?></p>
<p class="buttonArea"><?php echo '<input type="submit" value="' . BOX_GETA_SEARCH_SUBMIT . '" /><input type="reset" value="' . BOX_GETA_SEARCH_RESET . '" />'; ?></p>
</form>
<?php
  if (!$no_results) {
    echo '<p>' . sprintf(TEXT_RESULTS_COUNT, $start, $start + $results_count - 1) . '</p>' . "\n";
    
    echo '<table class="listBox">' . "\n";
    for ($i=0, $n=sizeof($s_result_products); $i<$n; $i++) {
      if (($s_result_products[$i]['list_num']/2) == floor($s_result_products[$i]['list_num']/2)) {
        $geta_row_style = 'even';
      } else {
        $geta_row_style = 'odd';
      }
      echo '<tr class="' . $geta_row_style . '">' . "\n";
      echo '<td class="num">' . $s_result_products[$i]['list_num'] . '</td>' . "\n";
      echo '<td><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $s_result_products[$i]['pID']) . '"><span class="pName">' . $s_result_products[$i]['products_name'] . '</span></a>';
      echo (($s_result_products[$i]['products_subtitle']) ? '<span class="pSubtitle">' . $s_result_products[$i]['products_subtitle'] . '</span> ' : '') . 
             (($s_result_products[$i]['products_series_title']) ? '<span class="pSeriesTitle">' . $s_result_products[$i]['products_series_title'] . '</span> ' : '') . "<br />\n" . 
             (($s_result_products[$i]['products_author']) ? '<span class="pAuthor">' . $s_result_products[$i]['products_author'] . '</span> ' : '') . 
             (($s_result_products[$i]['products_publishers']) ? '<span class="pPublishers">' . $s_result_products[$i]['products_publishers'] . '</span> ' : '') . 
             (($s_result_products[$i]['products_volume']) ? '<span class="pVolume">' . $s_result_products[$i]['products_volume'] . '</span> ' : '') . 
             (($s_result_products[$i]['products_published']) ? '<span class="pPublished">' . $s_result_products[$i]['products_published'] . '</span> ' : '') . 
             (($s_result_products[$i]['products_description']) ? '<span class="pDescription">' . $s_result_products[$i]['products_description'] . '</span> ' : '') . 
             (($s_result_products[$i]['products_isbn']) ? '<span class="pIsbn">(' . $s_result_products[$i]['products_isbn'] . ')</span>' : '') . 
             '</td>' . "\n";
      echo '<td class="pPrice">';
      if (tep_not_null($s_result_products[$i]['specials_new_products_price'])) {
        echo '<del>' .  $currencies->display_price($s_result_products[$i]['products_price'], tep_get_tax_rate($s_result_products[$i]['products_tax_class_id'])) . '</del><br /><span class="specialPrice">' . $currencies->display_price($s_result_products[$i]['specials_new_products_price'], tep_get_tax_rate($s_result_products[$i]['products_tax_class_id'])) . '</span>';
      } else {
        echo $currencies->display_price($s_result_products[$i]['products_price'], tep_get_tax_rate($s_result_products[$i]['products_tax_class_id']));
      }
      echo "</td>\n";
      echo "</tr>\n";
    }
    echo "</table>\n";
    if ($start > 1) {
      if ($start <= $max) {
        $prev_start = 1;
      } else {
        $prev_start = $start - $max;
      }
      echo '<p class="buttonPrev">' . tep_draw_form('results_prev', FILENAME_GETA_SEARCH_RESULT) . tep_draw_hidden_field('search_start', $prev_start) . tep_draw_hidden_field('search_text', $_POST['search_text']) . tep_image_submit('button_geta_results_prev.gif', IMAGE_BUTTON_GETA_RESULTS_PREV) . "</form></p>\n";
    }
    if ($results_count == $max) {
      $more_start = $start + $max;
      echo '<p class="buttonMore">' . tep_draw_form('results_more', FILENAME_GETA_SEARCH_RESULT) . tep_draw_hidden_field('search_start', $more_start) . tep_draw_hidden_field('search_text', $_POST['search_text']) . tep_image_submit('button_geta_results_more.gif', IMAGE_BUTTON_GETA_RESULTS_MORE) . "</form></p>\n";
    }
  } else {
    echo '<p class="noData">' . TEXT_NO_RESULTS . '</p>' . "\n";
  }
?>
<p class="getaLogo"><a href="http://geta.ex.nii.ac.jp/" target="_blank"><?php echo tep_image(DIR_WS_IMAGES . 'geta.gif', TEXT_GETA_LOGO); ?></a></p>
<div class="buttonLine">
<p class="buttonLeft"><?php echo '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH) . '">' . tep_image_button('button_back.gif', IMAGE_BUTTON_BACK) . '</a>'; ?></p>
</div>
</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'); ?>
