Fix tree view selection and add entity-specific sort options

- Fix validation mixin not being merged in _core.js which broke tree item selection
- Add data-entities attribute to sort options for entity-specific filtering
- Improve event handlers and filter panel updates

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-31 18:10:18 +00:00
parent af5066dd26
commit 743e9c8020
9 changed files with 316 additions and 31 deletions

View File

@@ -1147,6 +1147,13 @@ trait EntitySelector
$limit = (int) Tools::getValue('limit', 20);
$offset = (int) Tools::getValue('offset', 0);
// Sorting parameters
$sortBy = Tools::getValue('sort_by', 'name');
$sortDir = strtoupper(Tools::getValue('sort_dir', 'ASC'));
if (!in_array($sortDir, ['ASC', 'DESC'])) {
$sortDir = 'ASC';
}
$idLang = (int) $this->context->language->id;
$idShop = (int) $this->context->shop->id;
@@ -1217,6 +1224,9 @@ trait EntitySelector
'is_color' => $filterIsColor,
'is_custom' => $filterIsCustom,
'indexable' => $filterIndexable,
// Sorting
'sort_by' => $sortBy,
'sort_dir' => $sortDir,
];
// Delegate to EntitySearchEngine

View File

@@ -126,6 +126,29 @@ class EntitySearchEngine
return str_replace(['%', '_'], ['\\%', '\\_'], pSQL($pattern));
}
/**
* Build ORDER BY clause based on entity type and sort field
*
* @param string $entityType Entity type (products, categories, etc.)
* @param array $filters Filters containing sort_by and sort_dir
* @param array $columnMap Mapping of sort field names to SQL columns
* @return string ORDER BY clause
*/
protected function buildOrderBy($entityType, array $filters, array $columnMap)
{
$sortBy = $filters['sort_by'] ?? 'name';
$sortDir = strtoupper($filters['sort_dir'] ?? 'ASC');
if (!in_array($sortDir, ['ASC', 'DESC'])) {
$sortDir = 'ASC';
}
// Get the column for sorting
$column = $columnMap[$sortBy] ?? $columnMap['name'] ?? 'name';
return $column . ' ' . $sortDir;
}
// =========================================================================
// PRODUCTS
// =========================================================================
@@ -187,7 +210,17 @@ class EntitySearchEngine
}
}
$sql->orderBy('pl.name ASC');
// Sorting
// Note: Products don't have a global position - only per-category positions in ps_category_product
$productSortMap = [
'name' => 'pl.name',
'id' => 'p.id_product',
'price' => 'ps.price',
'reference' => 'p.reference',
'popularity' => 'sales_qty',
'stock' => 'stock_qty',
];
$sql->orderBy($this->buildOrderBy('products', $filters, $productSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -546,7 +579,14 @@ class EntitySearchEngine
$sql->where('c.active = 1');
}
$sql->orderBy('cl.name ASC');
// Sorting
$categorySortMap = [
'name' => 'cl.name',
'id' => 'c.id_category',
'position' => 'c.position',
'product_count' => 'product_count',
];
$sql->orderBy($this->buildOrderBy('categories', $filters, $categorySortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -876,7 +916,13 @@ class EntitySearchEngine
$sql->where('m.active = 1');
}
$sql->orderBy('m.name ASC');
// Sorting
$manufacturerSortMap = [
'name' => 'm.name',
'id' => 'm.id_manufacturer',
'product_count' => 'product_count',
];
$sql->orderBy($this->buildOrderBy('manufacturers', $filters, $manufacturerSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -1002,7 +1048,13 @@ class EntitySearchEngine
$sql->where('s.active = 1');
}
$sql->orderBy('s.name ASC');
// Sorting
$supplierSortMap = [
'name' => 's.name',
'id' => 's.id_supplier',
'product_count' => 'product_count',
];
$sql->orderBy($this->buildOrderBy('suppliers', $filters, $supplierSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -1128,7 +1180,13 @@ class EntitySearchEngine
$sql->where('c.active = 1');
}
$sql->orderBy('cl.meta_title ASC');
// Sorting
$cmsSortMap = [
'name' => 'cl.meta_title',
'id' => 'c.id_cms',
'position' => 'c.position',
];
$sql->orderBy($this->buildOrderBy('cms', $filters, $cmsSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -1258,7 +1316,13 @@ class EntitySearchEngine
$sql->where('cc.active = 1');
}
$sql->orderBy('ccl.name ASC');
// Sorting
$cmsCategorySortMap = [
'name' => 'ccl.name',
'id' => 'cc.id_cms_category',
'position' => 'cc.position',
];
$sql->orderBy($this->buildOrderBy('cms_categories', $filters, $cmsCategorySortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -1388,7 +1452,12 @@ class EntitySearchEngine
$sql->where('e.active = 1');
}
$sql->orderBy('e.lastname ASC, e.firstname ASC');
// Sorting
$employeeSortMap = [
'name' => 'e.lastname',
'id' => 'e.id_employee',
];
$sql->orderBy($this->buildOrderBy('employees', $filters, $employeeSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -1515,7 +1584,12 @@ class EntitySearchEngine
$sql->where('c.active = 1');
}
$sql->orderBy('c.lastname ASC, c.firstname ASC');
// Sorting
$customerSortMap = [
'name' => 'c.lastname',
'id' => 'c.id_customer',
];
$sql->orderBy($this->buildOrderBy('customers', $filters, $customerSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -1644,7 +1718,12 @@ class EntitySearchEngine
}
}
$sql->orderBy('gl.name ASC');
// Sorting
$customerGroupSortMap = [
'name' => 'gl.name',
'id' => 'g.id_group',
];
$sql->orderBy($this->buildOrderBy('customer_groups', $filters, $customerGroupSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -1768,7 +1847,13 @@ class EntitySearchEngine
$sql->where('c.active = 1');
}
$sql->orderBy('c.name ASC');
// Sorting
$carrierSortMap = [
'name' => 'c.name',
'id' => 'c.id_carrier',
'position' => 'c.position',
];
$sql->orderBy($this->buildOrderBy('carriers', $filters, $carrierSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -1895,7 +1980,12 @@ class EntitySearchEngine
$sql->where('z.active = 1');
}
$sql->orderBy('z.name ASC');
// Sorting
$zoneSortMap = [
'name' => 'z.name',
'id' => 'z.id_zone',
];
$sql->orderBy($this->buildOrderBy('zones', $filters, $zoneSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2022,7 +2112,14 @@ class EntitySearchEngine
$sql->where('c.active = 1');
}
$sql->orderBy('cl.name ASC');
// Sorting
$countrySortMap = [
'name' => 'cl.name',
'id' => 'c.id_country',
'zone' => 'z.name',
'iso_code' => 'c.iso_code',
];
$sql->orderBy($this->buildOrderBy('countries', $filters, $countrySortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2154,7 +2251,14 @@ class EntitySearchEngine
$sql->where('c.active = 1');
}
$sql->orderBy('cl.name ASC');
// Sorting
$currencySortMap = [
'name' => 'cl.name',
'id' => 'c.id_currency',
'iso_code' => 'c.iso_code',
'conversion_rate' => 'c.conversion_rate',
];
$sql->orderBy($this->buildOrderBy('currencies', $filters, $currencySortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2285,7 +2389,13 @@ class EntitySearchEngine
$sql->where('l.active = 1');
}
$sql->orderBy('l.name ASC');
// Sorting
$languageSortMap = [
'name' => 'l.name',
'id' => 'l.id_lang',
'iso_code' => 'l.iso_code',
];
$sql->orderBy($this->buildOrderBy('languages', $filters, $languageSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2414,7 +2524,13 @@ class EntitySearchEngine
$sql->where('s.active = 1');
}
$sql->orderBy('s.name ASC');
// Sorting
$shopSortMap = [
'name' => 's.name',
'id' => 's.id_shop',
'group' => 'sg.name',
];
$sql->orderBy($this->buildOrderBy('shops', $filters, $shopSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2534,7 +2650,12 @@ class EntitySearchEngine
}
}
$sql->orderBy('pl.name ASC');
// Sorting
$profileSortMap = [
'name' => 'pl.name',
'id' => 'p.id_profile',
];
$sql->orderBy($this->buildOrderBy('profiles', $filters, $profileSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2650,7 +2771,12 @@ class EntitySearchEngine
}
}
$sql->orderBy('osl.name ASC');
// Sorting
$orderStateSortMap = [
'name' => 'osl.name',
'id' => 'os.id_order_state',
];
$sql->orderBy($this->buildOrderBy('order_states', $filters, $orderStateSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2776,7 +2902,13 @@ class EntitySearchEngine
$sql->where('t.active = 1');
}
$sql->orderBy('tl.name ASC');
// Sorting
$taxSortMap = [
'name' => 'tl.name',
'id' => 't.id_tax',
'rate' => 't.rate',
];
$sql->orderBy($this->buildOrderBy('taxes', $filters, $taxSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);
@@ -2911,7 +3043,14 @@ class EntitySearchEngine
$sql->where('ag.is_color_group = 1');
}
$sql->orderBy('agl.name ASC, a.position ASC');
// Sorting
$attributeSortMap = [
'name' => 'al.name',
'id' => 'a.id_attribute',
'position' => 'a.position',
'group' => 'agl.name',
];
$sql->orderBy($this->buildOrderBy('attributes', $filters, $attributeSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -3056,7 +3195,13 @@ class EntitySearchEngine
$sql->where('fv.custom = 1');
}
$sql->orderBy('fl.name ASC, fvl.value ASC');
// Sorting
$featureSortMap = [
'name' => 'fvl.value',
'id' => 'fv.id_feature_value',
'feature' => 'fl.name',
];
$sql->orderBy($this->buildOrderBy('features', $filters, $featureSortMap));
$sql->limit((int) $limit, (int) $offset);
$results = Db::getInstance()->executeS($sql);
@@ -3189,7 +3334,13 @@ class EntitySearchEngine
}
}
$sql->orderBy('t.name ASC');
// Sorting
$tagSortMap = [
'name' => 't.name',
'id' => 't.id_tag',
'popularity' => 'product_count',
];
$sql->orderBy($this->buildOrderBy('tags', $filters, $tagSortMap));
$sql->limit($limit);
$results = Db::getInstance()->executeS($sql);