konratnox
4/6/2017 - 2:25 PM

Регистрация(обработчик) на makeupbox

Регистрация(обработчик) на makeupbox

<?php
include($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

use Bitrix\Main\Context,
    Bitrix\Main\Mail\Event;

$request = Context::getCurrent()->getRequest();

$acceptValue = $request->getPost('accept');
if (trim(strlen($acceptValue)) <= 0) {
    echo json_encode(array(
        'result' => 'error',
        'message' => 'Необходимо принять соглашение')
    );
    exit();
}
$registerValue = $request->getPost('REGISTER');

if (!validate($registerValue))
    exit();

$user = new CUser;
//$password = randString(8);
$arFields = array(
    'LOGIN' => trim($registerValue['EMAIL']),
    'EMAIL' => trim($registerValue['EMAIL']),
    'NAME' => trim($registerValue['NAME']),
    'LAST_NAME' => trim($registerValue['LAST_NAME']),
    'PASSWORD' => trim($registerValue['PASSWORD']),
    'CONFIRM_PASSWORD' => trim($registerValue['PASSWORD']),
    "ACTIVE" => "Y",
    "GROUP_ID" => array(3, 4, 5),
    "PERSONAL_CITY" => trim($registerValue['PERSONAL_CITY']),
);
if (trim(strlen($registerValue['PERSONAL_BIRTHDAY']) > 0))
    $arField['PERSONAL_BIRTHDAY'] = trim($registerValue['PERSONAL_BIRTHDAY']);

$ID = $user->Add($arFields);

if (intval($ID) > 0) {
//    Event::send(array(
//        "EVENT_NAME" => "NEW_USER_PASS",
//        "LID" => "s1",
//        "C_FIELDS" => array(
//            "USER_ID" => $ID,
//            "EMAIL" => trim($registerValue['EMAIL']),
//            "NAME" => trim($registerValue['NAME']),
//            "LAST_NAME" => trim($registerValue['LAST_NAME']),
//            "PASSWORD" => $password,
//            "PERSONAL_CITY" => trim($registerValue['PERSONAL_CITY']),
//        ),
//    ));

    global $USER;
    $USER->Authorize($ID);

    echo json_encode(array(
            'result' => 'success',
            'message' => "<h3>Вы успешно зарегестрировались!</h3>")
    );
}
else {
    $errorMessage = $user->LAST_ERROR;
    $errorMessage = preg_replace("/(.*)Пользователь с логином \"(.*?)\" уже существует.<br>(.*)/", "$1 $3", $errorMessage);
    echo json_encode(array(
            'result' => 'error',
            'message' => $errorMessage)
    );
    exit();
}


function validate($data) {

    $errorArray = array();
    foreach ($data as $fieldName => $field) {
        switch ($fieldName) {
            case "EMAIL" :
                if (!filter_var($field, FILTER_VALIDATE_EMAIL)) {
                    $errorArray['key'][] = $fieldName;
                    $errorArray['message'] .= 'Поле E-mail заполнено не верно<br />';
                }
                break;
            case "PERSONAL_CITY" :
                if (trim(strlen($field)) < 3) {
                    $errorArray['key'][] = $fieldName;
                    $errorArray['message'] .= 'Поле город должно содержать минимум 3 символа<br />';
                }
                break;
            case "PASSWORD" :
                if (trim(strlen($field)) < 3) {
                    $errorArray['key'][] = $fieldName;
                    $errorArray['message'] .= 'Поле пароль должно содержать минимум 3 символа<br />';
                }
                break;
            case "PERSONAL_BIRTHDAY" :
                if ( (trim(strlen($field)) > 0) && (!preg_match("/(\d{2})\/(\d{2})\/(\d{4})/", $field)) ) {
                    $errorArray['key'][] = $fieldName;
                    $errorArray['message'] .= 'Дата рождения введена неверно<br />';
                }
                break;
            default :
                if (trim(strlen($field)) <= 0) {
                    $errorArray['key'][] = $fieldName;
                    $errorArray['message'] .= 'Не все обязательные поля заполнены<br />';
                }
                break;
        }
    }

    if (count($errorArray['key']) > 0) {
        echo json_encode(array_merge(array('result' => 'error'), $errorArray));
        return false;
    }
    return true;
}