new-support.webeffector.ru/common/helpers/UserHelper.php

173 lines
4.6 KiB
PHP
Raw Permalink Normal View History

2024-07-04 06:42:20 +03:00
<?php
namespace common\helpers;
use Yii;
class UserHelper
{
//userGenders
const GENDER_MALE = 1;
const GENDER_FEMALE = 2;
//userStatuses
const STATUS_BLOCKED = 1;
const STATUS_NEW = 10;
const STATUS_ACTIVE = 100;
// userTypes
const TYPE_CUSTOMER = 1;
const TYPE_EMPLOYEE = 10;
const TYPE_ADMIN = 100;
//RBAC
const ROLE_ADMIN = 'admin';
const ROLE_SUPERVISOR = 'supervisor';
const ROLE_CUSTOMER = 'customer';
const ROLE_MANAGER = 'manager';
public static function statusList(): array
{
return [
self::STATUS_BLOCKED,
self::STATUS_NEW,
self::STATUS_ACTIVE,
];
}
public static function statusNameList($status = null)
{
$statusList = [
self::STATUS_BLOCKED => Yii::t('user', 'Забанен'),
self::STATUS_NEW => Yii::t('user', 'Новый'),
self::STATUS_ACTIVE => Yii::t('user', 'Активен'),
];
if ($status) {
return isset($statusList[$status]) ? $statusList[$status] : null;
}
return $statusList;
}
public static function typeList(): array
{
return [
self::TYPE_ADMIN,
self::TYPE_CUSTOMER,
self::TYPE_EMPLOYEE,
];
}
public static function clientTypeList(): array
{
return [
self::TYPE_CUSTOMER,
self::TYPE_EMPLOYEE,
];
}
public static function typeNameList($type = null): array|string|null
{
$typeNames = [
self::TYPE_ADMIN => Yii::t('user', 'Администратор'),
self::TYPE_CUSTOMER => Yii::t('user', 'Заказчик'),
self::TYPE_EMPLOYEE => Yii::t('user', 'Сотрудник'),
];
if ($type) {
return isset($typeNames[$type]) ? $typeNames[$type] : null;
}
return $typeNames;
}
public static function roleList(): array
{
return [
self::ROLE_ADMIN,
self::ROLE_SUPERVISOR,
self::ROLE_CUSTOMER,
self::ROLE_MANAGER,
];
}
public static function roleNameList($role = null)
{
$roleNames = [
self::ROLE_ADMIN => Yii::t('user', 'Администратор'),
self::ROLE_SUPERVISOR => Yii::t('user', 'Супервайзер'),
self::ROLE_CUSTOMER => Yii::t('user', 'Заказчик'),
self::ROLE_MANAGER => Yii::t('user', 'Контент менеджер'),
];
if ($role) {
return isset($roleNames[$role]) ? $roleNames[$role] : null;
}
return $roleNames;
}
public function locationPhoneCodes($country = null)
{
$phoneCodes = [
'rus' => '+7',
'blr' => '+375',
'kaz' => '+7',
'arm' => '+374',
'kgz' => '+996',
];
if ($country) {
return isset($phoneCodes[$country]) ? $phoneCodes[$country] : null;
}
return $phoneCodes;
}
public static function locationNames($country = null)
{
$countryNames = [
'rus' => Yii::t('user', 'Россия'),
'blr' => Yii::t('user', 'Белоруссия'),
'kaz' => Yii::t('user', 'Казахстан'),
'arm' => Yii::t('user', 'Армения'),
'kgs' => Yii::t('user', 'Кыргызстан'),
];
if ($country) {
return isset($countryNames[$country]) ? $countryNames[$country] : null;
}
return $countryNames;
}
public static function phoneRuFormat($phone)
{
$phone = trim($phone);
$result = preg_replace(
[
'/[\+]?([7|8])[-|\s]?\([-|\s]?(\d{3})[-|\s]?\)[-|\s]?(\d{3})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?(\d{3})[-|\s]?(\d{3})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?\([-|\s]?(\d{4})[-|\s]?\)[-|\s]?(\d{2})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?(\d{4})[-|\s]?(\d{2})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?\([-|\s]?(\d{4})[-|\s]?\)[-|\s]?(\d{3})[-|\s]?(\d{3})/',
'/[\+]?([7|8])[-|\s]?(\d{4})[-|\s]?(\d{3})[-|\s]?(\d{3})/',
],
[
'+7$2$3$4$5',
'+7$2$3$4$5',
'+7$2$3$4$5',
'+7$2$3$4$5',
'+7$2$3$4',
'+7$2$3$4',
],
$phone
);
$result = preg_replace('~\\D+~u', '', $result);
return $result;
}
}