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

	$DataBase = & singleton('DataBase');

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

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

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

	//Хлебные крошки
	$param['path_array'] = array(
	array (
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/seo/seo.php', '', 141, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/seo/seo.php', '', 141, 'load_data'),
	'name' => $GLOBALS['MSG_seo']['header_admin_characteristic_forms']),
	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/seo/seo.php', '', 142, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/seo/seo.php', '', 142, 'load_data'),
	'name' => $GLOBALS['MSG_seo']['menu_search_query']),
	array(
	'link' => $admin_forms->GetHtmlCallDoLoadAjax('/admin/seo/seo.php', '', 144, 'load_data'),
	'onclick' => $admin_forms->GetOnClickCallDoLoadAjax('/admin/seo/seo.php', '', 144, 'load_data'),
	'name' => $GLOBALS['MSG_seo']['menu_statistic']));

	$query = "SELECT `seo_search_query_value`
	FROM `seo_search_query_table`
	WHERE `seo_search_query_id` = '$seo_search_query_id'";
	$search_query_res = mysql_fetch_assoc($DataBase->query($query));

	//Заголовок формы
	$param['title'] = sprintf($GLOBALS['MSG_seo']['statistic_pos_query'], $search_query_res['seo_search_query_value']);

	$site_id = CURRENT_SITE;

	$position_yandex = $position_google = $position_rambler = $position_aport = $position_gogo = $position_yahoo = $position_livesearch = 1;


	$date_start = date("Y-m-d H:i:s", time() - 60*60*24*31);

	$date_end = date("Y-m-d H:i:s");

	// График
	$s = '<img src="/admin/seo/seo_graph.php?id='.$site_id.'&id_return=4&query_id='.$seo_search_query_id.'&date_start='.rawurlencode($date_start).'&date_end='.rawurlencode($date_end).'&position_yandex='.$position_yandex.'&position_google='.$position_google.'&position_rambler='.$position_rambler.'&position_aport='.$position_aport.'&position_gogo='.$position_gogo.'&position_yahoo='.$position_yahoo.'&position_livesearch='.$position_livesearch.'" align="center">';

	$admin_forms->data_under_menu = $s;
	
	// Добавляем подэлементы.
	$param['total_count'][] = $count;
	$param['current_page'] = $page;
	$param['on_page'] = $on_page;

	return $param;

}

//Обработчики событий

/**
 * Обработчик события "Редактировать позиции"
 *
 * @param array $param 
 * @return boolean
 */
function edit_position_search(&$param)
{
	ob_start();

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

		$kernel->ReadMode();
	}
	else
	{

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

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

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

					if ($user_access->IssetUserAccessForObject($object_user_id))
					{
						$edit_position_search_id = intval($key);
						include(CMS_FOLDER . 'admin/seo/action/form_edit_add_position_search.php');

						//Устанавливаем <title> для страницы
						$param['title'] = $edit_position_search_id == 0
						? $GLOBALS['MSG_seo']['position_query_add_form_title']
						: $GLOBALS['MSG_seo']['position_query_edit_form_title'];
						break;
					}
					else
					{
						$user_access->MessageOnlyOwn();
						$param['error'] = ob_get_clean();
						return ;
					}
				}
			}
		}
		$param['result'] = ob_get_clean();

		return true;
	}
}

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

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

		$kernel->ReadMode();
	}
	else
	{
		$count_deleted_position_search = 0;

		$Seo = new Seo();

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

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

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

					if ($user_access->IssetUserAccessForObject($object_user_id))
					{
						if (!$Seo->DeletePositionSearch($key))
						{
							$flag_deleted_position_search_access = false;
						}
						else
						{
							$count_deleted_position_search++;

							$flag_deleted_position_search_access = true;
						}
					}
					else
					{
						$user_access->MessageOnlyOwn();

						$flag_deleted_position_search_access = false;
					}
				}

				if ($flag_deleted_position_search_access)
				{
					if ($count_deleted_position_search == 1)
					{
						show_message($GLOBALS['MSG_seo']['delete_position_search_success']);
					}
					else
					{
						show_message($GLOBALS['MSG_seo']['delete_position_search_success']);
					}
				}
				else
				{
					show_error_message($GLOBALS['MSG_seo']['delete_position_search_error']);
				}
			}
		}
	}
	$param['error'] = ob_get_clean();
}


?>