-
-
Save nook-ru/c3675c28a0fcb0aae378d84c948f8ef8 to your computer and use it in GitHub Desktop.
/bitrix/php_interface/init.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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