public static function SynchronizeUserPhones() { $offset = intval(COption::GetOptionInt("voximplant", "sync_offset", 0)); $result = Bitrix\Main\UserTable::getList(array('select' => array('ID', 'WORK_PHONE', 'PERSONAL_PHONE', 'PERSONAL_MOBILE', 'UF_PHONE_INNER'), 'filter' => array('=Bitrix\\Voximplant\\Phone:USER.USER_ID' => 0), 'limit' => 100, 'offset' => $offset, 'order' => 'ID')); $count = 0; while ($user = $result->fetch()) { $user["WORK_PHONE"] = CVoxImplantPhone::Normalize($user["WORK_PHONE"]); if ($user["WORK_PHONE"]) { VI\PhoneTable::add(array('USER_ID' => intval($user['ID']), 'PHONE_NUMBER' => $user["WORK_PHONE"], 'PHONE_MNEMONIC' => "WORK_PHONE")); } $user["PERSONAL_PHONE"] = CVoxImplantPhone::Normalize($user["PERSONAL_PHONE"]); if ($user["PERSONAL_PHONE"]) { VI\PhoneTable::add(array('USER_ID' => intval($user['ID']), 'PHONE_NUMBER' => $user["PERSONAL_PHONE"], 'PHONE_MNEMONIC' => "PERSONAL_PHONE")); } $user["PERSONAL_MOBILE"] = CVoxImplantPhone::Normalize($user["PERSONAL_MOBILE"]); if ($user["PERSONAL_MOBILE"]) { VI\PhoneTable::add(array('USER_ID' => intval($user['ID']), 'PHONE_NUMBER' => $user["PERSONAL_MOBILE"], 'PHONE_MNEMONIC' => "PERSONAL_MOBILE")); } $user["UF_PHONE_INNER"] = intval(preg_replace("/[^0-9]/i", "", $user["UF_PHONE_INNER"])); if ($user["UF_PHONE_INNER"] > 0 && $user["UF_PHONE_INNER"] < 10000) { VI\PhoneTable::add(array('USER_ID' => intval($user['ID']), 'PHONE_NUMBER' => $user["UF_PHONE_INNER"], 'PHONE_MNEMONIC' => "UF_PHONE_INNER")); } $count++; } if ($count > 0) { $offset = $offset + 100; COption::SetOptionInt("voximplant", "sync_offset", $offset); return "CVoxImplantPhone::SynchronizeUserPhones();"; } else { return false; } }
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; } } }