Skip to content

Instantly share code, notes, and snippets.

@maximzasorin
Last active December 1, 2015 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximzasorin/6d359ff8f98342944d77 to your computer and use it in GitHub Desktop.
Save maximzasorin/6d359ff8f98342944d77 to your computer and use it in GitHub Desktop.
Наблюдатель, который заполняет множественное числовое доп. свойство на основе диапазона, указанного в элементе списочного свойства (HostCMS)
<?php
/*
* В bootstrap.php:
*
// Заполнение числового свойства на основе списочного свойства «Площадь освещения»
Core_Event::attach('shop_item.onAfterSave', array('Shop_Item_Observers_Updateulluminationarea', 'onAfterSave'));
*
*/
defined('HOSTCMS') || exit('HostCMS: access denied.');
class Shop_Item_Observers_Updateulluminationarea
{
// Удаляет значения свойства для заданного объекта
static protected function _eraseProperty($oSliderProperty, $object)
{
$oProperty_Values = $oSliderProperty->getValues($object->id);
foreach ($oProperty_Values as $oProperty_Value)
{
$oProperty_Value->delete();
}
}
static public function onAfterSave($object, $args)
{
// Списочное и числовое свойства
$oListProperty = Core_Entity::factory('Property', 80);
$oSliderProperty = Core_Entity::factory('Property', 306);
self::_eraseProperty($oSliderProperty, $object);
$aListPropertyValues = $oListProperty->getValues($object->id, FALSE);
foreach ($aListPropertyValues as $oListPropertyValue)
{
$oListItem = Core_Entity::factory('List', $oListProperty->list_id)
->List_Items
->getById($oListPropertyValue->value, FALSE);
if ($oListItem)
{
// create multiple values
$sIlluminationRange = str_replace(' ', '', $oListItem->value);
$aIlliminationRange = explode('-', $sIlluminationRange);
if (count($aIlliminationRange) == 2)
{
$startValue = intval($aIlliminationRange[0]);
$endValue = intval($aIlliminationRange[1]);
for ($illuminateArea = $startValue; $illuminateArea <= $endValue; $illuminateArea++)
{
$oSliderProperty_Value = $oSliderProperty->createNewValue($object->id);
$oSliderProperty_Value->setValue($illuminateArea);
$oSliderProperty_Value->save();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment