<?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/>.
 */

require_once (LIB_PATH.'/common/sectionIdxDB.php');
require_once (LIB_PATH.'/common/pageIdxDB.php');
require_once (LIB_PATH.'/common/pageunitDB.php');
require_once (LIB_PATH.'/common/pagecontainerDB.php');
require_once (LIB_PATH.'/common/templateDB.php');
require_once (LIB_PATH.'/common/sidebarDB.php');
class pageView extends pageunitDB
{
	var $_prev = false; //表示モード true:TMPデータ　false：公開データ
	var $_mprev = 0; //表示モード 1:編集中メニューで表示　0：公開中メニューで表示
	var $_dir = null; //セクションディレクトリ名
	var $_catdir = null; //カテゴリディレクトリ名
	var $_sidx_data = null; //セクション基本情報
	var $_cat_data = null; //カテゴリ基本情報
	var $_pidx_data = null; //ページデータ
	var $_pidx_url = null;  //ページurl
	var $_container = null; //コンテナ情報配列
	var $_unit = null;		//ユニット情報配列
	var $_header = null;	//ヘッダー登録内容配列
	var $_footer = null;	//フッター登録内容配列
	var $_skinsetting= null;	//ヘッダー、フッター、サイド、テンプレート毎の設定値 /template/○○○/config.incを読み込む
	var $_gmenu = null;		//グローバルメニュー配列
	var $_smenu = null;		//サブナビ
	var $_skin_data = null; //デザインテンプレートデータ
	var $_sidebar = null;
	//var $_allunitlist = null; //1ページを構成するユニットパーツを配列で格納、これを元にunit別CSSを読み込む
	var $_allunitCSS = null; //unit CSSインクルードhtml文
	
	var $_tpl_editting = false; //テンプレート編集画面中かどうか

	var $_bingotopurl = null;

	function pageView()
	{
		$this->__construct();
	}
	function __construct()
	{
		if(isset($_SERVER['HTTPS'])) {
			$this->_bingotopurl = WWW_SSL;
		} else {
			$this->_bingotopurl = WWW;
		}
		pageunitDB::__construct();
		$this->db = new DataBase();
	}

	function setViewMode() {
		if ($this->getRequest("prev")) {
			$this->_prev = true;//プレビューモード
		}
	}
	function setMenuViewMode() {
		if ($this->getRequest("mprev")) {
			$this->_mprev = 1;//プレビューモード
		}
	}

	function setSidxDir() {
		if ($this->getRequest("d")) {
			$this->_dir = $this->getRequest("d");
		}
	}

	function setCatDir() {
		if ($this->getRequest("c")) {
			$this->_catdir = $this->getRequest("c");
		}
	}

	function setPidxCode() {
		if ($this->getRequest("pidx_code")) {
			$this->_pidx_code = $this->getRequest("pidx_code");
		}
	}
	function setPidxUrl() {
		if ($this->getRequest("p")) {
			$this->_pidx_url = str_replace("index.html","",$this->getRequest("p"));
			$this->_pidx_url = str_replace(".html","",$this->getRequest("p"));
		}
	}

	function initPage($view=null) {
		//$this->setPidxCode();
		$this->setPidxUrl();
		$this->setViewMode(); //$this->_prevセット
		$this->setMenuViewMode(); //$this->_mprevセット
		$this->setSidxDir(); //$this->_dirセット
		$this->_sidx_data = sectionIdxDB::getSidxFromDir($this->_dir,$this->_mprev); //セクション基本情報取得

		//テンプレートデータの取得
		$templateDB = new templateDB();
		if($this->getRequest("tpl_code")) { //テンプレートプレビュー時のリクエスト
			$tpl_code = $this->getRequest("tpl_code");
		} else {
			$tpl_code = $this->_sidx_data["sidx_tpl_code"];
		}
		$this->_template_data = $templateDB->getData($tpl_code);
		if(is_array($this->_template_data)) {
			foreach($this->_template_data as $key => $val) {
				if(ereg('^skin_', $key)) {
					$this->_skin_data[$key] = $val;
				}
			}
		}
		
		//デザインテンプレートデータ
		$skinDB = new skinDB();
		$this->_sidx_data["skin_path"] = skinDB::getSkinPath($this->_skin_data); //デザインテンプレートパス
		$this->_skinsetting = $skinDB->getSetting($this->_skin_data["skin_path"]);

		//$this->_sidx_codeセット
		if(isset($this->_sidx_data["sidx_code"])&&is_numeric($this->_sidx_data["sidx_code"])){
			$this->_sidx_code = $this->_sidx_data["sidx_code"];
		} else {
			return false;
		}
		$this->setCatDir();
		$this->_cat_data = categoryDB::getCatFromDir($this->_sidx_code,$this->_catdir,$this->_mprev);
		if(isset($this->_cat_data["cat_code"])&&is_numeric($this->_cat_data["cat_code"])){
			$this->_cat_code = $this->_cat_data["cat_code"];
		} else {
			return false;
		}

		//ヘッダー情報の取得
		$this->_header = $this->getHeader($this->_template_data);
		//フッター情報の取得
		$this->_footer = $this->getFooter($this->_template_data);

		//グローバルメニュー（セクションリスト）の取得
		$this->_gmenu = $this->getSectionMenu();
		//サブ
		$this->_smenu = $this->getCategoryMenu($this->_sidx_data);

		//サイドバー
		if($this->_skinsetting["SIDEBARAREA"]&&$view!=null) {
			$sidebarDB=new sidebarDB();
			$side_units_ar = $sidebarDB->getData($tpl_code);
			$tmp = $sidebarDB->makeUnitListHTML($view,$side_units_ar);
			if(is_array($tmp)) {
				$this->_sidebar = implode("",$tmp);
			}
		}
		//テンプレートファイルパス
		$this->_skincss_path = $this->_bingotopurl.TEMPLATE_DIR."/".$this->_skin_data["skin_path"]."/css/imported.css";
		
		//すべてのコンテナ、ユニットCSSのよみこみ
		$this->_allunitCSS = $this->setAllUnitsCSS($this->_bingotopurl);
		if($view!=null) {
			$view->assign('UNITSCSS', $this->_allunitCSS);
		}
		
		return true;
	}

	function getPidxData() {
		$tbl = ($this->_prev) ? "tbl_page_idx_tmp" : "tbl_page_idx";
		$sql = "select * from ".$tbl;
		$sql .= " where pidx_active = 1 ";
		$whrdata = array();
		if($this->_pidx_url!=null){
			$sql .= " and pidx_url like ? ";
			$whrdata[] = $this->_pidx_url;
		} else {
			$sql .= " and pidx_url is null";
		}
		if(is_numeric($this->_sidx_code)){
			$sql .= " and pidx_sidx_code = ? ";
			$whrdata[] = $this->_sidx_code;
		}
		if(is_numeric($this->_cat_code)){
			$sql .= " and pidx_cat_code = ? ";
			$whrdata[] = $this->_cat_code;
		}
		if(!$this->_prev) {
			$sql .= " and pidx_view = 2";
			$nowdate = date('Y-m-d');
			$sql .= " and (pidx_date <= '{$nowdate}' and (pidx_edate is null or pidx_edate >= '{$nowdate}'))";
		}
		$order = " order by pidx_ptype desc, pidx_date desc limit 1";
		
		if(count($whrdata)>0) {
			$result = $this->db->prepare($sql.$order, $whrdata);
		} else {
			$result = $this->db->query($sql.$order);
		}
		if ($result != null) {
			return $result->fetchRow(DB_FETCHMODE_ASSOC);
		} else {
			return null;
		}
	}
	
	
	function getPageData($view, $usecache=false) {
		$this->_pidx_data = $this->getPidxData();
		if(isset($this->_pidx_data["pidx_code"])&&is_numeric($this->_pidx_data["pidx_code"])){
			$this->_pidx_code = $this->_pidx_data["pidx_code"];
			//SEO META,解析タグ取得
			if($this->_pidx_data["pidx_keywords"]) {
				$view->assign('METAKEY', $this->_pidx_data["pidx_keywords"]);
			}
			if($this->_pidx_data["pidx_description"]) {
				$view->assign('METADESC', $this->_pidx_data["pidx_description"]);
			}
			if($this->_pidx_data["pidx_analyticstags"]) {
				$view->assign('ANALYTICS', $this->_pidx_data["pidx_analyticstags"]);
			}
			if($this->_prev || $this->_mprev>0) {
				//プレビューの場合はアクセス解析させない
				$view->assign('ANALYTICS', '');
			}
			
		} else {
			return null;
		}
		if(is_numeric($this->_pidx_code)) {
			//コンテナの取得
			$tmp = pagecontainerDB::getData($this->_pidx_code,$this->_prev);
			if(is_array($tmp) && count($tmp)>0) {
				foreach($tmp as $key=>$val) {
					$this->_container[$val["pagec_code"]] = $val;
				}
			}
			//キャッシュONの場合はキャッシュされているtplとcssを返す
			$cache_file_path = UNITS_CACHE_DIR.'/'.$this->_pidx_data['pidx_sidx_code'].'_'.$this->_pidx_data['pidx_cat_code'].'_'.$this->_pidx_data['pidx_code'];
			$tplPath = $cache_file_path.'.tpl';
			$module_code_ar = array(); //ページ内にモジュールユニットがあれば格納

			if ($usecache && $this->_prev === false && file_exists(BASE_PATH.$tplPath)) {
				//キャッシュ利用
				return file_get_contents(BASE_PATH.$tplPath)."\n";
			} else {
				//完全動的
				//登録済みユニット構成の取得
				$units = pageunitDB::getData($this->_pidx_code,$this->_prev);
				if(is_array($units) && count($units)>0) {
					foreach($units as $key=>$val) {
						$this->_unit[$val["pageu_pagec_code"]][$val["pageu_column"]][$val["pageu_code"]] = $val;
					}
				}
				return $this->makeHTML($view,$this->_container, $this->_unit);
			}
		}
		return null;
	}
	
	//モジュール共通CSS読み込みhtml取得
	function getModuleCommonCSS($topurl) {
		$rtn = null;
		$sql = "select extmodule_path from tbl_extmodule 
					inner join tbl_unit on unit_extmodule_code = extmodule_code 
					where unit_type = 6 and extmodule_active = 1 and unit_active = 1 
					group by extmodule_path";
		$result = $this->db->query($sql);
		if($result != null) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
				$module_commoncss_url = MODULES_DIR."/".$row["extmodule_path"]."/css/common.css";
				if(file_exists(BASE_PATH.$module_commoncss_url)) {
					$rtn .= '<link href="'.$topurl.$module_commoncss_url.'" rel="stylesheet" type="text/css" media="all"/>'."\n";
				}
			}
		}
		return $rtn;
	}
	
	//キャッシュ用
	function setUnitsCacheCode($sidx_code, $cat_code, $pidx_url=null) {
		$this->_sidx_code = $sidx_code;
		$this->_cat_code = $cat_code;
		$this->_pidx_code = null;
		$this->_pidx_url = $pidx_url;
		$this->_prev = false;
		$this->_pidx_data = null;
		$this->_container = null;
		$this->_unit = null;
		
	}
	
	function setAllUnitsCSS($topurl) {
		if(file_exists(CACHE_PATH.'/AllUnits.css')) {
			$str = $this->getModuleCommonCSS($topurl);
			$str .= '<link href="'.$topurl.CACHE_DIR.'/AllUnits.css?'.date('Ymdhis').'" rel="stylesheet" type="text/css" media="all"/>'."\n";
			return $str;
		}
		return null;
	}

	/* ヘッダー情報の取得 */
	function getHeader($template_data) {
		$data = null;
		if(is_array($template_data)) {
			$data = templateDB::hsavnavi_explode($template_data["tpl_hsubnavi"]);
			foreach($template_data as $key => $val) {
				if(ereg('^tpl_h', $key)) {
					$data[$key] = $val;
				}
			}
		}
		
		$header = null;
		//ヘッダー画像
		if($this->_skinsetting["LOGO"]===TRUE) {
			if(isset($data["tpl_hlogo"])) {
				$header["LOGOIMG"] = "style='background-image: url(".$this->_bingotopurl.HEADER_IMG_PATH.$data["tpl_hlogo"].");'";
			} else if($this->_tpl_editting) {
				$header["LOGOIMG"] = "style='background-image: url(".$this->_bingotopurl.TEMPLATE_DIR."/".$this->_skin_data["skin_path"]."/".$this->_skinsetting["LOGOSAMPLE"].");'";
			}
		}
		//バックグラウンド
		if($this->_skinsetting["HEADERBG"]===TRUE) {
			if(isset($data["tpl_hbgimg"])) {
				$header["BGIMG"] = "style='background-image: url(".$this->_bingotopurl.HEADER_IMG_PATH.$data["tpl_hbgimg"].");'";
			} else if($this->_tpl_editting) {
				$header["BGIMG"] = "style='background-image: url(".$this->_bingotopurl.TEMPLATE_DIR."/".$this->_skin_data["skin_path"]."/".$this->_skinsetting["HEADERBGSAMPLE"].");'";
			}
		}
		//キャプション
		if($this->_skinsetting["HEADERCAP"]===TRUE) {
			if(isset($data["tpl_hcap"])) {
				$header["CAP"] = $data["tpl_hcap"];
			} else if($this->_tpl_editting) {
				$header["CAP"] = $this->_skinsetting["HEADERCAPSTRING"];
			}
		}
		//バナー
		if($this->_skinsetting["HEADERBANNER"]===TRUE) {
			if(isset($data["tpl_hbanner"])) {
				$header["BANNER"] = "<img src='".$this->_bingotopurl.HEADER_IMG_PATH.$data["tpl_hbanner"]."' width='".$this->_skinsetting["HEADERBANNERWIDTH"]."' height='".$this->_skinsetting["HEADERBANNERHEIGHT"]."' border=0>";
				} else if($this->_tpl_editting) {
				$header["BANNER"] = "<img src='".$this->_bingotopurl.TEMPLATE_DIR."/".$this->_skin_data["skin_path"]."/".$this->_skinsetting["HEADERBANNERSAMPLE"]."' width='".$this->_skinsetting["HEADERBANNERWIDTH"]."' height='".$this->_skinsetting["HEADERBANNERHEIGHT"]."' border=0>";
			}
		}
		//バナーリンク
		if(isset($data["tpl_hbannerurl"])) {
			if(isset($data["tpl_hbannertrgt"])&&$data["tpl_hbannertrgt"]==1) {
				$target="target='_blank'";
			}
			$header["BANNER"] = "<a href='".$data["tpl_hbannerurl"]."' ".$target.">".$header["BANNER"]."</a>";
		}

		//ヘッダーナビ
		if($this->_skinsetting["HEADERNAVI"]===TRUE) {
			if(is_array($data["tpl_hsubnavi_url"])&&count($data["tpl_hsubnavi_url"])>0) {
				foreach($data["tpl_hsubnavi_url"] as $key=>$val) {
					$target="";
					if(isset($data["tpl_hsubnavi_trgt"][$key])&&$data["tpl_hsubnavi_trgt"][$key]==1){
						$target="target='_blank'";
					}
					$snavi[]="<li><a href='".$val."' ".$target." title='".$data["tpl_hsubnavi_text"][$key]."'>".$data["tpl_hsubnavi_text"][$key]."</a></li>";
				}
				$header["HEADNAVI"] = "<ul>".implode("\n",$snavi)."</ul>";
			} else if($this->_tpl_editting) {
				//ダミーナビ
				if(is_array($this->_skinsetting["HEADERNAVISTRING"])) {
					foreach($this->_skinsetting["HEADERNAVISTRING"] as $key=>$val) {
						if($val!=null) {
							$snavi[]="<li><a href='#' title='".$val."'>".$val."</a></li>";
						}
					}
					if(is_array($snavi)) {
						$header["HEADNAVI"] = "<ul>".implode("\n",$snavi)."</ul>";
					}
				}
			}
		}
		return $header;
	}

	/* フッターー情報の取得 */
	function getFooter($template_data) {
		$data = null;
		if(is_array($template_data)) {
			$data = templateDB::fsavnavi_explode($template_data["tpl_fsubnavi"]);
			foreach($template_data as $key => $val) {
				if(ereg('^tpl_f', $key)) {
					$data[$key] = $val;
				}
			}
		}
		$footer = null;
		//キャプション
		if($this->_skinsetting["FOOTERCAP"]===TRUE) {
			if(isset($data["tpl_fcap"])) {
				$footer["CAP"] = $data["tpl_fcap"];
			} else if($this->_tpl_editting) {
				$footer["CAP"] = $this->_skinsetting["FOOTERCAPSTRING"];
			}
		}
		//ヘッダーナビ
		if($this->_skinsetting["FOOTERNAVI"]===TRUE) {
			if(is_array($data["tpl_fsubnavi_url"])&&count($data["tpl_fsubnavi_url"])>0) {
				foreach($data["tpl_fsubnavi_url"] as $key=>$val) {
					$target="";
					if(isset($data["tpl_fsubnavi_trgt"][$key])&&$data["tpl_fsubnavi_trgt"][$key]==1){
						$target="target='_blank'";
					}
					$snavi[]="<li><a href='".$val."' ".$target." title='".$data["tpl_fsubnavi_text"][$key]."'>".$data["tpl_fsubnavi_text"][$key]."</a></li>";
				}
				$footer["FOOTNAVI"] = "<ul>".implode("\n",$snavi)."</ul>";
			} else if($this->_tpl_editting) {
				//ダミーナビ
				if(is_array($this->_skinsetting["FOOTERNAVISTRING"])) {
					foreach($this->_skinsetting["FOOTERNAVISTRING"] as $key=>$val) {
						if($val!=null) {
							$snavi[]="<li><a href='#' title='".$val."'>".$val."</a></li>";
						}
					}
					if(is_array($snavi)) {
						$footer["FOOTNAVI"] = "<ul>".implode("\n",$snavi)."</ul>";
					}
				}
			}
		}
		return $footer;
	}

	/* Gメニュー取得 */
	function getSectionMenu() {
		$record = null;
		$tmp = sectionIdxDB::getList($usr_code=null,$this->_mprev);
		if(is_array($tmp)) {
			foreach($tmp as $key => $val) {
				if($val["sidx_menuflg"]==1 || ($this->_prev&&$val["sidx_menuflg"]==2)) {
		 			$record[$val["sidx_code"]] = $val;
		 			$record[$val["sidx_code"]]["url"] = $this-> getSiteURL($d=$val["sidx_dir"],$c=null,$p=null,$ssl=$val["sidx_ssl"],$prev=false);
					if($val["sidx_code"] == $this->_sidx_code){
						$record[$val["sidx_code"]]["active"] = "active";
					}
				}
			}
		}
		return $record;
	}
	
	/*Gメニュー取得（メニュー非表示含む）*/
	function getSectionMenuAll() {
		$record = null;
		$tmp = sectionIdxDB::getList($usr_code=null,$this->_mprev);
		if(is_array($tmp)) {
			foreach($tmp as $key => $val) {
	 			$record[$val["sidx_code"]] = $val;
	 			$record[$val["sidx_code"]]["url"] = $this-> getSiteURL($d=$val["sidx_dir"],$c=null,$p=null,$ssl=$val["sidx_ssl"],$prev=false);
				if($val["sidx_code"] == $this->_sidx_code){
					$record[$val["sidx_code"]]["active"] = "active";
				}
			}
		}
		return $record;
	}

	function getCategoryMenu($sidx_data) {
		$record = null;
		$categoryDB = new categoryDB();
		$tmp = $categoryDB->getlist($sidx_data["sidx_code"],$this->_mprev);
		if(is_array($tmp)) {
			foreach($tmp as $key => $val) {
				if($val["cat_menuflg"]==1 || ($this->_prev&&$val["cat_menuflg"]==2)) {
					$val["url"] = $this->getSiteURL($d=$sidx_data["sidx_dir"],$c=$val["cat_dir"],$p=null,$ssl=$sidx_data["sidx_ssl"],$prev=false);
					if($val["cat_code"] == $this->_cat_code){
						$val["active"] = "active";
					}
					$record[$val["cat_code"]] = $val;
				}
			}
		}
		if(is_array($record)&&count($record)>1 && isset($record[1])) { //共通カテゴリ「TOP」(code:1)の名前をグローバルメニューにする
			$record[1]["cat_name"] = $sidx_data["sidx_title"];
		} else {
			$record = null;
		}
		return $record;
	}


	//コンテナ一覧、ユニット情報一覧からページを生成
	function makeHTML($view, $container, $unit) {
		$html=null;
		$pageunitDB = new pageunitDB();
		if(is_array($container)) {
			foreach($container as $key => $val) {
				$data = array();
				for($i=0;$i<pagecontainerDB::getMaxColumn($val["pagec_type"]);$i++) {
					$unithtml_ar = array();
					if(is_array($unit[$val["pagec_code"]][$i])) {
						$unithtml_ar =  $pageunitDB->makeUnitListHTML($view,$unit[$val["pagec_code"]][$i],$this->_prev);
						foreach($unithtml_ar as $ukey => $uval) {
							$data[$i] .= $uval;
						}
					}
				}
				$view->assign("data",$data);
				$html.= $view->fetch(PAGEC_PATH.$val["pagec_type"].".tpl")."\n";
			}
		}
		return $html;
	}

	//下書きテーブルから本番用テーブルへのコピー処理
	// @param $pidx_code integer : ページ番号
	// @return boolean
	function setPublicData($pidx_code,$pidx_date) {
		if(is_numeric($pidx_code)&&$pidx_code>0) {
			if($pidx_date==null) {
				$pidx_date ="now()";
			}
			$this->db->query('BEGIN');
			//tbl_page_idx_tmp => tbl_page_idx
			if(!$this->db->delete("tbl_page_idx", "pidx_code=".$pidx_code)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			//tbl_page_idx_tmpのアップデート
			//承認フラグ、公開日(カテゴリにリスト型indexがない、またはpidx_ptype!=0のページは公開日=承認日とする)
			$record = array();
			$record["pidx_date"] = $pidx_date;
			$record["pidx_view"]=2;
			
			//承認フラグアップデート
			if(!$this->db->update("tbl_page_idx_tmp", $record,"pidx_code=".$pidx_code)) {
				$this->db->query('ROLLBACK');
				return false;
			}

			$sql = "insert into tbl_page_idx (select * from tbl_page_idx_tmp where pidx_code = ".$pidx_code.")";
			if(!$this->db->query($sql)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			if(!$this->db->delete("tbl_page_container", "pagec_pidx_code=".$pidx_code)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			$sql = "insert into tbl_page_container (select * from tbl_page_container_tmp where pagec_pidx_code = ".$pidx_code.")";
			if(!$this->db->query($sql)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			//tbl_page_unit_tmp => tbl_page_unit
			if(!$this->db->delete("tbl_page_unit", "pageu_pidx_code=".$pidx_code)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			$sql = "insert into tbl_page_unit (select * from tbl_page_unit_tmp where pageu_pidx_code = ".$pidx_code.")";
			if(!$this->db->query($sql)) {
				$this->db->query('ROLLBACK');
				return false;
			}

			//各ユニットの公開処理
			//dbとpageu_codeを各ユニットclassに渡して処理してもらう

			//ページに登録されているユニットの一覧取得
			$ulist = pageunitDB::getData($pidx_code, $prev=true);
			//各クラス読み込み
			pageunitDB::unitCall();

			if(is_array($ulist)) {
				foreach($ulist as $key => $val) {
					$unit_class = null;
					list($unit_class_name,$trash) = explode('.', $val["unit_php_path"]);
					$unit_class = new $unit_class_name;
					if(!$unit_class->TmptoCommon($val["pageu_code"],$this->db)) {
						$this->db->query('ROLLBACK');
						return false;
					}
				}
			}
			$this->db->query('COMMIT');
			return true;
		}
		return false;
	}

	//表記用URL取得処理
	function getSiteURL($d=null,$c=null,$p=null,$ssl=false,$prev=false,$mprev=false) {
		$siteurl=($ssl)?WWW_SSL:WWW;
		$url = null;
		if(URLTYPE && $prev==false && $mprev==false) {
			if($d!=null) {
				$url[]= $d;
			}
			if($c!=null) {
				$url[]= $c;
			}
			if($p!=null) {
				$url[]= $p.".html";
			}
			if(is_array($url)) {
				$siteurl .= "/".implode("/",$url);
			}
			if($p==null) {
				$siteurl .= "/";
			}
			return $siteurl;
		} else {
			$siteurl .= "/index.php";
			if($prev) {
				$url[] = "prev=1";
			}
			if($mprev) {
				$url[] = "mprev=1";
			}
			if($d!=null) {
				$url[] = "d=".$d;
			}
			if($c!=null) {
				$url[] = "c=".$c;
			}
			if($p!=null) {
				$url[] = "p=".$p.".html";
			}
			if(is_array($url)) {
				$siteurl .= "?".implode("&",$url);
			}
			return $siteurl;
		}
	}

	function getPagenavi() {
		$phtml = array();
		if(isset($this->_template_data["tpl_pagenavi"])&&$this->_template_data["tpl_pagenavi"]==1) {
			//パンくずリスト生成
			$plist = array();
			$plist[] = array("title"=>$this->_pidx_data["pidx_title"], "link"=>"", "dir"=>$this->_pidx_data["pidx_url"]);
			$plist[] = array("title"=>$this->_cat_data["cat_name"], "link"=>$this->getSiteURL($d=$this->_sidx_data["sidx_dir"],$c=$this->_cat_data["cat_dir"],$p=null,$ssl=$this->_sidx_data["sidx_ssl"],$prev=false), "dir"=>$this->_cat_data["cat_dir"]);
			$plist[] = array("title"=>$this->_sidx_data["sidx_title"], "link"=>$this->getSiteURL($d=$this->_sidx_data["sidx_dir"],$c=null,$p=null,$ssl=$this->_sidx_data["sidx_ssl"],$prev=false), "dir"=>$this->_sidx_data["sidx_dir"]);
	
			for($i=0;$i<3;$i++) {
				if($plist[$i]["dir"]!=null) {
					if(count($phtml)>0) {
						array_unshift($phtml, '<a href="'.$plist[$i]["link"].'">'.$plist[$i]["title"].'</a>');
					} else {
						array_unshift($phtml, $plist[$i]["title"]);
					}
				}
			}
			if(count($phtml)>0) {
					array_unshift($phtml, '<a href="'.$this->_gmenu[1]["url"].'">'.$this->_gmenu[1]["sidx_title"].'</a>');
			}
		}
		return $phtml;
	}

}
?>