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

$form_params = array();
$form_params['title'] = $users_id == 0 ? $GLOBALS['MSG_user_access']['ua_add_user_form_title'] : $GLOBALS['MSG_user_access']['ua_edit_user_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/users_access/users_access.php';
$form_params['form_attribs']['name'] = 'EditAddUser';

$user_access = new user_access();

$users_type_row = $user_access->GetUserType($users_type_id);

if ($users_type_row)
{
	$users_type_name = $users_type_row['users_type_name'];
}
else
{
	$users_type_name = '';
}

$admin_forms_fields = new admin_forms_fields();

$form_params['path_array'] = array(
array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/users_access/users_access.php', '', 7, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/users_access/users_access.php', '', 7, 'load_data'),
'name' => $GLOBALS['MSG_user_access']['ua_link_users_types']),

array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax($admin_forms_fields->AAction, "&action=show_users&users_type_id={$users_type_id}", 8, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax($admin_forms_fields->AAction, "&action=show_users&users_type_id={$users_type_id}", 8, 'load_data'),
'name' => sprintf($GLOBALS['MSG_user_access']['ua_show_users_title'], $users_type_name))
);

$admin_forms_fields->admin_forms_fields($form_params);

if ($users_id)
{
	$type = 1;
}
else
{
	$type = 0;
}

// Не было ошибок при добавлении/редактировании пользователя
if (!isset($error) && $users_id)
{
	if ($users_id)
	{
		// Не было ошибки при задании пароля ( форму отображаем первый раз)
		if (to_int($error_password) != 1)
		{
			// Получаем данные о пользователе системы администрирования из БД
			$user_row = $user_access->select_user($users_id);
			$user_name = $user_row['users_name'];
			$user_superuser = $user_row['users_superuser'];

			$users_name_text = $user_row['users_name_text'];
			$users_surname = $user_row['users_surname'];
			$users_patronymic = $user_row['users_patronymic'];
			$users_email = $user_row['users_email'];
			$users_position = $user_row['users_position'];
			$users_icq = $user_row['users_icq'];
			$users_site = $user_row['users_site'];
			$users_only_own = $user_row['users_only_own'];
			$users_type_id_as_param = $user_row['users_type_id'];
		}
	}
}
else // Произошла ошибка добавления/редактирования или идет добавление пользователя
{
	$user_name = to_str($_POST['users_name']);
	$user_superuser = isset($_POST['users_superuser']) ? 1: 0;

	$users_name_text = '';
	$users_surname = '';
	$users_patronymic = '';
	$users_email = '';
	$users_position = '';
	$users_icq = '';
	$users_site = '';
	$users_only_own = 0;
	$users_type_id_as_param = $users_type_id;
}

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

// Логин пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_name';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_login'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($user_name);
$param['attributes']['class'] = 'large';
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

$users_type_res = $user_access->GetAllUserTypes(CURRENT_SITE);

// Формируем список групп пользователей
$users_type_array = array();

if($users_type_res)
{
	while($users_type_row = mysql_fetch_assoc($users_type_res))
	{
		$users_type_array[$users_type_row['users_type_id']] = $users_type_row['users_type_name'];
	}
}

// Группа пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_type_id_as_param';
$param['caption'] = $GLOBALS['MSG_user_access']['users_type_form'];
$param['items'] = $users_type_array;
$param['type'] = 2; // Список.
$param['value'] = $users_type_id_as_param;

$admin_forms_fields->AddField($param);

// Пароль пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_password';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_password'];
$param['type'] = 6; // Пароль
$param['attributes']['size'] = '30';

if (!$users_id)
{
	$param['format']['minlen']['value'] = 5;
	$param['format']['maxlen']['value'] = 255;
}

$admin_forms_fields->AddField($param);

// Подтверждение пароля пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'confirm_password';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_password_second'];
$param['type'] = 6; // Пароль
$param['attributes']['size'] = '30';

if (!$users_id)
{
	$param['format']['minlen']['value'] = 5;
	$param['format']['maxlen']['value'] = 255;
}

$param['format']['fieldEquality']['value'] = 'field_id_2';
$param['format']['fieldEquality']['message'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_password_second_message'];

$admin_forms_fields->AddField($param);

// Привилегированность пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'users_superuser';
$param['html_id'] = 'id_user_superuser';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_superuser'];
$param['value'] = to_int($user_superuser);

$admin_forms_fields->AddField($param);

// Доcтуп только к своим элементам.
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'users_only_own';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_only_own'] ;
$param['value'] = to_int($users_only_own);

$admin_forms_fields->AddField($param);


// Добавляем скрытое поле с идентификатором группы пользователей.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_type_id';
$param['type'] = 4; // Скрытое поле.
$param['value'] = $users_type_id;
$admin_forms_fields->AddField($param);

// Личные данные.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_user_access']['users_type_form_tab_2']);

// Фамилия пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_surname';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_surname'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_surname);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

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

// Имя пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_name_text';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_name'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_name_text);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

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

// Отчество пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_patronymic';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_patronymic'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_patronymic);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

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

// Должность пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_position';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_position'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_position);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

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

// Электронный адрес пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_email';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_email'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_email);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;
$param['format']['lib']['value'] = 'email';

$admin_forms_fields->AddField($param);

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

// Номер icq пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_icq';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_icq'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_icq);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;

$admin_forms_fields->AddField($param);

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

// Сайт пользователя.
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'users_site';
$param['caption'] = $GLOBALS['MSG_user_access']['ua_add_edit_user_form_site'];
$param['type'] = 0; // Поле ввода.
$param['value'] = to_str($users_site);
$param['format']['minlen']['value'] = 0;
$param['format']['maxlen']['value'] = 255;
$param['format']['lib']['value'] = 'url';

$admin_forms_fields->AddField($param);

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



// Редактирование данных о пользователе
if ($users_id)
{
	// Добавляем скрытое поле с идентификатором пользователя.
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'users_id';
	$param['type'] = 4; // Скрытое поле.
	$param['value'] = $users_id;
	$admin_forms_fields->AddField($param);
}

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_user';
$param['caption'] = $GLOBALS['MSG_user_access']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$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);

// Кнопка "Применить".
$param = array();
$param['name'] = 'add_user';
$param['caption'] = $GLOBALS['MSG_user_access']['apply'];
$param['image'] = '/admin/images/add.gif';
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$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'] = 'submit';
$admin_forms_fields->AddButton($param);
 
// Отображение формы.
$admin_forms_fields->ShowForm();
?>