Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailcontact.php
1<?php
2
3namespace Bitrix\Mail\Helper;
9{
10 const COLOR_GREEN = '#9dcf01';
11 const COLOR_BLUE = '#2fc6f6';
12 const COLOR_LIGHT_BLUE = '#56d1e0';
13 const COLOR_ORANGE = '#ffa900';
14 const COLOR_CYAN = '#47e4c2';
15 const COLOR_PINK = '#ff5b55';
16 const COLOR_PURPLE = '#9985dd';
17 const COLOR_GREY = '#a8adb4';
18 const COLOR_BROWN = '#af7e00';
19 const COLOR_RED = '#F44336';
20 const COLOR_DEEP_PURPLE = '#673AB7';
21 const COLOR_INDIGO = '#3F51B5';
22 const COLOR_TEAL = '#009688';
23 const COLOR_LIGHT_GREEN = '#8BC34A';
24 const COLOR_LIME = '#CDDC39';
25 const COLOR_YELLOW = '#FFEB3B';
26 const COLOR_AMBER = '#FFC107';
27 const COLOR_DEEP_ORANGE = '#FF5722';
28 const COLOR_BLUE_GREY = '#607D8B';
29
37 public static function getIconData($email, $name, $lastName = null)
38 {
39 return [
40 'INITIALS' => static::getInitials($email, $name, $lastName),
41 'COLOR' => static::getRandomColor(),
42 ];
43 }
44
49 public static function getRandomColor()
50 {
51 static $colors = null;
52 if (is_null($colors))
53 {
54 $reflect = new \ReflectionClass(static::class);
55 foreach ($reflect->getConstants() as $name => $value)
56 {
57 if (strncmp($name, 'COLOR', 5) === 0)
58 {
59 $colors[] = $value;
60 }
61 }
62 }
63 return $colors[rand(0, count($colors) - 1)];
64 }
65
72 public static function getInitials($email, $name = null, $lastName = null)
73 {
74 if ($lastName && mb_substr($lastName, 0, 1) && $name && mb_substr($name, 0, 1))
75 {
76 return mb_strtoupper(mb_substr($name, 0, 1).mb_substr($lastName, 0, 1));
77 }
78
79 $name = trim(preg_replace('/([0-9]|[-&\/\'#,+()~%.":*?<>{}])/m', '',$name));
80
81 $name = explode(' ', $name);
82
83 if (is_array($name) && isset($name[0]) && $name[0])
84 {
85 if (isset($name[1]) && $name[1])
86 {
87 return mb_strtoupper(mb_substr($name[0], 0, 1).mb_substr($name[1], 0, 1));
88 }
89 else
90 {
91 return mb_strtoupper(mb_substr($name[0], 0, 1));
92 }
93 }
94 return mb_strtoupper(mb_substr($email, 0, 1));
95 }
96
104 public static function getContactsData($mailsField, $userId, $addedFrom)
105 {
106 if (!$mailsField)
107 {
108 return [];
109 }
110 $mails = explode(',', $mailsField);
111 $contacts = [];
112 foreach ($mails as $mail)
113 {
114 $mail = trim($mail);
115 $address = new \Bitrix\Main\Mail\Address($mail);
116 $emailToAdd = $nameToAdd = '';
117 if ($address->validate())
118 {
119 $emailToAdd = $address->getEmail();
120 $nameToAdd = trim($address->getName());
121 }
122 if ($emailToAdd)
123 {
124 $contacts[] = [
125 'USER_ID' => intval($userId),
126 'NAME' => $nameToAdd ? $nameToAdd : explode('@', $emailToAdd)[0],
127 'ICON' => static::getIconData($emailToAdd, $nameToAdd),
128 'EMAIL' => $emailToAdd,
129 'ADDED_FROM' => $addedFrom,
130 ];
131 }
132 }
133 return $contacts;
134 }
135}
static getInitials($email, $name=null, $lastName=null)
static getContactsData($mailsField, $userId, $addedFrom)
static getIconData($email, $name, $lastName=null)