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

	$shop_shops_id = to_int($_REQUEST['shop_shops_id']);
	
	$param = array();
	
	$shop = & singleton('shop');
	
	$warehouse = & singleton('warehouse');
	
	$row = $shop->GetShop($shop_shops_id);

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

	// Преобразуем идентификаторы групп в ссылки на группы.
	$new_path_array = array();
	$new_path_array[0]['name'] = $GLOBALS['MSG_shops']['header_admin__list_forms'];
	$new_path_array[0]['link'] = $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');
	$new_path_array[0]['onclick'] = $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data');
	
	$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);

	$new_path_array[1]['name'] = $row['shop_shops_name'];
	$new_path_array[1]['link'] = $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');
	$new_path_array[1]['onclick'] = $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');

	$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

	if ($path_array)
	{
		foreach ($path_array as $key => $value)
		{
			$new_path_array[] = array (
			'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
			'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
			'name' => $value);
		}
	}

	$new_path_array[] = array("name" => $GLOBALS['MSG_shops']['main_menu_warehouses_list'],
	"link" => $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 181, 'load_data'),
	"onclick" => $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 181, 'load_data')
	);
	$param['path_array'] = $new_path_array;

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

	// Число нужных записей.
	$count = "
	SELECT count(*) AS count FROM shop_warehouse_table WHERE `shop_shops_id`='$shop_shops_id'
	{FILTER_STRING}";

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

	$p = 0;

	$sub_menu = array();
	$sub_menu[$p]['name'] = $GLOBALS['MSG_shops']['main_menu_warehouses_add'];
	$sub_menu[$p]['link'] = $admin_forms->GetHtmlCallTrigerSingleAction('edit_warehouse', 'check_0_0', 181, 0, 0);
	$sub_menu[$p]['onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('edit_warehouse', 'check_0_0', 181, 0, 0);
	$sub_menu[$p]['icon'] = '/admin/images/company_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_shops']['main_menu_warehouses_list'];
 
	$result = $warehouse->GetDefaultWarehouse($shop_shops_id);
	
	if($result === false)
	{
		show_error_message($GLOBALS['MSG_shops']['warehouse_default_not_exist']);
	}
	
	return $param;
}

function edit_warehouse(&$param)
{
	ob_start();

	$warehouse = & singleton('warehouse');
	
	$user_access = & singleton('user_access');

	if (isset($param['data']) 
	&& isset($param['data'][0]))
	{
		foreach ($param['data'][0] as $shop_warehouse_id => $value)
		{
			// Определяем информацию об объекте
			$object_row = $warehouse->GetWarehouse($shop_warehouse_id);

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

			if ($user_access->IssetUserAccessForObject($object_user_id))
			{
				include(CMS_FOLDER.'admin/shop/action/form_edit_add_warehouse.php');
				
				$param['title'] = ($shop_warehouse_id == 0)
				? $GLOBALS['MSG_shops']['main_menu_warehouses_edit_add']
				: $GLOBALS['MSG_shops']['main_menu_warehouses_edit'];
			}
			else
			{
				$user_access->MessageOnlyOwn();
				$param['error'] = ob_get_clean();
				return ;
			}
		}
	}

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

function activity(&$param)
{
	ob_start();

	$shop_shops_id = to_int($_REQUEST['shop_shops_id']);
	
	if (defined('READ_ONLY') && READ_ONLY)
	{
		$kernel = & singleton('kernel');

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

		if (isset($param['data']) && isset($param['data'][0]))
		{
			$user_access = & singleton('user_access');
			
			$shop = & singleton('shop');

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

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

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

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					$warehouse_row = $warehouse->GetWarehouse($warehouse_id);
					
					if(!$warehouse->InsertWarehouse(array(
						'shop_warehouse_id' => $warehouse_id,
						'shop_warehouse_activity' => 1 - $warehouse_row['shop_warehouse_activity'],
						'shop_shops_id' => $shop_shops_id
					)))
					{
						show_error_message($GLOBALS['MSG_shops']['warehouse_default_not_set_activity']);
					}
				}
				else
				{
					$user_access->MessageOnlyOwn();
					$param['error'] = ob_get_clean();
					return ;
				}
			}
		}
	}
	
	$error = ob_get_clean();

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

function is_default(&$param)
{
	ob_start();

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

		$kernel->ReadMode();
	}
	else
	{
		$warehouse = & singleton('warehouse');
		
		$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

		if (isset($param['data']) && isset($param['data'][0]))
		{
			$user_access = & singleton('user_access');
			
			$shop = & singleton('shop');

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

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

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

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					$warehouse_row = $warehouse->GetWarehouse($warehouse_id);
					
					$warehouse->InsertWarehouse(array(
						'shop_warehouse_id' => $warehouse_id,
						'shop_warehouse_default' => 1 - $warehouse_row['shop_warehouse_default'],
						'shop_shops_id' => $shop_shops_id
					));
				}
				else
				{
					$user_access->MessageOnlyOwn();
					$param['error'] = ob_get_clean();
					return ;
				}
			}
		}
	}
	
	$error = ob_get_clean();

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


function delete_warehouse(&$param)
{
	ob_start();

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

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

		$warehouse = & singleton('warehouse');

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

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

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

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					if (!$warehouse->DeleteWarehouse($warehouse_id))
					{
						$flag_success = false;
					}
					else
					{
						$flag_success = true;
					}
				}
				else
				{
					$user_access->MessageOnlyOwn();

					$flag_success = false;
				}
			}

			if ($flag_success)
			{
				show_message($GLOBALS['MSG_shops']['warehouse_delete_success']);
			}
		}
	}

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

function apply(&$param)
{
	ob_start();
	
	$warehouse = & singleton('warehouse');
	
	if (defined('READ_ONLY') && READ_ONLY)
	{
		$kernel = & singleton('kernel');

		$kernel->ReadMode();
	}
	else
	{
		if (isset($param['data']) && isset($param['data'][0]))
		{
			// редактирование директории
			foreach ($param['data'][0] AS $shop_warehouse_id => $val)
			{
				// Определяем режим доступа пользователя и принадлежит ли объект пользователю
				$user_access = new user_access();

				// Определяем информацию об объекте
				$object_row = $warehouse->GetWarehouse($shop_warehouse_id);
				
				if ($object_row)
				{
					$object_user_id = $object_row['users_id'];
				}
				else
				{
					$object_user_id = 0;
				}

				if ($user_access->IssetUserAccessForObject($object_user_id))
				{
					$warehouse->InsertWarehouse(array(
						'shop_warehouse_id' => $shop_warehouse_id,
						'shop_warehouse_order' => $val['shop_warehouse_order']
					));
				}
				else
				{
					$user_access->MessageOnlyOwn();
					$param['error'] = ob_get_clean();
					return ;
				}
			}
		}
	}
	
	$error = ob_get_clean();

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

?>