// регистрируем обработчик
AddEventHandler("iblock", "OnAfterIBlockElementAdd", Array("MyClass", "OnAfterIBlockElementUpdateHandler"));
AddEventHandler("iblock", "OnAfterIBlockElementUpdate", Array("MyClass", "OnAfterIBlockElementUpdateHandler"));
AddEventHandler("iblock", "OnBeforeIBlockElementDelete", Array("MyClass", "OnBeforeIBlockElementDeleteHandler"));
class MyClass
{
// создаем обработчик события "OnAfterIBlockElementUpdate"
function OnAfterIBlockElementUpdateHandler(&$arFields)
{
if($arFields["RESULT"] && $arFields["IBLOCK_ID"] == 62)
{
$arSelectFields = array("ID", "NAME", "IBLOCK_ID", "IBLOCK_SECTION_ID", "CREATED_BY", "DATE_ACTIVE_FROM", "DATE_ACTIVE_TO", "DETAIL_TEXT", "PREVIEW_TEXT", "TIMESTAMP_X",
"MODIFIED_BY", "USER_NAME", "PROPERTY_PERIOD_TYPE", "PROPERTY_EVENT_LENGTH");
$dbElements = CIBlockElement::GetList(
array(),
array(
"IBLOCK_ID" => $arFields["IBLOCK_ID"],
"ID" => $arFields["ID"],
),
false,
false,
$arSelectFields
);
$arElement = $dbElements->GetNext();
if(isset($arElement["PREVIEW_TEXT"]) && !empty($arElement["PREVIEW_TEXT"]))
{
$arPassengers = unserialize($arElement["PREVIEW_TEXT"]);
$passengers = array();
foreach($arPassengers as $passenger)
{
$dbUser = CUser::GetByID($passenger);
if ($arUser = $dbUser->GetNext())
{
$arPass["ID"] = $arUser["ID"];
$arPass["NAME"] = CUser::FormatName($arParams['NAME_TEMPLATE_WO_NOBR'], $arUser, $bUseLogin);
$arPass["FIRST_NAME"] = $arUser["NAME"];
$arPass["LAST_NAME"] = $arUser["LAST_NAME"];
$arPass["SECOND_NAME"] = $arUser["SECOND_NAME"];
$arPass["LOGIN"] = $arUser["LOGIN"];
}
$passengers[] = $arPass["LOGIN"].' ('.$arPass["LAST_NAME"].((!empty($arPass["NAME"])) ? ' '.$arPass["NAME"] : '').')';
}
}
if ($arElement["PROPERTY_PERIOD_TYPE_VALUE"] != "NONE")
{
$arElement["DATE_ACTIVE_TO"] = Date(
$GLOBALS["DB"]->DateFormatToPHP(FORMAT_DATETIME),
MakeTimeStamp($arElement["DATE_ACTIVE_FROM"], FORMAT_DATETIME) + $arElement["PROPERTY_EVENT_LENGTH_VALUE"]
);
}
$arEventFields = array(
"ACTION_TYPE" => 'Изменен',
"ID" => $arFields["ID"],
"SECTION_ID" => $arElement["IBLOCK_SECTION_ID"],
"NAME" => $arElement["NAME"],
"TIMESTAMP_X" => $arElement["TIMESTAMP_X"],
// "MODIFIED_BY" => $modified_by,
"USER_NAME" => $arElement["USER_NAME"],
"DATE_ACTIVE_FROM" => FormatDate("FULL", strtotime($arElement["DATE_ACTIVE_FROM"])),
"DATE_ACTIVE_TO" => FormatDate("FULL", strtotime($arElement["DATE_ACTIVE_TO"])),
"DETAIL_TEXT" => $arElement["DETAIL_TEXT"],
"PASSENGERS" => implode(' / ', $passengers)
);
CEvent::Send("COURIER_PLANNER_EVENT", "s1", $arEventFields);
}
else
AddMessage2Log("Ошибка изменения записи ".$arFields["ID"]." (".$arFields["RESULT_MESSAGE"].").");
}
// создаем обработчик события "OnBeforeIBlockElementDelete"
function OnBeforeIBlockElementDeleteHandler($ID)
{
$res = CIBlockElement::GetByID($ID);
if($arElement = $res->GetNext())
{
if($arElement["IBLOCK_ID"] == 62)
{
if(isset($arElement["PREVIEW_TEXT"]) && !empty($arElement["PREVIEW_TEXT"]))
{
$arPassengers = unserialize($arElement["PREVIEW_TEXT"]);
$passengers = array();
foreach($arPassengers as $passenger)
{
$dbUser = CUser::GetByID($passenger);
if ($arUser = $dbUser->GetNext())
{
$arPass["ID"] = $arUser["ID"];
$arPass["NAME"] = CUser::FormatName($arParams['NAME_TEMPLATE_WO_NOBR'], $arUser, $bUseLogin);
$arPass["FIRST_NAME"] = $arUser["NAME"];
$arPass["LAST_NAME"] = $arUser["LAST_NAME"];
$arPass["SECOND_NAME"] = $arUser["SECOND_NAME"];
$arPass["LOGIN"] = $arUser["LOGIN"];
}
$passengers[] = $arPass["LOGIN"].' ('.$arPass["LAST_NAME"].((!empty($arPass["NAME"])) ? ' '.$arPass["NAME"] : '').')';
}
}
$arEventFields = array(
"ID" => $ID,
"NAME" => $arElement["NAME"],
"TIMESTAMP_X" => $arElement["TIMESTAMP_X"],
// "MODIFIED_BY" => $modified_by,
"USER_NAME" => $arElement["USER_NAME"],
"DATE_ACTIVE_FROM" => FormatDate("FULL", strtotime($arElement["DATE_ACTIVE_FROM"])),
"DATE_ACTIVE_TO" => FormatDate("FULL", strtotime($arElement["DATE_ACTIVE_TO"])),
"DETAIL_TEXT" => $arElement["DETAIL_TEXT"],
"PASSENGERS" => implode(' / ', $passengers)
);
CEvent::Send("COURIER_PLANNER_EVENT", "s1", $arEventFields);
}
}
}
} |