HEX
Server: Apache
System: Linux 4485441ca2e2 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 11:03:07 UTC 2025 aarch64
User: (1000)
PHP: 8.2.24
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/ganryu_shop/model/Config.php.161012.bak
<?php

require_once dirname(__FILE__).'/Abstract.php';

class ganryu_shop_model_Config extends ganryu_shop_model_Abstract{
	
	static $configCache = array();


	function getDefault($key){
		$default = array(
			'point_rate_base'=>0,
		);
		if (array_key_exists($key, $default)){
			return $default[$key];
		}
		return null;
	}
	
	function loadCache($key){
		if (!array_key_exists($key, self::$configCache)){
			return false;
		}
		return self::$configCache[$key];
	}
	function saveCache($key, $value){
		if ($value === false){
			unset(self::$configCache[$key]);
			return;
		}
		self::$configCache[$key] = $value;
	}

	function getConfig($key){
		$res = $this->getConfigArray(array($key));
		if ($res === false){
			return false;
		}
		return $res[$key];
	}

	function getConfigArray($ar){
		if (!$ar){
			return array();
		}
		
		$fromcache = array();
	
		$w = "";
		$b = array();
		$idx = 0;
		foreach ($ar as $key){
			$v = $this->loadCache($key);
			if ($v !== false){
				$fromcache[$key] = $v;
				continue;
			}
			if ($w) $w.=",";
			$w .= ":key".$idx;
			$b['key'.$idx] = $key;
			$idx++;
		}
		
		if ($idx == 0){
			return $fromcache;
		}
	
		$tbl = $this->tablename('config');
		$sql = "
			select
				configkey,
				value
			from
				$tbl
			where
				configkey in ($w)
		";
		$bind = $b;
		$res = $this->selectAll($sql, $bind);
		if ($res === false){
			return false;
		}
		$rdata = array();
		foreach ($res as $rd){
			$rdata[$rd['configkey']] = $rd['value'];
		}
		foreach ($ar as $key){
			if (!array_key_exists($key, $rdata)){
				$rdata[$key] = $this->getDefault($key);
			}
			$this->saveCache($key, $rdata[$key]);
		}
		
		$rdata = array_merge($rdata, $fromcache);
		
		return $rdata;
	}
	function setConfig($key, $value){
		return $this->setConfigArray(array($key=>$value));
	}
	
	function setConfigArray($confdata){
		$tbl = $this->tablename('config');
		
		foreach ($confdata as $key=>$value){
		
			$sql = "
				select
					configid,
					configkey
				from
					$tbl
				where
					configkey = :key
			";
			$bind = array('key'=>$key);
			$res = $this->selectRow($sql, $bind);
			if ($res === false){
				return false;
			}
			if (!$res){
				$sql = "
					insert into $tbl(
						configkey,
						value
					)
					values(
						:key,
						:value
					)
				";
				$bind = array('key'=>$key, 'value'=>$value);
				$res = $this->query($sql, $bind);
				if (!$res){
					return false;
				}
				return true;
			}
			else{
				$id = $res['configid'];
				$sql = "
					update $tbl set
						value = :value
					where
						configid = :id
				";
				$bind = array('id'=>$id, 'value'=>$value);
				$res = $this->query($sql, $bind);
				if (!$res){
					return false;
				}
				return true;
			}
			
			$this->saveCache($key, $value);
		}
	}
	

	function getItemTypeList(){
		$key = "itemtypelist";
		$res = $this->getConfig($key);
		if ($res === false){
			return false;
		}
		if (!$res){
			return array(
				'typeid_base'=>1,
				'typelist'=>array(),
			);
		}
		return json_decode($res, true);
	}
	function setItemTypeList($list){
		$key = "itemtypelist";
		$res = $this->setConfig($key, json_encode($list));
		if (!$res){
			return false;
		}
		return true;
	}

	function getDeliveryList(){
		$key = "shop_delivery_list";
		$res = $this->getConfig($key);
		if ($res === false){
			return false;
		}
		if (!$res){
			return array(
				'default'=>array(
					'deliv_all'=>0,
					'deliv_partial'=>array()
				)
			);
		}
		return json_decode($res, true);
	}
	function setDeliveryList($list){
		$key = "shop_delivery_list";
		$res = $this->setConfig($key, json_encode($list));
		if (!$res){
			return false;
		}
		return true;
	}


	function getPrefectureCodeDelivCost($prefcode, $type="default"){
		$list = $this->getDeliveryList();
		if (!$list){
			return false;
		}
		if (!isset($list[$type])){
			return false;
		}
		$delivdata = $list[$type];
		$delivdata = array_merge(array('deliv_all'=>0, 'deliv_partial'=>array()), $delivdata);
		
		if (array_key_exists($prefcode, $delivdata['deliv_partial'])){
			return $delivdata['deliv_partial'][$prefcode];
		}
		return $delivdata['deliv_all'];
	}
	
	function getDelivCostTicket(){
		return 500;	
	}


	function getTaxList(){
		$key = 'tax_config';
		$res = $this->getConfig($key);
		if ($res === false){
			return false;
		}
		if (!$res){
			return array(array('tax'=>'8'));
		}
		return json_decode($res, true);
	}
	
	
	function setTaxList($list){
		$key = 'tax_config';
		$res = $this->setConfig($key, json_encode($list));
		if ($res === false){
			return false;
		}
		return true;
	}
	
	function getCurrentTax(){
		$res = $this->getTaxList();
		if (!$res){
			return false;
		}
		$taxlist = $res;
		if (count($taxlist) == 0){
			return false;
		}
		$rtaxlist = array_reverse($taxlist);
		foreach ($taxlist as $tax){
			if (!isset($tax['start'])){
				return $tax['tax'];
			}
			$tm = time();
			$sttm = strtotime($tax['start']);
			if ($tm > $sttm){
				return $tax['tax'];
			}
		}
		return false;
	}
	
	function sortCallbackTax($a, $b){
		if (isset($a['start']) && isset($b['start'])){
			return strcmp($a['start'], $b['start']);
		}
		if (!isset($a['start']) && isset($b['start'])){
			return -1;
		}
		if (isset($a['start']) && !isset($b['start'])){
			return 1;
		}
		return 0;
	}
	
	
	function getMailTemplate($code){
		$confcode = "mailtemplate.".$code;
		$res = $this->getConfig($confcode);
		if ($res === false){
			return false;
		}
		if (!$res){
			return $this->defaultMailTemplate($code);
		}
		return json_decode($res, true);
	}
	
	function getMailTemplateMiddleSample($code){
		if ($code == "cart_complete" || $code == "cart_complete_admin" || $code == "deliv_complete"){
			return '
-----------------------------------------------
商品1(サイズ:L カラー:白)
¥4200 x1

商品1(サイズ:L カラー:黒)
¥4200 x1

商品合計 ¥8400
税込価格 ¥9000
送料 ¥500
ポイント利用 4000pt

合計 ¥4824

お届け先
〒123-4567
東京都○○○区○○○○○1-2-3
TEL111-222-333

○○○○企業
山田 太郎(ヤマダ タロウ)様

-----------------------------------------------
';
		}
		return null;
	}
	
	function defaultMailTemplate($code){
		if ($code == "cart_complete"){
			return array(
				'mailfrom'=>'',
				'mailto'=>'[[email]]',
				'mailtitle'=>'ご注文承りました',
				'mailheader'=>'[[name_sei]] [[name_mei]]様

ご注文ありがとうございます。

巌流島SHOPより下記商品をご注文を承りました。

',
				'mailfooter'=>'心当たりのない方はお手数ではございますが、下記サイトのお問い合わせまで
ご連絡ください。

巌流島SHOP
http://ganryujima.jp
',
			);
		}
		if ($code == "cart_complete_admin"){
			return array(
				'mailfrom'=>'',
				'mailto'=>'',
				'mailtitle'=>'注文がありました(注文No:[[purchasecode]])',
				'mailheader'=>'下記注文を受け付けました。

[[admin_url]]
',
				'mailfooter'=>'
巌流島SHOP
http://ganryujima.jp
',
			);
		}
		if ($code == "deliv_complete"){
			return array(
				'mailfrom'=>'',
				'mailto'=>'[[email]]',
				'mailtitle'=>'配送しました',
				'mailheader'=>'[[name_sei]] [[name_mei]]様

ご注文いただいた下記商品を配送いたしました。

',
				'mailfooter'=>'心当たりのない方はお手数ではございますが、下記サイトのお問い合わせまで
ご連絡ください。

巌流島SHOP
http://ganryujima.jp
',
			);
		}
	}
		
	function setMailTemplate($code, $data){
		$confcode = "mailtemplate.".$code;
		$res = $this->setConfig($confcode, json_encode($data));
		if ($res === false){
			return false;
		}
		return true;
	}

}