<?php

defined('HOSTCMS') || exit('HostCMS: access denied.');

/**
 * Online shop.
 *
 * @package HostCMS 6\Shop
 * @version 6.x
 * @author Hostmake LLC
 * @copyright © 2005-2013 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
class Shop_Price_Controller_Edit extends Admin_Form_Action_Controller_Type_Edit
{
	/**
	 * Set object
	 * @param object $object object
	 * @return self
	 */
	public function setObject($object)
	{
		if (is_null($object->id))
		{
			$object->shop_id = Core_Array::getGet('shop_id');
		}

		parent::setObject($object);

		$oMainTab = $this->getTab('main');
		$oAdditionalTab = $this->getTab('additional');

		$oImportExportTab = Core::factory('Admin_Form_Entity_Tab')
			->caption(Core::_('Shop_Price.import_export_tab'))
			->name('ImportExport');

		$this->addTabAfter($oImportExportTab, $oMainTab);

		$oMainTab->move($this->getField('guid'), $oImportExportTab);

		// Удаляем группу доступа
		$oAdditionalTab->delete
		(
			$this->getField('siteuser_group_id')
		);

		if (Core::moduleIsActive('siteuser'))
		{
			$oSiteuser_Controller_Edit =
								new Siteuser_Controller_Edit($this->_Admin_Form_Action);
			$aSiteuser_Groups =
				$oSiteuser_Controller_Edit->fillSiteuserGroups
				(
						$this->_object->Shop->site_id
				);
		}
		else
		{
			$aSiteuser_Groups = array();
		}

		// Создаем поле групп пользователей сайта как выпадающий список
		$oSiteUserGroupSelect = new Admin_Form_Entity_Select();
		$oSiteUserGroupSelect
			->caption(Core::_("Shop_Item.siteuser_group_id"))
			->options(
				array(
					-1 => Core::_('Shop_Item.shop_users_group_parrent')
				) + $aSiteuser_Groups
			)
			->name('siteuser_group_id')
			->value($this->_object->siteuser_group_id);

		// Добавляем группы пользователей сайта
		$oMainTab->addAfter(
				$oSiteUserGroupSelect, $this->getField('name')
			);
			
		$oAdditionalCheckbox = new Admin_Form_Entity_Checkbox();
		if (is_null($object->id))
		{
			$oAdditionalCheckbox->name('apply_for_all')->caption(Core::_("Shop_Item.prices_add_form_apply_for_all"));
		}
		else
		{
			$oAdditionalCheckbox->name('recalculate_price')->caption(Core::_("Shop_Item.prices_add_form_recalculate"));
		}
		$oAdditionalCheckbox->value(1);
		$oMainTab->addAfter($oAdditionalCheckbox, $this->getField('percent'));

		$title = $this->_object->id
					? Core::_('Shop_Price.prices_edit_form_title')
					: Core::_('Shop_Price.prices_add_form_title');

		$this->title($title);

		return $this;
	}
	
	/**
	 * Processing of the form. Apply object fields.
	 * @return self
	 */
	protected function _applyObjectProperty()
	{
		parent::_applyObjectProperty();
		
		if(!is_null(Core_Array::getPost('apply_for_all')))
		{
			$aShop_Items = $this->_object->Shop->Shop_Items->findAll(FALSE);
			foreach ($aShop_Items as $oShop_Item)
			{
				$oShop_Item_Price = Core_Entity::factory('Shop_Item_Price');
				$oShop_Item_Price->value = $oShop_Item->price / 100 * $this->_object->percent;
				$oShop_Item_Price->shop_price_id = $this->_object->id;
				$oShop_Item->add($oShop_Item_Price);
			}
		}
		elseif(!is_null(Core_Array::getPost('recalculate_price')))
		{
			$aShop_Item_Prices = $this->_object->Shop_Item_Prices->findAll(FALSE);
			foreach ($aShop_Item_Prices as $oShop_Item_Price)
			{
				$oShop_Item_Price->value = $oShop_Item_Price->Shop_Item->price / 100 * $this->_object->percent;
				$oShop_Item_Price->save();
			}
		}
		
		return $this;
	}
}