Пример #1
0
 public function OnBeforeUserUpdate(&$arFields)
 {
     if ($arFields["ID"] > 0) {
         $arPhones = array();
         $arCorrectPhones = array();
         $dbViPhone = VI\PhoneTable::getList(array('select' => array('ID', 'PHONE_MNEMONIC', 'PHONE_NUMBER'), 'filter' => array('=USER_ID' => intval($arFields['ID']))));
         while ($phone = $dbViPhone->fetch()) {
             if (strlen($phone['PHONE_MNEMONIC']) > 0) {
                 $arPhones[$phone['PHONE_MNEMONIC']] = $phone;
             }
         }
         global $APPLICATION;
         $error = false;
         if (is_set($arFields, "WORK_PHONE")) {
             if (strlen($arFields["WORK_PHONE"]) > 0) {
                 $arCorrectPhones["WORK_PHONE"] = CVoxImplantPhone::Normalize($arFields["WORK_PHONE"]);
                 if (!$arCorrectPhones["WORK_PHONE"]) {
                     $APPLICATION->throwException(GetMessage('ERROR_WORK_PHONE') . ' ' . GetMessage('ERROR_NUMBER'));
                     $error = true;
                 }
             } else {
                 $arCorrectPhones["WORK_PHONE"] = '';
             }
         }
         if (is_set($arFields, "PERSONAL_PHONE")) {
             if (strlen($arFields["PERSONAL_PHONE"]) > 0) {
                 $arCorrectPhones["PERSONAL_PHONE"] = CVoxImplantPhone::Normalize($arFields["PERSONAL_PHONE"]);
                 if (!$arCorrectPhones["PERSONAL_PHONE"]) {
                     $APPLICATION->throwException(GetMessage('ERROR_PERSONAL_PHONE') . ' ' . GetMessage('ERROR_NUMBER'));
                     $error = true;
                 }
             } else {
                 $arCorrectPhones["PERSONAL_PHONE"] = '';
             }
         }
         if (is_set($arFields, "PERSONAL_MOBILE")) {
             if (strlen($arFields["PERSONAL_MOBILE"]) > 0) {
                 $arCorrectPhones["PERSONAL_MOBILE"] = CVoxImplantPhone::Normalize($arFields["PERSONAL_MOBILE"]);
                 if (!$arCorrectPhones["PERSONAL_MOBILE"]) {
                     $APPLICATION->throwException(GetMessage('ERROR_PERSONAL_MOBILE') . ' ' . GetMessage('ERROR_NUMBER'));
                     $error = true;
                 }
             } else {
                 $arCorrectPhones["PERSONAL_MOBILE"] = '';
             }
         }
         if (is_set($arFields, "UF_PHONE_INNER")) {
             if (strlen($arFields["UF_PHONE_INNER"]) > 0) {
                 $phoneInner = intval(preg_replace("/[^0-9]/i", "", $arFields["UF_PHONE_INNER"]));
                 if ($phoneInner > 0 && $phoneInner < 10000) {
                     $result = \Bitrix\Main\UserTable::getList(array('select' => array('COUNT'), 'filter' => array('!=ID' => intval($arFields['ID']), '=UF_PHONE_INNER' => $phoneInner, '=ACTIVE' => 'Y'), 'runtime' => array('COUNT' => array('data_type' => 'integer', 'expression' => array('COUNT(1)')))));
                     $data = $result->fetch();
                     if ($data['COUNT'] > 0) {
                         $APPLICATION->throwException(GetMessage('ERROR_PHONE_INNER'));
                         $error = true;
                     } else {
                         $arFields["UF_PHONE_INNER"] = $phoneInner;
                         $arCorrectPhones["UF_PHONE_INNER"] = $phoneInner;
                     }
                 } else {
                     $APPLICATION->throwException(GetMessage('ERROR_PHONE_INNER_2'));
                     $error = true;
                 }
             } else {
                 $arCorrectPhones["UF_PHONE_INNER"] = '';
             }
         }
         if ($arFields["ACTIVE"] == 'N' && CVoximplantUser::GetPhoneActive($arFields['ID'])) {
             $viUser = new CVoximplantUser();
             $viUser->UpdateUserPassword($arFields['ID'], CVoxImplantUser::MODE_PHONE);
             $viUser->SetPhoneActive($arFields['ID'], false);
         }
         if (!$error) {
             foreach ($arCorrectPhones as $mnemonic => $phone) {
                 if (isset($arPhones[$mnemonic])) {
                     if ($phone != $arPhones[$mnemonic]['PHONE_NUMBER']) {
                         if (strlen($phone) == 0) {
                             VI\PhoneTable::delete($arPhones[$mnemonic]['ID']);
                         } else {
                             VI\PhoneTable::update($arPhones[$mnemonic]['ID'], array('PHONE_NUMBER' => $phone));
                         }
                     }
                 } else {
                     if (strlen($phone) > 0) {
                         VI\PhoneTable::add(array('USER_ID' => intval($arFields['ID']), 'PHONE_NUMBER' => $phone, 'PHONE_MNEMONIC' => $mnemonic));
                     }
                 }
             }
         } else {
             return false;
         }
     }
 }