public static
getMostViewedProducts
(mixed $limit = 5, mixed $checkPublished = false, mixed $checkAccess = false, mixed $count = false, mixed $type = array(0, 1))
public static function getMostViewedProducts($limit = 5, $checkPublished = false, $checkAccess = false, $count = false, $type = array(0, 1))
{
$db = Factory::getDBO();
$wheres = array();
if ($checkAccess) {
$user = PhocacartUser::getUser();
$userLevels = implode(',', $user->getAuthorisedViewLevels());
$userGroups = implode(',', PhocacartGroup::getGroupsById($user->id, 1, 1));
$wheres[] = " a.access IN (" . $userLevels . ")";
$wheres[] = " c.access IN (" . $userLevels . ")";
$wheres[] = " (ga.group_id IN (" . $userGroups . ") OR ga.group_id IS NULL)";
$wheres[] = " (gc.group_id IN (" . $userGroups . ") OR gc.group_id IS NULL)";
}
if ($checkPublished) {
$wheres[] = " a.published = 1";
}
if (!empty($type) && is_array($type)) {
$wheres[] = 'c.type IN (' . implode(',', $type) . ')';
}
$wheres[] = " a.hits > 0";
$where = count($wheres) ? ' WHERE ' . implode(' AND ', $wheres) : '';
if ($count) {
$q = 'SELECT SUM(a.hits)' . ' FROM #__phocacart_products AS a' . ' LEFT JOIN #__phocacart_product_categories AS pc ON pc.product_id = a.id' . ' LEFT JOIN #__phocacart_categories AS c ON c.id = pc.category_id';
if ($checkAccess) {
$q .= ' LEFT JOIN #__phocacart_item_groups AS ga ON a.id = ga.item_id AND ga.type = 3' . ' LEFT JOIN #__phocacart_item_groups AS gc ON c.id = gc.item_id AND gc.type = 2';
// type 2 is category
}
$q .= $where;
if ((int) $limit > 0) {
$q .= ' LIMIT ' . (int) $limit;
}
$db->setQuery($q);
$products = $db->loadResult();
} else {
$q = 'SELECT a.id, a.title, a.alias, SUM(a.hits) AS hits, GROUP_CONCAT(DISTINCT c.id) as catid, GROUP_CONCAT(DISTINCT c.alias) as catalias, GROUP_CONCAT(DISTINCT c.title) as cattitle, a.catid AS preferred_catid' . ' FROM #__phocacart_products AS a' . ' LEFT JOIN #__phocacart_product_categories AS pc ON pc.product_id = a.id' . ' LEFT JOIN #__phocacart_categories AS c ON c.id = pc.category_id';
if ($checkAccess) {
$q .= ' LEFT JOIN #__phocacart_item_groups AS ga ON a.id = ga.item_id AND ga.type = 3' . ' LEFT JOIN #__phocacart_item_groups AS gc ON c.id = gc.item_id AND gc.type = 2';
// type 2 is category
}
$q .= $where . ' GROUP BY a.id, a.title, a.alias, a.hits' . ' ORDER BY a.hits DESC';
if ((int) $limit > 0) {
$q .= ' LIMIT ' . (int) $limit;
}
$db->setQuery($q);
$products = $db->loadObjectList();
}
return $products;
}