<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добаления/редактирования аффилиата.
 * 
 * Файл: /admin/shop/action/form_edit_add_affiliate.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
$form_params = array();
$form_params['title'] = ($affiliate_plans_id == 0)?$GLOBALS['MSG_shops']['affiliate_form_add']:$GLOBALS['MSG_shops']['affiliate_form_edit'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'AddAffiliatePlan';

$shop = & singleton('shop');

$admin_forms_fields = new admin_forms_fields();

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

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


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

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

$shop_name = $shop_row['shop_shops_name'];

$new_path_array = array(
array(
'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data'),
'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', '', 54, 'load_data'),
'name' => $GLOBALS['MSG_shops']['header_admin__list_forms']),

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


// Передаем в массив для констурктора хлебные крошки
$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

$DataBase = & singleton('DataBase');

$DateTime = new DateClass();

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab']);

$shop = new shop();

if ($affiliate_plans_id)
{
	$row = $affiliate->GetAffiliatePlans($affiliate_plans_id);

	$site_id = $row['site_id'];

	$affiliate_plans_name = $row['affiliate_plans_name'];

	$affiliate_plans_description = $row['affiliate_plans_description'];

	$affiliate_plans_activity = $row['affiliate_plans_activity'];

	$site_users_group_id = $row['site_users_group_id'];

	$affiliate_plans_last_change_datetime = $DateTime->datetime_format($row['affiliate_plans_last_change_datetime']);

	$affiliate_plans_min_num_of_items = $row['affiliate_plans_min_num_of_items'];

	$affiliate_plans_min_sum_of_items = $row['affiliate_plans_min_sum_of_items'];

	$affiliate_plans_delivery_on = $row['affiliate_plans_delivery_on'];
}
else
{
	$site_id = CURRENT_SITE;

	$affiliate_plans_name = '';

	$affiliate_plans_description = '';

	$affiliate_plans_activity = 1;

	$site_users_group_id = 0;

	$affiliate_plans_last_change_datetime = date("d.m.Y H:i:s");

	$affiliate_plans_min_num_of_items = 0;

	$affiliate_plans_min_sum_of_items = '0.00';

	$affiliate_plans_delivery_on = 0;
}

// Название партнерки
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_name';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_name'];
$param['type'] = 0; // Поле ввода.
$param['value'] = $affiliate_plans_name;
$param['format']['minlen']['value'] = 1;
$param['format']['maxlen']['value'] = 255;
$param['attributes']['class'] = 'large';

$admin_forms_fields->AddField($param);

// Описание партнерки
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_description';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_description'];
$param['type'] = 5; // Поле ввода.
$param['attributes']['rows'] = 10;
$param['value'] = $affiliate_plans_description;

$admin_forms_fields->AddField($param);

$site = new site();

// Получаем список всех сайтов
$site_res = $site->select_site(-1);

$site_array = array(0 => ' ... ');

if ($site_res)
{
	while ($site_row = mysql_fetch_assoc($site_res))
	{
		$site_array[$site_row['site_id']] = $site_row['site_name'];
	}
}

// Сайт, которому принадлежит партнерка
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'site_id';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_site_id'];
$param['type'] = 2; // Выпадающий список
$param['value'] = $site_id;
$param['items'] = $site_array;

$admin_forms_fields->AddField($param);

// Активность партнерки
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_activity';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_activity'];
$param['type'] = 1; // Флажок
$param['value'] = $affiliate_plans_activity;

$admin_forms_fields->AddField($param);

$SiteUsers = new SiteUsers();

// Получаем все группы пользователей сайта
$site_users_groups_res = $SiteUsers->SelectSiteUsersGroups();

$groups_array = array(0 => ' ... ');

if ($site_users_groups_res)
{
	while ($site_users_groups_row = mysql_fetch_assoc($site_users_groups_res))
	{
		$groups_array[$site_users_groups_row['site_users_group_id']] = $site_users_groups_row['site_users_group_name'];
	}
}

// Группа пользователей сайта, для которых доступна партнерка
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'site_users_group_id';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_site_users_group_id'];
$param['type'] = 2; // Выпадающий список
$param['value'] = $site_users_group_id;
$param['items'] = $groups_array;

$admin_forms_fields->AddField($param);

// Дата/время последнего изменения партнерки
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_last_change_datetime';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_last_change_datetime'];
$param['type'] = 11; // Выпадающий список
$param['value'] = $affiliate_plans_last_change_datetime;
$param['items'] = $groups_array;

$admin_forms_fields->AddField($param);

// Минимальное количенство товара, которое необходимо приобрести для активации плана
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_min_num_of_items';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_min_num_of_items'];
$param['type'] = 0; // Поле ввода
$param['value'] = $affiliate_plans_min_num_of_items;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($param);

// Минимальная сумма, на которую нужно приобрести товара для активации плана
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_min_sum_of_items';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_min_sum_of_items'];
$param['type'] = 0; // Поле ввода
$param['value'] = $affiliate_plans_min_sum_of_items;
$param['div_attributes']['style'] = 'float: left; margin-right: 10px; width: 300px;';
$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Учитывать ли доставку при просчете комиссии аффилиата
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'affiliate_plans_delivery_on';
$param['caption'] = $GLOBALS['MSG_shops']['form_edit_add_affiliate_plans_delivery_on'];
$param['type'] = 1; // Флажок
$param['value'] = $affiliate_plans_delivery_on;

$admin_forms_fields->AddField($param);

if ($affiliate_plans_id != 0)
{
	$param = array();
	$param['tab_id'] = $tab_id;
	$param['name'] = 'affiliate_plans_id';
	$param['caption'] = '';
	$param['type'] = 4; // hidden.
	$param['value'] = $affiliate_plans_id;
	$admin_forms_fields->AddField($param);
}

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_affiliate_plan';
$param['caption'] = $GLOBALS['MSG_shops']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$param = array();
$param['name'] = 'add_affiliate_plan';
$param['caption'] = $GLOBALS['MSG_shops']['apply'];
$param['image'] = '/admin/images/add.gif';
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($param);

// Отображение формы.
$admin_forms_fields->ShowForm();
?>