Index: temp/trunk/data/update/admin/products/category.php
===================================================================
--- temp/trunk/data/update/admin/products/category.php	(revision 6330)
+++ temp/trunk/data/update/admin/products/category.php	(revision 6331)
@@ -1,3 +1,306 @@
 <?php
-	print("test");
+/*
+ * Copyright(c) 2000-2006 LOCKON CO.,LTD. All Rights Reserved.
+ *
+ * http://www.lockon.co.jp/
+ */
+
+class UC_Page {
+	function UC_Page() {
+		$this->tpl_mainpage = 'products/category.tpl';
+		$this->tpl_subnavi = 'products/subnavi.tpl';
+		$this->tpl_mainno = 'products';		
+		$this->tpl_subno = 'category';
+		$this->tpl_onload = " fnSetFocus('category_name'); ";
+		$this->tpl_subtitle = '¥«¥Æ¥´¥ê¡¼ÅÐÏ¿';
+	}
+}
+
+$conn = new SC_DBConn();
+$objPage = new UC_Page();
+$objView = new SC_AdminView();
+$objSess = new SC_Session();
+
+// Ç§¾Ú²ÄÈÝ¤ÎÈ½Äê
+sfIsSuccess($objSess);
+
+// ¥Ñ¥é¥á¡¼¥¿´ÉÍý¥¯¥é¥¹
+$objFormParam = new SC_FormParam();
+// ¥Ñ¥é¥á¡¼¥¿¾ðÊó¤Î½é´ü²½
+lfInitParam();
+// POSTÃÍ¤Î¼èÆÀ
+$objFormParam->setParam($_POST);
+
+// ÄÌ¾ï»þ¤Ï¿Æ¥«¥Æ¥´¥ê¤ò0¤ËÀßÄê¤¹¤ë¡£
+$objPage->arrForm['parent_category_id'] = $_POST['parent_category_id'];
+
+switch($_POST['mode']) {
+case 'edit':
+	$objFormParam->convParam();
+	$arrRet =  $objFormParam->getHashArray();
+	$objPage->arrErr = lfCheckError($arrRet);
+	
+	if(count($objPage->arrErr) == 0) {
+		if($_POST['category_id'] == "") {
+			$objQuery = new SC_Query();
+			$count = $objQuery->count("dtb_category");
+			if($count < CATEGORY_MAX) {			
+				lfInsertCat($_POST['parent_category_id']);
+			} else {
+				print("¥«¥Æ¥´¥ê¤ÎÅÐÏ¿ºÇÂç¿ô¤òÄ¶¤¨¤Þ¤·¤¿¡£");
+			}
+		} else {
+			lfUpdateCat($_POST['category_id']);
+		}
+	} else {
+		$objPage->arrForm = array_merge($objPage->arrForm, $objFormParam->getHashArray());
+		$objPage->arrForm['category_id'] = $_POST['category_id'];
+	}
+	break;
+case 'pre_edit':
+	// ÊÔ½¸¹àÌÜ¤Î¥«¥Æ¥´¥êÌ¾¤òDB¤è¤ê¼èÆÀ¤¹¤ë¡£
+	$oquery = new SC_Query();
+	$where = "category_id = ?";
+	$cat_name = $oquery->get("dtb_category", "category_name", $where, array($_POST['category_id']));
+	// ÆþÎÏ¹àÌÜ¤Ë¥«¥Æ¥´¥êÌ¾¤òÆþÎÏ¤¹¤ë¡£
+	$objPage->arrForm['category_name'] = $cat_name;
+	// POST¥Ç¡¼¥¿¤ò°ú¤­·Ñ¤°
+	$objPage->arrForm['category_id'] = $_POST['category_id'];
+	break;
+case 'delete':
+	$objQuery = new SC_Query();
+	// »Ò¥«¥Æ¥´¥ê¤Î¥Á¥§¥Ã¥¯
+	$where = "parent_category_id = ? AND del_flg = 0";
+	$count = $objQuery->count("dtb_category", $where, array($_POST['category_id']));
+	if($count != 0) {
+		$objPage->arrErr['category_name'] = "¢¨ »Ò¥«¥Æ¥´¥ê¤¬Â¸ºß¤¹¤ë¤¿¤áºï½ü¤Ç¤­¤Þ¤»¤ó¡£<br>";
+	}
+	// ÅÐÏ¿¾¦ÉÊ¤Î¥Á¥§¥Ã¥¯
+	$where = "category_id = ? AND del_flg = 0";
+	$count = $objQuery->count("dtb_products", $where, array($_POST['category_id']));
+	if($count != 0) {
+		$objPage->arrErr['category_name'] = "¢¨ ¥«¥Æ¥´¥êÆâ¤Ë¾¦ÉÊ¤¬Â¸ºß¤¹¤ë¤¿¤áºï½ü¤Ç¤­¤Þ¤»¤ó¡£<br>";
+	}	
+	
+	if(!isset($objPage->arrErr['category_name'])) {
+		// ¥é¥ó¥¯ÉÕ¤­¥ì¥³¡¼¥É¤Îºï½ü(¢¨½èÍýÉé²Ù¤ò¹ÍÎ¸¤·¤Æ¥ì¥³¡¼¥É¤´¤Èºï½ü¤¹¤ë¡£)
+		sfDeleteRankRecord("dtb_category", "category_id", $_POST['category_id'], "", true);
+	}
+	break;
+case 'up':
+	$objQuery = new SC_Query();
+	$objQuery->begin();
+	$up_id = lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
+	if($up_id != "") {
+		// ¾å¤Î¥°¥ë¡¼¥×¤Îrank¤«¤é¸º»»¤¹¤ë¿ô
+		$my_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
+		// ¼«Ê¬¤Î¥°¥ë¡¼¥×¤Îrank¤Ë²Ã»»¤¹¤ë¿ô
+		$up_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id);
+		if($my_count > 0 && $up_count > 0) {
+			// ¼«Ê¬¤Î¥°¥ë¡¼¥×¤Ë²Ã»»
+			lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $up_count);
+			// ¾å¤Î¥°¥ë¡¼¥×¤«¤é¸º»»
+			lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count);
+		}
+	}
+	$objQuery->commit();
+	break;
+case 'down':
+	$objQuery = new SC_Query();
+	$objQuery->begin();
+	$down_id = lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
+	if($down_id != "") {
+		// ²¼¤Î¥°¥ë¡¼¥×¤Îrank¤Ë²Ã»»¤¹¤ë¿ô
+		$my_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
+		// ¼«Ê¬¤Î¥°¥ë¡¼¥×¤Îrank¤«¤é¸º»»¤¹¤ë¿ô
+		$down_count = lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
+		if($my_count > 0 && $down_count > 0) {
+			// ¼«Ê¬¤Î¥°¥ë¡¼¥×¤«¤é¸º»»
+			lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
+			// ²¼¤Î¥°¥ë¡¼¥×¤Ë²Ã»»
+			lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $down_count);
+		}
+	}
+	$objQuery->commit();
+	break;
+case 'tree':
+	break;
+default:
+	$objPage->arrForm['parent_category_id'] = 0;
+	break;
+}
+
+$objPage->arrList = lfGetCat($objPage->arrForm['parent_category_id']);
+$objPage->arrTree = sfGetCatTree($objPage->arrForm['parent_category_id']);
+
+$objView->assignobj($objPage);
+$objView->display(MAIN_FRAME);
+
+//-----------------------------------------------------------------------------------------------------------------------------------
+
+// ¥«¥Æ¥´¥ê¤Î¿·µ¬ÄÉ²Ã
+function lfInsertCat($parent_category_id) {
+	global $objFormParam;
+	
+	$objQuery = new SC_Query();
+	$objQuery->begin();	// ¥È¥é¥ó¥¶¥¯¥·¥ç¥ó¤Î³«»Ï
+	
+	
+	if($parent_category_id == 0) {
+		// ROOT³¬ÁØ¤ÇºÇÂç¤Î¥é¥ó¥¯¤ò¼èÆÀ¤¹¤ë¡£		
+		$where = "parent_category_id = ?";
+		$rank = $objQuery->max("dtb_category", "rank", $where, array($parent_category_id)) + 1;
+	} else {
+		// ¿Æ¤Î¥é¥ó¥¯¤ò¼«Ê¬¤Î¥é¥ó¥¯¤È¤¹¤ë¡£
+		$where = "category_id = ?";
+		$rank = $objQuery->get("dtb_category", "rank", $where, array($parent_category_id));
+		// ÄÉ²Ã¥ì¥³¡¼¥É¤Î¥é¥ó¥¯°Ê¾å¤Î¥ì¥³¡¼¥É¤ò°ì¤Ä¤¢¤²¤ë¡£
+		$sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
+		$objQuery->exec($sqlup, array($rank));
+	}
+	
+	$where = "category_id = ?";
+	// ¼«Ê¬¤Î¥ì¥Ù¥ë¤ò¼èÆÀ¤¹¤ë(¿Æ¤Î¥ì¥Ù¥ë + 1)	
+	$level = $objQuery->get("dtb_category", "level", $where, array($parent_category_id)) + 1;
+	
+	// ÆþÎÏ¥Ç¡¼¥¿¤òÅÏ¤¹¡£
+	$sqlval = $objFormParam->getHashArray();
+	$sqlval['create_date'] = "Now()";
+	$sqlval['update_date'] = "Now()";
+	$sqlval['creator_id'] = $_SESSION['member_id'];
+	$sqlval['parent_category_id'] = $parent_category_id;
+	$sqlval['rank'] = $rank;
+	$sqlval['level'] = $level;
+	
+	// INSERT¤Î¼Â¹Ô
+	$objQuery->insert("dtb_category", $sqlval);
+	
+	$objQuery->commit();	// ¥È¥é¥ó¥¶¥¯¥·¥ç¥ó¤Î½ªÎ»
+}
+
+// ¥«¥Æ¥´¥ê¤ÎÊÔ½¸
+function lfUpdateCat($category_id) {
+	global $objFormParam;
+	$objQuery = new SC_Query();
+	// ÆþÎÏ¥Ç¡¼¥¿¤òÅÏ¤¹¡£
+	$sqlval = $objFormParam->getHashArray();
+	$sqlval['update_date'] = "Now()";
+	$where = "category_id = ?";
+	$objQuery->update("dtb_category", $sqlval, $where, array($category_id));
+}
+
+// ¥«¥Æ¥´¥ê¤Î¼èÆÀ
+function lfGetCat($parent_category_id) {
+	$objQuery = new SC_Query();
+	$col = "category_id, category_name, level, rank";
+	$where = "del_flg = 0 AND parent_category_id = ?";
+	$objQuery->setoption("ORDER BY rank DESC");
+	$arrRet = $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
+	return $arrRet;
+}
+
+/* ¥Ñ¥é¥á¡¼¥¿¾ðÊó¤Î½é´ü²½ */
+function lfInitParam() {
+	global $objFormParam;
+	$objFormParam->addParam("¥«¥Æ¥´¥êÌ¾", "category_name", STEXT_LEN, "KVa", array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
+}
+
+/* ÆþÎÏÆâÍÆ¤Î¥Á¥§¥Ã¥¯ */
+function lfCheckError($array) {
+	global $objFormParam;
+	$objErr = new SC_CheckError($array);
+	$objErr->arrErr = $objFormParam->checkError();
+	
+	// ³¬ÁØ¥Á¥§¥Ã¥¯
+	if(!isset($objErr->arrErr['category_name'])) {
+		$objQuery = new SC_Query();
+		$level = $objQuery->get("dtb_category", "level", "category_id = ?", array($_POST['parent_category_id']));
+		
+		if($level >= LEVEL_MAX) {
+			$objErr->arrErr['category_name'] = "¢¨ ".LEVEL_MAX."³¬ÁØ°Ê¾å¤ÎÅÐÏ¿¤Ï¤Ç¤­¤Þ¤»¤ó¡£<br>";
+		}
+	}
+		
+	// ½ÅÊ£¥Á¥§¥Ã¥¯
+	if(!isset($objErr->arrErr['category_name'])) {
+		$objQuery = new SC_Query();
+		$where = "parent_category_id = ? AND category_name = ?";
+		$arrRet = $objQuery->select("category_id, category_name", "dtb_category", $where, array($_POST['parent_category_id'], $array['category_name']));
+		// ÊÔ½¸Ãæ¤Î¥ì¥³¡¼¥É°Ê³°¤ËÆ±¤¸Ì¾¾Î¤¬Â¸ºß¤¹¤ë¾ì¹ç
+		if ($arrRet[0]['category_id'] != $_POST['category_id'] && $arrRet[0]['category_name'] == $_POST['category_name']) {
+			$objErr->arrErr['category_name'] = "¢¨ ´û¤ËÆ±¤¸ÆâÍÆ¤ÎÅÐÏ¿¤¬Â¸ºß¤·¤Þ¤¹¡£<br>";
+		}
+	}
+
+	return $objErr->arrErr;
+}
+
+
+// ÊÂ¤Ó¤¬1¤Ä²¼¤ÎID¤ò¼èÆÀ¤¹¤ë¡£
+function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
+	// ¿ÆID¤ò¼èÆÀ¤¹¤ë¡£
+	$col = "$pid_name";
+	$where = "$id_name = ?";
+	$pid = $objQuery->get($table, $col, $where, $id);
+	// ¤¹¤Ù¤Æ¤Î»Ò¤ò¼èÆÀ¤¹¤ë¡£
+	$col = "$id_name";
+	$where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
+	$arrRet = $objQuery->select($col, $table, $where, array($pid));
+	$max = count($arrRet);
+	$down_id = "";
+	for($cnt = 0; $cnt < $max; $cnt++) {
+		if($arrRet[$cnt][$id_name] == $id) {
+			$down_id = $arrRet[($cnt + 1)][$id_name];
+			break;
+		}
+	}
+	return $down_id;
+}
+
+// ÊÂ¤Ó¤¬1¤Ä¾å¤ÎID¤ò¼èÆÀ¤¹¤ë¡£
+function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
+	// ¿ÆID¤ò¼èÆÀ¤¹¤ë¡£
+	$col = "$pid_name";
+	$where = "$id_name = ?";
+	$pid = $objQuery->get($table, $col, $where, $id);
+	// ¤¹¤Ù¤Æ¤Î»Ò¤ò¼èÆÀ¤¹¤ë¡£
+	$col = "$id_name";
+	$where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
+	$arrRet = $objQuery->select($col, $table, $where, array($pid));
+	$max = count($arrRet);
+	$up_id = "";
+	for($cnt = 0; $cnt < $max; $cnt++) {
+		if($arrRet[$cnt][$id_name] == $id) {
+			$up_id = $arrRet[($cnt - 1)][$id_name];
+			break;
+		}
+	}
+	return $up_id;
+}
+
+function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
+	// »ÒID°ìÍ÷¤ò¼èÆÀ
+	$arrRet = sfGetChildrenArray($table, $pid_name, $id_name, $id);	
+	return count($arrRet);
+}
+
+function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
+	// »ÒID°ìÍ÷¤ò¼èÆÀ
+	$arrRet = sfGetChildrenArray($table, $pid_name, $id_name, $id);	
+	$line = sfGetCommaList($arrRet);
+	$sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
+	$sql.= "AND del_flg = 0";
+	$ret = $objQuery->exec($sql);
+	return $ret;
+}
+
+function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
+	// »ÒID°ìÍ÷¤ò¼èÆÀ
+	$arrRet = sfGetChildrenArray($table, $pid_name, $id_name, $id);	
+	$line = sfGetCommaList($arrRet);
+	$sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
+	$sql.= "AND del_flg = 0";
+	$ret = $objQuery->exec($sql);
+	return $ret;
+}
 ?>
