<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Ajax загрузчик формы списка констант.
 * 
 * Файл: /admin/constants/action/load_constants.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);

	// Выборка нужных записей.
	$param['data'][] = "SELECT * FROM constants_table WHERE (1) {FILTER_STRING} {ORDER_STRING} {LIMIT}";

	// Число нужных записей.
	$count = "SELECT count(constants_id) as count FROM constants_table WHERE (1) {FILTER_STRING}";

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

	$p = 0;

	$sub_menu = array();
	$sub_menu[$p]['name'] = $GLOBALS['MSG_constants']['constants_show_add'];
	$sub_menu[$p]['link'] = $admin_forms->GetHtmlCallTrigerSingleAction('edit_constant', 'check_0_0', 26, 0, 0);
	$sub_menu[$p]['onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('edit_constant', 'check_0_0', 26, 0, 0);
	$sub_menu[$p]['icon'] = '/admin/images/constant_add.gif';

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

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

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

	$param['title'] = $GLOBALS['MSG_constants']['constants_link'];
	return $param;

}

// Удаление константы
function delete_constant(&$param)
{
	ob_start();

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

		$kernel->ReadMode();
	}
	else
	{
		$constants = new Constants();

		$count = 0;

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

					$constants = new Constants();

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

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

					if ($user_access->IssetUserAccessForObject($object_user_id))
					{
						if (!$constants->DelConstants($key))
						{
							$flag_success = false;
						}
						else
						{
							++$count;
							$flag_success = true;
						}
					}
					else
					{
						$user_access->MessageOnlyOwn();
						$flag_success = false;
					}
				}
			}

			if ($flag_success)
			{
				if ($count == 1)
				{
					show_message($GLOBALS['MSG_constants']['constants_delete_success_message']);
				}
				else
				{
					show_message($GLOBALS['MSG_constants']['constantss_delete_success_message']);
				}
			}
			else
			{
				show_error_message($GLOBALS['MSG_constants']['constants_delete_error_message']);
			}
		}
	}
	$error = ob_get_clean();

	$param['error'] = $error;
}

// редактирование константы
function edit_constant(&$param)
{
	ob_start();

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

				$constants = new Constants();

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

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

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

					include(CMS_FOLDER . 'admin/constants/action/form_edit_add_constant.php');

					if ($constants_id)
					{
						$constant = new Constants();
						$row_constant = $constant->GetConstant($constants_id);
					}

					$param['title'] = $constants_id == 0
					? $GLOBALS['MSG_constants']['constants_add_header']
					: sprintf($GLOBALS['MSG_constants']['constants_edit_header'], to_str($row_constant['constants_name']));

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

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

// Обработчмк изменения статуса активности константы
function change_constant_status(&$param)
{
	ob_start();

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

		$kernel->ReadMode();
	}
	else
	{
		$DataBase =  & singleton('DataBase');

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

					$constants = new Constants();

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

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

					if ($user_access->IssetUserAccessForObject($object_user_id))
					{
						$query = "UPDATE `constants_table` SET `constants_activity`=1-`constants_activity`
					WHERE `constants_id` = '$key'";

						$DataBase->query($query);
					}
					else
					{
						$user_access->MessageOnlyOwn();
					}
				}
			}
		}
	}
	$error = ob_get_clean();

	$param['error'] = $error;
}

?>