diff --git a/api/index.php b/api/index.php index 8054042..f2fd852 100644 --- a/api/index.php +++ b/api/index.php @@ -167,8 +167,15 @@ switch($PARAM_1) { if($auth['username'] !== 'crm') { header('HTTP/1.0 401 Unauthorized'); print json_encode(["status" => "error", "error" => "unauthorized", "message" => "Unauthorized"]); die(); } - $existedUserRes = \CUser::GetByLogin($REQ["email"]); - if($existedUser = $existedUserRes->Fetch()) + $user_registered = false; + + $existed_users_res = \CUser::GetList(["ID" => "ASC"], false, [ "XML_ID" => $REQ['crm_id'] ], []); + while($existed_user = $existed_users_res->Fetch()) + { + $user_registered = true; + } + + if($user_registered) { print json_encode([ "status" => "error", @@ -235,12 +242,17 @@ switch($PARAM_1) { if($auth['username'] !== 'crm') { header('HTTP/1.0 401 Unauthorized'); print json_encode(["status" => "error", "error" => "unauthorized", "message" => "Unauthorized"]); die(); } - $existedUserRes = \CUser::GetByLogin($REQ["email"]); - if($existedUser = $existedUserRes->Fetch()) + $user_registered = false; + + $existed_users_res = \CUser::GetList(["ID" => "ASC"], false, [ "XML_ID" => $REQ['crm_id'] ], []); + while($existed_user = $existed_users_res->Fetch()) { + $user_registered = true; + $user = new \CUser; $profile = [ "XML_ID" => $REQ['crm_id'], + "LOGIN" => $REQ['email'], "NAME" => $REQ['firstname'], "SECOND_NAME" => $REQ['secondname'], "LAST_NAME" => $REQ['lastname'], @@ -254,7 +266,7 @@ switch($PARAM_1) "UF_PHONE_NUMBER" => (int)$REQ['phone'], ]; - $user->Update($existedUser['ID'], $profile); + $user->Update($existed_user['ID'], $profile); if($user->LAST_ERROR) { @@ -266,12 +278,24 @@ switch($PARAM_1) } else { + \Bitrix\Main\Mail\Event::send([ + "EVENT_NAME" => "USER_INFO", + "LID" => "s1", + "C_FIELDS" => Array( + "EMAIL" => $REQ['email'], + "ORG_NAME" => (string)$REQ['org_title'], + "LOGIN" => $REQ['email'], + "PASS" => $REQ['password'], + ) + ]); + print json_encode([ "status" => "success" ]); } } - else + + if(!$user_registered) { print json_encode([ "status" => "error", diff --git a/local/components/evolution/form.leasing/templates/.default/script.js b/local/components/evolution/form.leasing/templates/.default/script.js index 6c763f7..d1964c2 100644 --- a/local/components/evolution/form.leasing/templates/.default/script.js +++ b/local/components/evolution/form.leasing/templates/.default/script.js @@ -1,60 +1,99 @@ var leasing_form_submitting = false; +function check_errors() +{ + var valid = true; + + if(!$("#FORM_FIELD_FIO").val()) + { + valid = false; + $("#FORM_FIELD_FIO_wrapper").addClass("error").attr("data-error", "Необходимо указать Ваши ФИО"); + } + if(!$("#FORM_FIELD_PHONE").val()) + { + valid = false; + $("#FORM_FIELD_PHONE_wrapper").addClass("error").attr("data-error", "Необходимо указать номер Вашего телефона"); + } + if(!$("#FORM_FIELD_EMAIL").val()) + { + valid = false; + $("#FORM_FIELD_EMAIL_wrapper").addClass("error").attr("data-error", "Необходимо указать Ваш адрес E-mail"); + } + else + { + if(!is_email($("#FORM_FIELD_EMAIL").val())) + { + valid = false; + $("#FORM_FIELD_EMAIL_wrapper").addClass("error").attr("data-error", "Пожалуйста, укажите правильный адрес E-mail"); + } + } + + return valid; +} + function form_leasing_request_submit() { var accept = $("#policy").is(':checked'); if(!leasing_form_submitting && accept) { - leasing_form_submitting = true; - $("#send_button_text").css("display", "none"); - $("#send_button_spinner").css("display", "block"); - - $("#form_leasing_request").find("input").removeClass("error"); - - var formData = new FormData($("#form_leasing_request")[0]); - formData.append("form", "FORM_LEASING_REQUESTS"); - formData.append("FORM_FIELD_PAGE_NAME", document.title); - formData.append("FORM_FIELD_PAGE_URL", window.location.href); - formData.append("FORM_FIELD_BRAND", $("#FORM_FIELD_BRAND").val() ? $("#FORM_FIELD_BRAND").val() : ""); - formData.append("FORM_FIELD_MODEL", $("#FORM_FIELD_MODEL").val() ? $("#FORM_FIELD_MODEL").val() : ""); - formData.append("FORM_FIELD_MODIFICATION", $("#FORM_FIELD_MODIFICATION").val() ? $("#FORM_FIELD_MODIFICATION").val() : ""); - formData.append("FORM_FIELD_PRICE", $("#FORM_FIELD_PRICE").val() ? $("#FORM_FIELD_PRICE").val() : ""); - formData.append("FORM_FIELD_PREPAID", $("#FORM_FIELD_PREPAID").val() ? $("#FORM_FIELD_PREPAID").val() : ""); - formData.append("FORM_FIELD_TERM", $("#FORM_FIELD_TERM").val() ? $("#FORM_FIELD_TERM").val() : ""); - formData.append("FORM_FIELD_REDEMPTION", $("#FORM_FIELD_REDEMPTION").val() ? $("#FORM_FIELD_REDEMPTION").val() : ""); - - $.ajax( + if(check_errors()) { - url: "/api/forms/", type: 'POST', success: function(response) + leasing_form_submitting = true; + $("#send_button_text").css("display", "none"); + $("#send_button_spinner").css("display", "block"); + + $("#form_leasing_request").find("input").removeClass("error"); + + var formData = new FormData($("#form_leasing_request")[0]); + formData.append("form", "FORM_LEASING_REQUESTS"); + formData.append("FORM_FIELD_PAGE_NAME", document.title); + formData.append("FORM_FIELD_PAGE_URL", window.location.href); + formData.append("FORM_FIELD_BRAND", $("#FORM_FIELD_BRAND").val() ? $("#FORM_FIELD_BRAND").val() : ""); + formData.append("FORM_FIELD_MODEL", $("#FORM_FIELD_MODEL").val() ? $("#FORM_FIELD_MODEL").val() : ""); + formData.append("FORM_FIELD_MODIFICATION", $("#FORM_FIELD_MODIFICATION").val() ? $("#FORM_FIELD_MODIFICATION").val() : ""); + formData.append("FORM_FIELD_PRICE", $("#FORM_FIELD_PRICE").val() ? $("#FORM_FIELD_PRICE").val() : ""); + formData.append("FORM_FIELD_PREPAID", $("#FORM_FIELD_PREPAID").val() ? $("#FORM_FIELD_PREPAID").val() : ""); + formData.append("FORM_FIELD_TERM", $("#FORM_FIELD_TERM").val() ? $("#FORM_FIELD_TERM").val() : ""); + formData.append("FORM_FIELD_REDEMPTION", $("#FORM_FIELD_REDEMPTION").val() ? $("#FORM_FIELD_REDEMPTION").val() : ""); + + $.ajax( { - if(response.status == "complete") + url: "/api/forms/", type: 'POST', success: function(response) { - $("#form_leasing_process").css("display","none"); - $("#form_leasing_success").css("display","flex"); - - $("#send_button_text").css("display", "block"); - $("#send_button_spinner").css("display", "none"); - - leasing_form_submitting = false; - } - else if(response.status == "error") - { - for(var i in response.errors) + if(response.status == "complete") { - $("#form_leasing_request").find("*[name="+response.errors[i]+"]").addClass("error"); + $("#form_leasing_process").css("display","none"); + $("#form_leasing_success").css("display","flex"); + $("#send_button_text").css("display", "block"); + $("#send_button_spinner").css("display", "none"); + leasing_form_submitting = false; } - } - }, data: formData, - dataType: "json", - cache: false, contentType: false, processData: false - }); + else if(response.status == "error") + { + for(var i in response.errors) + { + $("#form_leasing_request").find("*[name="+response.errors[i]+"]").addClass("error"); + + leasing_form_submitting = false; + } + } + }, data: formData, + dataType: "json", + cache: false, contentType: false, processData: false + }); + } } } $(function() { + $("#form_leasing_request").find("input").on("keypress", function() + { + $(this).parent().removeClass("error").attr("data-error", null); + }); + $("#form_leasing_send_button").on("click", function() { form_leasing_request_submit(); diff --git a/local/components/evolution/form.leasing/templates/.default/template.php b/local/components/evolution/form.leasing/templates/.default/template.php index 216b5db..518c0bd 100644 --- a/local/components/evolution/form.leasing/templates/.default/template.php +++ b/local/components/evolution/form.leasing/templates/.default/template.php @@ -56,21 +56,21 @@ $this->setFrameMode(true);
-
- +
+
-
- +
+
-
- +
+
-
- +
+
- - + +