t_goods").$op;
		$result =& $this->db->query($sql, $limt, $start);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			while ($row = $this->db->fetchArray($result)) {
				$this->goods[$row['gid']] = $this->myts->htmlSpecialChars($row['top']);
				$this->goods_row[$row['gid']] = $row;
			}
			$this->goodsdata = true;
		} else {
			$this->goodsdata = false;
		}
	}


	function hits_count($gid)
	{
		$sql = "UPDATE ".$this->db->prefix("hisacart_goods")." SET hit = hit+1 WHERE gid = ".$gid;
		$result = $this->result_sql($sql);
	}

	function _buy_goods_count($gid)
	{
		$sql = "SELECT count(*) as c,gid FROM ".$this->db->prefix("hisacart_buytable")." WHERE gid = ".$gid." GROUP BY gid";
		return $this->_result($sql);
	}

	function _goods_count($gcid)
	{
		$sql = "SELECT count(*) as c,gcid FROM ".$this->db->prefix("hisacart_goods")." WHERE gcid = ".$gcid." GROUP BY gcid";
		return $this->_result($sql);
	}

	function _category($gcid)
	{
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_category")." WHERE gcid = ".$gcid;
		return $this->_result($sql);
	}

	function _hisacart_buyuser($id)
	{
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_buyuser")." WHERE id = ".$id;
		return $this->_result($sql);
	}

	function _goods_next($gid)
	{
		$sql = "SELECT gid,gcid FROM ".$this->db->prefix("hisacart_goods")." WHERE gid > ".$gid;
		return $this->_result($sql, 1);
	}

	function _goods($gid, $on_view = NULL)
	{
		$op = "";
		if ($on_view) $op = " AND on_view = 1";
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_goods")." WHERE gid = ".$gid. $op;
		return $this->_result($sql);
	}

	function _goods_plural($gid_arr)
	{
		$n_gid = array_map(create_function('$a', 'return intval($a);'), $gid_arr);
		$op = implode(' OR gid = ', $n_gid);
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_goods")." WHERE gid =".$op;
		return $this->_result_rows($sql);
	}

	function _goods_rev($gid)
	{
		$sql = "SELECT gid,gcid FROM ".$this->db->prefix("hisacart_goods")." WHERE gid < ".$gid;
		return $this->_result($sql, 1);
	}

	function _goods_stock($gid)
	{
		$sql = "SELECT gid,stock FROM ".$this->db->prefix("hisacart_goods")." WHERE gid = ".$gid;
		return $this->_result($sql, 1);
	}

	function _hisacart_category($gid)
	{
		$sql = "SELECT gid,gcid FROM ".$this->db->prefix("hisacart_goods")." WHERE gid = ".$gid;
		$res = $this->_result($sql);
		$sql = "SELECT gcid,top,images FROM ".$this->db->prefix("hisacart_category")." WHERE gcid = ".$res['gcid'];
		return $this->_result($sql);
	}

	function _pickup($on_view = NULL)
	{
		$today = date('Y-m-d', time());
		$op = '';
		$user = array();
		if ($on_view) $op = " AND tt.on_view = 1 AND t.type = 0";
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_pickup")." as t LEFT OUTER JOIN ".$this->db->prefix("hisacart_goods")." as tt USING(gid) WHERE t.ondays <= '".$today."' AND t.offdays >= '".$today."'".$op;
		$result =& $this->db->query($sql);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			while ($row = $this->db->fetchArray($result)) {
				$this->pickup_row[$row['gid']] = $row;
			}
			return $this->pickup_row;
		}
		return false;
	}

	function _osusume($on_view = NULL)
	{
		$op = '';
		$user = array();
		if ($on_view) $op = " AND tt.on_view = 1";
		$sql = "SELECT * FROM ". $this->db->prefix("hisacart_goods") . " as tt WHERE tt.osusume =1 " . $op . "  ORDER BY days DESC"; 
		$result =& $this->db->query($sql);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			while ($row = $this->db->fetchArray($result)) {
				$this->pickup2_row[$row['gid']] = $row;
			}
			return $this->pickup2_row;
		}
		return false;
	}




	function _pickup_lock()
	{
		$today = date('Y-m-d', time());
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_pickup")." as t LEFT OUTER JOIN ".$this->db->prefix("hisacart_goods")." as tt USING(gid) WHERE t.type = 1 AND t.ondays <= '".$today."' AND t.offdays >= '".$today."' AND tt.on_view = 1";
		return $this->_result($sql);
	}

	function _ranking()
	{
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_goods")." WHERE rank > 0 ORDER BY rank DESC";
		return $this->_result_rows($sql, 10, 0);
	}

	function _order_status($sess)
	{
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_goods")." as t LEFT OUTER JOIN ".$this->db->prefix("hisacart_buytable")." as tt USING(gid) WHERE tt.sessions = '".$sess."' AND tt.msend = 1";
		$result =& $this->db->query($sql);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			return $this->_arr_add($result);
		}
		return false;
	}

	function _order_inquiry($uniq)
	{
		$sql = "SELECT * , tt.cool as cool_charge, tt.daibiki_fee as collect_charge FROM ".$this->db->prefix("hisacart_goods")." as t LEFT OUTER JOIN ".$this->db->prefix("hisacart_buytable")." as tt USING(gid) WHERE tt.uniq = '".$uniq."' AND tt.falseness = 't'";
		$result =& $this->db->query($sql);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
//			return false;
			return $this->_arr_add($result);
		}
	
		return false;
	}

	function _order_history($id)
	{
		$user = array();
		$arr = array();

		$sql = "SELECT * , tt.cool as cool_charge, tt.daibiki_fee as collect_charge FROM ".$this->db->prefix("hisacart_goods")." as t LEFT OUTER JOIN ".$this->db->prefix("hisacart_buytable")." as tt USING(gid) WHERE tt.id = ".$id ." AND tt.falseness = 't'";

		$result =& $this->db->query($sql);

		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {

			while ($row = $this->db->fetchArray($result)) {
				$arr[] = $row;

			}
			return $arr;
		}

		return false;
	}


	function _arr_add($result)
	{
		$user = array();
		$arr = array();
		while ($row = $this->db->fetchArray($result)) {
			$arr[] = $row;
		}
		return $arr;
	}

	function hisa_admin_notation_chack()
	{
		$row = array();
		$row2 = array();
		
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_main_setting");
		$result =& $this->db->query($sql);
		$row = $this->db->fetchArray($result);
		$sql2 = "SELECT * FROM ".$this->db->prefix("hisacart_notation");
		$result2 =& $this->db->query($sql2);
		$row2 = $this->db->fetchArray($result2);

		if ($row) {
			if($row2)
			{
				$this->dat = $row2;
				$this->dat = array_merge($this->dat, $row);
			}
			else
				$this->dat = $row;
			
			return true;
		}
		return false;
	}

	function hisa_rev_select($gid, $page = 0)
	{
		$start = ($page) ? $page*5:0;
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_review")." WHERE gid = ".$gid." ORDER BY days DESC";
		$dat = $this->_result_rows($sql, 5, $start);
		if ($dat) {
			return $this->_array_walks($dat);
		}
		return false;
	}

	function hisa_rev_count($gid)
	{
		$sql = "SELECT count(*) as c FROM ".$this->db->prefix("hisacart_review")." WHERE gid = ".$gid;
		return $this->_result($sql);
	}

	function hisa_rev_delete($rid)
	{
		$sql = 'DELETE FROM '. $this->db->prefix("hisacart_review"). ' WHERE rid = '. $rid ;
		return $this->result_sql($sql);
	}


	function hisa_rev_insert($arr)
	{
		$column = $this->sql_parse_insert($arr);
		$sql ="INSERT INTO ".$this->db->prefix("hisacart_review")." (".implode(",", $column['key']).", days) VALUES(".implode(",", $column['val']).", NOW())";
		return $this->result_sql($sql);
	}

	function get_prefecture_list()
	{
		return $this->prefecture;
	}

	function get_welcome()
	{
		$dat = $this->hisa_admin_notation_chack();
		$welcome['top'] = $this->myts->htmlSpecialChars($this->dat['welcometop']);
		if ($this->dat['welcometype'] == 1) {
			$welcome['main'] = $this->myts->displayTarea($this->dat['welcome'], 1, 0, 0, 0, 0);
		} else {
			$welcome['main'] = $this->myts->displayTarea($this->dat['welcome'], 0, 1, 1, 1, 1);
		}
		return $welcome;
	}

	function get_use_im()
	{
		$dat = $this->hisa_admin_notation_chack();
		return $this->dat['use_im'];
	}

	function get_use_ssl()
	{
		$dat = $this->hisa_admin_notation_chack();
		return $this->dat['usessl'];
	}

	function get_sid()
	{
		$dat = $this->hisa_admin_notation_chack();
		return $this->dat['sid'];
	}

	function get_category_check($on_view = NULL)
	{
		$this->make_categorys($on_view);
		return $this->categorydata;
	}

	function get_goods_check($gcid = NULL)
	{
		$this->make_goods($gcid);
		return $this->goodsdata;
	}

	function get_goods_array($gcid = NULL)
	{
		$this->make_goods($gcid);
		if (isset($this->goods)) {
			return $this->_array_hSC_walk($this->goods);
		}
		return false;
	}

	function get_goods_list($gcid, $item = NULL, $limt = 0, $start = 0)
	{
		$this->make_goods($gcid, $item, $limt, $start);
		if (isset($this->goods_row)) {
			return $this->_array_myts_walk($this->goods_row);
		}
		return false;
	}

	function get_all_goods_list($item = NULL, $limt = 0, $start = 0)
	{
		$this->make_all_goods($item, $limt, $start);
		if (isset($this->goods_row)) {
			return $this->_array_myts_walk($this->goods_row);
		}
		return false;
	}


	function get_categorys_array($on_view = NULL)
	{
		$this->make_categorys($on_view);
		if (isset($this->categorys)) {
			return $this->_array_hSC_walk($this->categorys);
		}
		return false;
	}


	function get_category_view($on_view = NULL)
	{
		$this->make_categorys($on_view);
		if (isset($this->category_row)) return $this->_array_walks($this->category_row);
		return false;
	}

	function get_goods_plural($gid_arr)
	{
		$res = $this->_goods_plural($gid_arr);
		if ($res) return $this->_array_walks($res);
		return false;
	}

	function get_goods_view($gid = NULL, $on_view = NULL)
	{
		$dat = $this->_goods($gid, $on_view);
		if ($dat) {
			return $this->_array_myts_walk($dat);
		}
		return false;
	}
	
	function get_goods_form($gid = NULL, $on_view = NULL)
	{
		$dat = $this->_goods($gid, $on_view);
		if ($dat) {
			return $this->_array_hSC_walk($dat);
		}
		return false;
	}

	function get_category($gcid)
	{
		$row = $this->_category($gcid);
		if ($row) {
			return $this->_array_myts_walk($row);
		}
		return false;
	}

	function get_goods_next($gid)
	{
		$dat = $this->_goods_next($gid);
		if ($dat) {
			return intval($dat['gid']);
		}
		return false;
	}

	function get_goods_rev($gid)
	{
		$dat = $this->_goods_rev($gid);
		if ($dat) {
			return intval($dat['gid']);
		}
		return false;
	}

	function get_pickup_detail($on_view = "ON")
	{
		$src = array();
		$dat = array();
		$row = $this->_pickup($on_view);
		if (! $row) return _HISACART_PICKUP_NONE;
		$i = 0;
		foreach ($row as $val) {
			if (! $val) break;
			$dat = $this->get_goods_detail($val['gid'], $on_view);
			$src[] = $dat;
			$i++;
			if ($i == 10) break;
		}
		return $src;
	}

	function get_osusume_detail($on_view = "ON")
	{
		$src = array();
		$dat = array();
		$row = $this->_osusume($on_view);
		if (! $row) return _HISACART_OSUSUME_NONE;
		$i = 0;
		foreach ($row as $val) {
			if (! $val) break;
			$dat = $this->get_goods_detail($val['gid'], $on_view);
			$src[] = $dat;
			$i++;
			if ($i == 10) break;
		}
		return $src;
	}



	function get_ranking_detail()
	{
		$src = array();
		$dat = array();
		$row = $this->_ranking();
		foreach ($row as $val) {
			if (! $val) break;
			$dat = $this->_array_myts_walk($val);//$this->get_goods_detail($val['gid']);
			$src[] = $dat;
		}
		return $src;
	}

	function get_goods_detail($gid, $on_view = "ON")
	{
		$dat = $this->get_goods_view($gid);
		$dat['rev'] = $this->get_goods_rev($gid);
		$dat['next'] = $this->get_goods_next($gid);
		$category = $this->_hisacart_category($gid);
		$dat['categoryid'] = intval($category['gcid']);
		$dat['categorytop'] = $this->myts->htmlSpecialChars($category['top']);
		$dat['categoryimage'] = $this->myts->htmlSpecialChars($category['images']);
		$src = $this->_buy_goods_count($gid);
		$dat['buy_rank'] = $src['c'];
		return $dat;
	}

	function get_notation_view()
	{
		$res = $this->hisa_admin_notation_chack();
		if ($this->dat && isset($this->dat['shop_name'])) {
			return $this->_array_myts_walk($this->dat);
		}
		return false;
	}

	function get_notation()
	{
		$res = $this->hisa_admin_notation_chack();
		if (isset($this->dat)) {
			return $this->_array_hSC_walk($this->dat);
		}
		return false;
	}

	function _array_walks($arr)
	{
		$user = array();
		$src = array();
		foreach ($arr as $val) {
			array_walk($val, 'myts_call_back', $user);
			$src[] = $val;
		}
		return $src;
	}

	function _array_myts_walk($arr)
	{
		$user = array();
		array_walk($arr, 'myts_call_back', $user);
		return $arr;
	}

	function _array_hSC_walk($arr)
	{
		$user = array();
		array_walk($arr, 'myts_hSC_call_back', $user);
		return $arr;
	}

	function get_transporter_name($transport)
	{
		switch($transport) {
		case 0:
			return  _HISACART_SAGAWA;
		case 1:
			return _HISACART_KURONEKO;
		case 2:
			return  _HISACART_PELICAN;
		case 3:
			return _HISACART_UPACK;
		}
	}

	function get_transporter_url($transport)
	{
		switch($transport) {
		case 0:
			return 'http://k2k.sagawa-exp.co.jp/p/sagawa/web/okurijoinput.jsp';
		case 1:
			return 'http://toi.kuronekoyamato.co.jp/cgi-bin/tneko?init';
		case 2:
			return 'http://www19.nittsu.co.jp/confirm/index.php';
		case 3:
			return 'http://tracking.post.japanpost.jp/service/jsp/refi/DP311-00100.jsp';
		}
	}

/*********** add for category **********/
	function _catget($on_view)
	{
		$catgetdata = array();
		
		$op = '';
		if ($on_view) $op = " WHERE on_view = 1";
		$order = " ORDER BY gcid DESC";
		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_category").$op.$order;
		$result =& $this->db->query($sql);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			while ($row = $this->db->fetchArray($result)) {
				$catgetdata[$row['gcid']] = $row;
			}
			return $catgetdata;
		}
		return false;
	}

	function hisa_admin_transportdata_get()
	{
		$row = array();
		$retdata = array();


		$sql = "SELECT * FROM ".$this->db->prefix("hisacart_transport");
		$result =& $this->db->query($sql);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			while ($row = $this->db->fetchArray($result)) {
				$retdata[$row['tid']] = $row;
			}
			return $this->_array_hSC_walk($retdata);
//			return $retdata;
		}
		return false;
	}





}

class hisa_cart_db
{
	var $db;
	var $myts;

	function hisa_cart_db()
	{
		global $xoopsDB;
		$this->myts = icms_core_Textsanitizer::getInstance();
		$this->db = & $xoopsDB;
		if (empty($this->db) || ! is_object($this->db)) $this->set_BD_instance();
	}

	function set_BD_instance()
	{
		$this->db = icms_db_Factory::instance();
	}

	function set_BD_this_connection()
	{
		return $this->db;
	}

	function & getInstance()
	{
		static $instance;
		if (! isset($instance)) {
			$instance = new hisa_cart_user();
		}
		return $instance;
	}

	function result_sql($sql)
	{
		$result = $this->db->queryF($sql);
		return $result;
	}


	function _result($sql, $limit=0, $start=0)
	{
		$result =& $this->db->query($sql, $limit, $start);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			return $this->db->fetchArray($result);
		} else {
			return false;
		}
	}

	function _result_rows($sql, $limit=0, $start=0)
	{
		$src = array();
		$result =& $this->db->query($sql, $limit, $start);
		if (! $this->db->error($result) && $this->db->getRowsNum($result) > 0) {
			while ($row = $this->db->fetchArray($result)) {
				$src[] = $row;
			}
			return $src;
		} else {
			return false;
		}
	}

	function get_db_instance()
	{
		return $this->db;
	}

	function get_ts_instance()
	{
		return $this->myts;
	}

	function sql_parse_insert($sql)
	{
		$column = array();
		foreach ($sql as $key => $val) {
			$column['key'][] = $key;
			$column['val'][] = "'".$this->sql_escape_strings($val)."'";
		}
		return $column;
	}

	function sql_parse_update($sql)
	{
		$column = array();
		foreach ($sql as $key => $val) {
			$column[] = $key." = '".$this->sql_escape_strings($val)."'";
		}
		$column = implode(",", $column);
		return $column;
	}

	function sql_escape_strings($text) {
//		if (XOOPS_DB_TYPE == 'pgsql' && function_exists('pg_escape_string')) {
//			return pg_escape_string($text);
//		} else if (XOOPS_DB_TYPE == 'mysql' && function_exists('mysql_real_escape_string')) {
//			return mysql_real_escape_string($text);
//		} else if (XOOPS_DB_TYPE == 'mysql' && function_exists('mysql_escape_string')) {
//			return mysql_escape_string($text);
//		} else {
//			return $this->myts->addSlashes($text);
//		}
		return this->db->quoteString($text);
	}

	function _convert_alnum($str)
	{
		return preg_replace("/[^0-9]/i", "", mb_convert_kana($str, "a"));
	}

}

function myts_call_back(&$val, $key, &$user)
{
	$h_myts = icms_core_Textsanitizer::getInstance();
	if (is_string($val)) {
		$val = $h_myts->displayTarea($val, 0, 1, 1, 1, 1);
		$user[$val] = $key;
	} else if (is_int($val)) {
		$val = intval($val);
		$user[$val] = $key;
	}
}

function myts_hSC_call_back(&$val, $key, &$user)
{
	$h_myts = icms_core_Textsanitizer::getInstance();
	if (is_string($val)) {
		$val = $h_myts->htmlSpecialChars($val);
		$user[$val] = $key;
	} else if (is_int($val)) {
		$val = intval($val);
		$user[$val] = $key;
	}
}





?>
