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

	$shop = & singleton('shop');

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

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

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

	$row = $shop->GetShop($shop_shops_id);

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

	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '', 133, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '', 162, 'load_data'),
	'name' => $GLOBALS['MSG_shops']['affiliate_form_title']
	));*/

	// Строим хлебные крошки
	$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

	// Преобразуем идентификаторы групп в ссылки на группы.
	$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');

	$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');

	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" => sprintf($GLOBALS['MSG_shops']['affiliate_assotiation_form_title'], $row['shop_shops_name']),
	"link" => $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "", 166, 'load_data'),
	"onclick" => $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "", 166, 'load_data')
	);

	$param['path_array'] = $new_path_array;

	$site_id = CURRENT_SITE;

	// Выборка нужных записей.
	$param['data'][] = "SELECT affiliate_plans_table.*, IF(shop_shops_affiliate_plans_table.affiliate_plans_id IS NULL, 0, 1) AS enabled
	FROM affiliate_plans_table 
	LEFT JOIN shop_shops_affiliate_plans_table
	ON (shop_shops_affiliate_plans_table.shop_shops_id = '{$shop_shops_id}'
	AND affiliate_plans_table.affiliate_plans_id=shop_shops_affiliate_plans_table.affiliate_plans_id)
	WHERE (1)
	AND `site_id`='$site_id'
	{FILTER_STRING} {ORDER_STRING} {LIMIT}";

	// Число нужных записей.
	$count = "SELECT count(*) AS count
	FROM affiliate_plans_table 
	LEFT JOIN shop_shops_affiliate_plans_table
	ON (shop_shops_affiliate_plans_table.shop_shops_id = '{$shop_shops_id}'
	AND affiliate_plans_table.affiliate_plans_id=shop_shops_affiliate_plans_table.affiliate_plans_id)
	WHERE (1)
	AND `site_id`='$site_id'
	{FILTER_STRING}";

	$param['field_params'][0]['enabled']['admin_forms_field_type'] = 7;// картинка-ссылка
	$param['field_params'][0]['enabled']['admin_forms_field_link'] = $admin_forms->GetHtmlCallTrigerSingleAction('enabled', 'check_0_{affiliate_plans_id}', 166, 0, 0);
	$param['field_params'][0]['enabled']['admin_forms_field_onclick'] = $admin_forms->GetOnClickCallTrigerSingleAction('enabled', 'check_0_{affiliate_plans_id}', 166, 0, 0);

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

	//	$param['menus'] = $menu_array;
	$param['title'] = sprintf($GLOBALS['MSG_shops']['affiliate_assotiation_form_title'], $row['shop_shops_name']);

	return $param;
}

/**
 * Функция ассоциации партнерки магазину
 *
 * @param array $param
 * @return boolean
 */
function enabled(&$param)
{
	ob_start();

	$affiliate = new affiliate();

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

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

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

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

			if ($user_access->IssetUserAccessForObject($object_user_id))
			{
				$affiliate->AcceptAffiliatePlanToShop($key, $shop_shops_id);
			}
			else
			{
				$user_access->MessageOnlyOwn();
			}
		}
	}

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

/**
 * Обработчик события "Включить доступ"
 *
 * @param array $param
 * @return boolean
 */
function all_on(&$param)
{
	ob_start();

	$affiliate = new affiliate();

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

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

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

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

			if ($user_access->IssetUserAccessForObject($object_user_id))
			{
				$affiliate->EnableAffiliatePlanToShop($key, $shop_shops_id);
			}
			else
			{
				$user_access->MessageOnlyOwn();
			}
		}
	}

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

/**
 * Обработчик события "Выключить доступ"
 *
 * @param array $param
 * @return boolean
 */
function all_off(&$param)
{
	ob_start();

	$affiliate = new affiliate();

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

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

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

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

			if ($user_access->IssetUserAccessForObject($object_user_id))
			{
				$affiliate->DisableAffiliatePlanToShop($key, $shop_shops_id);
			}
			else
			{
				$user_access->MessageOnlyOwn();
			}
		}
	}

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