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

	$site_users_id = to_int($_REQUEST['site_users_id']);

	$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

	if (class_exists('SiteUsers'))
	{
		// Получаем информацию о пользователе сайта
		$SiteUser = & singleton('SiteUsers');

		$site_users_row = $SiteUser->GetSiteUser($site_users_id, array('cache_off' => true));

		$site_users_login = $site_users_row['site_users_login'];
	}
	else
	{
		$site_users_login = '';
	}

	// Получаем информацию о магазине
	$shop = & singleton('shop');

	$shop_row = $shop->GetShop($shop_shops_id, array('cache_off' => true));

	$shop_name = $shop_row['shop_shops_name'];

	// Строим хлебные крошки.
	$parray = array(
	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/Site_users/Site_users.php', '', 28, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/Site_users/Site_users.php', '', 28, 'load_data'),
	'name' => $GLOBALS['MSG_site_users']['su_users_link']),

	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/Site_users/Site_users.php', '', 30, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/Site_users/Site_users.php', '', 30, 'load_data'),
	'name' => $GLOBALS['MSG_site_users']['su_users_control_title']
	),

	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '&site_users_id='.$site_users_id, 133, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '&site_users_id='.$site_users_id, 133, 'load_data'),
	'name' => sprintf($GLOBALS['MSG_shops']['users_account_table'], $site_users_login)
	),

	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '&site_users_id='.$site_users_id.'&shop_shops_id='.$shop_shops_id, 134, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '&site_users_id='.$site_users_id.'&shop_shops_id='.$shop_shops_id, 134, 'load_data'),
	'name' => sprintf($GLOBALS['MSG_shops']['transaction_user_account'], $site_users_login, $shop_name)
	));

	$param['path_array'] = $parray;

	// Выборка нужных записей.
 	$param['data'][] = "SELECT *
	FROM `shop_site_users_account_table` 
	LEFT JOIN `shop_currency_table`
	USING (`shop_currency_id`) 
	WHERE `shop_shops_id`='$shop_shops_id'
	AND `site_users_id`='$site_users_id'
	{FILTER_STRING} {ORDER_STRING} {LIMIT}";

	// Число нужных записей.
	$count = "SELECT count(*)
	AS count 
	FROM `shop_site_users_account_table` 
	LEFT JOIN `shop_currency_table` 
	USING (`shop_currency_id`)
	WHERE `shop_shops_id`='$shop_shops_id'
	AND `site_users_id`='$site_users_id'
	{FILTER_STRING}";

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

	$sub_menu = array();
	$sub_menu[$p = 0]['name'] = $GLOBALS['MSG_shops']['transaction_menu_add'];
	$sub_menu[$p]['link'] = $admin_forms->GetHtmlCallTrigerSingleAction('edit_transaction', 'check_0_0', 134, 0, 0);
	$sub_menu[$p]['onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('edit_transaction', 'check_0_0', 134, 0, 0);
	$sub_menu[$p]['icon'] = '/admin/images/transaction_add.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'] = sprintf($GLOBALS['MSG_shops']['transaction_user_account'], $site_users_login, $shop_name);

	$param['field_params'][0]['shop_site_users_account_active']['admin_forms_field_type'] = 7;// картинка-ссылка
	$param['field_params'][0]['shop_site_users_account_active']['admin_forms_field_link'] = $admin_forms->GetHtmlCallTrigerSingleAction('set_active', 'check_0_{shop_site_users_account_id}', 134, 0, 0);
	$param['field_params'][0]['shop_site_users_account_active']['admin_forms_field_onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('set_active', 'check_0_{shop_site_users_account_id}', 134, 0, 0);
	$param['field_params'][0]['shop_site_users_account_active']['admin_forms_field_alt'] = "0=".$GLOBALS['MSG_shops']['transaction_is_active']
	. "\n1=".$GLOBALS['MSG_shops']['transaction_is_active'];
	$param['field_params'][0]['shop_site_users_account_active']['admin_forms_field_title'] = "0=".$GLOBALS['MSG_shops']['transaction_is_active']
	. "\n1=".$GLOBALS['MSG_shops']['transaction_is_active'];

	return $param;
}

// Обработчик события "Редактирование пользователя"

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

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

			$shop = & singleton('shop');

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

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

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

				include(CMS_FOLDER . 'admin/shop/action/form_add_transaction.php');

				// Устанавливаем <title> для страницы
				$param['title'] = ($transaction_id == 0)?$GLOBALS['MSG_shops']['transaction_add_form']:$GLOBALS['MSG_shops']['transaction_edit_form'];

				// Редактировать можно только 1 элемент
				break;
			}
			else
			{
				$user_access->MessageOnlyOwn();
				$param['error'] = ob_get_clean();
				return ;
			}
		}
	}

	$param['result'] = ob_get_clean();

	return true;
}

/**
 * Обработчик активности транзакции
 *
 * @param array $param
 */
function set_active(&$param)
{
	ob_start();
	if (defined('READ_ONLY') && READ_ONLY)
	{
		$kernel = & singleton('kernel');

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

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

					$shop = & singleton('shop');

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

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

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

						$DataBase->query("UPDATE `shop_site_users_account_table`
				SET `shop_site_users_account_active`=1-shop_site_users_account_active 
				WHERE `shop_site_users_account_id`='$key'");
					}
					else
					{
						$user_access->MessageOnlyOwn();
						$param['error'] = ob_get_clean();
						return ;
					}
				}
			}
		}
	}
	$error = ob_get_clean();

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