<?php
// Add custom field in email send order
AddEventHandler("sale", "OnOrderNewSendEmail", Array("EmailHandler", "SendInvoice"));
// require field and throw
AddEventHandler("main", "OnBeforeUserRegister", Array("UserHandler", "RequireField"));
AddEventHandler("main", "OnBeforeUserUpdate", Array("UserHandler", "RequireField"));
// PersonType If user IsAuthorized
AddEventHandler("sale", "OnSaleComponentOrderOneStepPersonType", Array("UserHandler", "PersonTypeOrderMake"));
// add custom field out $_REQUEST
AddEventHandler("main", "OnBeforeUserRegister", Array("UserHandler", "HandlerAddSecondName"));
// проверка login только латинские символы и цифры
AddEventHandler("main", "OnBeforeUserRegister", Array("UserHandler", "LoginCheck"));
AddEventHandler("main", "OnBeforeUserUpdate", Array("UserHandler", "LoginCheck"));
// Регистрация пользователя через корзину
AddEventHandler("main", "OnBeforeUserAdd", Array("GuestHandler", "RegistrationUser"));
// Восстановление пароля
AddEventHandler("main", "OnBeforeUserUpdate", Array("UserHandler", "ForgotPassword"));
/**
* Запись логов
* @param string $string строка с данными/массив
* @param string $path файл куда сохраняем
*/
function LogWriter($string, $path = "RegistrationUser")
{
$file = fopen($_SERVER['DOCUMENT_ROOT'].'/logs/'. $path .'.log', 'a+');
fwrite($file, json_encode($string, JSON_UNESCAPED_UNICODE).PHP_EOL."\t\t\t\t=========".PHP_EOL);
fclose($file);
}
$userInformation = Array();
class OrderHandler
{
function Agreement(&$arFields)
{
LogWriter($arFields, 'OrderHandler.Agreement.arFields');
LogWriter($_REQUEST, 'OrderHandler.Agreement.request');
}
}
class EmailHandler
{
function SendInvoice($ID, &$eventName, &$arFields, $val)
{
global $userInformation;
if ($userInformation['REG_NEW_USER'] == "Y"){
$arRegFields = Array(
"LOGIN" => $userInformation['arFields']['LOGIN'],
"PASSWORD" => $userInformation['arFields']['PASSWORD'],
"NAME" => $userInformation['arFields']['NAME'],
"LAST_NAME" => $userInformation['arFields']['LAST_NAME'],
"SECOND_NAME" => $userInformation['arFields']['SECOND_NAME'],
"URL_PERSONAL" => $_SERVER['HTTP_X_FORWARDED_PROTO'] . '://' . $_SERVER['HTTP_HOST']."/personal/profile/",
"EMAIL" => $userInformation['arFields']['EMAIL'],
);
CEvent::Send("NEW_USER_AUTO",'s1',$arRegFields, "N");
}
LogWriter($arFields, 'EmailHandler.SendInvoice.arFields');
LogWriter($_REQUEST, 'EmailHandler.SendInvoice.REQUEST');
LogWriter($userInformation, 'EmailHandler.SendInvoice.userInformation');
LogWriter($arRegFields, 'EmailHandler.SendInvoice.arRegFields');
$arFields["CUSTOM_FIELD_FILE"] = 'Подробнее о заказе и квитанция на оплату: ' . $_SERVER['HTTP_X_FORWARDED_PROTO'] . '://' . $_SERVER['HTTP_HOST'] . '/personal/orders/detail.php?ID=' . $ID;
}
}
class UserHandler
{
function ForgotPassword(&$arFields)
{
if ($_REQUEST['TYPE'] == "CHANGE_PWD" && $_REQUEST['AUTH_FORM'] == "Y" && $_REQUEST['change_password'] == "yes"){
global $USER;
$USER = !is_object($USER) ? new CUser : $USER;
$USER->Authorize($arFields['ID']);
}
}
function LoginCheck(&$arFields)
{
if (!preg_match("/^[a-zA-Z0-9_-]+$/", $arFields['LOGIN']) && $_REQUEST['TYPE'] != "CHANGE_PWD"){
global $APPLICATION;
$APPLICATION->throwException('Логин может состоять из латинских букв, цифр, знаков "-" и "_"');
return false;
}
}
function RequireField(&$arFields)
{
if ($_SERVER['SCRIPT_NAME'] != "/bitrix/admin/user_edit.php" && $_REQUEST['TYPE'] != "CHANGE_PWD"):
$errors = false;
global $APPLICATION;
if (!$_REQUEST['USER_SECOND_NAME'] && !$arFields['SECOND_NAME']){
$APPLICATION->throwException("Пожалуйста, введите Отчество.");
$errors = true;
}
if (!$arFields['NAME']){
$APPLICATION->throwException("Пожалуйста, введите Имя.");
$errors = true;
}
if (!$arFields['LAST_NAME']){
$APPLICATION->throwException("Пожалуйста, введите Фамилию.");
$errors = true;
}
return ($errors) ? false : true;
endif;
}
function PersonTypeOrderMake(&$arResult, &$arUserResult)
{
global $USER;
if($USER->IsAuthorized())
{
$rsUser = $USER->GetByID($USER->GetID());
$arUser = $rsUser->Fetch();
$personType = $arUser['UF_TYPE_FACE'];
//очищаем текущее значение типа плательщика
foreach($arResult['PERSON_TYPE'] as $key => $type){
if($type['CHECKED'] == 'Y'){
unset($arResult['PERSON_TYPE'][$key]['CHECKED']);
}
}
//устанавливаем новое значение типа плательщика
$arResult['PERSON_TYPE'][$personType]['CHECKED'] = 'Y';
$arUserResult['PERSON_TYPE_ID'] = $personType;
}
}
function HandlerAddSecondName(&$arFields)
{
LogWriter($arFields, "UserHandler.HandlerAddSecondName.arFields");
LogWriter($_REQUEST, "UserHandler.HandlerAddSecondName.REQUEST");
$arFields["SECOND_NAME"] = $_REQUEST['USER_SECOND_NAME'];
}
}
class GuestHandler
{
function RegistrationUser(&$arFields)
{
LogWriter($_REQUEST, "GuestHandler.RegistrationUser.REQUEST");
LogWriter($arFields, "GuestHandler.RegistrationUser.arFields");
if ($arFields['NAME'] != 'Анонимный пользователь' && $arFields['ACTIVE'] == 'Y' && $_REQUEST['TYPE'] != 'REGISTRATION'){
global $userInformation;
$fio = $_REQUEST['PERSON_TYPE'] == 2 ? $_REQUEST['ORDER_PROP_12'] : $_REQUEST['ORDER_PROP_1'];
$arFields['SECOND_NAME'] = explode(" ", $fio)[2]; // отчество
$arFields['UF_TYPE_FACE'] = $_REQUEST['PERSON_TYPE']; // Тип плательщика
$arFields['UF_INN'] = $_REQUEST['ORDER_PROP_10']; // ИНН
$arFields['UF_KPP'] = $_REQUEST['ORDER_PROP_11']; // КПП
$arFields['WORK_COMPANY'] = $_REQUEST['ORDER_PROP_8']; // Название компании
$userInformation['arFields'] = $arFields;
$userInformation['REQUEST'] = $_REQUEST;
$userInformation['REG_NEW_USER'] = "Y";
}
}
}
include_once($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/wsrubi.smtp/classes/general/wsrubismtp.php");