<?php
/**
 * Online shop.
 *
 * @package HostCMS
 * @version 6.x
 * @author Hostmake LLC
 * @copyright © 2005-2013 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
require_once('../../../../../bootstrap.php');

Core_Auth::authorization('shop');

$oShopItemParent = Core_Entity::factory('Shop_Item', intval(Core_Array::getGet('shop_item_id', 0)));
$oShop = $oShopItemParent->Shop;
$oShopGroup = $oShopItemParent->Shop_Group;
$oShopDir = $oShop->Shop_Dir;

$sFormTitle = Core::_("Shop_Item.item_modification_title", $oShopItemParent->name);

$oAdmin_Form_Controller = new Admin_Form_Controller();
$oAdmin_Form_Controller
	->setUp()
	->path('/admin/shop/item/modification/create/index.php')
	->title(Core::_('Shop_Item.create_modification_title'));

// Хлебные крошки
$oAdmin_Form_Entity_Breadcrumbs = new Admin_Form_Entity_Breadcrumbs();

// Первая крошка на список магазинов
$oAdmin_Form_Entity_Breadcrumbs->add(
Core::factory('Admin_Form_Entity_Breadcrumb')
	->name(Core::_('Shop.menu'))
	->href($oAdmin_Form_Controller->getAdminLoadHref('/admin/shop/index.php'))
	->onclick($oAdmin_Form_Controller->getAdminLoadAjax('/admin/shop/index.php'))
);

// Крошки по директориям магазинов
if($oShopDir->id)
{
	$oShopDirBreadcrumbs = $oShopDir;

	$aBreadcrumbs = array();

	do
	{
		$aBreadcrumbs[] = Core::factory('Admin_Form_Entity_Breadcrumb')
		->name($oShopDirBreadcrumbs->name)
		->href($oAdmin_Form_Controller->getAdminLoadHref(
				'/admin/shop/index.php', NULL, NULL, "shop_dir_id={$oShopDirBreadcrumbs->id}"
		))
		->onclick($oAdmin_Form_Controller->getAdminLoadAjax(
				'/admin/shop/index.php', NULL, NULL, "shop_dir_id={$oShopDirBreadcrumbs->id}"
		));
	} while($oShopDirBreadcrumbs = $oShopDirBreadcrumbs->getParent());

	$aBreadcrumbs = array_reverse($aBreadcrumbs);

	foreach ($aBreadcrumbs as $oBreadcrumb)
	{
		$oAdmin_Form_Entity_Breadcrumbs->add($oBreadcrumb);
	}
}

// Крошка на список товаров и групп товаров магазина
$oAdmin_Form_Entity_Breadcrumbs->add(
	Core::factory('Admin_Form_Entity_Breadcrumb')
	->name($oShop->name)
	->href($oAdmin_Form_Controller->getAdminLoadHref('/admin/shop/item/index.php', NULL, NULL, "shop_id={$oShop->id}"))
	->onclick($oAdmin_Form_Controller->getAdminLoadAjax('/admin/shop/item/index.php', NULL, NULL, "shop_id={$oShop->id}"))
);

// Крошки по группам товаров
if($oShopGroup->id)
{
	$oShopGroupBreadcrumbs = $oShopGroup;

	$aBreadcrumbs = array();

	do
	{
		$aBreadcrumbs[] = Core::factory('Admin_Form_Entity_Breadcrumb')
			->name($oShopGroupBreadcrumbs->name)
			->href($oAdmin_Form_Controller->getAdminLoadHref('/admin/shop/item/index.php', NULL, NULL, "shop_id={$oShop->id}&shop_group_id={$oShopGroup->id}"))
			->onclick($oAdmin_Form_Controller->getAdminLoadAjax('/admin/shop/item/index.php', NULL, NULL, "shop_id={$oShop->id}&shop_group_id={$oShopGroup->id}"));
	} while($oShopGroupBreadcrumbs = $oShopGroupBreadcrumbs->getParent());

	$aBreadcrumbs = array_reverse($aBreadcrumbs);

	foreach ($aBreadcrumbs as $oBreadcrumb)
	{
		$oAdmin_Form_Entity_Breadcrumbs->add($oBreadcrumb);
	}
}

// Последняя крошка на текущую форму
$oAdmin_Form_Entity_Breadcrumbs->add(
	Core::factory('Admin_Form_Entity_Breadcrumb')
		->name($sFormTitle)
		->href($oAdmin_Form_Controller->getAdminLoadHref("/admin/shop/item/modification/index.php", NULL, NULL, "shop_item_id={$oShopItemParent->id}"))
		->onclick($oAdmin_Form_Controller->getAdminLoadAjax("/admin/shop/item/modification/index.php", NULL, NULL, "shop_item_id={$oShopItemParent->id}"))
);

$oAdmin_Form_Controller->addEntity($oAdmin_Form_Entity_Breadcrumbs);

ob_start();

$oAdmin_Form_Entity_Title = new Admin_Form_Entity_Title();
$oAdmin_Form_Entity_Title->name = Core::_('Shop_Item.create_modification_title');
$oAdmin_Form_Entity_Title->execute();

$oAdmin_Form_Entity_Form = new Admin_Form_Entity_Form($oAdmin_Form_Controller);
$oAdmin_Form_Entity_Form->action('/admin/shop/item/modification/index.php');
$oAdmin_Form_Entity_Form->enctype('multipart/form-data');
$oAdmin_Form_Entity_Form->add($oAdmin_Form_Entity_Breadcrumbs);

$aProperties = Core_Entity::factory('Shop_Item_Property_List', $oShop->id)->getPropertiesForGroup($oShopItemParent->shop_group_id);

$aNamePattern = array();

foreach($aProperties as $oProperty)
{
	// Если тип свойства - "Список" и модуль списков активен
	if($oProperty->type == 3 && Core::moduleIsActive('list'))
	{
		$aNamePattern[] = sprintf("%s {P%s}", $oProperty->name, $oProperty->id);

		$aListItems = $oProperty->List->List_Items->getAllByActive(1);
		$aValues = array();
		foreach($aListItems as $oListItem)
		{
			$aValues[$oListItem->id] = array('value' => $oListItem->value, 'attr' => array('selected' => 'selected'));
		}
		
		$oAdmin_Form_Entity_Form->add(
			Core::factory('Admin_Form_Entity_Checkbox')
				->name("property_{$oProperty->id}")
				->caption(Core::_('Shop_Item.create_modification_property_enable', $oProperty->name, $oProperty->id))
		)
		->add(
			Core::factory('Admin_Form_Entity_Select')
				->options($aValues)
				->multiple("multiple")
				->name("property{$oProperty->id}list[]")
				->style('width:300px;')
				->filter(FALSE)
		);
	}
}

$aCurrencies = array(' … ');
$aShop_Currencies = Core_Entity::factory('Shop_Currency')->findAll();
foreach($aShop_Currencies as $oShop_Currency)
{
	$aCurrencies[$oShop_Currency->id] = $oShop_Currency->name;
}

$aMeasures = array(' … ');
$aShop_Measures = Core_Entity::factory('Shop_Measure')->findAll();

foreach($aShop_Measures as $oShop_Measure)
{
	$aMeasures[$oShop_Measure->id] = $oShop_Measure->name;
}

$oAdmin_Form_Entity_Form->add(
	Core::factory('Admin_Form_Entity_Input')
		->name("price")
		->divAttr(array('style' => 'width: 75px; display: inline-block; padding-right:20px;'))
		->caption(Core::_('Shop_Item.create_modification_price'))
		->value($oShopItemParent->price)
)->add(
	Core::factory('Admin_Form_Entity_Select')
		->name('currency')
		->divAttr(array('style' => 'width: 75px; display: inline-block;'))
		->caption(' ')
		->options($aCurrencies)
		->value($oShopItemParent->shop_currency_id)
)->add(
	Core::factory('Admin_Form_Entity_Select')
		->name('measure')
		->divAttr(array('style' => 'width: 75px;'))
		->options($aMeasures)
		->value($oShopItemParent->shop_measure_id)
)->add(
	Core::factory('Admin_Form_Entity_Input')
		->name('marking')
		->caption(Core::_('Shop_Item.create_modification_mark'))
		->value(sprintf("%s-{N}", $oShopItemParent->marking))
)->add(
	Core::factory('Admin_Form_Entity_Input')
		->name('name')
		->caption(Core::_('Shop_Item.create_modification_name', $oShopItemParent->name, $oShopItemParent->name))
		->value(sprintf("%s %s", $oShopItemParent->name, implode(', ', $aNamePattern)))
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_main_properties')
		->caption(Core::_('Shop_Item.create_modification_copy_main_properties'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_seo')
		->caption(Core::_('Shop_Item.create_modification_copy_seo'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_export_import')
		->caption(Core::_('Shop_Item.create_modification_copy_export_import'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_prices_to_item')
		->caption(Core::_('Shop_Item.create_modification_copy_prices_to_item'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_specials_prices_to_item')
		->caption(Core::_('Shop_Item.create_modification_copy_specials_prices_to_item'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_tying_products')
		->caption(Core::_('Shop_Item.create_modification_copy_tying_products'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_external_property')
		->caption(Core::_('Shop_Item.create_modification_copy_external_property'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Checkbox')
		->name('copy_tags')
		->caption(Core::_('Shop_Item.create_modification_copy_tags'))
		->value(0)
)->add(
	Core::factory('Admin_Form_Entity_Input')
		->type('hidden')
		->name('shop_item_id')
		->value($oShopItemParent->id)
)->add(
	Core::factory('Admin_Form_Entity_Button')
	 ->name('create_modifications')
		->type('submit')
		->value(Core::_('Shop_Item.create_modification'))
		->class('applyButton')
		->onclick($oAdmin_Form_Controller->getAdminSendForm('generateModifications', NULL, 'hostcms[checked][0][0]=1&shop_item_id=' . $oShopItemParent->id))
);

$oAdmin_Form_Entity_Form->execute();

$oAdmin_Answer = Core_Skin::instance()->answer();

$oAdmin_Answer
->ajax(Core_Array::getRequest('_', FALSE))
->content(
	iconv("UTF-8", "UTF-8//IGNORE//TRANSLIT", ob_get_clean())
)
->title($sFormTitle)
->execute();