<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Ajax загрузчик списка форм Центра администрирования.
 * 
 * Файл: /admin/admin_forms/action/load_admin_forms.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
function LoadAjaxData(&$admin_forms)
{
	$on_page = $admin_forms->GetOnPageCount();
	$page = to_int($_REQUEST['limit']);
	$begin = ($page == 0) ? 0 : ($page - 1) * $on_page;

	$param = array();

	$param['limit'][0] = array('begin' => $begin, 'count' => $on_page);

	// Запрос для выборки нужных данных.
	$s1 = $param['data'][] = "SELECT * FROM `admin_forms_table`
	LEFT JOIN `admin_words_table`
		ON admin_forms_table.admin_words_id = admin_words_table.admin_words_id
	LEFT JOIN `admin_words_value_table`
		ON admin_words_table.admin_words_id = admin_words_value_table.admin_words_id
	WHERE (admin_words_value_table.admin_language_id = ".CURRENT_LANGUAGE_ID."
		OR admin_forms_table.admin_words_id = 0)
	{FILTER_STRING} {ORDER_STRING} {LIMIT}";

	// Посчитаем общее число, запрос обязательно должен возвращать столбец `count`.
	$count = "SELECT count(admin_forms_id) as count FROM `admin_forms_table`
	LEFT JOIN `admin_words_table`
		ON admin_forms_table.admin_words_id = admin_words_table.admin_words_id
	LEFT JOIN `admin_words_value_table`
		ON admin_words_table.admin_words_id = admin_words_value_table.admin_words_id
	WHERE (admin_words_value_table.admin_language_id = ".CURRENT_LANGUAGE_ID."
		OR admin_forms_table.admin_words_id = 0)
	{FILTER_STRING}";

	// Формируем верхнее меню.
	$menu = array();
	$menu[$k = 0]['name'] = $GLOBALS['MSG_admin_forms']['show_form_menu_admin_forms_top1'];
	$menu[$k]['link'] = '';
	$menu[$k]['icon'] = '';

	$sub_menu = array();
	$sub_menu[$p = 0]['name'] = $GLOBALS['MSG_admin_forms']['show_form_menu_admin_forms_sub_add'];
	$sub_menu[$p]['link'] = $admin_forms->GetHtmlCallTrigerSingleAction('edit_form', 'check_0_0', 1, 0, 0);
	$sub_menu[$p]['onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('edit_form', 'check_0_0', 1, 0, 0);
	$sub_menu[$p]['icon'] = '/admin/images/table_add.gif';

	// Добавляем подэлементы.
	$menu[$k]['sub_items'] = $sub_menu;

	$menu[++$k]['name'] = $GLOBALS['MSG_admin_forms']['show_form_menu_admin_forms_top2'];
	$menu[$k]['link'] = '';
	$menu[$k]['icon'] = '';

	$sub_menu = array();
	$sub_menu[$p = 0]['name'] = $GLOBALS['MSG_admin_forms']['show_form_menu_admin_forms_top2_sub_add'];
	$sub_menu[$p]['link'] = $admin_forms->GetHtmlCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 93, 'load_data');
	$sub_menu[$p]['onclick'] = $admin_forms->GetOnClickCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 93, 'load_data');
	$sub_menu[$p]['icon'] = '/admin/images/languages.gif';

	// Добавляем подэлементы.
	$menu[$k]['sub_items'] = $sub_menu;

	// Формируем массив меню.
	$menu_array = array($menu);

	$param['total_count'][] = $count;
	$param['current_page'] = $page;
	$param['on_page'] = $on_page;
	$param['menus'] = $menu_array;

	$param['title'] = $GLOBALS['MSG_admin_forms']['show_forms_title'];

	return $param;
}

/**
 * Обработчик события "Применить" формы Центра администрирования
 */
function apply_changes(&$param)
{
	ob_start();

	if (defined('READ_ONLY') && READ_ONLY)
	{
		$kernel = & singleton('kernel');

		$kernel->ReadMode();
	}
	else
	{
		$adm_forms = new admin_forms();

		$count_changes_forms = 0;

		if (isset($param['data'][0]) && is_array($param['data'][0]))
		{
			foreach ($param['data'][0] as $key => $row)
			{
				// Определяем режим доступа пользователя и принадлежит ли объект пользователю
				$user_access = new user_access();

				// Определяем информацию об объекте
				$object_row = $adm_forms->GetAdminForm($key);

				if ($object_row)
				{
					$object_user_id = $object_row['users_id'];
				}
				else
				{
					$object_user_id = 0;
				}

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					$form_param = array();
					$form_param['admin_forms_id'] = $key;

					$form_param['admin_forms_show_operations'] = to_int($row['admin_forms_show_operations']);
					$form_param['admin_forms_show_group_operations'] = to_int($row['admin_forms_show_group_operations']);

					if ($adm_forms->InsertAdminForm($form_param))
					{
						++$count_changes_forms;
						$flag_success_changes_form = true;
					}
					else
					{
						$flag_success_changes_form = false;
					}
				}
				else
				{
					$flag_success_changes_form = false;
					$user_access->MessageOnlyOwn();
				}
			}

			if ($flag_success_changes_form)
			{

				if ($count_changes_forms == 1)
				{
					show_message($GLOBALS['MSG_admin_forms']['form_form_changes_success']);
				}
				elseif ($count_changes_forms > 1)
				{
					show_message($GLOBALS['MSG_admin_forms']['form_forms_changes_success']);
				}
			}
			else
			{
				show_error_message($GLOBALS['MSG_admin_forms']['form_form_changes_error']);
			}
		}


	}
	$param['error'] = ob_get_clean();

}


/**
 * Обработчик события "Редактировать" формы Центра администрирования
 */
function edit_form(&$param)
{
	ob_start();

	$adm_forms = & singleton('admin_forms');

	if (isset($param['data'][0]))
	{
		foreach ($param['data'][0] as $key => $value)
		{
			// Определяем режим доступа пользователя и принадлежит ли объект пользователю
			$user_access = new user_access();

			// Определяем информацию об объекте
			$object_row = $adm_forms->GetAdminForm($key);

			if ($object_row)
			{
				$object_user_id = $object_row['users_id'];
			}
			else
			{
				$object_user_id = 0;
			}

			if ($user_access->IssetUserAccessForObject($object_user_id))
			{
				$admin_forms_id = intval($key);

				include(CMS_FOLDER . 'admin/admin_forms/action/form_edit_add_admin_forms.php');

				if ($admin_forms_id)
				{
					$admin_forms = & singleton('admin_forms');

					$form_row = $admin_forms->GetAdminForm($admin_forms_id);

					if ($form_row)
					{
						$form_word_row = $admin_forms->GetAdminFormsWord(to_int($form_row['admin_words_id']));
					}
				}

				$param['title'] = $admin_forms_id == 0
				? $GLOBALS['MSG_admin_forms']['form_add_forms_title']
				: sprintf($GLOBALS['MSG_admin_forms']['form_edit_forms_title'], to_str($form_word_row['name']));

				break;
			}
			else
			{
				$user_access->MessageOnlyOwn();
				$param['error'] = ob_get_clean();
				return ;
			}
		}
	}

	$param['result'] = ob_get_clean();
	return true;
}


/**
 * Обработчик события "Удалить" формы Центра администрирования
 */
function delete_form(&$param)
{
	ob_start();

	if (defined('READ_ONLY') && READ_ONLY)
	{
		$kernel = & singleton('kernel');

		$kernel->ReadMode();
	}
	else
	{
		$adm_forms = new admin_forms();

		$count_deleted_admin_forms = 0;

		if (isset($param['data'][0]))
		{
			foreach ($param['data'][0] as $key => $value)
			{
				// Определяем режим доступа пользователя и принадлежит ли объект пользователю
				$user_access = new user_access();

				// Определяем информацию об объекте
				$object_row = $adm_forms->GetAdminForm($key);

				if ($object_row)
				{
					$object_user_id = $object_row['users_id'];
				}
				else
				{
					$object_user_id = 0;
				}

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					if ($adm_forms->DeleteAdminForm($key))
					{
						++$count_deleted_admin_forms;
						$flag_success_delete_admin_form = true;
					}
					else
					{
						$flag_success_delete_admin_form = false;
					}
				}
				else
				{
					$user_access->MessageOnlyOwn();
					$flag_success_delete_admin_form = false;
				}
			}

			if ($flag_success_delete_admin_form)
			{
				if ($count_deleted_admin_forms == 1)
				{
					show_message($GLOBALS['MSG_admin_forms']['show_form_delete_success']);
				}
				elseif ($count_deleted_admin_forms > 1)
				{
					show_message($GLOBALS['MSG_admin_forms']['show_forms_delete_success']);
				}
			}
			else
			{
				show_error_message($GLOBALS['MSG_admin_forms']['show_form_delete_error']);
			}
		}


	}
	$param['error'] = ob_get_clean();
}
?>