83 if (empty($contactsData))
88 $contactsToCheck = [];
89 foreach ($contactsData as $index => $item)
91 $item[
'EMAIL'] = trim($item[
'EMAIL']);
92 $contactsToCheck[$item[
'USER_ID']][] = $item;
97 foreach ($contactsToCheck as $userId => $items)
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'))
108 $alreadyAddedEmail = array_column($alreadyAdded,
'EMAIL');
110 foreach ($items as $item)
112 if (!in_array($item[
'EMAIL'], $alreadyAddedEmail,
true))
114 $contactsToAdd[$item[
'EMAIL']] = $item;
119 if (empty($contactsToAdd))
121 return $alreadyAdded;
126 foreach ($contactsToAdd as $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']) .
"'",
135 $values[] = implode(
", ", $item);
137 $keys = implode(
', ', array_keys(reset($contactsToAdd)));
138 $values = implode(
'), (', $values);
140 $tableName = static::getTableName();
141 $sql = $sqlHelper->getInsertIgnore($tableName,
"($keys)",
" VALUES($values)");