<?php
/*
 * content/page/Preview.class.php
 * 
 * CopyRight(C) 2010 Shopformer Development Team. All Right Reserved
 * URL: http://sourceforge.jp/projects/shopformer/
 * 
 * 
 * Mail: m_nakashima@users.sourceforge.jp
 * Auther: Masanori Nakashima
 * Last Update: 2010-06-18
 */
require_once( dirname(dirname(dirname(__FILE__)))
	.DIRECTORY_SEPARATOR.'system'
	.DIRECTORY_SEPARATOR.'login'
	.DIRECTORY_SEPARATOR.'ModuleBase.class.php');
require_once( dirname(dirname(dirname(__FILE__)))
	.DIRECTORY_SEPARATOR.'util'
	.DIRECTORY_SEPARATOR.'File.class.php');
require_once( dirname(dirname(__FILE__))
	.DIRECTORY_SEPARATOR.'DaoPage.class.php');
class content_page_Preview extends system_login_ModuleBase {
	function content_page_Preview() {
		array_push($this->require_module_array,'content.page.setting.Load');
	}
	function execute( & $request ) {
		// 古いプレビューファイルを削除
		system_DataFileWriter::deleteDir( $request, '/upload/tmp/preview/page', 'public', 60*30 );
		if( !$request->isError() ) {
			$agent						= strtolower(trim(stripslashes($_POST['agent'])));
			$bodyStrings				= stripslashes($_POST['body_strings']);
			$pageObject					= $request->getAttribute('content.page.pageObject' );
			$isExistAgent	= false;
			foreach( $GLOBALS['SPIDER_USER_AGENT_CLASS_HASH'] as $agentType => $agentName ) {
				if( $agentType == $agent ) {
					$isExistAgent	= true;
					break;
				}
			}
			if( !$isExistAgent ) {
				$agent			= 'default';
			}
			// 一時URIに設定して保存
			$pageObject->uri	= '/upload/tmp/preview/page'.$pageObject->uri;
			$pageObject->save( $request );
			// {dynamic-page}置き換え
			$pageContents	= system_DataFileWriter::getContents($request,$pageObject->uri,'public');
			$pageContents	= str_replace('{dynamic-page}',$bodyStrings,$pageContents);
			$writer			= new system_DataFileWriter();
			if( $writer->open( $request, $pageObject->uri, 'w', 'public' ) ) {
				$writer->write( $pageContents );
				$writer->close( $request );
			}
			$viewFileUri	= SPIDER_URI_BASE.preg_replace('/^\\//','',$pageObject->uri);
			$request->setAttribute('content.page.previewUri',$viewFileUri );
		}
	}
}
?>