3namespace Bitrix\Sale\Compatible;
5use Bitrix\Main\Application;
8 Bitrix\Main\Entity\Query,
9 Bitrix\Main\Entity\Field,
10 Bitrix\Main\Entity\ReferenceField,
11 Bitrix\Main\Entity\ExpressionField,
12 Bitrix\Main\SystemException;
14use Bitrix\Sale\Compatible;
18 private $aliases =
array();
22 parent::__construct($source);
24 $aliases = &$this->aliases;
27 foreach ($this->
getEntity()->getFields() as $field)
29 if (! $field instanceof ReferenceField)
31 $name = $field->getName();
39 return $this->aliases;
44 foreach ($aliases as $alias => $field)
52 public function addAlias($alias, $field =
null)
54 if (($this->aliases[$alias] ??
false))
56 throw new SystemException(
"'$alias' already added", 0, __FILE__, __LINE__);
60 $this->aliases[$alias] = $alias;
62 elseif (is_string($field) || (is_array($field) && $field[
'expression']))
64 $this->aliases[$alias] = $field;
68 throw new SystemException(
"invalid '$alias' type", 0, __FILE__, __LINE__);
76 if (!isset($this->aliases[$alias]))
81 $field = $this->aliases[$alias];
83 if (is_string($field))
89 $name =
'__'.$alias.
'_ALIAS__';
90 $field[
'registered'] ??=
false;
91 if (!$field[
'registered'])
93 $field[
'registered'] =
true;
100 throw new SystemException(
"invalid alias '$alias' type", 0, __FILE__, __LINE__);
199 $cnt =
$query->exec()->getSelectedRowsCount();
203 if(isset($arNavStartParams[
"SubstitutionFunction"]))
205 $arNavStartParams[
"SubstitutionFunction"]($this,
$query->getLastQuery(), $cnt, $arNavStartParams);
209 if(isset($arNavStartParams[
"bDescPageNumbering"]))
215 $this->NavRecordCount = $cnt;
217 if($this->NavShowAll)
221 $this->NavPageCount = ($this->NavPageSize>0 ? floor($this->NavRecordCount/$this->NavPageSize) : 0);
225 if($this->NavPageSize > 0)
227 if($this->NavPageCount == 0 && $makeweight > 0)
228 $this->NavPageCount = 1;
234 $NavFirstRecordShow = 0;
235 if($this->NavPageNomer != $this->NavPageCount)
236 $NavFirstRecordShow += $makeweight;
239 $NavLastRecordShow = $makeweight + ($this->NavPageCount - $this->NavPageNomer + 1) * $this->NavPageSize;
243 if($this->NavPageSize > 0 && ($this->NavRecordCount % $this->NavPageSize > 0))
244 $this->NavPageCount++;
247 $this->
calculatePageNumber(1,
true, (
bool)($arNavStartParams[
"checkOutOfRange"] ??
false));
248 if ($this->NavPageNomer ===
null)
254 $NavFirstRecordShow = $this->NavPageSize*($this->NavPageNomer-1);
258 $NavAdditionalRecords = 0;
259 if(
is_set($arNavStartParams,
"iNavAddRecords"))
260 $NavAdditionalRecords = $arNavStartParams[
"iNavAddRecords"];
262 if(!$this->NavShowAll)
264 $query->setOffset($NavFirstRecordShow);
265 $query->setLimit($NavLastRecordShow - $NavFirstRecordShow + $NavAdditionalRecords);
268 $res_tmp =
$query->exec();
277 if($this->SqlTraceIndex)
280 $temp_arrray =
array();
281 $temp_arrray_add =
array();
284 while(
$ar = $res_tmp->fetch())
287 if (intval($NavLastRecordShow - $NavFirstRecordShow) > 0 && $tmp_cnt > ($NavLastRecordShow - $NavFirstRecordShow))
288 $temp_arrray_add[] =
$ar;
290 $temp_arrray[] =
$ar;
293 if($this->SqlTraceIndex)
296 $exec_time = round(microtime(
true) -
$start_time, 10);
297 $DB->addDebugTime($this->SqlTraceIndex, $exec_time);
298 $DB->timeQuery += $exec_time;
301 $this->arResult = (!empty($temp_arrray)? $temp_arrray :
false);
302 $this->arResultAdd = (!empty($temp_arrray_add)? $temp_arrray_add :
false);
303 $this->nSelectedCount = $cnt;
305 $this->bFromLimited =
true;
312 private $fetchAdapters =
array();
316 $this->fetchAdapters[] = $adapter;
321 if ($row = parent::Fetch())
322 foreach ($this->fetchAdapters as $adapter)
323 $row = $adapter->adapt($row);
336 private $aggregated =
array();
340 $this->aggregated = $aggregated;
345 foreach ($this->aggregated as $alias =>
$name)
347 $row[
$name] = $row[$alias];
348 unset ($row[$alias]);
357 private $counted, $grouped, $allSelected, $aggregated =
array();
361 return $this->counted;
366 return $this->grouped;
371 return $this->allSelected;
379 private function addAggregatedSelect($alias, $aggregate,
$name =
null)
381 $aggregateAlias =
'__'.$aggregate.
'_'.$alias.
'_ALIAS__';
393 preg_match(
'/^([!+*]{0,1})([<=>@%~?]{0,2})(.*)$/',
$key,
$matches);
404 $keyMatch = static::explodeFilterKey(
$key);
405 $modifier = $keyMatch[
'modifier'];
406 $operator = $keyMatch[
'operator'];
407 $alias = $keyMatch[
'alias' ];
415 case '@': $operator = ($modifier ===
'*' ?
'' :
'=');
418 case '~': $operator =
'';
426 case '' :
return $this->
addFilter($modifier.$operator.$name, $value);
428 case '!':
return $operator ==
'=' && $value
430 : $this->
addFilter($modifier.$operator.$name, $value);
434 default :
throw new SystemException(
"invalid modifier '$modifier'", 0, __FILE__, __LINE__);
455 static $aggregates =
array(
'COUNT'=>1,
'AVG'=>1,
'MIN'=>1,
'MAX'=>1,
'SUM'=>1);
477 if (is_string(
$key) && ($aggregate = mb_strtoupper(
$key)) && $aggregates[$aggregate])
479 $this->addAggregatedSelect($alias, $aggregate,
$name);
492 $this->addAggregatedSelect(
'CNT',
'COUNT(*)');
503 foreach ($this->
getAliases() as $alias => $field)
518 if (is_string(
$key) && ($aggregate = mb_strtoupper(
$key)) && $aggregates[$aggregate])
520 $this->addAggregatedSelect($alias, $aggregate,
$name);
531 foreach (
$order as $alias => $value)
547 if ($v instanceof
Field)
548 $names[$v->getName()] =
true;
573 if (!empty($navStart) && is_array($navStart))
575 if (!empty($navStart[
'nTopCount']))
577 $this->
setLimit($navStart[
'nTopCount']);
581 $result->compatibleNavQuery($this, $navStart);
605 if ($row[
'TYPE'] ===
'LOCATION' && !empty($row[
'VALUE']))
607 if (is_array($row[
'VALUE']))
609 $locationIds = array_merge($locationIds, $row[
'VALUE']);
613 $locationIds[] = $row[
'VALUE'];
618 if (!empty($locationIds))
621 $locationRaw = \Bitrix\Sale\Location\LocationTable::getList([
622 'filter' => [
'=CODE' => $locationIds],
623 'select' => [
'ID',
'CODE']
625 while (
$location = $locationRaw->fetch())
631 foreach (
$rows as &$row)
633 if (isset($row[
'VALUE']))
635 if ($row[
'TYPE'] ===
'LOCATION' && !empty($row[
'VALUE']))
637 if (is_array($row[
'VALUE']))
639 foreach ($row[
'VALUE'] as &$valueItem)
641 $valueItem = $locationMap[$valueItem];
646 $row[
'VALUE'] = $locationMap[$row[
'VALUE']];
650 $row[
'PROXY_VALUE'] = $row[
'VALUE'];
651 if (is_array($row[
'VALUE']))
653 $row[
'PROXY_VALUE'] = serialize($row[
'VALUE']);
655 unset($row[
'VALUE']);
667 public function addLocationRuntimeField($fieldName, $ref =
false)
669 if((
string) $fieldName ==
'')
676 'data_type' =>
'\Bitrix\Sale\Location\LocationTable',
677 'reference' =>
array(
678 '=this.'.$fieldName =>
'ref.CODE'
680 'join_type' =>
'left'
689 $fieldType =
"VARCHAR";
693 $fieldType =
"VARCHAR2";
697 'PROXY_'.$fieldName.
'_LINK',
699 'data_type' =>
'string',
700 'expression' =>
array(
701 "CASE WHEN %s = 'LOCATION' THEN CAST(%s AS ".$fieldType.
"(500)) ELSE %s END",
702 ($ref !==
false ? $ref.
'.' :
'').
'TYPE',
710 $fieldName.
'_ORIG' => $fieldName,
711 'PROXY_'.$fieldName =>
'PROXY_'.$fieldName.
'_LINK'
714 $this->locationFieldMap[$fieldName] =
true;
721 $parsed = static::explodeFilterKey($field);
723 if (isset($this->locationFieldMap[$parsed[
'alias']]))
725 return $parsed[
'modifier'].$parsed[
'operator'].
'PROXY_'.$parsed[
'alias'];
734 if (isset($this->locationFieldMap[$field]))
736 return 'PROXY_'.$field;
750 $lastQuery = Query::getLastQuery();
751 return $query == $lastQuery ?
"ok\n" :
"\n$name - Assert Last Query Failed!\nExpected:\n($query)\nGiven:\n($lastQuery)\n\n";
756 foreach (glob(
$_SERVER[
'DOCUMENT_ROOT'].
'/bitrix/modules/sale/lib/compatible/tests/*.test.php') as
$filename)
addOrder($definition, $order='ASC')
registerRuntimeField($name, $fieldInfo=null)
addSelect($definition, $alias='')
__construct(array $aggregated)
addAliasOrder($alias, $order)
addAliases(array $aliases)
addAliasFilter($key, $value)
addAlias($alias, $field=null)
compatibleNavQuery(Query $query, array $arNavStartParams)
addFetchAdapter(FetchAdapter $adapter)
prepareCompatibleRows(array $rows)
prepare(array $order, array $filter, $group, array $select)
mapLocationRuntimeField($field, $asFilter=false)
static explodeFilterKey($key)
compatibleAddFilter($key, $value)
compatibleExec(CDBResult $result, $navStart)
prepareCompatibleRows(array $rows)
mapLocationRuntimeField($field, $asFilter=false)
static assertLastQuery($name, $query)
InitNavStartVars($nPageSize=0, $bShowAll=true, $iNumPage=false)
calculatePageNumber(int $defaultNumber=1, bool $useSession=true, bool $checkOutOfRange=false)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
$_SERVER["DOCUMENT_ROOT"]
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key