<?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/pageView.php');
require_once (LIB_PATH.'/common/pagecontainerDB.php');
/**
 * 【ユニットキャッシュ機能】
 * ユーザ編集エリア部分を_cache/items/以下に(sidxcode_catcode_pagecode.tplの命名規則で)書き出し
 * @author 	hagiwara
 * @version	
 */
class unitCache extends Model
{
	var $db=null;
	/**
	 * コンストラクタ php4対応・・・
	 */
	function unitCache()
	{
		$this->__construct();
	}
	function __construct()
	{
		Model::__construct();
		$this->db = new DataBase();
	}
	
	/**
	 * ユニットキャッシュ作成
	 * @param $view 
	 * @return null
	 */
	function makeCache($view)
	{
		//既存キャッシュファイルの削除
		commonFile::AllDeleteFiles(UNITS_CACHE_PATH);
		$pageView = new pageView();
		//キャッシュ化出来ないユニット取得
		$unUnit = $this->getUnUnits();

		//キャッシュ化出来る公開中ページ
		$sql = 'SELECT pidx_code,pidx_cat_code,pidx_sidx_code,pidx_url,sidx_ssl 
					FROM tbl_page_idx 
					inner join tbl_section_idx on sidx_code = pidx_sidx_code 
					WHERE pidx_active = 1 AND pidx_view = 2';
		if (!empty($unUnit)) {
			$sql .= ' AND pidx_code NOT IN ('.$unUnit.')';
		}
		$order = ' ORDER BY pidx_ptype DESC, pidx_date DESC';
		$result = $this->db->query($sql.$order);
		$page = array();
		if($result != null) {
			//BINGOTOPURLのバックアップ
			if(isset($_SERVER['HTTPS'])) {
				$_TMP_BINGOTOPURL = WWW_SSL;
			} else {
				$_TMP_BINGOTOPURL = WWW;
			}
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$page[] = $row;
				//TPLデータ取得
				$htmldata = null;
				$pageView->setUnitsCacheCode($row['pidx_sidx_code'], $row['pidx_cat_code'], $row['pidx_url']);
				if(isset($row['sidx_ssl'])&&$row['sidx_ssl']==1) {
					$view->assign('BINGOTOPURL', WWW_SSL);
				} else {
					$view->assign('BINGOTOPURL', WWW);
				}
				$htmldata = $pageView->getPageData(&$view);
				//新しいTPLパス作り
				$newPath = $row['pidx_sidx_code'].'_'.$row['pidx_cat_code'].'_'.$row['pidx_code'];
				$newTplPath = UNITS_CACHE_PATH.'/'.$newPath.'.tpl';
				//新しいTPLファイル作成
				$newTplFile = fopen($newTplPath, 'w');
				fwrite($newTplFile, $htmldata);
				fclose($newTplFile);
				@chmod($newTplPath, 0644);
			}
			//BINGOTOPURLを元に戻す
			$view->assign('BINGOTOPURL', $_TMP_BINGOTOPURL);
		}
	}
	
	/**
	 * キャッシュ化出来ないページコードの取得
	 * @return null or string
	 */
	function getUnUnits()
	{
		//$sql = 'SELECT pageu_pidx_code FROM tbl_unit,tbl_page_unit'
		//	.' WHERE pageu_unit_code = unit_code AND unit_cache = 0';
		
		$sql = "select pageu_pidx_code from tbl_unit 
inner join tbl_page_unit on unit_code = pageu_unit_code
inner join tbl_page_container on pagec_code = pageu_pagec_code
where unit_cache = 0 
group by pageu_pidx_code";
		$result = $this->db->query($sql);
		$unUnitArr = array();
		if($result != null) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$unUnitArr[] = $row['pageu_pidx_code'];
			}
		}
		$unUnit = null;
		if (count($unUnitArr) > 0) {
			$unUnit = implode(',', $unUnitArr);
		}
		return $unUnit;
	}
}
?>