<?php include_once( "../../../../spider.inc.php" ); ?>
{charset:UTF-8}
{template:admin_default.tpl}
{module:system.login.Require}
{module:design.template.Form}
{preset:page_title 'デザイン管理 > デザインテンプレートHTML編集'}

	<div class="panc">
		<a href="../../../" title="管理パネルのトップページを表示します">管理パネルトップ</a>
		&gt; <a href="../../" title="デザイン管理トップページを表示します">デザイン管理</a>
		&gt; <a href="../" title="デザインテンプレートの一覧を表示します">デザインテンプレート一覧</a>
		&gt; HTML編集
	</div>
	<h1>デザイン管理 &gt; デザインテンプレートHTML編集 - {write:design.template.form.templateName}</h1>
	
	{if:IS_ERROR}
	<ul class="error">
	{foreach:errors error}
		<li>{write:error}</li>
	{/foreach}
	</ul>
	{else}
	
	<div class="edit-area-panel" id="frexible-block">
		<form name="pagehtmlform" action="./confirm.php" method="post">
		<div style="width:95%;margin:0 auto;padding:5px;" class="menu">
			{foreach:SPIDER_USER_AGENT_CLASS_HASH key name}
			{if:system_configuration_agent_type_hash_loop_counter>1}&nbsp;&nbsp;{/if}
			<input type="radio" name="agent_type" id="agent_type_{write:key}" value="{write:key}"
				onclick="designTemplateAgentChange('{write:key}')"
				onkeypress="designTemplateAgentChange('{write:key}')"
				{if:design.template.form.agentType=key} checked{/if}
				/><label for="agent_type_{write:key}" style="cursor:pointer;">&nbsp;{write:name}</label>
			{/foreach}
		</div>
		<div class="editor">
			<div style="width:20%;float:right;background-color:#ffffff;">
				<div style="font-weight:bold;background-color:#008000;color:#ffffff;padding:3px;">アップロード画像</div>
				<div id="image-current-path" style="font-size:7px;overflow:hidden;"></div>
				<select name="targetFolder" style="width:100%;" onchange="designTemplateHtmlCreateGetImages()">
					<option value="">フォルダ選択</option>
				</select>
				<input type="button" value="画像アップロード" style="width:100%;" onclick="uploadImage()" />
				<div id="insert-image-panel"></div>
			</div>
			<div style="width:80%;float:left;"><textarea id="form_html" name="form_html" cols="30" rows="50" style="width:100%;"></textarea></div>
			<br style="clear:both;" />
		</div>
		</form>
	</div>

<script language="JavaScript" type="text/javascript" src="/js/textformer/textformer.js"></script>  
<script language="JavaScript" type="text/javascript"><!--
var designTemplateTextformerObject;
var designTemplateAgentHash	= new Array();
if(window.attachEvent){
	window.attachEvent('onload',designtplFormMassageFormInitialize);
}else{
	window.addEventListener('DOMContentLoaded',designtplFormMassageFormInitialize,false);
}
function designtplFormMassageFormInitialize() {
	designTemplateAgentHash	= new Array();
{foreach:SPIDER_USER_AGENT_CLASS_HASH key name}
	designTemplateAgentHash['{write:key}']	= '{write:name}';
{/foreach}
	designTemplateTextformerObject	= TextFormer( document.pagehtmlform.form_html, "designTemplateSaveBody()", "designTemplateEditClose()" );
	designTemplateAgentGet('{write:design.template.form.agentType}');
	contentGetFolderList('',designTemplateHtmlCreateFolderOptions);
}
/**
 * 対象ユーザーエージェントを切り替える
 */
var designTemplateAgentGetHttpObject;
var desingTemplateSelectedAgent;
var designTemplateBodyIsSave	= false;
function designTemplateAgentChange(agentType) {
	if( desingTemplateSelectedAgent	== agentType ) {
		return;
	}
	if( designTemplateBodyIsSave == false && !confirm('保存しないで編集する対象ユーザーエージェントを切り替えます。\n'
		+'このまま切り替えてよろしければOKをクリックしてください。\n'
		+'保存するにはキャンセルして編集欄左上のSaveボタンをクリックしてください。') ) {
		document.getElementById('agent_type_'+desingTemplateSelectedAgent).click();
		document.getElementById('agent_type_'+desingTemplateSelectedAgent).blur();
		return;
	}
	designTemplateAgentGet(agentType);
}
function designTemplateAgentGet(agentType) {
	designTemplateAgentGetHttpObject		= FormerCommon.createHttpRequest(designTemplateAgentGetResult);
	if (designTemplateAgentGetHttpObject) {
		FormerCommon.displayGrayOut(9999);
		var url	= './gethtml.php?agent=' + agentType + '&fmt={write:urlencode(design.template.form.templateName)}';
		designTemplateAgentGetHttpObject.open("GET", url, true);
		designTemplateAgentGetHttpObject.send(null);
		designTemplateTextformerObject.setSubject(
			'{write:design.template.form.templateName} - ' + designTemplateAgentHash[agentType] + '用HTML編集');
		designTemplateBodyIsSave	= false;
		desingTemplateSelectedAgent	= agentType;
		document.getElementById('agent_type_'+desingTemplateSelectedAgent).blur();
	} else {
		setTimeout(FormerCommon.displayWhiteIn, 500);
		alert('サーバーへの接続作成に失敗しました。');
	}
}
function designTemplateAgentGetResult() {
	if ((designTemplateAgentGetHttpObject.readyState == 4) && (designTemplateAgentGetHttpObject.status == 200)){
		designTemplateTextformerObject.setText('');
	    designTemplateTextformerObject.setText(designTemplateAgentGetHttpObject.responseText);
		setTimeout(FormerCommon.displayWhiteIn, 100);
	} else if ((designTemplateAgentGetHttpObject.readyState == 4) && (designTemplateAgentGetHttpObject.status != 200)){
		setTimeout(FormerCommon.displayWhiteIn, 100);
		alert('HTML取得時にサーバー通信でエラーが発生しました。');
	} else {
	}
}
/**
 * 現在の状態を保存する
 */
var designTemplateSaveBodyHttpObject;
function designTemplateSaveBody() {
	var body	= designTemplateTextformerObject.getText();
	designTemplateSaveBodyHttpObject	= FormerCommon.createHttpRequest(designTemplateSaveBodyResult);
	var contents	= "command=0";
	contents	+= "&" + encodeURIComponent('agent')+ "="+encodeURIComponent(desingTemplateSelectedAgent);
	contents	+= "&" + encodeURIComponent('file_name')+ "="+encodeURIComponent('{write:design.template.form.templateName}');
	contents	+= "&" + encodeURIComponent('body_strings')+ "="+encodeURIComponent(body);
	if (designTemplateSaveBodyHttpObject) {
		FormerCommon.displayGrayOut(9999);
		designTemplateSaveBodyHttpObject.open("POST",'./save.php',true);
		designTemplateSaveBodyHttpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		designTemplateSaveBodyHttpObject.send(contents);
	} else {
		alert('サーバーへの接続作成に失敗しました。');
	}
}
function designTemplateSaveBodyResult() {
	if ((designTemplateSaveBodyHttpObject.readyState == 4) && (designTemplateSaveBodyHttpObject.status == 200)){
	    var lines	= designTemplateSaveBodyHttpObject.responseText.cr2lf().split(LF);
	    var isError = false;
	    var error_message	= '';
	    for( var i=0; i<lines.length; i++ ){
			if( String(lines[i]).cr2lf().trim().length>0 ){
				var result	= String(lines[i]).cr2lf().trim();
				if( result == 'OK' ) {
					break;
				} else if( isError ) {
					error_message += result + "\n";
				} else {
					isError	= true;
				}
			}
		}
		if( isError ) {
			alert('HTML本文の保存に失敗しました!\n'+error_message);
			setTimeout(FormerCommon.displayWhiteIn, 100);
		} else {
			alert('保存が完了しました');
			designTemplateBodyIsSave	= true;
			setTimeout(FormerCommon.displayWhiteIn, 100);
		}
	} else if ((designTemplateSaveBodyHttpObject.readyState == 4) && (designTemplateSaveBodyHttpObject.status != 200)){
		setTimeout(FormerCommon.displayWhiteIn, 100);
		alert('適用時にサーバー通信でエラーが発生しました。');
	} else {
	}
}
// 閉じる処理
function designTemplateEditClose() {
	if( confirm('編集を終了して一覧ページに戻ります。\n変更内容は最後の保存の状態に戻ります。\n本当に戻ってよろしいですか？') ) {
		window.location.href	= './';
	}
}
// 画像表示フォルダ切り替え
var currentFolder;
function designTemplateHtmlCreateGetImages() {
	var targetFolderUri	= document.pagehtmlform.targetFolder.options[document.pagehtmlform.targetFolder.selectedIndex].value;
	designTemplateHtmlCreateGetFolderImages(targetFolderUri);
}
function designTemplateHtmlCreateGetFolderImages(targetFolderUri){
	// 選択オプションの切り替え
	contentGetFolderList(targetFolderUri,designTemplateHtmlCreateFolderOptions);
	// 上に戻るリンクの作成
	var curPathDiv = document.getElementById('image-current-path');
	if( curPathDiv ) {
		curPathDiv.innerHTML = '';
		var elms = targetFolderUri.split('/');
		if( elms.length > 1 ){
			var tPath = '';
			for( var num=0; num<elms.length; num++ ){
				if(elms[num].length==0){
					curPathDiv.innerHTML	+= '<a href="javascript:designTemplateHtmlCreateGetFolderImages(\'\');" style="text-decoration:none;">T</a>';
				} else if( elms.length - 1 > num ) {
					tPath += '/' + elms[num];
					curPathDiv.innerHTML	+= '/<a href="javascript:designTemplateHtmlCreateGetFolderImages(\''+tPath+'\');" style="text-decoration:none;">' + elms[num] + '</a>';
				} else {
					curPathDiv.innerHTML	+= '/' + elms[num] + '';
				}
			}
		}
	}
	currentFolder = targetFolderUri;
	contentToImagePastePanel( document.getElementById('insert-image-panel'), targetFolderUri, designTemplateTextformerObject );
}
function designTemplateHtmlCreateFolderOptions( folderObjectArray ) {
	var selectObject	= document.pagehtmlform.targetFolder;
	for( var num=selectObject.length-1; num>=0; num-- ) {
		var optionObject	= document.pagehtmlform.targetFolder.options[num];
		selectObject.removeChild(optionObject);
	}
	var optionObject	= new Option();
	optionObject.label	= 'フォルダ選択';
	optionObject.text	= 'フォルダ選択';
	optionObject.value	= '';
	selectObject.appendChild(optionObject);
	for( var num=0; num<folderObjectArray.length; num++ ) {
		var folderInfo	= folderObjectArray[num];
		var uri			= folderInfo.parent + '/' + folderInfo.name;
		var extOption	= new Option();
		extOption.label	= folderInfo.name;
		extOption.text	= folderInfo.name;
		extOption.value	= uri;
		selectObject.appendChild(extOption);

	}
}
function uploadImage(){
	contentFileViewUploadForm(currentFolder,'',true,designTemplateHtmlCreateGetImages)
}
//--></script>
	{/if}
	<p style="width:95%;margin-top:2em;text-align:right;">
		<a href="../setting/form.php?fmt={write:urlencode(design.template.form.templateName)}">&gt;&gt; {write:design.template.form.templateName}の設定</a>
		&nbsp;
		<a href="../">&gt;&gt; デザインテンプレート一覧ページに戻る</a>
	</p>
	