public function getSmartCatalog($template = 'default', $categoryId, $limit, $ignorePaging = false, $level = 1, $fieldName = false, $isAsc = true) {
/**
* @var CatalogMacros|catalog $module
*/
$module = $this->module;
if (!is_string($template)) {
$template = 'default';
}
list(
$itemsTemplate,
$emptyItemsTemplate,
$emptySearchTemplates,
$itemTemplate
) = def_module::loadTemplates(
'catalog/' . $template,
'objects_block',
'objects_block_empty',
'objects_block_search_empty',
'objects_block_line'
);
$umiHierarchy = umiHierarchy::getInstance();
/* @var iUmiHierarchyElement $category */
$category = $umiHierarchy->getElement($categoryId);
if (!$category instanceof iUmiHierarchyElement) {
throw new publicException(__METHOD__ . ': cant get page by id = '. $categoryId);
}
$limit = $limit ? $limit : $this->module->per_page;
$currentPage = $ignorePaging ? 0 : (int) getRequest('p');
$offset = $currentPage * $limit;
if (!is_numeric($level)) $level = 1;
$filteredProductsIds = null;
$queriesMaker = null;
if (is_array(getRequest('filter'))) {
$emptyItemsTemplate = $emptySearchTemplates;
$queriesMaker = $module->getCatalogQueriesMaker($category, $level);
if (!$queriesMaker instanceof FilterQueriesMaker) {
return $module->makeEmptyCatalogResponse($emptyItemsTemplate, $categoryId);
}
$filteredProductsIds = $queriesMaker->getFilteredEntitiesIds();
if (count($filteredProductsIds) == 0) {
return $module->makeEmptyCatalogResponse($emptyItemsTemplate, $categoryId);
}
}
$products = new selector('pages');
$products->types('hierarchy-type')->name('catalog', 'object');
if (is_null($filteredProductsIds)) {
$products->where('hierarchy')->page($categoryId)->childs($level);
} else {
$products->where('id')->equals($filteredProductsIds);
}
// Сортировка
$orderBy = urldecode($_REQUEST['order_by']);
$sortFields = sortFields();
list($defSortField, $defSortDirect) = explode(':', key($sortFields));
if (isset($sortFields[$orderBy])) {
$_ = explode(':', $orderBy, 2);
if (count($_) < 2) $_[1] = 'asc';
$fieldName = $_[0];
}
if ($fieldName) {
$isAlias = false;
if ($fieldName == 'price') {
$isAlias = true;
$priceFieldId = $products->searchField('price', 1);
$discountFieldId = $products->searchField('discount_price', 1);
$products->addColumns([
"oc_{$priceFieldId}_lj.float_val base_price",
"oc_{$discountFieldId}_lj.float_val discount_price",
"IF(oc_{$discountFieldId}_lj.float_val, oc_{$priceFieldId}_lj.float_val-oc_{$priceFieldId}_lj.float_val*oc_{$discountFieldId}_lj.float_val/100, oc_{$priceFieldId}_lj.float_val) price"
]);
$products->addFields(['price', 'discount_price']);
// Приоритет в порядке добавления
$products->firstOrderBy = ['IF(price, 1, -100) '.($isAsc ? 'DESC' : 'ASC'), 'price '.($isAsc ? 'ASC' : 'DESC')];
$products->requireTables = ['o' => 'cms3_objects'];
} else {
if ($isAsc) {
$products->order($fieldName)->asc();
} else {
$products->order($fieldName)->desc();
}
}
} else $products->order($defSortField)->{$defSortDirect}(); // Сортировка по умолчанию
if ($queriesMaker instanceof FilterQueriesMaker) {
if (!$queriesMaker->isPermissionsIgnored()) {
$products->option('no-permissions')->value(true);
}
}
$products->option('load-all-props')->value(true);
if ((string) getRequest('p') != 'all') $products->limit($offset, $limit);
$pages = $products->result();
$total = $products->length();
if (($currentPage > 0) && (count($pages) == 0)) {
$module->redirect(preg_replace('/page\d+\.html\/?/', '', $_SERVER['REQUEST_URI']));
}
if ($total == 0) {
return $module->makeEmptyCatalogResponse($emptyItemsTemplate, $categoryId);
}
$result = array();
$items = array();
$umiLinksHelper = umiLinksHelper::getInstance();
/* @var iUmiHierarchyElement|umiEntinty $page */
foreach ($pages as $page) {
$item = array();
$pageId = $page->getId();
$item['attribute:id'] = $pageId;
$item['attribute:alt_name'] = $page->getAltName();
$item['attribute:price'] = $page->getValue('price');
$item['attribute:link'] = $umiLinksHelper->getLinkByParts($page);
$item['xlink:href'] ='upage://' . $pageId;
$item['node:text'] = $page->getName();
$items[] = catalog::parseTemplate($itemTemplate, $item, $pageId);
catalog::pushEditable('catalog', 'object', $pageId);
$umiHierarchy->unloadElement($pageId);
}
$result['subnodes:lines'] = $items;
$result['numpages'] = umiPagenum::generateNumPage($total, $limit);
$result['total'] = $total;
$result['per_page'] = $limit;
$result['category_id'] = $categoryId;
return catalog::parseTemplate($itemsTemplate, $result, $categoryId);
}
}