<?php
/*
  categories_edit.php
  作成日：2004/12/2
　最終更新日：2005/9/24

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

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  if ($_GET['action']) {
    switch ($_GET['action']) {
      case 'new_category_check':
        // 新規登録するカテゴリーIDの存在確認チェック
        if ($_POST['categories_id']) {
          $check_category_id = tep_db_input($_POST['categories_id']);
          $category_check_query = tep_db_query("select count(*) as total from " . TABLE_CATEGORIES . " where categories_id = '" . $check_category_id . "'");
          $category_check = tep_db_fetch_array($category_check_query);
          if ($category_check['total'] == '0') {
        	   // 既存のデータがない場合は、カテゴリー新規登録へ
        	   tep_redirect(tep_href_link(FILENAME_CATEGORIES_EDIT,'action=new_category&cPath=' . $cPath . '&cID=' . $check_category_id));
          } else {
        	   // 既存のデータがある場合は、エラーメッセージ
            $messageStack->add_session(ERROR_INSERT_CATEGORY_ALREADY_EXIST, 'error');
            tep_redirect(tep_href_link(FILENAME_CATEGORIES_EDIT,'cPath=' . $cPath));
          }
        } else {
        	$messageStack->add_session(ERROR_CATEGORIES_ID_IS_EMPTY, 'error');
         tep_redirect(tep_href_link(FILENAME_CATEGORIES_EDIT,'cPath=' . $cPath));
        }
        break;
      case 'new_category':
        // カテゴリーの新規登録
        $edit_category = array();
        $edit_category['categories_id'] = tep_db_input($_GET['cID']);
        $edit_category['categories_name'] = '';
        $edit_category['categories_image'] = '';
        $edit_category['parent_id'] = $current_category_id;
        $edit_category['sort_order'] = tep_db_input($_GET['cID']);
        $edit_category['date_added'] = '';
        $edit_category['last_modified'] = '';
        $edit_category['categories_description'] = '';
        $edit_category['categories_keywords'] = '';
        $cInfo = new objectInfo($edit_category);
        $action = 'new_category_confirm';
        break;
      case 'edit_category':
        // カテゴリーの編集
        $edit_category_id = tep_db_input($_GET['cID']);
        $edit_category_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified, cd.categories_description, cd.categories_keywords from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $edit_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "'");
        $edit_category = tep_db_fetch_array($edit_category_query);
        $cInfo = new objectInfo($edit_category);
        $action = 'edit_category_confirm';
        break;
      case 'new_category_confirm':
        // カテゴリーの新規登録確認
        if (!$_POST['categories_name']) {
        	 // カテゴリー名が未入力のときは編集画面へ
          $edit_category = array();
          $edit_category['categories_id'] = $_POST['categories_id'];
          $edit_category['categories_name'] = $_POST['categories_name'];
          $edit_category['categories_image'] = $_POST['categories_image_path'];
          $edit_category['parent_id'] = $_POST['parent_id'];
          $edit_category['sort_order'] = $_POST['sort_order'];
          $edit_category['categories_description'] = $_POST['categories_description'];
          $edit_category['categories_keywords'] = $_POST['categories_keywords'];
          $cInfo = new objectInfo($edit_category);
          $_GET['action'] = 'new_category';
          $action = 'new_category_confirm';
          $messageStack->add(ERROR_CATEGORIES_NAME_IS_EMPTY, 'error');
        } else {
          // 画像のアップロード
          $categories_image = tep_get_uploaded_file('categories_image');
          $image_directory = tep_get_local_path(DIR_FS_CATALOG_IMAGES . DIR_WS_CATEGORIES_IMAGES);

          if (is_uploaded_file($categories_image['tmp_name'])) {
            $categories_image_path = DIR_WS_CATEGORIES_IMAGES . $categories_image['name'];
            tep_copy_uploaded_file($categories_image, $image_directory);
          }
          $action = 'insert_category';
        }
        break;
      case 'edit_category_confirm':
        // カテゴリーの編集確認
        if (!$_POST['categories_name']) {
        	 // カテゴリー名が未入力のときは編集画面へ
          $edit_category = array();
          $edit_category['categories_id'] = $_POST['categories_id'];
          $edit_category['categories_name'] = $_POST['categories_name'];
          $edit_category['categories_image'] = $_POST['categories_image_path'];
          $edit_category['parent_id'] = $_POST['parent_id'];
          $edit_category['sort_order'] = $_POST['sort_order'];
          $edit_category['categories_description'] = $_POST['categories_description'];
          $edit_category['categories_keywords'] = $_POST['categories_keywords'];
          $cInfo = new objectInfo($edit_category);
          $_GET['action'] = 'edit_category';
          $action = 'edit_category_confirm';
          $messageStack->add(ERROR_CATEGORIES_NAME_IS_EMPTY, 'error');
        } else {
        // 画像のアップロード
          $categories_image = tep_get_uploaded_file('categories_image');
          $image_directory = tep_get_local_path(DIR_FS_CATALOG_IMAGES . DIR_WS_CATEGORIES_IMAGES);

          if ($_POST['delete_image']) {
            $categories_image_path = '';
            if (file_exists(DIR_FS_CATALOG_IMAGES . $_POST['previous_category_image'])) {
              @unlink(DIR_FS_CATALOG_IMAGES . $_POST['previous_category_image']);
            }
          } else {
            if (is_uploaded_file($categories_image['tmp_name'])) {
              // 以前の画像ファイルがあれば削除して、アップロードした画像ファイルを登録
              if (file_exists(DIR_FS_CATALOG_IMAGES . $_POST['previous_category_image'])) {
                @unlink(DIR_FS_CATALOG_IMAGES . $_POST['previous_category_image']);
              }
              $categories_image_path = DIR_WS_CATEGORIES_IMAGES . $categories_image['name'];
              tep_copy_uploaded_file($categories_image, $image_directory);
            } else {
              $categories_image_path = $_POST['previous_category_image'];
            }
          }
          $action = 'update_category';
        }
        break;
      case 'insert_category':
      case 'update_category':
        if (($_POST['edit_x']) || ($_POST['edit_y'])) {
        	 // プレビュー画面で戻るボタンを押されたとき
          $edit_category = array();
          $edit_category['categories_id'] = $_POST['categories_id'];
          $edit_category['categories_name'] = $_POST['categories_name'];
          $edit_category['categories_image'] = $_POST['categories_image_path'];
          $edit_category['parent_id'] = $_POST['parent_id'];
          $edit_category['sort_order'] = $_POST['sort_order'];
          $edit_category['categories_description'] = $_POST['categories_description'];
          $edit_category['categories_keywords'] = $_POST['categories_keywords'];
          $cInfo = new objectInfo($edit_category);
          if ($_GET['action'] == 'insert_category') {
            $_GET['action'] = 'new_category';
            $action = 'new_category_confirm';
          } elseif ($_GET['action'] == 'update_category') {
            $_GET['action'] = 'edit_category';
            $action = 'edit_category_confirm';
          }
        } else {
          // categoriesテーブルの更新
          $categories_id = tep_db_prepare_input($_POST['categories_id']);
          $parent_id = tep_db_prepare_input($_POST['parent_id']);
          $sort_order = tep_db_prepare_input($_POST['sort_order']);
          $categories_image_path = tep_db_prepare_input($_POST['categories_image_path']);
          $sql_data_array = array('categories_image' => $categories_image_path,
                                     'sort_order' => $sort_order);
          if ($_GET['action'] == 'insert_category') {
            $insert_sql_data = array('categories_id' => $categories_id,
                                     'parent_id' => $current_category_id,
                                     'date_added' => 'now()');
            $sql_data_array = tep_array_merge($sql_data_array, $insert_sql_data);
            tep_db_perform(TABLE_CATEGORIES, $sql_data_array);
            $categories_id = tep_db_insert_id();
          } elseif ($_GET['action'] == 'update_category') {
            $update_sql_data = array('last_modified' => 'now()');
            $sql_data_array = tep_array_merge($sql_data_array, $update_sql_data);
            tep_db_perform(TABLE_CATEGORIES, $sql_data_array, 'update', 'categories_id = \'' . $categories_id . '\'');
          }
          // categories_descriptionテーブルの更新
          $sql_data_array = array('categories_name' => tep_db_prepare_input($_POST['categories_name']),
                                        'categories_description' => tep_db_prepare_input($_POST['categories_description']),
                                        'categories_keywords' => tep_db_prepare_input($_POST['categories_keywords']));
          if ($_GET['action'] == 'insert_category') {
            $insert_sql_data = array('categories_id' => $categories_id,
                                     'language_id' => $languages_id);
            $sql_data_array = tep_array_merge($sql_data_array, $insert_sql_data);
            tep_db_perform(TABLE_CATEGORIES_DESCRIPTION, $sql_data_array);
          } elseif ($_GET['action'] == 'update_category') {
            tep_db_perform(TABLE_CATEGORIES_DESCRIPTION, $sql_data_array, 'update', 'categories_id = \'' . $categories_id . '\' and language_id = \'' . $languages_id . '\'');
          }
          
          if (USE_CACHE == 'true') {
            tep_reset_cache_block('categories');
            tep_reset_cache_block('also_purchased');
          }

          tep_redirect(tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&cID=' . $categories_id));
        }
        break;
      case 'delete_category_confirm':
        if ($_GET['cID']) {
          $categories_id = tep_db_prepare_input($_GET['cID']);
          $categories = tep_get_category_tree($categories_id, '', '0', '', true);
          $delete_product_count = 0;

          for ($i = 0, $n = sizeof($categories); $i < $n; $i++) {
            $product_count_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . $categories[$i]['id'] . "'");
            $product_count = tep_db_fetch_array($product_count_query);
            $delete_product_count += $product_count['total'];
          }
        }
        break;
      case 'delete_category':
        if ($_GET['cID']) {
          $categories_id = tep_db_prepare_input($_GET['cID']);

          $categories = tep_get_category_tree($categories_id, '', '0', '', true);
          $products = array();
          $products_delete = array();

          for ($i = 0, $n = sizeof($categories); $i < $n; $i++) {
            $product_ids_query = tep_db_query("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . $categories[$i]['id'] . "'");
            while ($product_ids = tep_db_fetch_array($product_ids_query)) {
              $products[$product_ids['products_id']]['categories'][] = $categories[$i]['id'];
            }
          }

          reset($products);
          while (list($key, $value) = each($products)) {
            $category_ids = '';
            for ($i = 0, $n = sizeof($value['categories']); $i < $n; $i++) {
              $category_ids .= '\'' . $value['categories'][$i] . '\', ';
            }
            $category_ids = substr($category_ids, 0, -2);

            $check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $key . "' and categories_id not in (" . $category_ids . ")");
            $check = tep_db_fetch_array($check_query);
            if ($check['total'] < '1') {
              $products_delete[$key] = $key;
            }
          }

          // Removing categories can be a lengthy process
          tep_set_time_limit(0);
          for ($i = 0, $n = sizeof($categories); $i < $n; $i++) {
            tep_remove_category($categories[$i]['id']);
          }

          reset($products_delete);
          while (list($key) = each($products_delete)) {
            tep_remove_product($key);
          }
        }

        if (USE_CACHE == 'true') {
          tep_reset_cache_block('categories');
          tep_reset_cache_block('also_purchased');
        }

        tep_redirect(tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath));
        break;
    }
  } else {
    // 通常画面（カテゴリー一覧）
    $cInfo_array = array();
    $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified, cd.categories_description, cd.categories_keywords from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . $current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "' order by c.sort_order, cd.categories_name");
    while ($categories = tep_db_fetch_array($categories_query)) {
      $category_childs = array('childs_count' => tep_childs_in_category_count($categories['categories_id']));
      $category_products = array('products_count' => tep_products_in_category_count($categories['categories_id']));
      $cInfo_array[] = tep_array_merge($categories, $category_childs, $category_products);
    }
  }

// check if the catalog image directory exists
  if (is_dir(DIR_FS_CATALOG_IMAGES)) {
    if (is_writeable(DIR_FS_CATALOG_IMAGES)) {
      if (is_dir(DIR_FS_CATALOG_IMAGES . DIR_WS_CATEGORIES_IMAGES)) {
        if (!is_writeable(DIR_FS_CATALOG_IMAGES . DIR_WS_CATEGORIES_IMAGES)) {
        	 $messageStack->add(ERROR_CATEGORIES_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
        }
      } else {
        // カテゴリー画像用ディレクトリがない場合、作成する。
        @mkdir(DIR_FS_CATALOG_IMAGES . DIR_WS_CATEGORIES_IMAGES, 0777);
        if (is_dir(DIR_FS_CATALOG_IMAGES . DIR_WS_CATEGORIES_IMAGES)) {
          $messageStack->add(ERROR_CATEGORIES_IMAGE_DIRECTORY_DOES_NOT_EXIST, 'error');
        }
      }
    } else {
      $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
    }
  } else {
    $messageStack->add(ERROR_CATALOG_IMAGE_DIRECTORY_DOES_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">
<?php
  switch ($_GET['action']) {
    case 'new_category':
    case 'edit_category':
      // カテゴリーの編集（新規登録、修正）
?>
      <tr>
        <td class="pageHeading"><?php echo TEXT_EDIT_CATEGORY; ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr><?php echo tep_draw_form('preview', FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&action=' . $action, 'post', 'enctype="multipart/form-data"'); ?>
        <td><table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td class="main"><?php echo TEXT_PARENT_CATEGORY; ?></td>
            <td class="main"><?php echo $cInfo->parent_id . '&nbsp;&nbsp;' . tep_get_category_name($cInfo->parent_id, $languages_id) . tep_draw_hidden_field('parent_id', $cInfo->parent_id); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_CATEGORIES_ID; ?></td>
            <td class="main"><?php echo $cInfo->categories_id . tep_draw_hidden_field('categories_id', $cInfo->categories_id); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_CATEGORIES_NAME; ?></td>
            <td class="main"><?php echo tep_draw_input_field('categories_name', $cInfo->categories_name); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main" valign="top"><?php echo TEXT_CATEGORIES_DESCRIPTION; ?></td>
            <td class="main"><?php echo tep_draw_textarea_field('categories_description', 'soft', '70', '10', $cInfo->categories_description); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main" valign="top"><?php echo TEXT_CATEGORIES_KEYWORDS; ?></td>
            <td class="main"><?php echo tep_draw_textarea_field('categories_keywords', 'soft', '70', '10', $cInfo->categories_keywords) . '<br>' . TEXT_CATEGORIES_KEYWORDS_NOTE; ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_CATEGORIES_IMAGE; ?></td>
            <td class="main">
<?php
      echo tep_draw_file_field('categories_image');
      if ($cInfo->categories_image) {
        // 画像の変更確認用に既存の画像パスをとっておく
        echo '<br>' . $cInfo->categories_image;
        // 画像削除用チェックボックス
        echo tep_draw_hidden_field('previous_category_image', $cInfo->categories_image);
        echo '&nbsp;&nbsp;' . tep_draw_checkbox_field('delete_image') . '&nbsp;' . TEXT_DELETE;
      }
?>
            </td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_SORT_ORDER; ?></td>
            <td class="main"><?php echo tep_draw_input_field('sort_order', $cInfo->sort_order, 'size="5"'); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td class="main" align="right"><?php echo tep_image_submit('button_preview.gif', IMAGE_PREVIEW) . '&nbsp;&nbsp;<a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'; ?></td>
      </form></tr>
<?php
      break;
    case 'new_category_confirm':
    case 'edit_category_confirm':
      // カテゴリーの編集確認（新規登録、修正）
?>
      <tr>
        <td class="pageHeading"><?php echo TEXT_EDIT_CATEGORY_CONFIRM; ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr><?php echo tep_draw_form('submit', FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&action=' . $action, 'post', 'enctype="multipart/form-data"'); ?>
        <td><table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td class="main"><?php echo TEXT_PARENT_CATEGORY; ?></td>
            <td class="main"><?php echo $_POST['parent_id'] . '&nbsp;&nbsp;' . tep_get_category_name($_POST['parent_id'], $languages_id) . tep_draw_hidden_field('parent_id', $_POST['parent_id']); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_CATEGORIES_ID; ?></td>
            <td class="main"><?php echo $_POST['categories_id'] . tep_draw_hidden_field('categories_id', $_POST['categories_id']); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_CATEGORIES_NAME; ?></td>
            <td class="main"><?php echo $_POST['categories_name'] . tep_draw_hidden_field('categories_name', $_POST['categories_name']); ?><td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main" valign="top"><?php echo TEXT_CATEGORIES_DESCRIPTION; ?></td>
            <td class="main"><?php echo nl2br($_POST['categories_description']) . tep_draw_hidden_field('categories_description', $_POST['categories_description']); ?><td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main" valign="top"><?php echo TEXT_CATEGORIES_KEYWORDS; ?></td>
            <td class="main">
<?php
      if (function_exists("mbereg_replace")) {
        $_POST['categories_keywords'] = mbereg_replace("　", " ", $_POST['categories_keywords']);
      } else {
        $_POST['categories_keywords'] = str_replace("　", " ", $_POST['categories_keywords']);
      }
      echo $_POST['categories_keywords'] . tep_draw_hidden_field('categories_keywords', $_POST['categories_keywords']);
?><td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main" valign="top"><?php echo TEXT_CATEGORIES_IMAGE; ?></td>
            <td class="main" valign="top"><?php echo $categories_image_path . tep_draw_hidden_field('categories_image_path', $categories_image_path) . '&nbsp;' . tep_image(DIR_WS_CATALOG_IMAGES . $categories_image_path, $_POST['categories_name'], HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT, 'hspace="5" vspace="5"'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_SORT_ORDER; ?></td>
            <td class="main"><?php echo $_POST['sort_order'] . tep_draw_hidden_field('sort_order', $_POST['sort_order']); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td class="main" align="right">
<?php
      echo tep_image_submit('button_back.gif', IMAGE_BACK, 'name="edit"') . '&nbsp;&nbsp;';
      
      if ($action == 'update_category') {
        echo tep_image_submit('button_update.gif', IMAGE_UPDATE);
      } else {
        echo tep_image_submit('button_insert.gif', IMAGE_INSERT);
      }

      echo '&nbsp;&nbsp;<a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>';
?>
        </td>
      </form></tr>
<?php
      break;
    case 'delete_category_confirm':
      // カテゴリーの削除確認
?>
      <tr>
        <td class="pageHeading"><?php echo TEXT_DELETE_CATEGORY_CONFIRM; ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr><?php echo tep_draw_form('delete_category', FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&cID=' . $categories_id . '&action=delete_category', 'post', 'enctype="multipart/form-data"'); ?>
        <td><table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td class="main" valign="top"><?php echo TEXT_DELETE_CATEGORY; ?></td>
            <td class="main">
<?php
      if (sizeof($categories) > 0) {
        for ($i = 0, $n = sizeof($categories); $i < $n; $i++) {
          echo ($i == 0 ? $categories[$i]['id'] . '&nbsp;:&nbsp;' . $categories[$i]['text'] : '<br>' . $categories[$i]['id'] . '&nbsp;:&nbsp;' . $categories[$i]['text']);
        }
      }
?>
            </td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_DELETE_PRODUCTS_COUNT; ?></td>
            <td class="main"><?php echo $delete_product_count; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td class="main" align="right"><?php echo tep_image_submit('button_delete.gif', IMAGE_DELETE) . '&nbsp;&nbsp;<a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'; ?>
        </td>
      </form></tr>
<?php
      break;

    default:
      // 通常画面（カテゴリー一覧）
      if ($current_category_id) {
        // 中分類以降
        $current_category_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified, cd.categories_description, cd.categories_keywords from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $current_category_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "' order by c.sort_order, cd.categories_name");
        $current_category = tep_db_fetch_array($current_category_query);
        $current_category_name = $current_category['categories_name'];
        $category_heading_title = HEADING_TITLE . '&nbsp;&nbsp;' . $current_category_id . '&nbsp;:&nbsp;' . $current_category_name;
        $current_category_description = $current_category['categories_description'];
        if ($current_category['categories_image']) {
          $current_category_image = tep_image(DIR_WS_CATALOG_IMAGES . $current_category['categories_image'], $current_category['categories_name'], HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT, 'hspace="5" vspace="5"');
        } else {
          $current_category_image = '';
        }
        $current_category_keywords = $current_category['categories_keywords'];
        $current_category_date_added = TABLE_HEADING_DATE_ADDED . $current_category['date_added'];
        if ($current_category['last_modified']) {
          $current_category_last_modified = TABLE_HEADING_LAST_MODIFIED . $current_category['last_modified'];
        } else {
          $current_category_last_modified = '';
        }
      } else {
        // 大分類
        $current_category_name = TEXT_TOP_CATEGORY;
        $category_heading_title = HEADING_TITLE . '&nbsp;&nbsp;' . $current_category_name;
        $current_category_description = '';
        $current_category_image = '';
        $current_category_keywords = '';
        $current_category_date_added = '';
        $current_category_last_modified = '';
      }
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr>
            <td class="pageHeading" colspan="2"><?php echo $category_heading_title; ?></td>
          </tr>
          <tr>
            <td class="dataTableContent" valign="top"><?php echo $current_category_description; ?></td>
            <td class="dataTableContent"><?php echo $current_category_image; ?></td>
          </tr>
          <tr>
            <td class="dataTableContent" colspan="2"><font color="#FF0000"><?php echo $current_category_keywords; ?></font></td>
          </tr>
          <tr>
            <td class="dataTableContent" colspan="2" align="right"><?php echo $current_category_date_added . "&nbsp;" . $current_category_last_modified; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
              <tr><?php echo tep_draw_form('new_category_check', FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&action=new_category_check', 'post'); ?>
                <td class="smallText"><?php echo $current_category_name . TEXT_CREATE_NEW_CATEGORY; ?></td>
                <td align="right" class="smallText" nowrap><?php echo TEXT_CATEGORIES_ID . '&nbsp;' . tep_draw_input_field('categories_id', '', 'size="5"') . tep_image_submit('button_new_category.gif', IMAGE_NEW_CATEGORY, 'align="top"'); ?></td>
              </form></tr>
            </table></td>
          </tr>
          <tr>
            <td valign="top"><table border="1" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent" width="10%"><?php echo TABLE_HEADING_CATEGORIES_ID; ?></td>
                <td class="dataTableHeadingContent" width="45%"><?php echo TABLE_HEADING_CATEGORIES_NAME; ?></td>
                <td class="dataTableHeadingContent" width="30%"><?php echo TABLE_HEADING_SUB_CATEGORIES_PRODUCTS; ?></td>
                <td class="dataTableHeadingContent" width="10%" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
                <td class="dataTableHeadingContent" width="5%" align="right" nowrap><?php echo TABLE_HEADING_SORT_ORDER; ?></td>
              </tr>
<?php
      if (sizeof($cInfo_array) > 0) {
        for ($i = 0, $n = sizeof($cInfo_array); $i < $n; $i++) {
          echo '              <tr class="dataTableRow">' . "\n";
          echo '                <td nowrap><b>' . $cInfo_array[$i]['categories_id'] . "</b></td>\n";
          echo '                <td><a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, tep_get_path($cInfo_array[$i]['categories_id'])) . '"><b><font size="+1">' . $cInfo_array[$i]['categories_name'] . "</font></b></a></td>\n";
          echo '                <td>' . "\n";
          echo '                  <table border="0" width="100%" cellspacing="0" cellpadding="2">' . "\n";
          echo '                    <tr><td class="dataTableContent">' . TABLE_HEADING_SUBCATEGORIES . '</td><td align="right" class="dataTableContent">' . $cInfo_array[$i]['childs_count'] . '</td><td><a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, tep_get_path($cInfo_array[$i]['categories_id'])) . '">' . TEXT_GO_TO_LIST . "</a></td></tr>\n";
          echo '                    <tr><td class="dataTableContent">' . TABLE_HEADING_PRODUCTS . '</td><td align="right" class="dataTableContent">' . $cInfo_array[$i]['products_count'] .'</td><td>' . (file_exists(FILENAME_PRODUCTS_EDIT) ? '<a href="' . tep_href_link(FILENAME_PRODUCTS_EDIT, 'action=search&subject=categories_id&value=' . $cInfo_array[$i]['categories_id']) . '">' . TEXT_GO_TO_LIST . '</a>' : "&nbsp;") .  "</td></tr>\n";
          echo '                  </table>' . "\n";
          echo "                </td>\n";
          echo '                <td>' . "\n";
          echo '                  <table border="0" width="100%" cellspacing="0" cellpadding="2">' . "\n";
          echo '                    <tr><td valign="top" align="right">' . '<a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&cID=' . $cInfo_array[$i]['categories_id'] . '&action=edit_category') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . "</a></td></tr>\n";
          echo '                    <tr><td valign="top" align="right">' . '<a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, 'cPath=' . $cPath . '&cID=' . $cInfo_array[$i]['categories_id'] . '&action=delete_category_confirm') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . "</a></td></tr>\n";
          echo '                  </table>' . "\n";
          echo "                </td>\n";
          echo '                <td align="right">' . $cInfo_array[$i]['sort_order'] . "</td>\n";
          echo "              </tr>\n";
        }
      } else {
        // カテゴリーが全くない場合
        echo '              <tr class="dataTableRow">' . "\n";
        echo '                <td class="dataTableContent" colspan="5"><b>' . EMPTY_CATEGORY . '</b></td>';
        echo "              </tr>\n";
      }
?>
            </table></td>
          </tr>
          <tr>
            <td align="right" class="smallText">
<?php
      // 戻り先のカテゴリーの獲得
      if ($current_category_id > 0) {
        if ($cPath_array) {
          $cPath_back = '';
          for($i = 0, $n = sizeof($cPath_array) - 1; $i < $n; $i++) {
            if ($cPath_back == '') {
              $cPath_back .= $cPath_array[$i];
            } else {
              $cPath_back .= '_' . $cPath_array[$i];
            }
          }
        }
        $cPath_back = ($cPath_back) ? 'cPath=' . $cPath_back : '';
        echo '<a href="' . tep_href_link(FILENAME_CATEGORIES_EDIT, $cPath_back . '&cID=' . $current_category_id) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>';
      }
?>
            </td>
          </tr>
        </table></td>
      </tr>
<?php
      break;
  }
?>
      </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'); ?>