public function getItemsByCategoriesIds(array $array, $onlyPublic = false, $toTop = false)
{
if (!sizeof($array))
{
return array();
}
$toTopStr = $toTop ? "`app_catalog_item`.`isTop` desc, " : null;
$db = new app_db();
array_walk($array, array($db, "escape"));
$add = array();
if(is_array($this->filter)) {
foreach($this->filter as $param=>$value) {
if($value==0) continue;
$value_arr = explode(",", $value);
$add_str = " (`app_catalog_filter_values`.`param`='" . $db->escape($param) . "'";
if (sizeof($value_arr) != 0) {
$add_str .= " AND (";
foreach ($value_arr as $elem) {
$add_str .= "`app_catalog_filter_values`.`value` = '" . $db->escape($elem) . "' or ";
}
$add_str = substr($add_str, 1, -3);
$add_str .= ")";
}
$add_str .= ")";
$add[] = $add_str;
}
}
if(!is_array($this->filter) || count($add)==0) {
$sql = "select " . ($this->getPagination()?"sql_calc_found_rows":"") . " `app_catalog_item`.* from `app_catalog_item`
LEFT JOIN `app_catalog_category` ON `app_catalog_category`.`id` = `app_catalog_item`.`categoryId`
where ".($onlyPublic ? "`app_catalog_item`.`public` = '1' and (" : null)." `categoryId` IN ('".implode("', '", $array)."') ".($onlyPublic ? ")" : null)."
order by {$toTopStr} `app_catalog_category`.`position`, `position`";
} else {
$sql = "select " . ($this->getPagination()?"sql_calc_found_rows":"") . "`app_catalog_item`.*, count(`app_catalog_item`.`id`) as matched
from `app_catalog_item`
LEFT JOIN `app_catalog_category` ON `app_catalog_category`.`id` = `app_catalog_item`.`categoryId`
RIGHT JOIN `app_catalog_filter_values` ON(`app_catalog_filter_values`.`item` = `app_catalog_item`.`id` )
WHERE ".($onlyPublic ? "`app_catalog_item`.`public` = '1' and (" : null)."`categoryId` IN ('".implode("', '", $array)."') AND (";
$sql .= implode(" OR ", $add) . ") ".($onlyPublic ? ")" : null)." GROUP BY `app_catalog_item`.`id` HAVING `matched` = '" . count($add) . "'";
$sql .= " order by {$toTopStr} `app_catalog_category`.`position`, `position`";
}
if($this->getPagination()) {
$sql .= " LIMIT ".$this->getPagination()->getOffset().", ".$this->getPagination()->getLimit() . "";
}
$db->query($sql);
//echo $sql; die();
if($this->getPagination()) {
$this->getPagination()->setCount($db->found_rows());
}
$objects = array();
while ($row = $db->fetch())
{
$objects[] = (new app_catalog_item())->setAttributes($row);
}
return $objects;
}