Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
aggregate.php
1<?php
2
4
8
10{
12 private static $entities = [];
13
26 public static function buildAggregateQuery(array $params): Main\ORM\Query\Query
27 {
28 if (empty($params['GROUP_BY']))
29 {
30 throw new Main\ArgumentException('Parameter GROUP_BY has not defined');
31 }
32
33 $query = self::buildQuery($params);
34
35 $query->addSelect($params['GROUP_BY']);
36 $query->addGroup($params['GROUP_BY']);
37
38 if (empty($params['CURRENT_LANG']))
39 {
40 throw new Main\ArgumentException('Parameter CURRENT_LANG has not defined');
41 }
42 $currentLanguage = $params['CURRENT_LANG'];
43
44 if (empty($params['LANGUAGES']))
45 {
46 $languages = Translate\Config::getEnabledLanguages();
47 }
48 else
49 {
50 $languages = $params['LANGUAGES'];
51 }
52 \usort($languages, function ($langId) use ($currentLanguage) {
53 return $langId === $currentLanguage ? 0 : 1;
54 });
55 $languageUpperKeys = \array_combine($languages, \array_map('mb_strtoupper', $languages));
56
57 foreach ($languageUpperKeys as $langId => $alias)
58 {
59 // phrase count
60 $query->addSelect(new Main\ORM\Fields\ExpressionField("{$alias}_CNT", "SUM({$alias}_CNT)"));
61
62 // file count
63 $query->addSelect(new Main\ORM\Fields\ExpressionField("{$alias}_FILE_CNT", "SUM({$alias}_FILE_CNT)"));
64 // file excess
65 $query->addSelect(new Main\ORM\Fields\ExpressionField("{$alias}_FILE_EXCESS", "SUM({$alias}_FILE_EXCESS)"));
66 // phrase excess
67 $query->addSelect(new Main\ORM\Fields\ExpressionField("{$alias}_EXCESS", "SUM({$alias}_EXCESS)"));
68
69 if ($langId != $currentLanguage)
70 {
71 // file deficiency
72 $query->addSelect(new Main\ORM\Fields\ExpressionField("{$alias}_FILE_DEFICIENCY", "SUM({$alias}_FILE_DEFICIENCY)"));
73 // phrase deficiency
74 $query->addSelect(new Main\ORM\Fields\ExpressionField("{$alias}_DEFICIENCY", "SUM({$alias}_DEFICIENCY)"));
75 }
76 }
77
78 return $query;
79 }
80
94 public static function buildQuery(array $params): Main\ORM\Query\Query
95 {
96 if (empty($params['PARENT_ID']))
97 {
98 throw new Main\ArgumentException('Parameter PARENT_ID has not defined');
99 }
100 $topIndexPathId = (int)$params['PARENT_ID'];
101
102 if (empty($params['CURRENT_LANG']))
103 {
104 throw new Main\ArgumentException('Parameter CURRENT_LANG has not defined');
105 }
106 $currentLanguage = $params['CURRENT_LANG'];
107
108 $className = "Aggregate_{$topIndexPathId}_{$currentLanguage}";
109
110 if (empty($params['LANGUAGES']))
111 {
112 $languages = Translate\Config::getEnabledLanguages();
113 }
114 else
115 {
116 $languages = $params['LANGUAGES'];
117 }
118 \usort($languages, function ($langId) use ($currentLanguage) {
119 return $langId === $currentLanguage ? 0 : 1;
120 });
121
122 $className .= "_". \implode('', $languages);
123
124 if (!empty($params['PATH_LIST']))
125 {
126 $className .= "_". \md5(\implode('', $params['PATH_LIST']));
127 }
128
129 $languageUpperKeys = \array_combine($languages, \array_map('mb_strtoupper', $languages));
130
131 if (!isset(self::$entities[$className]))
132 {
133 $entity = Index\Internals\PathTreeTable::getEntity();
134 $query = new Main\ORM\Query\Query($entity);
135
136 $query->registerRuntimeField(new Main\ORM\Fields\Relations\Reference(
137 'FOLDER_NODE',
138 Translate\Index\Internals\PathIndexTable::class,
139 Main\ORM\Query\Join::on('ref.ID', '=', 'this.PATH_ID')->where('ref.IS_DIR', '=', 'Y'),
140 ['join_type' => 'INNER']
141 ));
142
143
144 $query->registerRuntimeField(new Main\ORM\Fields\Relations\Reference(
145 'FILE_LIST',
146 Translate\Index\Internals\PathTreeTable::class,
147 Main\ORM\Query\Join::on('ref.PARENT_ID', '=', 'this.PATH_ID'),
148 ['join_type' => 'INNER']
149 ));
150 $query->registerRuntimeField(new Main\ORM\Fields\Relations\Reference(
151 'FILE_NODE',
152 Translate\Index\Internals\PathIndexTable::class,
153 Main\ORM\Query\Join::on('ref.ID', '=', 'this.FILE_LIST.PATH_ID')->where('ref.IS_DIR', '=', 'N'),
154 ['join_type' => 'INNER']
155 ));
156
157 //$query->addSelect('PARENT_ID');
158 $query->addSelect('FOLDER_NODE.PATH', 'PARENT_PATH');
159 $query->addSelect('FILE_NODE.OBLIGATORY_LANGS', 'OBLIGATORY_LANGS');
160 $query->addSelect('FILE_NODE.PATH', 'FILE_PATH');
161
162
163 foreach ($languageUpperKeys as $langId => $alias)
164 {
165 $tblAlias = "File{$alias}";
166
167 $query->registerRuntimeField(new Main\ORM\Fields\Relations\Reference(
168 $tblAlias,
169 Translate\Index\Internals\FileIndexTable::class,
170 Main\ORM\Query\Join::on('ref.PATH_ID', '=', 'this.FILE_NODE.ID')->where('ref.LANG_ID', '=', $langId),
171 ['join_type' => 'LEFT']
172 ));
173
174 $query->addSelect(new Main\ORM\Fields\ExpressionField(
175 "{$alias}_OBLI",
176 "@{$alias}_OBLI := case when (INSTR(IFNULL(%s, '{$langId}'), '{$langId}') > 0) then 1 else 0 end",
177 'FILE_NODE.OBLIGATORY_LANGS'
178 ));
179
180
181 if ($langId == $currentLanguage)
182 {
183 // phrase count
184 $query->addSelect(new Main\ORM\Fields\ExpressionField(
185 "{$alias}_CNT",
186 '@ETHALON_CNT := IFNULL(%s, 0)',
187 "{$tblAlias}.PHRASE_COUNT"
188 ));
189 // file count
190 $query->addSelect(new Main\ORM\Fields\ExpressionField(
191 "{$alias}_FILE_CNT",
192 "case when (@ETHALON_CNT > 0) then 1 else 0 end"
193 ));
194 // file excess
195 $query->addSelect(new Main\ORM\Fields\ExpressionField(
196 "{$alias}_FILE_EXCESS",
197 "case when (@ETHALON_CNT > 0) and (@{$alias}_OBLI = 0) then 1 else 0 end"
198 ));
199 // phrase excess
200 $query->addSelect(new Main\ORM\Fields\ExpressionField(
201 "{$alias}_EXCESS",
202 "case when (@ETHALON_CNT > 0) and (@{$alias}_OBLI = 0) then @ETHALON_CNT else 0 end"
203 ));
204 }
205 else
206 {
207 // phrase count
208 $query->addSelect(new Main\ORM\Fields\ExpressionField(
209 "{$alias}_CNT",
210 "@{$alias}_CNT := IFNULL(%s, 0)",
211 ["{$tblAlias}.PHRASE_COUNT"]
212 ));
213 // phrase count diff from ethalon
214 $query->addSelect(new Main\ORM\Fields\ExpressionField(
215 "{$alias}_DIFF",
216 "@{$alias}_DIFF := @{$alias}_CNT - @ETHALON_CNT"
217 ));
218 // file count
219 $query->addSelect(new Main\ORM\Fields\ExpressionField(
220 "{$alias}_FILE_CNT",
221 "case when (@{$alias}_CNT > 0) then 1 else 0 end"
222 ));
223 // file deficiency
224 $query->addSelect(new Main\ORM\Fields\ExpressionField(
225 "{$alias}_FILE_DEFICIENCY",
226 "case when (@{$alias}_CNT = 0) and (@ETHALON_CNT > 0) AND (@{$alias}_OBLI = 1) then 1 else 0 end"
227 ));
228 // file excess
229 $query->addSelect(new Main\ORM\Fields\ExpressionField(
230 "{$alias}_FILE_EXCESS",
231 "case when (@{$alias}_CNT > 0) and (@ETHALON_CNT = 0) then 1 ".
232 "when (@{$alias}_CNT > 0) and (@ETHALON_CNT > 0) AND (@{$alias}_OBLI = 0) then 1 ".
233 "else 0 end"
234 ));
235 // phrase excess
236 $query->addSelect(new Main\ORM\Fields\ExpressionField(
237 "{$alias}_EXCESS",
238 "case when (@{$alias}_CNT > 0) and (@ETHALON_CNT = 0) then @{$alias}_CNT ".
239 "when (@{$alias}_CNT > 0) and (@ETHALON_CNT > 0) AND (@{$alias}_DIFF > 0) and (@{$alias}_OBLI = 1) then @{$alias}_DIFF ".
240 "when (@{$alias}_CNT > 0) and (@ETHALON_CNT > 0) and (@{$alias}_OBLI = 0) then @{$alias}_CNT ".
241 "else 0 end"
242 ));
243 // phrase deficiency
244 $query->addSelect(new Main\ORM\Fields\ExpressionField(
245 "{$alias}_DEFICIENCY",
246 "case when (@{$alias}_CNT = 0) and (@ETHALON_CNT > 0) AND (@{$alias}_OBLI = 1) then @ETHALON_CNT ".
247 "when (@{$alias}_CNT > 0) and (@ETHALON_CNT > 0) AND (@{$alias}_DIFF < 0) and (@{$alias}_OBLI = 1) then - @{$alias}_DIFF ".
248 "else 0 end"
249 ));
250 }
251 }
252 unset($langId, $langUpper, $alias, $tblAlias);
253
254 $query->addFilter('=PARENT_ID', $topIndexPathId);
255 $query->addFilter('=DEPTH_LEVEL', '1');
256
257 if (!empty($params['PATH_LIST']))
258 {
259 $query->addFilter('=FOLDER_NODE.PATH', $params['PATH_LIST']);
260 }
261
262 $fields = [
263 'PARENT_ID' => ['data_type' => 'integer'],
264 'PARENT_PATH' => ['data_type' => 'string'],
265 'FILE_PATH' => ['data_type' => 'string'],
266 'OBLIGATORY_LANGS' => ['data_type' => 'string'],
267 ];
268
269 foreach ($languageUpperKeys as $langId => $alias)
270 {
271 $query->addSelect("{$alias}_CNT");
272 $query->addSelect("{$alias}_FILE_CNT");
273 $query->addSelect("{$alias}_FILE_EXCESS");
274 $query->addSelect("{$alias}_EXCESS");
275
276 $fields["{$alias}_CNT"] = ['data_type' => 'integer'];
277 $fields["{$alias}_FILE_CNT"] = ['data_type' => 'integer'];
278 $fields["{$alias}_FILE_EXCESS"] = ['data_type' => 'integer'];
279 $fields["{$alias}_EXCESS"] = ['data_type' => 'integer'];
280
281 if ($langId != $currentLanguage)
282 {
283 $query->addSelect("{$alias}_DIFF");
284 $query->addSelect("{$alias}_FILE_DEFICIENCY");
285 $query->addSelect("{$alias}_DEFICIENCY");
286
287 $fields["{$alias}_DIFF"] = ['data_type' => 'integer'];
288 $fields["{$alias}_FILE_DEFICIENCY"] = ['data_type' => 'integer'];
289 $fields["{$alias}_DEFICIENCY"] = ['data_type' => 'integer'];
290 }
291 }
292
293 self::$entities[$className] = Main\ORM\Entity::compileEntity(
294 $className,
295 $fields,
296 [
297 'table_name' => '('.$query->getQuery().')',
298 'namespace' => __NAMESPACE__,
299 ]
300 );
301 }
302
303 return new Main\ORM\Query\Query(self::$entities[$className]);
304 }
305}
static buildQuery(array $params)
Definition aggregate.php:94
static buildAggregateQuery(array $params)
Definition aggregate.php:26