<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * デザインテンプレートマスタDBアクセスクラス
 * @author 	yamamoto
 * @version	
 */
class skinDB extends Model
{
	var $_form = null;
	var $erchk = 'server';//quickformエラーチェックのフラグ'client'or'server;
	var $db=null;
/*
DROP TABLE IF EXISTS tbl_skin CASCADE;
CREATE TABLE tbl_skin (
	skin_code			serial,				--デザインテンプレートマスタコード
	skin_name			text not null,		--デザインテンプレート名（識別名称）
	skin_type			text,				--デザインテンプレートタイプ（ナビタイプ、サイドバーあり、なし等）
	skin_side			smallint default 0,	--サイドバー有り・無し 1:有り
	skin_ver			text,				--バージョン
	skin_author			text,				--作者
	skin_path			text not null,		--ディレクトリ名
	skin_active			smallint default 1,	--削除:0 有効:1
	skin_use			smallint default 0, --1:利用する 0:利用しない
	primary key (skin_code)
);
*/
	//KVa => 半カナを全カナ,濁点つきの文字を１文字に変換する (K、H と共に利用する）,全角英数字を半角英数字に変換する
	//KVC => 半カナを全カナ,濁点つきの文字を１文字に変換する (K、H と共に利用する）,全角ひらがなを全角カタカナに変換する
	//n   => 全角数字を半角数字に変換する
	var $columns = array(
						array("column"=>"skin_code","conv"=>"n","tags"=>false),			//登録番号
						array("column"=>"skin_name","conv"=>"KVa","tags"=>false),		//ユテンプレート名
						array("column"=>"skin_type","conv"=>"KVa","tags"=>false),		//デザインテンプレートタイプ（ナビタイプ、サイドバーあり、なし等）
						array("column"=>"skin_side","conv"=>"n","tags"=>false),			//サイド有り無し
						array("column"=>"skin_ver","conv"=>"KVa","tags"=>false),		//version
						array("column"=>"skin_author","conv"=>"KVa","tags"=>false),		//作者
						array("column"=>"skin_path","conv"=>"KVa","tags"=>false),		//ディレクトリ名 /www/usr-data/template/○○○○
						array("column"=>"skin_active","conv"=>"n","tags"=>false),		//削除:0 有効:1
						array("column"=>"skin_use","conv"=>"n","tags"=>false)			//利用可否
						);
						
	function skinDB()
	{
		$this->__construct();
	}
	function __construct()
	{
		Model::__construct();
		$this->db = new DataBase();
	}
	
	//フォームエレメントの作成
	function setElements()
	{

	}
	//フォーム入力エラーチェックの設定
	function setRules()
	{


	}
	//ボタン作成
	function setButtons($frozen = false) {

	}
	
	/**
	 * 一覧取得
	 * @param $type 
	 * @return 
	 */
	function getList($skin_use=null)
	{
		$whrsql=null;
		$whrdata=null;
		
		$sql = "select * from tbl_skin where skin_active = 1 ";
		$order = " order by skin_use desc, skin_name, skin_code desc";
		if(is_numeric($skin_use)){
			 $whrsql[] = " skin_use = ?";
			 $whrdata[] = $skin_use;
		}
		if(count($whrdata) > 0) {
			$sql .= "and ".implode(" and ", $whrsql);
			$result = $this->db->prepare($sql.$order,$whrdata);
		} else {
			$result = $this->db->query($sql.$order);
		}
		$rtn=null;
		if($result != null) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$rtn[$row["skin_code"]] = $row;
			}
		}
		return $rtn;
	}
	
	function getData($skin_code=null,$skin_path=null) {
		$data = null;
		$sql = "select * from tbl_skin ";
		if(is_numeric($skin_code)){
			 $whrsql[] = " skin_code = ?";
			 $whrdata[] = $skin_code;
		}
		if($skin_path!=null) {
			$whrsql[] = " skin_path like ?";
			$whrdata[] = $skin_path;
		}
		if(count($whrdata) > 0) {
			$sql .= "where ".implode(" and ", $whrsql);
			$result = $this->db->prepare($sql,$whrdata);
		} else {
			$result = $this->db->query($sql);
		}
		if($result != null) {
			return $result->fetchRow(DB_FETCHMODE_ASSOC);
		}
		return $data;
	}
	
	function getSetting($skin_path) {
		if(isset($skin_path)) {
			if(is_file(TEMPLATE_PATH."/".$skin_path."/config.inc")) {
				require_once(TEMPLATE_PATH."/".$skin_path."/config.inc");
			}
			$setting = array(
						"LOGO"=>$LOGO,
						"LOGOSAMPLE"=>isset($LOGOSAMPLE)?$LOGOSAMPLE:FALSE,
						"LOGOWIDTH"=>isset($LOGOWIDTH)?$LOGOWIDTH:100,
						"LOGOHEIGHT"=>isset($LOGOHEIGHT)?$LOGOHEIGHT:50,
						"HEADERBG"=>isset($HEADERBG)?$HEADERBG:FALSE,
						"HEADERBGSAMPLE"=>$HEADERBGSAMPLE,
						"HEADERBGWIDTH"=>isset($HEADERBGWIDTH)?$HEADERBGWIDTH:700,
						"HEADERBGHEIGHT"=>isset($HEADERBGHEIGHT)?$HEADERBGHEIGHT:80,
						"HEADERCAP"=>isset($HEADERCAP)?$HEADERCAP:FALSE,
						"HEADERCAPSTRING"=>isset($HEADERCAPSTRING)?$HEADERCAPSTRING:"このサイトはbingoCMSによって生成されています",
						"HEADERBANNER"=>isset($HEADERBANNER)?$HEADERBANNER:FALSE,
						"HEADERBANNERSAMPLE"=>$HEADERBANNERSAMPLE,
						"HEADERBANNERWIDTH"=>isset($HEADERBANNERWIDTH)?$HEADERBANNERWIDTH:200,
						"HEADERBANNERHEIGHT"=>isset($HEADERBANNERHEIGHT)?$HEADERBANNERHEIGHT:50,
						"HEADERBANNERLINK"=>$HEADERBANNERLINK,
						"HEADERNAVI"=>isset($HEADERNAVI)?$HEADERNAVI:FALSE,
						"HEADERNAVISTRING"=>array(),
						"FOOTERCAP"=>isset($FOOTERCAP)?$FOOTERCAP:FALSE,
						"FOOTERCAPSTRING"=>isset($FOOTERCAPSTRING)?$FOOTERCAPSTRING:"Copyright©Shift Tech Inc., Japan, 2010 All rights reserved.",
						"FOOTERNAVI"=>isset($FOOTERNAVI)?$FOOTERNAVI:FALSE,
						"FOOTERNAVISTRING"=>array(),
						"SIDEBARAREA"=>isset($SIDEBARAREA)?$SIDEBARAREA:FALSE,
						"USERAREAWIDTH"=>isset($USERAREAWIDTH)?$USERAREAWIDTH:900,
						"SIDEBARAREAWIDTH"=>isset($SIDEBARAREAWIDTH)?$SIDEBARAREAWIDTH:100,
			);
			$setting["HEADERNAVISTRING"] = $HEADERNAVISTRING;
			$setting["FOOTERNAVISTRING"] = $FOOTERNAVISTRING;
			return $setting;
		} else {
			return null;
		}
	}
	
	
	/*
	 * テンプレートコードからTPLのディレクトリパスを取得
	 * @param $skin_data
	 * @return $str 
	 */
	function getSkinPath($skin_data) {
		$path = null;
		if(isset($skin_data["skin_path"])) {
			return TEMPLATE_PATH."/".$skin_data["skin_path"]."/index.tpl";
		}
		return ERR404TPL;
	}
	
	/*
	 * skin_useのステータス変更
	 * return boolean
	 */
	function setSkinUse($chg_skin_code,$chg_skin_use) {
		if(is_numeric($chg_skin_code)&&is_numeric($chg_skin_use)){
			$this->db->query('BEGIN');
			if($this->db->update("tbl_skin", array("skin_use"=>$chg_skin_use),"skin_code=".$chg_skin_code)) {
				$this->db->query('COMMIT');
				return true;
			}
			$this->db->query('ROLLBACK');
			return false;
		}
		return false;
	}
	
	function delete($skin_code) {
		if(is_numeric($skin_code)){
			//利用中チェック
			$sql = "select count(*) from tbl_template where tpl_active=1 and tpl_skin_code = ".$skin_code;
			$result = $this->db->getOne($sql);
			if($result>0) {
				return "テンプレート管理にて利用中のため削除できません。";
			}
			$this->db->query('BEGIN');
			if($this->db->update("tbl_skin",array("skin_active"=>0),"skin_code=".$skin_code)) {
				$this->db->query('COMMIT');
				return true;
			} else {
				return false;	
			}
		}
		return "削除コードが不明です。";
	}
}
?>