<?php
/*
  products_uploader.php
  作成日：2004/12/2
　最終更新日：2006/1/31

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

  Released under the GNU General Public License
*/
  
  require('includes/application_top.php');
  
  require('includes/products_uploader_config.php');
  
  // 設定情報の取得＆更新
  if ($_POST['specify_id']) {
    $update = array('upfile_mode' => $_POST['specify_id']);
    $ini_data = inifile_setup($update);
  } else {
    $ini_data = inifile_setup();
  }
  
  if ($_GET['action']) {
    switch ($_GET['action']) {
      // アップロードステップ１：アップロードの設定確認
      case 'upload_confirm01':
      case 'upload_config':
        // アップロードファイルの件数確認、文字コード変換
        if ($_GET['action'] == 'upload_confirm01') {
          $uploadfile = tep_get_uploaded_file('uploadfile');
          $upfile_count = 0;
          if (Is_Uploaded_File($uploadfile['tmp_name'])) {
            $euc_file = fopen(FILENAME_UPLOAD_TEMPFILE01, 'w');
            $up_file = fopen($uploadfile['tmp_name'], 'r');
            fwrite($euc_file, $uploadfile['name'] . "\n");			// アップロード履歴用にファイル名を記録
            while (!feof($up_file)) {
              $upfile_data = fgets($up_file, $ini_data['upfile_size']);
              if (empty($upfile_data)) {
                continue;							// 空白行は読み飛ばす
              }
              if ($_POST['encoding'] == 'SJIS') {
                mb_substitute_character(0x3013);				// 無効な文字の置換
                mb_convert_variables("EUC-JP", "SJIS", $upfile_data);	// SJIS　⇒　EUC　変換
              }
              $upfile_data = rtrim($upfile_data) . "\n";		// MAC改行対応。別途 auto_detect_line_endings のonが必要
              fwrite($euc_file, $upfile_data);					// 一時ファイルに書き出し
              $upfile_count++;
            }
            fclose($up_file);
            fclose($euc_file);
          } else {
            // アップロードファイルがないとき
            $messageStack->add_session(ERROR_UPLOADFILE_DOES_NOT_EXIST, 'error');
            tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
          }
        }

        // アップロード設定ファイルの更新
        if (($_GET['action'] == 'upload_config') && ($_POST)) {
          rename(FILENAME_UPLOAD_CONFIG, FILENAME_UPLOAD_CONFIG_BACKUP);    // バックアップの作成
          $upload_cfg_file = @fopen(FILENAME_UPLOAD_CONFIG, 'w');
          if ($upload_cfg_file) {
            for ($i = 0, $n = sizeof($_POST['cfgKey']); $i < $n; $i++) {
              $update_cfg_data = '';
              $update_cfg_data = $_POST['cfgSort'][$i] . ',' . 
                                       $_POST['cfgKey'][$i] . ',' . 
                                       $_POST['cfgSelect'][$i] . ',' . 
                                       $_POST['cfgDefault'][$i] . ',' . 
                                       $_POST['cfgMin'][$i] . ',' . 
                                       $_POST['cfgMax'][$i] . ',' . 
                                       $_POST['cfgRequired'][$i] . "\n";
              fWrite($upload_cfg_file, $update_cfg_data);
            }
          }
          fclose($upload_cfg_file);
          $messageStack->add(SUCCESS_UPLOAD_CONFIG_UPDATED, 'success');
        }

        // アップロード設定ファイルのチェック
        $cfg_data = array();
        $cfg_data_row = 0;
        $cfg_title = '';
        $cfg_table = '';
        $cfg_field = '';
        $cfg_type = '';
        $cfg_error = array();
        $cfg_error_flg = false;
        $cfg_key_check = array();
        $sort_order_check = array();
        $upload_cfg_file = @fopen(FILENAME_UPLOAD_CONFIG, 'r');
        if ($upload_cfg_file) {
          while (!feof($upload_cfg_file)) {
            $cfg_rec = fgetcsv($upload_cfg_file,200);
            if (empty($cfg_rec)) {
              continue;								// 空白行は読み飛ばす
            }
            $cfg_data_row++;
            // 並び順の不備チェック
            if (($cfg_rec[0] == '') || (!is_numeric($cfg_rec[0]))) {
              $cfg_error_flg = true;
              $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_CONFIG_COLUMN_IS_EMPTY, TEXT_UPLOAD_CONFIG_COLUMN_00);
            } else {
              if ($cfg_rec[0] > 0) {
                // 並び順の重複チェック
                $duplicate_sort_order = array_search($cfg_rec[0], $sort_order_check);
                if (($duplicate_sort_order === null) || ($duplicate_sort_order === false)) {
                  $sort_order_check[] = $cfg_rec[0];
                } else {
                  $sort_order_check[] = $cfg_rec[0];
                	$cfg_error_flg = true;
                  $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_CONFIG_SORT_ORDER_DUPLICATE, $cfg_rec[0]);
                }                
              }
            }
            // データ名の不備チェック
            if ($cfg_rec[1] == '') {
              $cfg_error_flg = true;
              $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_CONFIG_COLUMN_IS_EMPTY, TEXT_UPLOAD_CONFIG_COLUMN_01);
            } else {
              // データ名の重複チェック
              $duplicate_key = array_search($cfg_rec[1], $cfg_key_check);
              if (($duplicate_key === null) || ($duplicate_key === false)) {
                $cfg_key_check[] = $cfg_rec[1];
                // データ名とフィールド名のマッチング
                $data_match = false;
                for ($i = 0, $n = sizeof($field_check); $i < $n; $i++) {
                  if ($cfg_rec[1] == $field_check[$i]['id']) {
                    $data_match = true;
                    $cfg_title = $field_check[$i]['title'];
                    $cfg_table = $field_check[$i]['table'];
                    $cfg_field = $field_check[$i]['field'];
                    $cfg_type = $field_check[$i]['type'];
                    // 実際のフィールド定義をチェック
                    $field_match = false;
                    $fields_query = tep_db_query('show columns from ' . $cfg_table);
                    while ($fields = tep_db_fetch_array($fields_query)) {
                      if ($cfg_field == $fields['Field']) {
                        $field_match = true;
                        if ((strlen($fields['Default']) == 0) && ($fields['Null'] != 'YES')) {
                          if ((strlen($cfg_rec[3]) == 0) && ($cfg_rec[6] != '*')) {
                             // 必須項目に必須チェックがなく、初期値が設定されていない場合、エラーとする。
                             $cfg_error_flg = true;
                             $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_FIELD_IS_REQUIRED, $cfg_field);
                          }
                        }
                      }
                    }
                    if (!$field_match) {
                      // 実際のフィールドがない場合、エラーとする。
                      $cfg_title = '';
                      $cfg_table = '';
                      $cfg_field = '';
                      $cfg_type = '';
                      $cfg_error_flg = true;
                      $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_FIELD_DOES_NOT_EXIST, $cfg_field);
                    }
                  }
                }
                if (!$data_match) {
                  // 設定があるデータ（field_checkにあるデータ）でない場合、エラーとする。
                  $cfg_error_flg = true;
                  $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_DATA_DOES_NOT_EXIST, $cfg_rec[1]);
                }
              } else {
                // 設定ファイルのデータが重複している場合、エラーとする。
                $cfg_key_check[] = $cfg_rec[1];
                $cfg_error_flg = true;
                $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_CONFIG_ID_DUPLICATE, $cfg_rec[1]);
              }                
            }
            // 設定データをまとめる。　行番号、項目名、並び順、指定方法、初期値、テーブル名、フィールド名、データタイプ、最小桁数、最大桁数、必須フラグ、データ名
            $cfg_data[] = array($cfg_data_row, $cfg_title, $cfg_rec[0], $cfg_rec[2], $cfg_rec[3], $cfg_table, $cfg_field, $cfg_type, $cfg_rec[4], $cfg_rec[5], $cfg_rec[6], $cfg_rec[1]);
          }
          fclose($upload_cfg_file);
        } else {
          // アップロード設定ファイルがない場合は、初期画面へ
          tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
        }
        break;
        
      case 'upload_confirm02':
        // アップロードステップ２：アップロードデータの内容確認
        // チェック用カテゴリーリスト作成
        $categories_id_array = array();
        $categories_query = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id='" . tep_db_input($_POST['language_id']) ."'");
        while ($categories = tep_db_fetch_array($categories_query)) {
          $categories_id_array[$categories['categories_id']] = $categories['categories_name'];
        }
        // チェック用メーカーリスト作成
        $manufacturers_id_array = array();
        $manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS);
        while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
          $manufacturers_id_array[$manufacturers['manufacturers_id']] = $manufacturers['manufacturers_name'];
        }
        // 設定ファイルの読み込み
        $cfg_data = array();
        $cfg_data_row = 0;
        $upload_cfg_file = @fopen(FILENAME_UPLOAD_CONFIG, 'r');
        if ($upload_cfg_file) {
          while (!feof($upload_cfg_file)) {
            $cfg_rec = fgetcsv($upload_cfg_file, 200);
            if (empty($cfg_rec)) {
              continue;								// 空白行は読み飛ばす
            }
            $cfg_data_row++;
            for ($i = 0, $n = sizeof($field_check); $i < $n; $i++) {
              if ($cfg_rec[1] == $field_check[$i]['id']) {
                $cfg_title = $field_check[$i]['title'];
                $cfg_table = $field_check[$i]['table'];
                $cfg_field = $field_check[$i]['field'];
                $cfg_type = $field_check[$i]['type'];
              }
            }
            // 設定データをまとめる。　行番号、項目名、並び順、指定方法、初期値、テーブル名、フィールド名、データタイプ、最小桁数、最大桁数、必須フラグ、データ名
            $cfg_data[] = array($cfg_data_row, $cfg_title, $cfg_rec[0], $cfg_rec[2], $cfg_rec[3], $cfg_table, $cfg_field, $cfg_type, $cfg_rec[4], $cfg_rec[5], $cfg_rec[6], $cfg_rec[1]);
          }
          // アップロードデータ（一時ファイル）の読み込み
          $ok_file = fopen(FILENAME_UPLOAD_TEMPFILE02, 'w');
          $up_file = fopen(FILENAME_UPLOAD_TEMPFILE01, 'r');
          $upload_filename = trim(fgets($up_file, 100));
          fwrite($ok_file, $upload_filename);					// アップロード履歴用にファイル名を記録
          $up_rec_count = 0;
          $up_rec_ok_count = 0;
          $up_rec_err_count = 0;
          $up_rec_err_array = array();
          while (!feof($up_file)) {
            $up_rec = fgetcsv($up_file, $ini_data['upfile_size'], $ini_data['upfile_delimiter']);
            if (empty($up_rec)) {
              continue;								// 空白行は読み飛ばす
            }
            $up_rec_count++;
            $up_rec_err_flg = false;
            $ok_rec = array();
            for ($i = 0, $n = sizeof($cfg_data); $i < $n; $i++) {
              if ($cfg_data[$i][2] == 0) {					// 並び順が0のものはCSVファイルにないデータ
                // 初期値をセット
                $ok_rec[$i] = $cfg_data[$i][4];
              } else {
                $up_rec_idx = $cfg_data[$i][2] - 1;				// インデックスの修正（０から始まるように）
                if (isset($up_rec[$up_rec_idx])) {
                	// CSVにデータがあったらそれを、なかったら設定ファイルの初期値をセット
                	if ($up_rec[$up_rec_idx] == '') {
                	  $ok_rec[$i] = $cfg_data[$i][4];
                	} else {
                	  $ok_rec[$i] = $up_rec[$up_rec_idx];
                	}
                } else {
                  // 該当並び順のアップロードデータがないとき
                  $up_rec_err_flg = true;
                  $up_rec_err_count++;
                	if (is_array($up_rec)) {
                	  $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_DOSE_NOT_EXIST);
                	} else {
                	  $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_DOSE_NOT_EXIST);
                	}
                	continue;
                }
              }
        	     // 項目チェック開始
              // 必須チェック
              if (($cfg_data[$i][10] == '*') && ($ok_rec[$i] == '')) {
                $up_rec_err_flg = true;
                $up_rec_err_count++;
                if (is_array($up_rec)) {
                  $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_REQUIRED);
                } else {
                  $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_REQUIRED);
                }
                continue;
        	     } else {
        	       // 管理コードをキーに使用する場合は、必須チェックをする。
        	       if (($cfg_data[$i][6] == 'products_model') && (($ini_data['upfile_mode'] == 'yes_ignore') || ($ini_data['upfile_mode'] == 'yes_replace')) && ($ok_rec[$i] == '')) {
                  $up_rec_err_flg = true;
                  $up_rec_err_count++;
                  if (is_array($up_rec)) {
                    $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_REQUIRED);
                  } else {
                    $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_REQUIRED);
                  }
                  continue;
        	       }
        	     }
              if ($ok_rec[$i] != '') {
                // categories_idのチェック
                if (($cfg_data[$i][6] == 'categories_id') && ($cfg_data[$i][3] == 'NAME')) {
                  $category_match = array_search($ok_rec[$i], $categories_id_array);
                  if (($category_match === null) || ($category_match === false)) {
                    $up_rec_err_flg = true;
                    $up_rec_err_count++;
                    if (is_array($up_rec)) {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_CATEGORY_DOES_NOT_EXIST . $ok_rec[$i]);
                    } else {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_CATEGORY_DOES_NOT_EXIST . $ok_rec[$i]);
                    }
                    continue;
                  } else {
                    $ok_rec[$i] = $category_match;		// カテゴリー名に対応したcategories_idをセット
                  }
                }
                if (($cfg_data[$i][6] == 'categories_id') && ($cfg_data[$i][3] == 'ID')) {
                  if (!isset($categories_id_array[(int)$ok_rec[$i]])) {
                    $up_rec_err_flg = true;
                    $up_rec_err_count++;
                    if (is_array($up_rec)) {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_CATEGORY_DOES_NOT_EXIST . $ok_rec[$i]);
                    } else {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_CATEGORY_DOES_NOT_EXIST . $ok_rec[$i]);
                    }
                    continue;
                  }
                }
                // manufacturers_idのチェック
                if (($cfg_data[$i][6] == 'manufacturers_id') && ($cfg_data[$i][3] == 'NAME')) {
                  $manufacturer_match = array_search($ok_rec[$i], $manufacturers_id_array);
                  if (($manufacturer_match === null) || ($manufacturer_match === false)) {
                    $up_rec_err_flg = true;
                    $up_rec_err_count++;
                    if (is_array($up_rec)) {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_MANUFACTURER_DOES_NOT_EXIST . $ok_rec[$i]);
                    } else {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_MANUFACTURER_DOES_NOT_EXIST . $ok_rec[$i]);
                    }
                    continue;
                  } else {
                    $ok_rec[$i] = $manufacturer_match;		// メーカー名に対応したmanufacturer_idをセット
                  }
                }
                if (($cfg_data[$i][6] == 'manufacturers_id') && ($cfg_data[$i][3] == 'ID')) {
                  if (!isset($manufacturers_id_array[$ok_rec[$i]])) {
                    $up_rec_err_flg = true;
                    $up_rec_err_count++;
                    if (is_array($up_rec)) {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_MANUFACTURER_DOES_NOT_EXIST . $ok_rec[$i]);
                    } else {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_MANUFACTURER_DOES_NOT_EXIST . $ok_rec[$i]);
                    }
                    continue;
                  }
                }
                // 共通チェック項目
        	       if (($cfg_data[$i][8] != '') && (strlen($ok_rec[$i]) < $cfg_data[$i][8])) {
        	         // 最小桁数チェック
                  $up_rec_err_flg = true;
                  $up_rec_err_count++;
                  $short_length = $cfg_data[$i][8] - strlen($ok_rec[$i]);
                  if (is_array($up_rec)) {
                    $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_SHORTAGE . $short_length);
                  } else {
                    $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_SHORTAGE . $short_length);
                  }
                } elseif (($cfg_data[$i][9] != '') && (strlen($ok_rec[$i]) > $cfg_data[$i][9])) {
                  // 最大桁数チェック
                  $up_rec_err_flg = true;
                  $up_rec_err_count++;
                  $over_length = strlen($ok_rec[$i]) - $cfg_data[$i][9];
                  if (is_array($up_rec)) {
                    $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_OVERFLOW . $over_length);
                  } else {
                    $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_OVERFLOW . $over_length);
                  }
                } elseif (($cfg_data[$i][7] == 'N') && (!is_numeric($ok_rec[$i]))) {
                  // データタイプチェック（数値）
                  if (is_numeric(str_replace(',', '', $ok_rec[$i]))) {
                    // データタイプチェック（数値カンマ対応）
                    $ok_rec[$i] = str_replace(',', '', $ok_rec[$i]);
                  } else {
                    $up_rec_err_flg = true;
                    $up_rec_err_count++;
                    if (is_array($up_rec)) {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_NOT_NUMERIC . $ok_rec[$i]);
                    } else {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_NOT_NUMERIC . $ok_rec[$i]);
                    }
                  }
                } elseif ($cfg_data[$i][7] == 'D') {
                  if ((!ereg("^([0-9]+)-([0-9]+)-([0-9]+)$", $ok_rec[$i], $date_part)) || (!checkdate($date_part[2], $date_part[3], $date_part[1]))) {
                    // データタイプチェック（日付）
                    $up_rec_err_flg = true;
                    $up_rec_err_count++;
                    if (is_array($up_rec)) {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], implode($ini_data['upfile_delimiter'], $up_rec), $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_NOT_DATE . $ok_rec[$i]);
                    } else {
                      $up_rec_err_array[] = array($up_rec_count, $cfg_data[$i][2], $up_rec, $cfg_data[$i][1], ERROR_UPLOAD_DATA_IS_NOT_DATE . $ok_rec[$i]);
                    }
                  }
                }
              $ok_rec[$i] = '"' . $ok_rec[$i] . '"';
              }
            }
            if (!$up_rec_err_flg) {
              $ok_data = "\n" . implode($ini_data['upfile_delimiter'], $ok_rec);			// １行目にアップロード履歴用ファイル名を格納しているので、改行を先にセットする。
              fwrite($ok_file, $ok_data);
              $up_rec_ok_count++;
            }
          }
          fclose($up_file);
          fclose($ok_file);
        } else {
          // 設定ファイルがないとき
          tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
        }
        fclose($upload_cfg_file);
        break;

      case 'upload':
        // 設定ファイルの読み込み
        $cfg_data = array();
        $cfg_data_row = 0;
        $upload_cfg_file = @fopen(FILENAME_UPLOAD_CONFIG, 'r');
        if ($upload_cfg_file) {
          while (!feof($upload_cfg_file)) {
            $cfg_rec = fgetcsv($upload_cfg_file, 200);
            if (empty($cfg_rec)) {
              continue;								// 空白行は読み飛ばす
            }
            $cfg_data_row++;
            for ($i = 0, $n = sizeof($field_check); $i < $n; $i++) {
              if ($cfg_rec[1] == $field_check[$i]['id']) {
                $cfg_title = $field_check[$i]['title'];
                $cfg_table = $field_check[$i]['table'];
                $cfg_field = $field_check[$i]['field'];
                $cfg_type = $field_check[$i]['type'];
              }
            }
            // 設定データをまとめる。　行番号、項目名、並び順、指定方法、初期値、テーブル名、フィールド名、データタイプ、最小桁数、最大桁数、必須フラグ、データ名
            $cfg_data[] = array($cfg_data_row, $cfg_title, $cfg_rec[0], $cfg_rec[2], $cfg_rec[3], $cfg_table, $cfg_field, $cfg_type, $cfg_rec[4], $cfg_rec[5], $cfg_rec[6], $cfg_rec[1]);
          }
        } else {
          // 設定ファイルがないとき
          tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
        }
        fclose($upload_cfg_file);
        
        if (File_Exists(FILENAME_UPLOAD_TEMPFILE02)) {
          $insert_ok_count = 0;
          $upload_date = date("Y-m-d H:i:s");			// 一括登録の登録日時用
          $up_file = fopen(FILENAME_UPLOAD_TEMPFILE02, 'r');
          $upload_filename = trim(fgets($up_file, 100));
          while (!feof($up_file)) {
            $up_rec = fgetcsv($up_file, $ini_data['upfile_size'], $ini_data['upfile_delimiter']);
            $up_rec = str_replace("\\n", "\n", $up_rec);
            for ($i = 0, $n = sizeof($cfg_data); $i < $n; $i++) {
              if ($cfg_data[$i][5] == 'products') {
                if (($cfg_data[$i][6] == 'products_model') && (($ini_data['upfile_mode'] == 'yes_ignore') || ($ini_data['upfile_mode'] == 'yes_replace'))) {
                	$specified_id = tep_db_prepare_input($up_rec[$i]);
                }
                $p_data_array[$cfg_data[$i][6]] = tep_db_prepare_input($up_rec[$i]);
              } elseif ($cfg_data[$i][5] == 'products_to_categories') {
                $p2c_data_array[$cfg_data[$i][6]] = tep_db_prepare_input($up_rec[$i]);
              } elseif ($cfg_data[$i][5] == 'products_description') {
                $pd_data_array[$cfg_data[$i][6]] = tep_db_prepare_input($up_rec[$i]);
              }
            }
            if (($ini_data['upfile_mode'] == 'yes_ignore') || ($ini_data['upfile_mode'] == 'yes_replace')) {
              // まだ同じキーのデータがない場合
              if (tep_db_num_rows(tep_db_query("select * from " . TABLE_PRODUCTS . " where products_id = '" . tep_db_input($specified_id) . "'")) == 0) {
                // yes_ignore、yes_replaceともinsert
                // productsテーブルへの登録
                $insert_p_data = array('products_id' => $specified_id,
                                             'products_date_added' => $upload_date);
                $sql_data_array = tep_array_merge($p_data_array, $insert_p_data);
                tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
                // products_to_categoriesテーブルへの登録
                $insert_p2c_data = array('products_id' => $specified_id);
                $sql_data_array = tep_array_merge($p2c_data_array, $insert_p2c_data);
                tep_db_perform(TABLE_PRODUCTS_TO_CATEGORIES, $sql_data_array);
                // products_descriptionテーブルへの登録
                $insert_pd_data = array('products_id' => $specified_id,
                                              'language_id' => tep_db_prepare_input($_POST['language_id']));
                $sql_data_array = tep_array_merge($pd_data_array, $insert_pd_data);
                tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array);
                $insert_ok_count++;
              } else {
                // 既に同じキーのデータがある場合
                if ($ini_data['upfile_mode'] == 'yes_replace') {
                  // yes_replaceの場合は、既存のデータをdeleteしてからinsert　yes_ignoreの場合はなにもしない
                  // productsテーブルへの更新
                  $update_p_data = array('products_date_added' => $upload_date);
                  $sql_data_array = tep_array_merge($p_data_array, $update_p_data);
                  tep_db_perform(TABLE_PRODUCTS, $sql_data_array, 'update', 'products_id = \'' . tep_db_input($specified_id) . '\'');
                  // products_to_categoriesテーブルはreplace
                  $categories_id = $p2c_data_array['categories_id'];
                  tep_db_query("replace into " . TABLE_PRODUCTS_TO_CATEGORIES . " values ('" . tep_db_input($specified_id) . "', '" . tep_db_input($categories_id) . "')");
                  // products_descriptionテーブルへの更新
                  $update_pd_data = array('language_id' => tep_db_prepare_input($_POST['language_id']));
                  $sql_data_array = tep_array_merge($pd_data_array, $update_pd_data);
                  tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array, 'update', 'products_id = \'' . tep_db_input($specified_id) . '\'');
                  $insert_ok_count++;
                }
              }
            } else {
              // no_useの場合はそのままinsert
              // productsテーブルへの登録
              $insert_p_data = array('products_date_added' => $upload_date);
              $sql_data_array = tep_array_merge($p_data_array, $insert_p_data);
              tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
              // products_to_categoriesテーブルへの登録
              $products_id = tep_db_insert_id();
              $insert_p2c_data = array('products_id' => $products_id);
              $sql_data_array = tep_array_merge($p2c_data_array, $insert_p2c_data);
              tep_db_perform(TABLE_PRODUCTS_TO_CATEGORIES, $sql_data_array);
              // products_descriptionテーブルへの登録
              $insert_pd_data = array('products_id' => $products_id,
                                            'language_id' => tep_db_prepare_input($_POST['language_id']));
              $sql_data_array = tep_array_merge($pd_data_array, $insert_pd_data);
              tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array);
              $insert_ok_count++;
            }
          }
        fclose($up_file);
        @unlink(FILENAME_UPLOAD_TEMPFILE01);
        @unlink(FILENAME_UPLOAD_TEMPFILE02);
        }

        // アップロード履歴の記録
        $historydata = @file(FILENAME_UPLOAD_HISTORY);
        if (is_array($historydata)) {
          array_unshift($historydata, $upload_date . ',"' . $upload_filename . '",' . $insert_ok_count . "\n");
        } else {
          $historydata[0] = $upload_date . ',' . $upload_filename . ',' . $insert_ok_count . "\n";
          $historydata[1] = "---,---,0\n";
          $historydata[2] = "---,---,0\n";
          $historydata[3] = "---,---,0\n";
          $historydata[4] = "---,---,0\n";
        }
        $historyfile = fopen(FILENAME_UPLOAD_HISTORY, 'w');
        for ($i = 0, $n = 5; $i < $n; $i++) {
          fwrite($historyfile, $historydata[$i]);
        }
        fclose($historyfile);
          
        if (USE_CACHE == 'true') {
          tep_reset_cache_block('categories');
          tep_reset_cache_block('also_purchased');
        }
        break;
        
      case 'download_confirm':
      case 'download_config':
        // ダウンロード設定ファイルの更新
        if ($_GET['action'] == 'download_config') {
          rename(FILENAME_DOWNLOAD_CONFIG, FILENAME_DOWNLOAD_CONFIG_BACKUP);    // バックアップの作成
          $download_cfg_file = @fopen(FILENAME_DOWNLOAD_CONFIG, 'w');
          if ($download_cfg_file) {
            for ($i = 0, $n = sizeof($_POST['cfgKey']); $i < $n; $i++) {
              $update_cfg_data = '';
              $update_cfg_data = $_POST['cfgSort'][$i] . ',' . 
                                       $_POST['cfgKey'][$i] . ',' . 
                                       $_POST['cfgSelect'][$i] . "\n";
              fWrite($download_cfg_file, $update_cfg_data);
            }
          }
          fclose($download_cfg_file);
          $messageStack->add(SUCCESS_DOWNLOAD_CONFIG_UPDATED, 'success');
        }
        
        // ダウンロード設定ファイルの読み込み
        $cfg_data = array();
        $cfg_data_row = 0;
        $cfg_title = '';
        $cfg_table = '';
        $cfg_field = '';
        $cfg_type = '';
        $cfg_error = array();
        $cfg_error_flg = false;
        $cfg_key_check = array();
        $sort_order_check = array();
        $download_cfg_file = @fopen(FILENAME_DOWNLOAD_CONFIG, 'r');
        if ($download_cfg_file) {
          while (!feof($download_cfg_file)) {
            $cfg_rec = fgetcsv($download_cfg_file, 200);
            if (empty($cfg_rec)) {
              continue;								// 空白行は読み飛ばす
            }
            $cfg_data_row++;
            // 並び順の不備チェック
            if (($cfg_rec[0] == '') || (!is_numeric($cfg_rec[0]))) {
              $cfg_error_flg = true;
              $cfg_error[] = array($cfg_data_row, ERROR_DOWNLOAD_CONFIG_COLUMN_IS_EMPTY, TEXT_DOWNLOAD_CONFIG_COLUMN_00);
            } else {
              if ($cfg_rec[0] > 0) {
                // 並び順の重複チェック
                $duplicate_sort_order = array_search($cfg_rec[0], $sort_order_check);
                if (($duplicate_sort_order === null) || ($duplicate_sort_order === false)) {
                  $sort_order_check[] = $cfg_rec[0];
                } else {
                  $sort_order_check[] = $cfg_rec[0];
                	$cfg_error_flg = true;
                  $cfg_error[] = array($cfg_data_row, ERROR_DOWNLOAD_CONFIG_SORT_ORDER_DUPLICATE, $cfg_rec[0]);
                }                
              }
            }
            // データ名の不備チェック
            if ($cfg_rec[1] == '') {
              $cfg_error_flg = true;
              $cfg_error[] = array($cfg_data_row, ERROR_UPLOAD_CONFIG_COLUMN_IS_EMPTY, TEXT_UPLOAD_CONFIG_COLUMN_01);
            } else {
              // データ名の重複チェック
              $duplicate_key = array_search($cfg_rec[1], $cfg_key_check);
              if (($duplicate_key === null) || ($duplicate_key === false)) {
                $cfg_key_check[] = $cfg_rec[1];
                // データ名とフィールド名のマッチング
                $data_match = false;
                for ($i = 0, $n = sizeof($field_check); $i < $n; $i++) {
                  if ($cfg_rec[1] == $field_check[$i]['id']) {
                    $data_match = true;
                    $cfg_title = $field_check[$i]['title'];
                    $cfg_table = $field_check[$i]['table'];
                    $cfg_field = $field_check[$i]['field'];
                    // 実際のフィールド定義をチェック
                    $field_match = false;
                    $fields_query = tep_db_query('show columns from ' . $cfg_table);
                    while ($fields = tep_db_fetch_array($fields_query)) {
                      if ($cfg_field == $fields['Field']) {
                        $field_match = true;
                      }
                    }
                    if (!$field_match) {
                      // 実際のフィールドがない場合、エラーとする。
                      $cfg_title = '';
                      $cfg_table = '';
                      $cfg_field = '';
                      $cfg_type = '';
                      $cfg_error_flg = true;
                      $cfg_error[] = array($cfg_data_row, ERROR_DOWNLOAD_FIELD_DOES_NOT_EXIST, $cfg_field);
                    }
                  }
                }
                if (!$data_match) {
                  // 設定があるデータ（field_checkにあるデータ）でない場合、エラーとする。
                  $cfg_error_flg = true;
                  $cfg_error[] = array($cfg_data_row, ERROR_DOWNLOAD_DATA_DOES_NOT_EXIST, $cfg_rec[1]);
                }
              } else {
                // 設定ファイルのデータが重複している場合、エラーとする。
                $cfg_key_check[] = $cfg_rec[1];
                $cfg_error_flg = true;
                $cfg_error[] = array($cfg_data_row, ERROR_DOWNLOAD_CONFIG_ID_DUPLICATE, $cfg_rec[1]);
              }                
            }
            // 設定データをまとめる。　行番号、項目名、並び順、指定方法、テーブル名、フィールド名、データ名
            $cfg_data[] = array($cfg_data_row, $cfg_title, $cfg_rec[0], $cfg_rec[2], $cfg_table, $cfg_field, $cfg_rec[1]);
          }
        } else {
          // 設定ファイルがないとき
          tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
        }
        fclose($download_cfg_file);
        break;

      case 'download':
        // ヘッダーの変更
        header("Content-disposition: attachment; filename=" . FILENAME_DOWNLOAD_FILE);
        header("Content-type: application/octet-stream; name=" . FILENAME_DOWNLOAD_FILE);
        // ダウンロード設定ファイルの読み込み
        $cfg_data = array();
        $cfg_data_row = 0;
        $select_column_list = '';
        $csv_count_max = 0;
        $cname_flg = false;
        $mname_flg = false;
        $download_cfg_file = @fopen(FILENAME_DOWNLOAD_CONFIG, 'r');
        if ($download_cfg_file) {
          while (!feof($download_cfg_file)) {
            $cfg_rec = fgetcsv($download_cfg_file, 200);
            if (empty($cfg_rec)) {
              continue;								// 空白行は読み飛ばす
            }
            $cfg_data_row++;
            $cfg_field = '';
            for ($i = 0, $n = sizeof($field_check); $i < $n; $i++) {
              if ($cfg_rec[1] == $field_check[$i]['id']) {
                $cfg_field = $field_check[$i]['field'];
                if (($cfg_field == 'categories_id') && ($cfg_rec[2] == 'NAME')) {
                  $cname_flg = true;
                  // カテゴリー名獲得用カテゴリーリスト作成
                  $categories_id_array = array();
                  $categories_query = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id='" . tep_db_input($_POST['language_id']) ."'");
                  while ($categories = tep_db_fetch_array($categories_query)) {
                    $categories_id_array[$categories['categories_id']] = $categories['categories_name'];
                  }
                }
                if (($cfg_field == 'manufacturers_id') && ($cfg_rec[2] == 'NAME')) {
                  $mname_flg = true;
                  // メーカー名獲得用メーカーリスト作成
                  $manufacturers_id_array = array();
                  $manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS);
                  while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
                    $manufacturers_id_array[$manufacturers['manufacturers_id']] = $manufacturers['manufacturers_name'];
                  }
                }
              }
            }
            if ($cfg_field) {
              $column_list[$cfg_rec[0]] = $cfg_field;				// CSVファイルの並び順用
              if ($csv_count_max < $cfg_rec[0]) {
                $csv_count_max = $cfg_rec[0];				// 並び順の最大値をCSVファイルの項目数とする
              }
              // データ抽出SQLの作成
              if ($cfg_data_row == 1) {
                $select_column_list = $cfg_field;
              } else {
                $select_column_list .= ', ' . $cfg_field;
              }
            }
          }
        } else {
          // 設定ファイルがないとき
          tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
        }
        fclose($download_cfg_file);
       
        $download_sql = "select " . $select_column_list . " from (" . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd) left join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p.products_id = p2c.products_id where pd.products_id = p.products_id and pd.language_id = '" . tep_db_input($_POST['language_id']) . "'";
        $download_query = tep_db_query($download_sql);
        while($download = tep_db_fetch_array($download_query)) {
        	 $csv_rec = '';
        	 $csv_row_flg = false;
          for ($i = 1, $csv_count_max; $i <= $csv_count_max; $i++) {
            if (isset($column_list[$i])) {
              if (($column_list[$i] == 'categories_id') && ($cname_flg)) {
                $csv_rec .= ($csv_row_flg ? $ini_data['upfile_delimiter'] . '"' : '"') . $categories_id_array[$download[$column_list[$i]]] . '"';
              } elseif (($column_list[$i] == 'manufacturers_id') && ($mname_flg)) {
                $csv_rec .= ($csv_row_flg ? $ini_data['upfile_delimiter'] . '"' : '"') . $manufacturers_id_array[$download[$column_list[$i]]] . '"';
              } elseif (($column_list[$i] == 'products_price')) {
                $csv_rec .= ($csv_row_flg ? $ini_data['upfile_delimiter'] . '"' : '"') . (int)$download[$column_list[$i]] . '"';
              } elseif (($column_list[$i] == 'products_date_available')) {
                $csv_rec .= ($csv_row_flg ? $ini_data['upfile_delimiter'] . '"' : '"') . (($download[$column_list[$i]] == '0000-00-00 00:00:00') ? '0000-00-00' : date("Y-m-d",strtotime($download[$column_list[$i]]))) . '"';
              } else {
                $csv_rec .= ($csv_row_flg ? $ini_data['upfile_delimiter'] . '"' : '"') . $download[$column_list[$i]] . '"';
              }
            } else {
              $csv_rec .= ($csv_rec ? $ini_data['upfile_delimiter'] : '');
            }
            $csv_row_flg = true;
          }
          // 改行の変換
          $csv_rec = str_replace("\r\n", "\r", $csv_rec);
          $csv_rec = str_replace("\r", "\n", $csv_rec);
          $csv_rec = str_replace("\n", "\\n", $csv_rec);
          // データの書き出し
          if ($_POST['encoding'] == 'SJIS') {
            echo mb_convert_encoding($csv_rec, "SJIS", mb_internal_encoding()) . "\r\n";
          } else {
            echo $csv_rec . "\n";
          }
        }
        exit;
        break;

      case 'delete_confirm':
        $delete_type = $_GET['type'];
        $delete_value = "";
        switch ($delete_type) {
          case 'date_added':
        	   // アップロード履歴による削除の対象件数確認
            if ($_GET['value']) {
              $products_date_added = urldecode($_GET['value']);
              $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " where products_date_added = '" . tep_db_input($products_date_added) . "'");
              $products = tep_db_fetch_array($products_query);
              $delete_count = $products['total'];
              $delete_value = $products_date_added;
            } else {
              $delete_count = 0;
            }
            break;
          case 'model':
        	   // 商品の型番による削除の対象件数確認
            require(DIR_WS_CLASSES . 'currencies.php');
            $currencies = new currencies();
            if ($_POST['value']) {
              // textarea内の商品の型番を取り出す
              $textarea = $_POST['value'];
              $textarea = str_replace("\r\n", "\r", $textarea);
              $textarea = str_replace("\r", "\n", $textarea);
              $textarea_array = explode("\n", $textarea);
              $model_array = preg_grep('/.+/', $textarea_array);
              // 商品の存在チェック
              if (is_array($model_array)) {
                $delete_count = 0;
                $delete_value = '';
                $delete_products = array();
                foreach ($model_array as $value) {
                  $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_model, p.products_price, p.products_date_added, p.products_status, p2c.categories_id, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "' and p.products_id = p2c.products_id and p.products_model = '" . tep_db_input($value) . "'");
                  while ($products = tep_db_fetch_array($products_query)) {
                    $delete_products[] = $products;
                    $delete_value .= ($delete_value ? "\n" : '') . $products['products_id'];
                    $delete_count++;
                  }
                }
              } else {
                $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_model, p.products_price, p.products_date_added, p.products_status, p2c.categories_id, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "' and p.products_id = p2c.products_id and p.products_model = '" . tep_db_input($model_array) . "'");
                while ($delete_products = tep_db_fetch_array($products_query)) {
                  $delete_value = $delete_products['products_id'];
                  $delete_count = 1;
                }
              }
            } else {
              $delete_count = 0;
            }
            break;
          case 'status':
        	   // 品切れ削除の対象件数確認
            require(DIR_WS_CLASSES . 'currencies.php');
            $currencies = new currencies();
            $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_model, p.products_price, p.products_date_added, p.products_status, p2c.categories_id, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "' and p.products_id = p2c.products_id and products_status = 0");
            while ($products = tep_db_fetch_array($products_query)) {
              $delete_products[] = $products;
              $delete_count++;
            }
            break;
          case 'all':
            // 全件削除の対象件数確認
            $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS);
            $products = tep_db_fetch_array($products_query);
            $delete_count = $products['total'];
            break;
        }
        break;

      case 'delete':
        switch ($_GET['type']) {
          case 'date_added':
        	   // アップロード履歴による削除
            if ($_POST['value']) {
              $products_date_added = $_POST['value'];
              $products_query = tep_db_query("select products_id from " . TABLE_PRODUCTS . " where products_date_added = '" . tep_db_input($products_date_added) . "'");
              while ($products = tep_db_fetch_array($products_query)) {
                tep_remove_product($products['products_id']);
              }
            }
            break;
          case 'model':
        	   // 商品の型番による削除
            if ($_POST['value']) {
              $delete_array = explode("\n", $_POST['value']);
              if (is_array($delete_array)) {
                foreach ($delete_array as $value) {
                  tep_remove_product($value);
                }
              } else {
                tep_remove_product($_POST['value']);
              }
            }
            break;
          case 'status':
        	  // 品切れ削除
            $products_query = tep_db_query("select products_id from " . TABLE_PRODUCTS . " where products_status = 0");
            while ($products = tep_db_fetch_array($products_query)) {
              tep_remove_product($products['products_id']);
            }
            break;
          case 'all':
            // 全件削除
            tep_db_query("truncate table " . TABLE_SPECIALS);
            tep_db_query("truncate table " . TABLE_PRODUCTS);
            tep_db_query("truncate table " . TABLE_PRODUCTS_TO_CATEGORIES);
            tep_db_query("truncate table " . TABLE_PRODUCTS_DESCRIPTION);
            tep_db_query("truncate table " . TABLE_PRODUCTS_ATTRIBUTES);
            tep_db_query("truncate table " . TABLE_CUSTOMERS_BASKET);
            tep_db_query("truncate table " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
            tep_db_query("truncate table " . TABLE_REVIEWS_DESCRIPTION);
            tep_db_query("truncate table " . TABLE_REVIEWS);
            break;
        }
        
        if (USE_CACHE == 'true') {
          tep_reset_cache_block('categories');
          tep_reset_cache_block('also_purchased');
        }

        tep_redirect(tep_href_link(FILENAME_PRODUCTS_UPLOADER));
        break;
      default:
      // アップロード用一時ファイルを削除
        if (file_exists(FILENAME_UPLOAD_TEMPFILE)) {
          @unlink(FILENAME_UPLOAD_TEMPFILE);
        }
        break;
    }
  } else {
    // アップロード用一時ファイルを削除
    if (file_exists(FILENAME_UPLOAD_TEMPFILE)) {
      @unlink(FILENAME_UPLOAD_TEMPFILE);
    }
  }
// 一括登録ファイル一時保存用フォルダのチェック
  if (is_dir(DIR_FS_UPLOAD)) {
    if (!is_writeable(DIR_FS_UPLOAD)) $messageStack->add(ERROR_CATALOG_UPLOAD_DIRECTORY_NOT_WRITEABLE, 'error');
  } else {
    $messageStack->add(ERROR_CATALOG_UPLOAD_DIRECTORY_DOES_NOT_EXIST, 'error');
  }
// アップロード設定ファイルのチェック
  if (!is_file(FILENAME_UPLOAD_CONFIG)) {
    $messageStack->add(ERROR_UPLOAD_CONFIGFILE_DOSE_NOT_EXIST, 'error');
  }
// ダウンロード設定ファイルのチェック
  if (!is_file(FILENAME_DOWNLOAD_CONFIG)) {
    $messageStack->add(ERROR_DOWNLOAD_CONFIGFILE_DOSE_NOT_EXIST, 'error');
  }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/kn_stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onload="SetFocus();">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table width="<?php echo BOX_WIDTH; ?>" class="knMenuBar">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top">
   <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
          </tr>
        </table></td>
      </tr>
<?php
switch ($_GET['action']) {
  //アップロード設定確認画面
  case 'upload_config':
  $upfile_count = $_POST['upfile_count'];		// アップロードファイルの件数を入れなおす
  case 'upload_confirm01':
?>
     <tr>
        <td width="100%" class="main"><b><?php echo TEXT_UPLOAD_CONFIRM_01; ?></b></td>
     </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
        <td width="100%" class="main"><?php echo TEXT_UPFILE_COUNT . ' : ' . $upfile_count; ?></td>
     </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
    // アップロードデータが１件もないとき
    if (($_GET['action'] == 'upload_comfirm01') && ($upfile_count == 0)) {
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_NO_UPLOAD_DATA; ?></td>
      </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
      <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
      </td>
     </tr>
<?php
    } else {
    // アップロード設定確認
      // 設定エラーがあるとき
      if ($cfg_error_flg) {
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_UPLOAD_CONFIG_ERROR; ?></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td align="right" class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_ERROR_ROW; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_ERROR_MESSAGE; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_ERROR_DATA; ?></td>
              </tr>
<?php
        // エラーリストの表示
        for ($i = 0, $n = sizeof($cfg_error); $i < $n; $i++) {
          echo '              <tr class="dataTableRow">' . "\n";
          echo '                <td align="right" class="dataTableContent">' . $cfg_error[$i][0] . "</td>\n";
          echo '                <td class="dataTableContent">' . $cfg_error[$i][1] . "</td>\n";
          echo '                <td class="dataTableContent">' . $cfg_error[$i][2] . "</td>\n";
          echo "             </tr>\n";
        }
?>
            </table></td>
          </tr>
        </table>
      </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
      }
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_UPLOAD_CONFIG; ?></td>
      </tr>
      <tr><?php echo tep_draw_form('upload_config', FILENAME_PRODUCTS_UPLOADER, 'action=upload_config'); ?>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td align="right" class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_NO; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_NAME; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_SORT_ORDER; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_SELECT; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_DEFAULT; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_MIN; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_MAX; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_REQUIRED; ?></td>
              </tr>
<?php
      // 設定リストの表示
      for ($i = 0, $n = sizeof($cfg_data); $i < $n; $i++) {
        if (empty($cfg_data[$i])) {
          continue;
        }
        $cfg_row_count++;
        if  (($cfg_row_count/2) == floor($cfg_row_count/2)) {
          echo '              <tr class="dataTableRowEven">' . "\n";
        } else {
          echo '              <tr class="dataTableRowOdd">' . "\n";
        }
        echo '                <td align="right" class="dataTableContent">' . $cfg_data[$i][0] . "</td>\n";
        if (defined($cfg_data[$i][1])) {
          $field_name = constant($cfg_data[$i][1]);
        } else {
          $field_name = $cfg_data[$i][1];
        }
        echo '                <td class="dataTableContent">' . $field_name . tep_draw_hidden_field("cfgKey[$i]", $cfg_data[$i][11]) . '  ( ' . $cfg_data[$i][5] . ' : ' . $cfg_data[$i][6] . ' )' . tep_draw_hidden_field("cfgTable[$i]", $cfg_data[$i][5]) . tep_draw_hidden_field("cfgField[$i]", $cfg_data[$i][6]) . "</td>\n";
        echo '                <td class="dataTableContent">' . tep_draw_input_field("cfgSort[$i]", $cfg_data[$i][2], 'size="3"') . "</td>\n";
        switch ($cfg_data[$i][3]) {
          case 'ID':
            echo '                <td class="dataTableContent">' . tep_draw_radio_field("cfgSelect[$i]", 'ID', true) . '  ' . TEXT_ID . '     ' . tep_draw_radio_field("cfgSelect[$i]", 'NAME') . '  ' . TEXT_NAME . "</td>\n";
            break;
          case 'NAME':
            echo '                <td class="dataTableContent">' . tep_draw_radio_field("cfgSelect[$i]", 'ID') . '  ' . TEXT_ID . '     ' . tep_draw_radio_field("cfgSelect[$i]", 'NAME', true) . '  ' . TEXT_NAME . "</td>\n";
            break;
          default:
            echo '                <td class="dataTableContent">' . tep_draw_hidden_field("cfgSelect[$i]", $cfg_data[$i][3]) . "</td>\n";
            break;
        }
        echo '                <td class="dataTableContent">' . tep_draw_input_field("cfgDefault[$i]", $cfg_data[$i][4], 'size="5"') . "</td>\n";
        echo '                <td class="dataTableContent">' . tep_draw_input_field("cfgMin[$i]", $cfg_data[$i][8], 'size="5"') . "</td>\n";
        echo '                <td class="dataTableContent">' . tep_draw_input_field("cfgMax[$i]", $cfg_data[$i][9], 'size="5"') . "</td>\n";
        $required_array = array(array('id' => '-', 'text' =>TEXT_DATA_NOT_REQUIRED),
                                      array('id' => '*', 'text' => TEXT_DATA_REQUIRED));
        echo '                <td class="dataTableContent">' . tep_draw_pull_down_menu("cfgRequired[$i]", $required_array, $cfg_data[$i][10]) . "</td>\n";
        echo "             </tr>\n";
      }
?>
            </table></td>
          </tr>
        </table><?php echo tep_draw_hidden_field("upfile_count", $upfile_count) . tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td>
      </form></tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
      // エラーのときは、次ステップへのボタン等は表示しない
      if ($cfg_error_flg) {
?>
      <tr><td class="main"><b><font color="Red"><?php echo TEXT_UPLOAD_CONFIG_ERROR_NOTICE; ?></font></b></td></tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr>
        <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
        </td>
      </tr>
<?php
      } else {
?>
      <tr><td class="main"><b><font color="Red"><?php echo TEXT_UPLOAD_CONFIRMATION; ?></font></b></td></tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr><?php echo tep_draw_form('upload', FILENAME_PRODUCTS_UPLOADER, 'action=upload_confirm02'); ?>
        <td WIDTH="100%" class="main">
<?PHP
        // アップロードデータの言語選択
        echo TEXT_UPFILE_LANGUAGE . '&nbsp;&nbsp;';
        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
          if ($languages[$i]['code'] == DEFAULT_LANGUAGE) {
            echo tep_draw_radio_field('language_id', $languages[$i]['id'], true);
          } else {
            echo tep_draw_radio_field('language_id', $languages[$i]['id']);
          }
          echo '  ' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '    ';
        }
?>
        </td>
      </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr><td class="main">
<?php
        switch ($ini_data['upfile_mode']) {
          case 'no_use':
            $flg_yes_ignore = false;
            $flg_yes_replace = false;
            $flg_no = true;
            break;
          case 'yes_replace':
            $flg_yes_ignore = false;
            $flg_yes_replace = true;
            $flg_no = false;
            break;
          case 'yes_ignore':
          default:
            $flg_yes_ignore = true;
            $flg_yes_replace = false;
            $flg_no = false;
            break;
        }
        echo TEXT_USE_PRODUCTS_MODEL_AS_ID . '&nbsp;&nbsp;';
        echo tep_draw_radio_field('specify_id', 'yes_ignore', $flg_yes_ignore) . '&nbsp;' . TEXT_USE_PRODUCTS_MODEL_YES_IGNORE . '&nbsp;&nbsp;';
        echo tep_draw_radio_field('specify_id', 'yes_replace', $flg_yes_replace) . '&nbsp;' . TEXT_USE_PRODUCTS_MODEL_YES_REPLACE . '&nbsp;&nbsp;';
        echo tep_draw_radio_field('specify_id', 'no_use', $flg_no) . '&nbsp;' . TEXT_USE_PRODUCTS_MODEL_NO;
?></td></tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr>
        <td width="100%" class="main"><?php echo tep_image_submit('button_confirm.gif', IMAGE_CONFIRM) . ' <a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
        </td>
      </form></tr>
<?php
      }
    }
    break;

  case 'upload_confirm02':
  //アップロード確認画面
?>
     <tr>
        <td width="100%" class="main"><b><?php echo TEXT_UPLOAD_CONFIRM_02; ?></b></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
        <td width="100%" class="main"><?php echo TEXT_UPLOAD_CONFIRM; ?></td>
     </tr>
      <tr>
        <td><table border="0" cellspacing="0" cellpadding="0">
         <tr class="dataTableHeadingRow">
           <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_UPFILE_COUNT; ?></td>
           <td class="dataTableHeadingContent" align="right">&nbsp;&nbsp;<?php echo TABLE_HEADING_OK_COUNT; ?></td>
           <td class="dataTableHeadingContent" align="right">&nbsp;&nbsp;<?php echo TABLE_HEADING_NG_COUNT; ?></td>
           <td class="dataTableHeadingContent" align="right">&nbsp;&nbsp;<?php echo TABLE_HEADING_ERROR_COUNT; ?></td>
         </tr>
         <tr class="dataTableRow">
           <td class="dataTableContent" align="right"><?php echo $up_rec_count; ?></td>
           <td class="dataTableContent" align="right"><?php echo $up_rec_ok_count; ?></td>
           <td class="dataTableContent" align="right"><?php echo $up_rec_count - $up_rec_ok_count; ?></td>
           <td class="dataTableContent" align="right"><?php echo $up_rec_err_count; ?></td>
         </tr>
        </table></td>
      </tr>
      <tr><td>&nbsp;</td></tr>
<?php
    if ($up_rec_err_count == 0) {
      // アップロードデータにエラーがないとき
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_NO_ERROR; ?></td>
      </tr>
      <tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
    } else {
      // アップロードデータにエラーがあるとき
?>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td valign="top" class="dataTableHeadingContent"><?php echo TABLE_HEADING_UPLOAD_DATA_NO; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_UPLOAD_ERROR_DATA; ?><br><?php echo TABLE_HEADING_UPLOAD_ERROR; ?></td>
              </tr>
<?php
   // エラーリストの表示
      if (is_array($up_rec_err_array)) {
        foreach ($up_rec_err_array as $value) {
        	 if (defined($value[3])) {
            $field_name = constant($value[3]);
          } else {
            $field_name = $value[3];
          }
          echo '              <tr class="dataTableRow">' . "\n";
          echo '                <td valign="top" class="dataTableContent">' . $value[0] . ' - ' . $value[1] . "</td>\n";
          echo '                <td  class="dataTableContent">' . $value[2] . '<br><font color="red"><b>[ ' .  $field_name . ' ] ' . $value[4] . "</b></font></td>\n";
          echo "             </tr>\n";
        }
      }
?>
            </table></td>
          </tr>
        </table></td>
      </tr>
<?php
    }
    if ($up_rec_ok_count == 0) {
?>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
      <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
     </tr>
<?php
    } else {
?>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr><?php echo tep_draw_form('upload', FILENAME_PRODUCTS_UPLOADER, 'action=upload') . tep_draw_hidden_field("language_id", $_POST['language_id']); ?>
        <td width="100%" class="main"><?php echo tep_image_submit('button_upload.gif', IMAGE_UPLOAD) . ' <a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
        </td>
      </form></tr>
<?php
    }
    break;

  case 'upload':
  // アップロード結果画面
?>
     <tr>
        <td width="100%" class="main"><b><?php echo TEXT_UPLOAD_CONFIRM_03; ?></b></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
       <td width="100%" class="main"><?php echo TEXT_UPLOAD_RESULT; ?></td>
     </tr>
       <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
       <td width="100%" class="main"><?php echo sprintf(TEXT_UPLOAD_WAS_COMPLETED, $insert_ok_count); ?></td>
     </tr>

     <tr>
       <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
     </tr>
<?php
    break;

  case 'download_confirm':
  case 'download_config':
    // 商品総点数の獲得
    $products_count = 0;
    $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS);
    $products = tep_db_fetch_array($products_query);
    $products_count += $products['total'];
 ?>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
    if ($products_count == 0) {
?>
     <tr>
       <td width="100%" class="main"><?php echo TEXT_NO_DATA; ?></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
      <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
      </td>
     </tr>
<?php
    } else {
?>
     <tr>
       <td width="100%" class="main"><?php echo TEXT_DOWNLOAD_COUNT . ' : ' . $products_count; ?></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
    // ダウンロード設定確認
      // 設定エラーがあるとき
      if ($cfg_error_flg) {
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_DOWNLOAD_CONFIG_ERROR; ?></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td align="right" class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_ERROR_ROW; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_ERROR_MESSAGE; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_ERROR_DATA; ?></td>
              </tr>
<?php
        // エラーリストの表示
        for ($i = 0, $n = sizeof($cfg_error); $i < $n; $i++) {
          echo '              <tr class="dataTableRow">' . "\n";
          echo '                <td align="right" class="dataTableContent">' . $cfg_error[$i][0] . "</td>\n";
          echo '                <td class="dataTableContent">' . $cfg_error[$i][1] . "</td>\n";
          echo '                <td class="dataTableContent">' . $cfg_error[$i][2] . "</td>\n";
          echo "             </tr>\n";
        }
?>
            </table></td>
          </tr>
        </table>
      </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
      }
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_DOWNLOAD_CONFIG; ?></td>
      </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr><?php echo tep_draw_form('download_config', FILENAME_PRODUCTS_UPLOADER, 'action=download_config'); ?>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td align="right" class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_NO; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_NAME; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_SORT_ORDER; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CONFIG_SELECT; ?></td>
              </tr>
<?php
      // 設定リストの表示
      for ($i = 0, $n = sizeof($cfg_data); $i < $n; $i++) {
        if (empty($cfg_data[$i])) {
          continue;
        }
        $cfg_row_count++;
        if  (($cfg_row_count/2) == floor($cfg_row_count/2)) {
          echo '              <tr class="dataTableRowEven">' . "\n";
        } else {
          echo '              <tr class="dataTableRowOdd">' . "\n";
        }
        echo '                <td align="right" class="dataTableContent">' . $cfg_data[$i][0] . "</td>\n";
        if (defined($cfg_data[$i][1])) {
          $field_name = constant($cfg_data[$i][1]);
        } else {
          $field_name = $cfg_data[$i][1];
        }
        echo '                <td class="dataTableContent">' . $field_name . tep_draw_hidden_field("cfgKey[$i]", $cfg_data[$i][6]) . '  ( ' . $cfg_data[$i][4] . ' : ' . $cfg_data[$i][5] . ' )' . tep_draw_hidden_field("cfgTable[$i]", $cfg_data[$i][5]) . tep_draw_hidden_field("cfgField[$i]", $cfg_data[$i][6]) . "</td>\n";
        echo '                <td class="dataTableContent">' . tep_draw_input_field("cfgSort[$i]", $cfg_data[$i][2], 'size="3"') . "</td>\n";
        switch ($cfg_data[$i][3]) {
          case 'ID':
            echo '                <td class="dataTableContent">' . tep_draw_radio_field("cfgSelect[$i]", 'ID', true) . '  ' . TEXT_ID . '     ' . tep_draw_radio_field("cfgSelect[$i]", 'NAME') . '  ' . TEXT_NAME . "</td>\n";
            break;
          case 'NAME':
            echo '                <td class="dataTableContent">' . tep_draw_radio_field("cfgSelect[$i]", 'ID') . '  ' . TEXT_ID . '     ' . tep_draw_radio_field("cfgSelect[$i]", 'NAME', true) . '  ' . TEXT_NAME . "</td>\n";
            break;
          default:
            echo '                <td class="dataTableContent">' . tep_draw_hidden_field("cfgSelect[$i]", $cfg_data[$i][3]) . "</td>\n";
            break;
        }
        echo "             </tr>\n";
      }
?>
            </table></td>
          </tr>
        </table><?php echo tep_draw_hidden_field("upfile_count", $upfile_count) . tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td>
      </form></tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
      // エラーのときは、次ステップへのボタン等は表示しない
      if ($cfg_error_flg) {
?>
     <tr><td class="main"><b><font color="Red"><?php echo TEXT_DOWNLOAD_CONFIG_ERROR_NOTICE; ?></font></b></td></tr>
     <tr>
      <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
     </tr>
<?php
      } else {
?>
      <tr><td class="main"><b><font color="Red"><?php echo TEXT_DOWNLOAD_CONFIRMATION; ?></font></b></td></tr>
      <tr><?php echo tep_draw_form('upload', FILENAME_PRODUCTS_UPLOADER, 'action=download'); ?>
        <td WIDTH="100%" class="main">
<?PHP
        // ダウンロードデータの言語選択
        echo TEXT_DOWNLOAD_LANGUAGE . '&nbsp;&nbsp;';
        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
          if ($languages[$i]['code'] == DEFAULT_LANGUAGE) {
            echo tep_draw_radio_field('language_id', $languages[$i]['id'], true);
          } else {
            echo tep_draw_radio_field('language_id', $languages[$i]['id']);
          }
          echo '  ' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '    ';
        }
?>
        </td>
      </tr>
      <tr><td width="100%" class="main"><?php echo TEXT_ENCODE_TYPE; ?>&nbsp;&nbsp;<?php echo tep_draw_radio_field('encoding', 'SJIS', true); ?>&nbsp;<?php echo TEXT_ENCODE_TYPE_SJIS; ?>&nbsp;&nbsp;<?php echo tep_draw_radio_field('encoding', 'EUC'); ?>&nbsp;<?php echo TEXT_ENCODE_TYPE_EUC; ?></td></tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr>
        <td width="100%" class="main"><?php echo tep_image_submit('button_send.gif', IMAGE_SEND) . ' <a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
        </td>
      </form></tr>
<?php
      }
    }
    break;

  case 'delete_confirm':
  // 削除確認画面
?>
     <tr>
        <td width="100%" class="main"><?php echo TEXT_DELETE_CONFIRM; ?></td>
     </tr>
<?php
    if ($delete_count == 0) {
?>
     <tr>
       <td width="100%" class="main"><?php echo TEXT_NO_DATA; ?></td>
     </tr>
     <tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
      <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
      </td>
     </tr>
<?php
    } else {
?>
     <tr>
       <td width="100%" class="main"><?php echo sprintf(TEXT_DELETE_COUNT,$delete_count); ?></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
      if (($delete_type == 'model') || (($delete_type == 'status') && (sizeof($delete_products) < 100))) {
?>
      <tr>
        <td width="100%" class="main"><?php echo TEXT_DELETE_PRODUCTS_LIST; ?></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TEXT_CATEGORIES_NAME; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TEXT_PRODUCTS_MODEL; ?></td>
                <td class="dataTableHeadingContent"><?php echo TEXT_PRODUCTS_NAME; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TEXT_PRODUCTS_QUANTITY; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TEXT_PRODUCTS_PRICE; ?></td>
              </tr>
<?php
        // 削除対象商品の一覧表示
        for ($i = 0, $n = sizeof($delete_products); $i < $n; $i++) {
          $categories_query = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$delete_products[$i]['categories_id'] . "' and language_id = '" . $languages_id . "'");
          $categories = tep_db_fetch_array($categories_query);
          echo '              <tr class="dataTableRow">' . "\n";
          echo '                <td class="dataTableContent">' . $categories['categories_name'] . "</td>\n";
          echo '                <td class="dataTableContent" align="right">' . $delete_products[$i]['products_model'] . "</td>\n";
          echo '                <td class="dataTableContent">' . $delete_products[$i]['products_name'] . "</td>\n";
          echo '                <td class="dataTableContent" align="right">' . $delete_products[$i]['products_quantity'] . "</td>\n";
          echo '                <td class="dataTableContent" align="right">' . $currencies->display_price($delete_products[$i]['products_price'], tep_get_tax_rate($delete_products[$i]['products_tax_class_id'])) . "</td>\n";
          echo "             </tr>\n";
        }
?>
            </table></td>
          </tr>
        </table>
      </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
<?php
      }
?>
     <tr>
      <td width="100%" class="main"><?php echo tep_draw_form('delete', FILENAME_PRODUCTS_UPLOADER, 'action=delete&type=' . $delete_type); echo tep_draw_hidden_field('value', $delete_value); ?><?php echo tep_image_submit('button_delete.gif', IMAGE_DELETE) . '&nbsp;&nbsp;<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'; ?></form>
      </td>
     </tr>
<?php
    }
?>
<?php
    break;
  default:
  // デフォルト画面
    // 商品総点数と売り切れ商品点数の獲得
    $products_count = 0;
    $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS);
    $products = tep_db_fetch_array($products_query);
    $products_count += $products['total'];
    $products_deactivate_count = 0;
    $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " where products_status = '0'");
    $products = tep_db_fetch_array($products_query);
    $products_deactivate_count += $products['total'];
    // アップロード履歴の獲得
    if (file_exists(FILENAME_UPLOAD_HISTORY)) {
      $historyfile = fopen(FILENAME_UPLOAD_HISTORY, 'r');
      $historydata = array();
      for ($i = 0, $n = 5; $i < $n; $i++) {
        $historydata[$i] = fgetcsv($historyfile, 100);
      }
      fclose($historyfile);
    } else {
    	$historydata = TEXT_NO_UPLOAD_HISTORY;
    }
?>
      <tr>
        <td><table border="0" cellspacing="0" cellpadding="0">
         <tr class="dataTableHeadingRow">
           <td class="dataTableHeadingContent" width="100" align="right"><?php echo TABLE_HEADING_PRODUCTS_COUNT; ?></td>
           <td class="dataTableHeadingContent" width="100" align="right"><?php echo TABLE_HEADING_PRODUCTS_DEACTIVATE_COUNT; ?></td>
         </tr>
         <tr class="dataTableRow">
           <td class="dataTableContent" align="right"><?php echo $products_count; ?></td>
           <td class="dataTableContent" align="right"><?php echo $products_deactivate_count; ?></td>
         </tr>
        </table></td>
      </tr>
     <tr>
      <td width="100%" class="main"><br><b><?php echo TEXT_PRODUCTS_PACKAGE_REGISTRATION; ?></b></td>
     </tr>
     <tr>
      <td width="100%" class="main"><?php echo tep_draw_form('upload_confirm01', FILENAME_PRODUCTS_UPLOADER, 'action=upload_confirm01', 'post', 'enctype="multipart/form-data"'); ?>
        <?php echo TEXT_UPLOAD_FILE . tep_draw_input_field('uploadfile', '', 'size=40', '', 'file'); ?>
        <p><?php echo TEXT_ENCODE_TYPE; ?>&nbsp;&nbsp;<?php echo tep_draw_radio_field('encoding', 'SJIS', true); ?>&nbsp;<?php echo TEXT_ENCODE_TYPE_SJIS; ?>&nbsp;&nbsp;
        <?php echo tep_draw_radio_field('encoding', 'EUC'); ?>&nbsp;<?php echo TEXT_ENCODE_TYPE_EUC; ?></p>
        <p><?php echo tep_image_submit('button_confirm.gif', IMAGE_CONFIRM); ?></p></form>
      </td>
     </tr>
      <tr>
        <td valign="top"><table border="0" cellspacing="0" cellpadding="2">
          <tr class="dataTableHeadingRow">
            <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_UPLOAD_DATE; ?></td>
            <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_UPLOAD_FILENAME; ?></td>
            <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_INSERT_COUNT; ?></td>
            <td class="dataTableHeadingContent"> </td>
          </tr>
<?php
    // アップロード履歴の表示
    if (is_array($historydata)) {
      for ($i = 0, $n = 5; $i < $n; $i++) {
        echo '              <tr class="dataTableRow">' . "\n";
        echo '                <td class="dataTableContent">' . $historydata[$i][0] . "</td>\n";
        echo '                <td class="dataTableContent">' . $historydata[$i][1] . "</td>\n";
        echo '                <td class="dataTableContent" align="right">' . $historydata[$i][2] . "</td>\n";
        echo '                <td class="dataTableContent" align="right"><a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER, 'action=delete_confirm&type=date_added&value=' . urlencode($historydata[$i][0])) . '">' . TEXT_DELETE . "</a></td>\n";
        echo "             </tr>\n";
      }
    } else {
      echo '<tr class="dataTableRow"><td class="dataTableContent" colspan=4>' . $historydata . "</td></tr>\n";
    }
?>
       </table></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
       <td width="100%" class="main"><b><?php echo TEXT_DELETION_BY_PRODUCTS_STATUS; ?></b></td>
     </tr>
     <tr>
       <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER, 'action=delete_confirm&type=status') . '">' . tep_image_button('button_confirm.gif', IMAGE_CONFIRM) . '</a>'; ?></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
       <td width="100%" class="main"><br><b><?php echo TEXT_DELETION_BY_PRODUCTS_MODEL; ?></b></td>
     </tr>
     <tr><?php echo tep_draw_form('delete', FILENAME_PRODUCTS_UPLOADER, 'action=delete_confirm&type=model'); ?>
       <td><table border="0" cellspacing="0" cellpadding="2">
         <tr>
           <td class="main" valign="top"><?php echo TEXT_PRODUCTS_MODEL; ?>:&nbsp;</td>
           <td class="main" valign="top"><?php echo tep_draw_textarea_field('value', 'soft', '50', '5'); ?></td>
         </tr>
         <tr>
           <td class="main" valign="top">&nbsp;</td>
           <td class="main" valign="top"><?php echo TEXT_NOTES_TO_INPUT_PRODUCTS_MODELS; ?></td>
         </tr>
         <tr>
           <td class="main" valign="top"><?php echo tep_image_submit('button_confirm.gif', IMAGE_CONFIRM); ?></td>
         </tr>
       </table></td>
     </tr></form>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
       <td width="100%" class="main"><b><?php echo TEXT_PRODUCTS_DATA_DOWNLOAD; ?></b></td>
     </tr>
     <tr>
       <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER, 'action=download_confirm') . '">' . tep_image_button('button_confirm.gif', IMAGE_CONFIRM) . '</a>'; ?></td>
     </tr>
     <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
     <tr>
       <td width="100%" class="main"><b><?php echo TEXT_ALL_PRODUCTS_DELETION; ?></b></td>
     </tr>
     <tr>
       <td width="100%" class="main"><?php echo '<a href="' . tep_href_link(FILENAME_PRODUCTS_UPLOADER, 'action=delete_confirm&type=all') . '">' . tep_image_button('button_confirm.gif', IMAGE_CONFIRM) . '</a>'; ?></td>
     </tr>
<?php
}
?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
