Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
querycount.php
1<?php
10
14
15Loc::loadMessages(__FILE__);
16
22{
30 public static function getUnionizedCount(array $queries, $dataTypeId = null)
31 {
32 foreach ($queries as $query)
33 {
34 self::prepare($query, $dataTypeId);
35 }
36
37 $query = array_pop($queries);
38 foreach ($queries as $unionQuery)
39 {
40 $query->unionAll($unionQuery);
41 }
42
43 return self::exec($query, $dataTypeId);
44 }
45
53 public static function getCount(Entity\Query $query, $dataTypeId = null)
54 {
55 self::prepare($query, $dataTypeId);
56 return self::exec($query, $dataTypeId);
57 }
58
59
67 public static function getPreparedCount(
68 Entity\Query $query,
69 string $entityDbName,
70 string $entityName,
71 $dataTypeId = null
72 )
73 {
74 self::prepare($query, $dataTypeId, $entityDbName, $entityName);
75 return self::exec($query, $dataTypeId, $entityDbName, $entityName);
76 }
77
88 protected static function exec(Entity\Query $query, $dataTypeId = null, $entityDbName = null, $entityName = null)
89 {
90 $result = array();
91 $resultDb = Helper::prepareQuery($query, $dataTypeId, $entityDbName, $entityName)->exec();
92 while ($row = $resultDb->fetch())
93 {
94 $ignoredTypes = [];
95 foreach (self::getTypes() as $typeId => $field)
96 {
97 $fieldName = $field['COLUMN_ALIAS'] ?? 'COUNT_' . $field['DATA_COLUMN'];
98 if (!isset($row[$fieldName]))
99 {
100 continue;
101 }
102
103 $type = Recipient\Type::getCode($typeId);
104 if (!isset($result[$type]))
105 {
106 $result[$type] = 0;
107 }
108 $result[$type] += (int) $row[$fieldName];
109
110 if (isset($field['IGNORE_TYPES']) && $row[$fieldName] > 0)
111 {
112 $ignoredTypes = array_merge($ignoredTypes, $field['IGNORE_TYPES']);
113 }
114 }
115 foreach(array_unique($ignoredTypes) as $ignoreTypeId)
116 {
117 $ignoreType = Recipient\Type::getCode($ignoreTypeId);
118 unset($result[$ignoreType]);
119 }
120 }
121
122 return $result;
123 }
124
132 private static function prepare(Entity\Query $query, $dataTypeId = null, $entityDbName = null, $entityName = null)
133 {
134 $sqlHelper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
135 $fields = array();
136 foreach (self::getTypes() as $typeId => $field)
137 {
138 if ($dataTypeId && $dataTypeId != $typeId)
139 {
140 continue;
141 }
142
143 $entityName = $entityName ?? mb_strtoupper($query->getEntity()->getName());
144 $entityDbName = $entityDbName ?? "crm_".mb_strtolower($query->getEntity()->getName());
145
146 $useEmptyValue = false;
147 if (mb_strpos($field['DATA_COLUMN'], '.') > 0)
148 {
149 $dataColumn = explode('.', $field['DATA_COLUMN']);
150 $refFieldName = array_shift($dataColumn);
151 if (!array_key_exists($refFieldName, $query->getRuntimeChains()))
152 {
153 $useEmptyValue = true;
154 }
155 }
156 if (!empty($field['ENTITIES']) && !in_array($entityName, $field['ENTITIES']))
157 {
158 $useEmptyValue = true;
159 }
160
161 $fieldName = $field['COLUMN_ALIAS'] ?? 'COUNT_' . $field['DATA_COLUMN'];
162 $fields[] = $fieldName;
163
164 if (isset($field['HAS']))
165 {
166 $query->registerRuntimeField(new Entity\ExpressionField(
167 $fieldName,
168 "COUNT(DISTINCT CASE WHEN %s = 'Y' THEN {$sqlHelper->quote($entityDbName)}.ID END)",
169 $field['HAS']
170 ));
171 }
172 elseif ($useEmptyValue)
173 {
174 $query->registerRuntimeField(new Entity\ExpressionField(
175 $fieldName,
176 "0"
177 ));
178 }
179 else
180 {
181 if ($typeId === Recipient\Type::CRM_COMPANY_ID && in_array($entityName, ['CONTACT']))
182 {
183 $field['DATA_COLUMN'] = 'CRM_COMPANY_ID';
184 }
185
186
187 $query->registerRuntimeField(new Entity\ExpressionField(
188 $fieldName,
189 "COUNT(DISTINCT CASE WHEN %s > 0 THEN {$sqlHelper->quote($entityDbName)}.ID END)",
190 $field['DATA_COLUMN']
191 ));
192 }
193 }
194 $query->setSelect($fields);
195
196 return $query;
197 }
198
199 private static function getTypes()
200 {
201 return array(
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',
207 'HAS' => null
208 ],
209 Recipient\Type::CRM_DEAL_PRODUCT_CONTACT_ID => [
210 'DATA_COLUMN' => 'SGT_DEAL.ID',
211 'COLUMN_ALIAS' => 'COUNT_CONTACT_DEAL_PRODUCT',
212 'HAS' => null,
213 'ENTITIES' => ['CONTACT']
214 ],
215 Recipient\Type::CRM_ORDER_PRODUCT_CONTACT_ID => [
216 'DATA_COLUMN' => 'PROD_CRM_ORDER.ID',
217 'COLUMN_ALIAS' => 'COUNT_CONTACT_ORDER_PRODUCT',
218 'HAS' => null,
219 'IGNORE_TYPES' => [Recipient\Type::CRM_CONTACT_ID],
220 'ENTITIES' => ['CONTACT']
221 ],
222 Recipient\Type::CRM_COMPANY_ID => [
223 'DATA_COLUMN' => 'COMPANY_ID',
224 'HAS' => null
225 ],
226 Recipient\Type::CRM_DEAL_PRODUCT_COMPANY_ID => [
227 'DATA_COLUMN' => 'SGT_DEAL.ID',
228 'COLUMN_ALIAS' => 'COUNT_COMPANY_DEAL_PRODUCT',
229 'HAS' => null,
230 'ENTITIES' => ['COMPANY']
231 ],
232 Recipient\Type::CRM_ORDER_PRODUCT_COMPANY_ID => [
233 'DATA_COLUMN' => 'PROD_CRM_ORDER.ID',
234 'COLUMN_ALIAS' => 'COUNT_COMPANY_ORDER_PRODUCT',
235 'HAS' => null,
236 'IGNORE_TYPES' => [Recipient\Type::CRM_COMPANY_ID],
237 'ENTITIES' => ['COMPANY']
238 ],
239 );
240 }
241}
static loadMessages($file)
Definition loc.php:64
static prepareQuery(Entity\Query $query, $dataTypeId=null, $entityDbName=null, $entityName=null)
Definition helper.php:268
static getUnionizedCount(array $queries, $dataTypeId=null)
static exec(Entity\Query $query, $dataTypeId=null, $entityDbName=null, $entityName=null)
static getCount(Entity\Query $query, $dataTypeId=null)
static getPreparedCount(Entity\Query $query, string $entityDbName, string $entityName, $dataTypeId=null)