Skip to content

Instantly share code, notes, and snippets.

@nook-ru

nook-ru/init.php Secret

Created October 12, 2018 10:00
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 nook-ru/c3675c28a0fcb0aae378d84c948f8ef8 to your computer and use it in GitHub Desktop.
Save nook-ru/c3675c28a0fcb0aae378d84c948f8ef8 to your computer and use it in GitHub Desktop.
/bitrix/php_interface/init.php
<?php if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();
use Bitrix\Main\EventManager;
use Bitrix\Main\Loader;
use Citrus\Arealty;
use Citrus\Yandex\Geo\GeoObject;
/**
* Заполняет значения свойств на основе данных Местоположения (свойства geodata)
*
* @param int $ELEMENT_ID
* @param \Citrus\Yandex\Geo\GeoObject $geo
*/
function fillAddressProperties($ELEMENT_ID, GeoObject $geo)
{
// street — символьный код свойства Улица
CIBlockElement::SetPropertyValueCode($ELEMENT_ID, 'street', $geo->getThoroughfareName());
}
/**
* Вызывается при изменении значения свойства geodata
*
* @param int $elementId
* @param int $iblockId
* @param string $data Необработанное значение свойства, строка, которая хранится в БД
*/
function onGeodataChanged($elementId, $iblockId, $data)
{
$geodata = Arealty\Object\GeoProperty::convertFromDB(
Arealty\Iblock::getProperties($iblockId)['geodata'],
$data
)['VALUE'];
if ($geodata instanceof GeoObject)
{
fillAddressProperties($elementId, $geodata);
}
}
$eventManager = EventManager::getInstance();
$eventManager->addEventHandlerCompatible(
"iblock",
"OnAfterIBlockElementSetPropertyValuesEx",
function($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $FLAGS) {
if (
!empty($PROPERTY_VALUES['geodata'])
&& is_array($PROPERTY_VALUES['geodata'])
)
{
onGeodataChanged($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES['geodata']);
}
}
);
$eventManager->addEventHandlerCompatible(
"iblock",
"OnAfterIBlockElementSetPropertyValues",
function($ELEMENT_ID, $IBLOCK_ID, $PROPERTY_VALUES, $PROPERTY_CODE) {
if (!Loader::includeModule('citrus.arealty'))
{
return;
}
$properties = Arealty\Iblock::getProperties($IBLOCK_ID);
$propertyCode2Id = array_column($properties, 'ID', 'CODE');
if (
!empty($PROPERTY_VALUES[$propertyCode2Id['geodata']])
&& is_array($PROPERTY_VALUES[$propertyCode2Id['geodata']])
)
{
$data = reset($PROPERTY_VALUES[$propertyCode2Id['geodata']]);
onGeodataChanged($ELEMENT_ID, $IBLOCK_ID, $data);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment