<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Ajax загрузчик списка языков Центра администрирования.
 * 
 * Файл: /admin/admin_forms/action/load_admin_languages.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_language_table` WHERE 1 {FILTER_STRING} {ORDER_STRING} {LIMIT}";

	// Посчитаем общее число, запрос обязательно должен возвращать столбец `count`.
	$count = "SELECT count(admin_language_id) as count  FROM `admin_language_table` WHERE 1  {FILTER_STRING}";

	// Формируем верхнее меню.
	$menu = array();
	$menu[$k = 0]['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_add'];

	$sub_menu[$p]['link'] = $admin_forms->GetHtmlCallTrigerSingleAction('edit_admin_language', 'check_0_0', 93, 0, 0);
	$sub_menu[$p]['onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('edit_admin_language', 'check_0_0', 93, 0, 0);

	$sub_menu[$p]['icon'] = '/admin/images/language_add.gif';

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

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

	// Формируем "хлебные крошки"
	$new_path_array[] = array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 1, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 1, 'load_data'),
	'name' => $GLOBALS['MSG_admin_forms']['show_form_fields_menu_admin_forms']);

	$new_path_array[] = array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 93, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/admin_forms/admin_forms.php', '', 93, 'load_data'),
	'name' => $GLOBALS['MSG_admin_forms']['show_form_fields_menu_admin_languages']);

	$param['path_array'] = $new_path_array;

	$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_languages'];

	return $param;
}

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

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

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

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

				$adm_forms = new admin_forms();

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

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

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					$language_param = array();

					$language_param['admin_language_id'] = $key;
					$language_param['admin_language_order'] = $row['admin_language_order'];

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


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

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

			$adm_forms = new admin_forms();

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

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

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

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

				$param['title'] = $admin_language_id == 0
				? $GLOBALS['MSG_admin_forms']['form_add_forms_add_language_title']
				: $GLOBALS['MSG_admin_forms']['form_add_forms_edit_language_title'];

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

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


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

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

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



		$count_deleted_admin_languages = 0;

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

				$adm_forms = new admin_forms();

				// Определяем информацию об объекте
				$object_row = $adm_forms->GetLanguage($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->DeleteAdminLanguage($key))
					{
						++$count_deleted_admin_languages;
						$flag_success_delete_admin_language = true;
					}
					else
					{
						$flag_success_delete_admin_language = false;
					}
				}
				else
				{
					$flag_success_delete_admin_language = false;
					$user_access->MessageOnlyOwn();
				}
			}

			if ($flag_success_delete_admin_language)
			{
				if ($count_deleted_admin_languages == 1)
				{
					show_message($GLOBALS['MSG_admin_forms']['language_delete_success']);
				}
				elseif ($count_deleted_admin_languages > 1)
				{
					show_message($GLOBALS['MSG_admin_forms']['languages_delete_success']);
				}
			}
			else
			{
				show_error_message($GLOBALS['MSG_admin_forms']['language_delete_error']);
			}
		}
	}
	$param['error'] = ob_get_clean();
}
?>