32 foreach ($queries as $query)
34 self::prepare($query, $dataTypeId);
37 $query = array_pop($queries);
38 foreach ($queries as $unionQuery)
40 $query->unionAll($unionQuery);
55 self::prepare($query, $dataTypeId);
74 self::prepare($query, $dataTypeId, $entityDbName, $entityName);
75 return self::exec($query, $dataTypeId, $entityDbName, $entityName);
88 protected static function exec(
Entity\
Query $query, $dataTypeId =
null, $entityDbName =
null, $entityName =
null)
92 while ($row = $resultDb->fetch())
95 foreach (self::getTypes() as $typeId => $field)
97 $fieldName = $field[
'COLUMN_ALIAS'] ??
'COUNT_' . $field[
'DATA_COLUMN'];
98 if (!isset($row[$fieldName]))
103 $type = Recipient\Type::getCode($typeId);
104 if (!isset($result[$type]))
108 $result[$type] += (int) $row[$fieldName];
110 if (isset($field[
'IGNORE_TYPES']) && $row[$fieldName] > 0)
112 $ignoredTypes = array_merge($ignoredTypes, $field[
'IGNORE_TYPES']);
115 foreach(array_unique($ignoredTypes) as $ignoreTypeId)
117 $ignoreType = Recipient\Type::getCode($ignoreTypeId);
118 unset($result[$ignoreType]);
132 private static function prepare(
Entity\
Query $query, $dataTypeId =
null, $entityDbName =
null, $entityName =
null)
134 $sqlHelper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
136 foreach (self::getTypes() as $typeId => $field)
138 if ($dataTypeId && $dataTypeId != $typeId)
143 $entityName = $entityName ?? mb_strtoupper($query->getEntity()->getName());
144 $entityDbName = $entityDbName ??
"crm_".mb_strtolower($query->getEntity()->getName());
146 $useEmptyValue =
false;
147 if (mb_strpos($field[
'DATA_COLUMN'],
'.') > 0)
149 $dataColumn = explode(
'.', $field[
'DATA_COLUMN']);
150 $refFieldName = array_shift($dataColumn);
151 if (!array_key_exists($refFieldName, $query->getRuntimeChains()))
153 $useEmptyValue =
true;
156 if (!empty($field[
'ENTITIES']) && !in_array($entityName, $field[
'ENTITIES']))
158 $useEmptyValue =
true;
161 $fieldName = $field[
'COLUMN_ALIAS'] ??
'COUNT_' . $field[
'DATA_COLUMN'];
162 $fields[] = $fieldName;
164 if (isset($field[
'HAS']))
166 $query->registerRuntimeField(
new Entity\ExpressionField(
168 "COUNT(DISTINCT CASE WHEN %s = 'Y' THEN {$sqlHelper->quote($entityDbName)}.ID END)",
172 elseif ($useEmptyValue)
174 $query->registerRuntimeField(
new Entity\ExpressionField(
181 if ($typeId === Recipient\Type::CRM_COMPANY_ID && in_array($entityName, [
'CONTACT']))
183 $field[
'DATA_COLUMN'] =
'CRM_COMPANY_ID';
187 $query->registerRuntimeField(
new Entity\ExpressionField(
189 "COUNT(DISTINCT CASE WHEN %s > 0 THEN {$sqlHelper->quote($entityDbName)}.ID END)",
190 $field[
'DATA_COLUMN']
194 $query->setSelect($fields);
199 private static function getTypes()
202 Recipient\Type::EMAIL => [
'DATA_COLUMN' =>
'EMAIL',
'HAS' =>
'HAS_EMAIL'],
203 Recipient\Type::PHONE => [
'DATA_COLUMN' =>
'PHONE',
'HAS' =>
'HAS_PHONE'],
204 Recipient\Type::IM => [
'DATA_COLUMN' =>
'IMOL',
'HAS' =>
'HAS_IMOL'],
205 Recipient\Type::CRM_CONTACT_ID => [
206 'DATA_COLUMN' =>
'CONTACT_ID',
209 Recipient\Type::CRM_DEAL_PRODUCT_CONTACT_ID => [
210 'DATA_COLUMN' =>
'SGT_DEAL.ID',
211 'COLUMN_ALIAS' =>
'COUNT_CONTACT_DEAL_PRODUCT',
213 'ENTITIES' => [
'CONTACT']
215 Recipient\Type::CRM_ORDER_PRODUCT_CONTACT_ID => [
216 'DATA_COLUMN' =>
'PROD_CRM_ORDER.ID',
217 'COLUMN_ALIAS' =>
'COUNT_CONTACT_ORDER_PRODUCT',
219 'IGNORE_TYPES' => [Recipient\Type::CRM_CONTACT_ID],
220 'ENTITIES' => [
'CONTACT']
222 Recipient\Type::CRM_COMPANY_ID => [
223 'DATA_COLUMN' =>
'COMPANY_ID',
226 Recipient\Type::CRM_DEAL_PRODUCT_COMPANY_ID => [
227 'DATA_COLUMN' =>
'SGT_DEAL.ID',
228 'COLUMN_ALIAS' =>
'COUNT_COMPANY_DEAL_PRODUCT',
230 'ENTITIES' => [
'COMPANY']
232 Recipient\Type::CRM_ORDER_PRODUCT_COMPANY_ID => [
233 'DATA_COLUMN' =>
'PROD_CRM_ORDER.ID',
234 'COLUMN_ALIAS' =>
'COUNT_COMPANY_ORDER_PRODUCT',
236 'IGNORE_TYPES' => [Recipient\Type::CRM_COMPANY_ID],
237 'ENTITIES' => [
'COMPANY']
static loadMessages($file)