<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2008 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добаления/редактирования дополнительных свойств узлов структуры.
 * 
 * Файл: /admin/structure/action/edit_structure_propertys.php
 * 
 * @author Hostmake LLC
 * @version 5.2
 */
if (!isset($structure_propertys_id))
{
	$structure_propertys_id = $_REQUEST['structure_propertys_id'];
}

$datetime = new DateClass();

if (isset($structure_parent_id))
{
	$structure_parent_id = $structure_parent_id;
}
else
{
	$structure_parent_id = 0;
}

$admin_forms_fields = new admin_forms_fields();

$form_params = array();
$form_params['title'] = $structure_propertys_id == 0 ? $GLOBALS['MSG_structure']['structure_propertys_add_form_title'] : $GLOBALS['MSG_structure']['structure_propertys_edit_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/structure/structure.php';
$form_params['form_attribs']['name'] = 'EditAddNodeProp';

$DataBase = & singleton('DataBase');

$struct = & singleton('Structure');

$path_array = $struct->GetStructurePathArray($structure_parent_id, false);

$new_path_array = array();

$new_path_array[0]['name'] = $GLOBALS['MSG_structure']['parent_dir'];
$new_path_array[0]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/structure/structure.php', "&action=show&structure_parent_id=0", 82, 'load_data');
$new_path_array[0]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/structure/structure.php', "&action=show&structure_parent_id=0", 82, 'load_data');

//$addon_path[0] = '';

if ($path_array)
{
	foreach ($path_array as $keys => $values)
	{
		foreach ($values as $key => $value)
		{
			if (!is_numeric($key))
			{
				//$addon_path[] = $value;
			}
			else
			{
				$new_path_array[] = array (
				'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/structure/structure.php', "&action=show&structure_parent_id={$key}", 82, 'load_data', 0, 0, 0, 0),
				'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/structure/structure.php', "&action=show&structure_parent_id={$key}", 82, 'load_data', 0, 0, 0, 0),
				'name' => $value);
			}
		}
	}
}

$new_path_array[] = array(
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/structure/structure.php', "&action=show_structure_propertys&structure_parent_id={$structure_parent_id}", 86, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/structure/structure.php', "&action=show_structure_propertys&structure_parent_id={$structure_parent_id}", 86, 'load_data'),
"name" => $GLOBALS['MSG_structure']['structure_propertys_show_title']);

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние дополнительного свойства
if ($structure_propertys_id)
{
	$type = 1;

	$res = $struct->SelectStructurePropertys(CURRENT_SITE, $structure_propertys_id);

	$res = mysql_fetch_assoc($res);

	$site_id = to_int($res['site_id']);

	$structure_propertys_name = to_str($res['structure_propertys_name']);

	$structure_propertys_type = to_str($res['structure_propertys_type']);

	$structure_propertys_order = to_int($res['structure_propertys_order']);

	$structure_propertys_define_value = to_str($res['structure_propertys_define_value']);

	// Дата
	$structure_propertys_date_default_value = $structure_propertys_define_value;

	// ДатаВремя
	$structure_propertys_datetime_default_value = $structure_propertys_define_value;

	$structure_propertys_define_checked_value = to_int($structure_propertys_date_default_value);

	$structure_propertys_xml_name = to_str($res['structure_propertys_xml_name']);

	$structure_propertys_lists_id = to_int($res['structure_propertys_lists_id']);

	$structure_propertys_information_systems_id = to_int($res['structure_propertys_information_systems_id']);

	$users_id = to_int($res['users_id']);
}
else
{
	$site_id = CURRENT_SITE;

	$structure_propertys_name = '';

	$structure_propertys_type = 0;

	$structure_propertys_order = 0;

	$structure_propertys_define_value = '';

	// Дата
	$structure_propertys_date_default_value = '';

	// ДатаВремя
	$structure_propertys_datetime_default_value = '';

	$structure_propertys_define_checked_value = 0;

	$structure_propertys_xml_name = '';

	$structure_propertys_lists_id = 0;

	$structure_propertys_information_systems_id = 0;
}

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_menu']['menu_tab_main_params']);

// Название
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'structure_propertys_name';
$params['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_name'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $structure_propertys_name;
$params['attributes']['class'] = 'large';
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);


// Заполняем поля элемента "Тип"
$arr = array(0 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name1'],
1 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name2'],
2 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name3'],
4 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name5'],
5 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name6'],
6 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name7'],
7 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name8'],
8 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name9'],
9 => $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name10']);

if (class_exists('lists'))
{
	$arr[3] = $GLOBALS['MSG_structure']['structure_propertys_add_form_type_name4'];
}

// Тип
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'structure_propertys_type';
$params['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_type'];
$params['type'] = 2; // Выпадающий список
$params['value'] = $structure_propertys_type;

$params['attributes']['onchange'] = 'javascript:ShowRows(this.value)';

$items = array();

foreach ($arr as $key => $value)
{
	if ($key == 0) // число
	{
		// Список слоев, которые необходимо скрыть при показе данного элемента списка
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'structure_propertys_information_systems_id';

		// Список слоев, которые необходимо отобразить при показе данного элемента списка
		$param['show_divs'][$key] = 'structure_propertys_define_value';
	}
	elseif ($key == 1) // строка
	{
		// Список слоев, которые необходимо скрыть при показе данного элемента списка
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,default_datetime_value_id, structure_propertys_information_systems_id';

		// Список слоев, которые необходимо отобразить при показе данного элемента списка
		$param['show_divs'][$key] = 'structure_propertys_define_value';
	}

	elseif ($key == 2) // файл
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,default_datetime_value_id,structure_propertys_define_value, structure_propertys_information_systems_id';

		$param['show_divs'][$key] = '';
	}

	elseif ($key == 3) // список
	{
		$param['hide_divs'][$key] = 'default_date_value_id,default_datetime_value_id,structure_propertys_define_value, structure_propertys_information_systems_id';

		if (class_exists('lists'))
		{
			$param['show_divs'][$key] = 'structure_propertys_lists_id';
		}
		else 
		{
			$param['show_divs'][$key] = '';
		}
	}

	elseif ($key == 4) // большое текстовое поле
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,default_datetime_value_id, structure_propertys_information_systems_id';

		$param['show_divs'][$key] = 'structure_propertys_define_value';
	}

	elseif ($key == 5) // информационная система
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,default_datetime_value_id,structure_propertys_define_value';

		$param['show_divs'][$key] = 'structure_propertys_information_systems_id';
	}

	elseif ($key == 6) // визуальный редактор
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,default_datetime_value_id,structure_propertys_define_value,structure_propertys_information_systems_id';

		$param['show_divs'][$key] = '';
	}

	elseif ($key == 7) // флажок
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,default_datetime_value_id,structure_propertys_define_value,structure_propertys_information_systems_id';

		$param['show_divs'][$key] = 'default_checked_value_id';
	}

	elseif ($key == 8) // Дата
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_datetime_value_id,structure_propertys_information_systems_id';

		$param['show_divs'][$key] = 'default_date_value_id';
	}

	elseif ($key == 9) // ДатаВремя
	{
		$param['hide_divs'][$key] = class_exists('lists') ? 'structure_propertys_lists_id,' : '' . 'default_date_value_id,structure_propertys_define_value,structure_propertys_information_systems_id';

		$param['show_divs'][$key] = 'default_datetime_value_id';
	}

	$items[$key] = $value;
}

$params['items'] = $items;

$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Порядковый номер
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'structure_propertys_order';
$params['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_order'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $structure_propertys_order;
$params['format']['lib']['value'] = 'integer';
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 6;
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Имя XML тэга
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'structure_propertys_xml_name';
$params['caption'] = $GLOBALS['MSG_structure']['structure_add_form_xml_name'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $structure_propertys_xml_name;
$params['format']['lib']['value'] = 'latinBase';
$params['format']['lib']['message'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_xml_name_error'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Далее указываем поля, которые будут отображаться в зависимости от выбранного элемента
// поля "Тип" при выборе "Файл" ничего добавляться не будет

// Флажок
$param = array();
$param['tab_id'] = $tab_id;
$param['div_id'] = 'default_checked_value_id';
$param['type'] = 1;
$param['name'] = 'information_propertys_items_define_checked_value';
$param['html_id'] = 'id_define_checked_value';
$param['caption'] = $GLOBALS['MSG_structure']['structure_add_form_default_checked'];
$param['lable'] = $GLOBALS['MSG_structure']['structure_add_form_show_yes'];
$param['value'] = $structure_propertys_define_checked_value;

$admin_forms_fields->AddField($param);

// Значение по умолчанию (Число, Строка, Большое текстовое поле)
$params = array();
$params['tab_id'] = $tab_id;
$params['div_id'] = 'structure_propertys_define_value';
$params['name'] = 'structure_propertys_define_value';
$params['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_default_value'];
$params['type'] = 0; // Текстовое поле
$params['value'] = $structure_propertys_define_value;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Значение по умолчанию для поля типа "Дата"
$param = array();

$param['tab_id'] = $tab_id;
$param['div_id'] = 'default_date_value_id';
$param['name'] = 'structure_propertys_date_default_value';
$param['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_default_value'];
$param['type'] = 10; // Поле ввода даты.
$param['value'] = $structure_propertys_date_default_value;
$param['attributes'] = array();
$param['attributes']['size'] = 10;

$admin_forms_fields->AddField($param);

// Значение по умолчанию для поля типа "Дата-время"
$param = array();

$param['tab_id'] = $tab_id;
$param['div_id'] = 'default_datetime_value_id';
$param['name'] = 'structure_propertys_datetime_default_value';
$param['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_default_value'];
$param['type'] = 11; // Поле ввода датывремени.
$param['value'] = $structure_propertys_datetime_default_value;
$param['attributes'] = array();
$param['attributes']['size'] = 22;

$admin_forms_fields->AddField($param);


if (class_exists('lists'))
{
	// Заполняенм поля элемента "Списки"
	$arr = array();
	
	$list = new lists();
	
	$res = $list->GetAllListsForSite(CURRENT_SITE);

	while ($row = mysql_fetch_assoc($res))
	{
		$arr[$row['lists_id']] = $row['lists_name'];
	}

	// Списки (Список)
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['div_id'] = 'structure_propertys_lists_id';
	$params['name'] = 'structure_propertys_lists_id';
	$params['caption'] = $GLOBALS['MSG_structure']['structure_add_form_lists'];
	$params['type'] = 2; // Выпадающий список
	$params['items'] = $arr;
	$params['value'] = $structure_propertys_lists_id;
	$admin_forms_fields->AddField($params);

	// Разделитель.
	$admin_forms_fields->AddSeparator($tab_id);

}

// Заполняем поля элемента "Информационная система"
$arr = array();

$is = & singleton('InformationSystem');

$res = $is->GetAllInformationSystems(false);

while ($row = mysql_fetch_assoc($res))
{
	$arr[$row['information_systems_id']] = $row['information_systems_name'];
}

// Информационная система
$params = array();
$params['tab_id'] = $tab_id;
$params['div_id'] = 'structure_propertys_information_systems_id';
$params['name'] = 'structure_propertys_information_systems_id';
$params['caption'] = $GLOBALS['MSG_structure']['structure_propertys_add_form_inf_system'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['value'] = $structure_propertys_information_systems_id;
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

if (isset($structure_propertys_id))
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'structure_propertys_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $structure_propertys_id;
	$admin_forms_fields->AddField($param);
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'action';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = 'show_structure_propertys';
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'structure_parent_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $structure_parent_id;
$admin_forms_fields->AddField($params);

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'site_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $site_id;
$admin_forms_fields->AddField($params);

$external_html = "
<script>
function ShowRows(index)
		{
			index = index*1;
						
			document.getElementById(\"structure_propertys_define_value\").style.display =\"\" ;
			" .
			(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"\" ;" : '')
			. "
			document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"\" ;
			document.getElementById(\"default_date_value_id\").style.display = \"\" ;
			document.getElementById(\"default_datetime_value_id\").style.display = \"\" ;
			document.getElementById(\"default_checked_value_id\").style.display = \"\" ;
			
									
			switch (index)
			{
				case 0: // Число
				{
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')					
					. "
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\" ;
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;
					
					break;
				}
								
				case 1: // Строка
				{
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\" ;
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;
					
					break;
				}
				
				case 2: // Файл
				{
					document.getElementById(\"structure_propertys_define_value\").style.display = \"none\";
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\" ;
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;

					break;
				}
				
				case 3: // Список
				{
					document.getElementById(\"structure_propertys_define_value\").style.display = \"none\";
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\" ;
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;

					break;
				}

				case 4: // Большое текстовое поле
				{
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\";
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;

					break;
				}

				case 5: // Информационная система
				{
					document.getElementById(\"structure_propertys_define_value\").style.display = \"none\" ;
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;

					break;
				}

				case 6: // Визуальный редактор
				{
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\";
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;
					
					break;
				}
				
				case 7: // Флажок
				{
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\";
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"structure_propertys_define_value\").style.display = \"none\" ;
					
					break;
				}
				
					
				case 8: // ДатаВремя
				{
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\";
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"structure_propertys_define_value\").style.display =\"none\" ;					
					document.getElementById(\"default_datetime_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;
					
					break;
				}		
				
				case 9: // ДатаВремя
				{
					document.getElementById(\"structure_propertys_information_systems_id\").style.display = \"none\";
					" .
					(class_exists('lists') ? "document.getElementById(\"structure_propertys_lists_id\").style.display = \"none\";" : '')
					. "
					document.getElementById(\"structure_propertys_define_value\").style.display =\"none\" ;					
					document.getElementById(\"default_date_value_id\").style.display = \"none\" ;
					document.getElementById(\"default_checked_value_id\").style.display = \"none\" ;
					
					break;
				}
			}
		}
		ShowRows($structure_propertys_type)
</script>";

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_structure_propertys';
$param['caption'] = $GLOBALS['MSG_structure']['save'];
$param['attributes']['onclick'] = "return doSendForm('{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$params = array();
$params['name'] = 'add_structure_propertys';
$params['caption'] = $GLOBALS['MSG_structure']['apply'];
$params['image'] = '/admin/images/add.gif';
$params['attributes']['type'] = 'submit';
$params['attributes']['onclick'] = "return doSendForm('{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).")";
$admin_forms_fields->AddButton($params);

$admin_forms_fields->SetExternalHtml($external_html);

// Отображение формы.
$admin_forms_fields->ShowForm();
?>
