class.php
<?php if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();
use Bitrix\Main\Engine\Contract\Controllerable;
use Bitrix\Main\Engine\ActionFilter;
class AjaxComponent extends CBitrixComponent implements Controllerable
{
/**
* @inheritDoc
*/
public function configureActions()
{
return [
'save' => [
'prefilters' => [
new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_POST]),
new ActionFilter\Csrf(),
],
'postfilters' => []
],
'final' => [
'prefilters' => [
new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_POST]),
new ActionFilter\Csrf(),
],
'postfilters' => []
],
];
}
public function finalAction()
{
return new \Bitrix\Main\Engine\Response\Component(
'edcrunch:user.profile',
'onboarding',
[
'AVA_WIDTH' => 120,
'AVA_HEIGHT' => 120,
'SHOW_TRACKS' => 'Y',
'SHOW_TAGS' => 'Y',
]
);
}
public function saveAction()
{
}
public function executeComponent()
{
$this->getResult();
$this->includeComponentTemplate();
}
public function getResult()
{
}
}
Пример компонента формы
<?php if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();
use Bitrix\Main\Engine\Contract\Controllerable;
use Bitrix\Main\Engine\ActionFilter;
use Bitrix\Main\Localization\Loc;
Loc::loadMessages(__FILE__);
class PartnersFormComponent extends CBitrixComponent implements Controllerable
{
public function onPrepareComponentParams($arParams)
{
return $arParams;
}
public function executeComponent()
{
$this->includeComponentTemplate();
}
/**
* @return array
*/
public function configureActions()
{
return [
'save' => [
'prefilters' => [
new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_POST]),
new ActionFilter\Csrf(),
new \Filters\PartnersFilter()
],
'postfilters' => []
],
];
}
public function saveAction($name, $phone, $email, $format = '', $agreement = '', $url = '')
{
$result = ['status' => 'success', 'message' => Loc::getMessage('EDCRUNCH_PARTNERS_FORM_CLASS_MESSAGE_SUCCESS')];
$fields = [
'IBLOCK_ID' => IBLOCK_ID_PARTNERS_FORM,
'ACTIVE' => 'N',
'ACTIVE_FROM' => date('d.m.Y'),
'NAME' => $name,
'CODE' => CUtil::translit($name, 'ru') . '_' . time(),
'PROPERTY_VALUES' => [
'EMAIL' => $email,
'PHONE' => $phone,
'FORMAT' => $format,
]
];
if ($itemId = $this->saveElement($fields)) {
$this->saveConsent("partners/form", $itemId, $url);
$this->sendEmail('EDCRUNCH_PARTNERS_FORM_MESSAGE', $itemId, $name, $email, $phone, $format);
} else {
$result = ['status' => 'error', 'message' => Loc::getMessage('EDCRUNCH_PARTNERS_FORM_CLASS_MESSAGE_ERROR')];
}
return $result;
}
public function sendEmail($event, $itemId, $name, $email, $phone, $format)
{
CEvent::Send($event, SITE_ID, [
'NAME' => $name,
'EMAIL' => $email,
'PHONE' => $phone,
'FORMAT' => $format,
'ITEM_ID' => $itemId,
]);
}
public function saveConsent($originatorId, $itemId, $url)
{
\Bitrix\Main\UserConsent\Consent::addByContext(AGREEMENT_ID, $originatorId, $itemId, [
'URL' => htmlspecialcharsbx($url)
]);
}
public function saveElement($fields)
{
\Bitrix\Main\Loader::includeModule('iblock');
$element = new CIBlockElement;
return $element->Add($fields);
}
}
script.js
$(document).ready(function () {
$(document).on('submit', '#subscribe_form', function (e) {
e.preventDefault();
let formData = new FormData(document.forms.subscribe_form);
let request = BX.ajax.runComponentAction('edcrunch:subscribe.form', 'save', {
mode:'class',
data: formData
});
request.then(function(successResult){
}, function (errorResult) {
});
})
});