<?php
/*
=====================================================
 Модуль создал: REZER (http://rezer.net)
=====================================================
 Файл: field.php
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Назначение: Опции для дополнительных полей
=====================================================
*/

if( !defined( "DATALIFEENGINE" ) ) die( "Hacking attempt!" );

if( $Board->Field === false ) $Board->StartField( true );
$ShowAction = $_REQUEST['show_action'];

//---------------------------------------------======
//	Добавление дополнительного поля
//---------------------------------------------======

if( $ShowAction == "add" )
	{
		$OkAdd = false;
		miniloader( "Добавление доп. поля" );
		
		if( isset( $_POST['submit'] ) )
			{
				$OkAdd = $Board->Field->AddField();
				if( $OkAdd === true )
					{
						echo ThisClose();
						minimsg( "Добавление дополнительного поля", "Дополнительное поле было успешно создано<script language=\"javascript\" type=\"text/javascript\">UpdateFields();</script>" );
					}
						else
					{
						$FieldsValue = $Board->Field->ReturnFieldsValueOnError;
						opentable();
						tableheader( "Ошибка" );
						echo "<span style=\"color: #F00;\"><ol>{$OkAdd}</ol></span>";
						closetable();
					}
			}
				else
			{
				$Board->Field->ReturnFieldsValueOnError['allow_edit'] = 1;
			}
		
		if( $OkAdd !== true )
			{
				opentable();
				tableheader( "Добавление дополнительного поля" );
				
				$AllowGroups = BoardGroups( explode( ",", $Board->Field->ReturnFieldsValueOnError['AllowGroups'] ), "yes", true );
				$EditGroups = BoardGroups( explode( ",", $Board->Field->ReturnFieldsValueOnError['EditGroups'] ), "no", true );
				$AllowType = SelectList( $Board->Field->TypeName, $Board->Field->ReturnFieldsValueOnError['type'] );
				$FormType = $Board->Field->ReturnFormType();
				
				$HtmlChecked = $Board->Field->ReturnFieldsValueOnError['filtre_html'] == 1 ? "checked=\"checked\"" : "";
				$JsDisabled = $HtmlChecked ? "" : "disabled=\"disabled\"";
				$JsChecked = $Board->Field->ReturnFieldsValueOnError['filtre_js'] == 1 && !$JsDisabled ? "checked=\"checked\"" : "";
				$BrChecked = $Board->Field->ReturnFieldsValueOnError['filtre_br'] == 1 ? "checked=\"checked\"" : "";
				$ShowCreateChecked = $Board->Field->ReturnFieldsValueOnError['show_create'] == 1 ? "checked=\"checked\"" : "";
				$AllowEditChecked = $Board->Field->ReturnFieldsValueOnError['allow_edit'] == 1 ? "checked=\"checked\"" : "";
				
				$RequiredDisabled = !$ShowCreateChecked && !$AllowEditChecked ? "disabled=\"disabled\"" : "";
				$RequiredChecked = $Board->Field->ReturnFieldsValueOnError['required'] == 1 && !$RequiredDisabled ? "checked=\"checked\"" : "";
				
				$About = $Board->Field->ReturnFieldsValueOnError['about'];
				$LinkAbout = empty( $About ) ? "" : "display: none;";
				$divAbout = empty( $About ) ? "display: none;" : "";
				
				if( !$Board->Field->ReturnFieldsValueOnError['AllowCats'] ) $Board->Field->ReturnFieldsValueOnError['AllowCats'] = "all";
				$AllowCats = $Board->SelectCategory( explode( ",", $Board->Field->ReturnFieldsValueOnError['AllowCats'] ), "", "", "", true );
		
echo <<<HTML

<form method="post" action="" name="field_add">
	<table width="100%" border="0">
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="30px">
			<td width="200px" style="padding: 0px 0px 0px 5px;">Тип поля: *</td>
			<td>
				<select name="type" onkeyup="showtype();" onkeydown="showtype();" onchange="showtype();" id="type">
					{$AllowType}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="30px">
			<td style="padding: 0px 0px 0px 5px;">Имя: *</td>
			<td>
				<input type="text" name="name" value="{$FieldsValue[name]}" style="width: 50%;" class="edit" />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="30px">
			<td style="padding: 0px 0px 0px 5px;">Альтернативное имя:</td>
			<td>
				<input type="text" name="alt_name" value="{$FieldsValue[alt_name]}" style="width: 50%;" class="edit" />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="30px">
			<td style="padding: 0px 0px 0px 5px;">Описание:</td>
			<td>
				<a style="{$LinkAbout}" id="LinkShowAbout" href="javascript:void(0);" onclick="ShowOrHide( 'LinkShowAbout' ); ShowOrHide( 'FormAbout' ); return false;">Добавить описание</a>
				<div id="FormAbout" style="{$divAbout} padding: 3px 0px 3px 0px;">
					<textarea name="about" style="width: 90%; height: 80px;">{$About}</textarea><br />
					<span style="color: #999;">Разрешён HTML и JS</span>
				</div>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
	</table>
		
	$FormType
		
	<table width="100%" border="0">
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td width="200px" style="padding: 0px 0px 0px 5px;">Фильтрация:</td>
			<td>
				<input type="checkbox" name="filtre_html" value="1" id="filtre_html" onclick="if( this.checked === true ){ document.getElementById( 'filtre_js' ).disabled = false; } else { document.getElementById( 'filtre_js' ).checked = false; document.getElementById( 'filtre_js' ).disabled = true; }" {$HtmlChecked} />
				<label for="filtre_html">Разрешить HTML</label><br />
				<input type="checkbox" name="filtre_js" value="1" id="filtre_js" {$JsDisabled} {$JsChecked} />
				<label for="filtre_js">Разрешить Javascript</label><br />
				<input type="checkbox" name="filtre_br" value="1" id="filtre_br" {$BrChecked} />
				<label for="filtre_br">Новые строки заменять тегом &lt;br /&gt;</label><br />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td style="padding: 0px 0px 0px 5px;">Разрешить редактировать: *</td>
			<td>
				<select name="EditGroups[]" multiple="multiple" style="height: 60px;">
					{$EditGroups}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td style="padding: 0px 0px 0px 5px;">Разрешить просмотр: *</td>
			<td>
				<select name="AllowGroups[]" multiple="multiple" style="height: 60px;">
					{$AllowGroups}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="110px">
			<td style="padding: 0px 0px 0px 5px;">Разрешённые категории: *</td>
			<td>
				<select name="AllowCats[]" multiple="multiple" style="height: 100px;">
					{$AllowCats}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td style="padding: 0px 0px 0px 5px;">Другие опции:</td>
			<td>
				<input type="checkbox" name="show_create" value="1" id="show_create" onclick="FieldRequired();" {$ShowCreateChecked} />
				<label for="show_create">Показывать при создании объявления</label><br />
				<input type="checkbox" name="allow_edit" value="1" id="allow_edit" onclick="FieldRequired();" {$AllowEditChecked} />
				<label for="allow_edit">Разрешить изменять после создания</label><br />
				<input type="checkbox" name="required" value="1" id="required" {$RequiredChecked} {$RequiredDisabled} />
				<label for="required">Обязательное поле</label><br />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
	</table>
	<div style="padding: 5px;">
		<input type="submit" name="submit" value="Добавить" class="buttons" />
		<input type="reset" value="Сбросить" class="buttons" />
		<input type="button" value="Закрыть" class="buttons" onclick="window.close()" />
	</div>
</form>
HTML;
				closetable();
				echo EchoAJAX( true );
				echo FieldsType( $Board->Field->AllowType );
				minifooter();
			}
	}
	
//---------------------------------------------======
//	Редактирование дополнительного поля
//---------------------------------------------======

elseif( $ShowAction == "edit" )
	{
		$id = intval( $_REQUEST['id'] );
		if( !$id ) minimsg( "Ошибка", "Не указан ID редактируемого поля" );
		if( !is_array( $Board->Field->DB[ $id ] ) ) minimsg( "Ошибка", "Указнное поле не существует" );
		
		$OkAdd = false;
		miniloader( "Редактирование доп. поля" );
		
		if( isset( $_POST['submit'] ) )
			{
				$OkAdd = $Board->Field->AddField( $id );
				if( $OkAdd === true )
					{
						echo ThisClose();
						minimsg( "Редактирование дополнительного поля", "Дополнительное поле было успешно отредактировано<script language=\"javascript\" type=\"text/javascript\">UpdateFields( true );</script>", "$PHP_SELF?mod=board&amp;action=field&amp;show_action=edit&amp;id={$id}" );
					}
						else
					{
						$FieldsValue = $Board->Field->ReturnFieldsValueOnError;
						opentable();
						tableheader( "Ошибка" );
						echo "<span style=\"color: #F00;\"><ol>{$OkAdd}</ol></span>";
						closetable();
					}
			}
				else
			{
				$Board->Field->PreparationEditing( $id );
				$FieldsValue = $Board->Field->ReturnFieldsValueOnError;
			}
		
		if( $OkAdd !== true )
			{
				opentable();
				tableheader( "Редактирование дополнительного поля" );
				$AllowGroups = BoardGroups( explode( ",", $Board->Field->ReturnFieldsValueOnError['AllowGroups'] ), "yes", true );
				$EditGroups = BoardGroups( explode( ",", $Board->Field->ReturnFieldsValueOnError['EditGroups'] ), "no", true );
				$FormType = $Board->Field->ReturnFormType( $id );
				
				$HtmlChecked = $Board->Field->ReturnFieldsValueOnError['filtre_html'] == 1 ? "checked=\"checked\"" : "";
				$JsDisabled = $HtmlChecked ? "" : "disabled=\"disabled\"";
				$JsChecked = $Board->Field->ReturnFieldsValueOnError['filtre_js'] == 1 && !$JsDisabled ? "checked=\"checked\"" : "";
				$BrChecked = $Board->Field->ReturnFieldsValueOnError['filtre_br'] == 1 ? "checked=\"checked\"" : "";
				$ShowCreateChecked = $Board->Field->ReturnFieldsValueOnError['show_create'] == 1 ? "checked=\"checked\"" : "";
				$AllowEditChecked = $Board->Field->ReturnFieldsValueOnError['allow_edit'] == 1 ? "checked=\"checked\"" : "";
				
				$RequiredDisabled = !$ShowCreateChecked && !$AllowEditChecked ? "disabled=\"disabled\"" : "";
				$RequiredChecked = $Board->Field->ReturnFieldsValueOnError['required'] == 1 && !$RequiredDisabled ? "checked=\"checked\"" : "";
				
				$About = $Board->Field->ReturnFieldsValueOnError['about'];
				$LinkAbout = empty( $About ) ? "" : "display: none;";
				$divAbout = empty( $About ) ? "display: none;" : "";
				
				$AllowCats = $Board->SelectCategory( explode( ",", $Board->Field->ReturnFieldsValueOnError['AllowCats'] ), "", "", "", true );
				
		
echo <<<HTML

<form method="post" action="" name="field_add">
	<table width="100%" border="0">
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="30px">
			<td width="200px" style="padding: 0px 0px 0px 5px;">Имя: *</td>
			<td>
				<input type="text" name="name" value="{$FieldsValue[name]}" style="width: 50%;" class="edit" />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="30px">
			<td style="padding: 0px 0px 0px 5px;">Описание:</td>
			<td>
				<a style="{$LinkAbout}" id="LinkShowAbout" href="javascript:void(0);" onclick="ShowOrHide( 'LinkShowAbout' ); ShowOrHide( 'FormAbout' ); return false;">Добавить описание</a>
				<div id="FormAbout" style="{$divAbout} padding: 3px 0px 3px 0px;">
					<textarea name="about" style="width: 90%; height: 80px;">{$About}</textarea><br />
					<span style="color: #999;">Разрешён HTML и JS</span>
				</div>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
	</table>
		
	$FormType
		
	<table width="100%" border="0">
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td width="200px" style="padding: 0px 0px 0px 5px;">Фильтрация:</td>
			<td>
				<input type="checkbox" name="filtre_html" value="1" id="filtre_html" onclick="if( this.checked === true ){ document.getElementById( 'filtre_js' ).disabled = false; } else { document.getElementById( 'filtre_js' ).checked = false; document.getElementById( 'filtre_js' ).disabled = true; }" {$HtmlChecked} />
				<label for="filtre_html">Разрешить HTML</label><br />
				<input type="checkbox" name="filtre_js" value="1" id="filtre_js" {$JsDisabled} {$JsChecked} />
				<label for="filtre_js">Разрешить Javascript</label><br />
				<input type="checkbox" name="filtre_br" value="1" id="filtre_br" {$BrChecked} />
				<label for="filtre_br">Новые строки заменять тегом &lt;br /&gt;</label><br />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td style="padding: 0px 0px 0px 5px;">Разрешить редактировать: *</td>
			<td>
				<select name="EditGroups[]" multiple="multiple" style="height: 60px;">
					{$EditGroups}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td style="padding: 0px 0px 0px 5px;">Разрешить просмотр: *</td>
			<td>
				<select name="AllowGroups[]" multiple="multiple" style="height: 60px;">
					{$AllowGroups}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="110px">
			<td style="padding: 0px 0px 0px 5px;">Разрешённые категории: *</td>
			<td>
				<select name="AllowCats[]" multiple="multiple" style="height: 100px;">
					{$AllowCats}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr height="70px">
			<td style="padding: 0px 0px 0px 5px;">Другие опции:</td>
			<td>
				<input type="checkbox" name="show_create" value="1" id="show_create" onclick="FieldRequired();" {$ShowCreateChecked} />
				<label for="show_create">Показывать при создании объявления</label><br />
				<input type="checkbox" name="allow_edit" value="1" id="allow_edit" onclick="FieldRequired();" {$AllowEditChecked} />
				<label for="allow_edit">Разрешить изменять после создания</label><br />
				<input type="checkbox" name="required" value="1" id="required" {$RequiredChecked} {$RequiredDisabled} />
				<label for="required">Обязательное поле</label><br />
			</td>
		</tr>
		<tr>
			<td colspan="2" background="/engine/skins/images/mline.gif" height="1"></td>
		</tr>
		<tr>
			<td colspan="2"><div class="hr_line"></div></td>
		</tr>
	</table>
	<div style="padding: 5px;">
		<input type="submit" name="submit" value="Сохранить" class="buttons" />
		<input type="reset" value="Сбросить" class="buttons" />
		<input type="button" value="Закрыть" class="buttons" onclick="window.close()" />
	</div>
</form>
HTML;
				closetable();
				echo EchoAJAX( true );
				echo FieldsType( $Board->Field->AllowType );
				minifooter();
			}
	}

//---------------------------------------------======
//	Получение тега дополнительного поля
//---------------------------------------------======

elseif( $ShowAction == "tag" )
	{
		$OneID = 0;
		$ListField = array();
		foreach( $Board->Field->DB as $row )
			{
				if( $row[0] < $OneID || !$OneID ) $OneID = $row[0];
				if( $row[0] == $_REQUEST['id'] ) $select = $row[1];
				$ListField[ $row[1] ] = $row[2];
			}
			
		$ListField = SelectList( $ListField, $select );
		if( $Board->Field->DB[ $_REQUEST['id'] ] ) $OneID = $_REQUEST['id'];
		$StartAltName = $Board->Field->DB[ $OneID ][1];
		
		$preview = "{Boardfield:{$StartAltName}}";
		$block = "[Boardfield:{$StartAltName}]Здесь любой текст[/Boardfield:{$StartAltName}]";
		$tag_preview = "[not-Boardfield:{$StartAltName}]Здесь любой текст[/not-Boardfield:{$StartAltName}]";
		
		miniloader( "Получение кода для показа дополнительного поля" );
		opentable();
		tableheader( "Получение кода для показа дополнительного поля" );
		
echo <<<HTML

<div style="padding: 5px;">
<table width="100%">
	<tr>
		<td colspan="2" background="engine/skins/images/mline.gif" height="1"></td>
	</tr>
    <tr height="29px">
        <td width="130px">Выберите поле:</td>
        <td>
			<select id="field" onKeyUp="select_field();" onKeyDown="select_field();" onChange="select_field();">
				$ListField
			</select>
		</td>
    </tr>
	<tr>
		<td colspan="2" background="engine/skins/images/mline.gif" height="1"></td>
	</tr>	
</table>
</div>
<div class="hr_line"></div>

<div style="padding: 5px;">
<table width="100%">
	<tr>
		<td colspan="2" background="engine/skins/images/mline.gif" height="1"></td>
	</tr>
    <tr height="29px">
        <td width="130px">Код для показа:</td>
        <td>
			<input type="text" class="edit" style="width: 90%;" id="preview" onfocus="this.select()" value="$preview" />
			<img src="engine/inc/board/style/images/hint.gif" class="hintanchor" onMouseover="showhint('Данный код вы можете вставлять абсолютно в любые шаблоны вашего сайта (будь то любой чужой модуль или панель пользователя). Вместо этого кода автоматически будет вставляться меню, иначе оно будет также автоматически скрываться.', this, event, '220px')" />
		</td>
    </tr>
	<tr>
		<td colspan="2" background="engine/skins/images/mline.gif" height="1"></td>
	</tr>
	<tr height="29px">
        <td>Теги для скрытия:</td>
        <td>
			<input type="text" class="edit" style="width: 90%;" id="block" onfocus="this.select()" value="$block" />
			<img src="engine/inc/board/style/images/hint.gif" class="hintanchor" onMouseover="showhint('Если меню показалось, то содержимое данных тегов покажется, иначе скроется автоматически. Использовать можете также в любых шаблонах сайта. Текст \'Здесь любой тескт\' заменяете на свой.', this, event, '220px')" />
		</td>
    </tr>
	<tr>
		<td colspan="2" background="engine/skins/images/mline.gif" height="1"></td>
	</tr>	
	<tr height="29px">
        <td>Теги для показа:</td>
        <td>
			<input type="text" class="edit" style="width: 90%;" id="tag_preview" onfocus="this.select()" value="$tag_preview" />
			<img src="engine/inc/board/style/images/hint.gif" class="hintanchor" onMouseover="showhint('Данные теги полностью противоположны тегам для скрытия. Если меню показалось, то содержимое тегов скроется, иначе покажется.', this, event, '220px')" />
		</td>
    </tr>
	<tr>
		<td colspan="2" background="engine/skins/images/mline.gif" height="1"></td>
	</tr>	
</table>
</div>


<div class="hr_line"></div>
<div style="padding:5px;">
	<input class="buttons" type="button" value="Закрыть окно" onclick="window.close();" />
</div>

<script language="javascript">

function select_field(){
	
	var field = document.getElementById( "field" ).value;
	
	document.getElementById( "preview" ).value = "{Boardfield:" + field + "}";
	document.getElementById( "block" ).value = "[Boardfield:" + field + "]Здесь любой текст[/Boardfield:" + field + "]";
	document.getElementById( "tag_preview" ).value = "[not-Boardfield:" + field + "]Здесь любой текст[/not-Boardfield:" + field + "]";
			
}

</script>

HTML;
		closetable();
		minifooter();
	}
	
//---------------------------------------------======
//	Вывод списка дополнительных полей
//---------------------------------------------======

		else
	{
		minimsg( "Дополнительные поля", "Не верный запрос" );
	}
?>