Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailcontact.php
1<?php
2
4
8
9Localization\Loc::loadMessages(__FILE__);
10
29{
30 const ADDED_TYPE_FROM = 'FROM';
31 const ADDED_TYPE_CC = 'CC';
32 const ADDED_TYPE_BCC = 'BCC';
33 const ADDED_TYPE_TO = 'TO';
34 const ADDED_TYPE_REPLY_TO = 'REPLY_TO';
35
36 public static function getFilePath()
37 {
38 return __FILE__;
39 }
40
41 public static function getTableName()
42 {
43 return 'b_mail_contact';
44 }
45
46 public static function getMap()
47 {
48 return [
49 'ID' => [
50 'data_type' => 'integer',
51 'primary' => true,
52 'autocomplete' => true,
53 ],
54 'EMAIL' => [
55 'data_type' => 'string',
56 ],
57 'NAME' => [
58 'data_type' => 'string',
59 ],
60 'ICON' => [
61 'data_type' => 'string',
62 'serialized' => true,
63 ],
64 'FILE_ID' => [
65 'data_type' => 'integer',
66 ],
67 'USER_ID' => [
68 'data_type' => 'integer',
69 'required' => true,
70 ],
71 'ADDED_FROM' => [
72 'data_type' => 'string',
73 ],
74 ];
75 }
76
81 public static function addContactsBatch($contactsData)
82 {
83 if (empty($contactsData))
84 {
85 return true;
86 }
87
88 $contactsToCheck = [];
89 foreach ($contactsData as $index => $item)
90 {
91 $item['EMAIL'] = trim($item['EMAIL']);
92 $contactsToCheck[$item['USER_ID']][] = $item;
93 }
94
95 $alreadyAdded = [];
96
97 foreach ($contactsToCheck as $userId => $items)
98 {
99 $alreadyAdded = static::query()
100 ->addSelect('EMAIL', 'EMAIL')
101 ->addSelect('ID', 'ID')
102 ->addSelect('NAME', 'NAME')
103 ->where('USER_ID', $userId)
104 ->whereIn('EMAIL', array_column($items, 'EMAIL'))
105 ->exec()
106 ->fetchAll();
107
108 $alreadyAddedEmail = array_column($alreadyAdded, 'EMAIL');
109
110 foreach ($items as $item)
111 {
112 if (!in_array($item['EMAIL'], $alreadyAddedEmail, true))
113 {
114 $contactsToAdd[$item['EMAIL']] = $item;
115 }
116 }
117 }
118
119 if (empty($contactsToAdd))
120 {
121 return $alreadyAdded;
122 }
123
124 $sqlHelper = Application::getConnection()->getSqlHelper();
125 $values = [];
126 foreach ($contactsToAdd as $item)
127 {
128 $item = [
129 'USER_ID' => intval($item['USER_ID']),
130 'NAME' => "'" . $sqlHelper->forSql(trim($item['NAME'])) . "'",
131 'ICON' => "'" . $sqlHelper->forSql(serialize($item['ICON'])) . "'",
132 'EMAIL' => "'" . $sqlHelper->forSql(trim($item['EMAIL'])) . "'",
133 'ADDED_FROM' => "'" . $sqlHelper->forSql($item['ADDED_FROM']) . "'",
134 ];
135 $values[] = implode(", ", $item);
136 }
137 $keys = implode(', ', array_keys(reset($contactsToAdd)));
138 $values = implode('), (', $values);
139
140 $tableName = static::getTableName();
141 $sql = $sqlHelper->getInsertIgnore($tableName, "($keys)", " VALUES($values)");
142 Application::getConnection()->query($sql);
143
144 return true;
145 }
146}
static addContactsBatch($contactsData)
static getConnection($name="")