public static function productHit($productId = 0)
{
if ($productId == 0) {
return false;
}
$app = Factory::getApplication();
$paramsC = PhocacartUtils::getComponentParameters();
$additional_hits = $paramsC->get('additional_hits', array());
// 1 ... product view
if (!in_array(1, $additional_hits)) {
return false;
}
$user = PhocacartUser::getUser();
$db = Factory::getDbo();
$ip = PhocacartUtils::getIp();
$date = Factory::getDate();
$item = array();
$q = 'SELECT a.id, a.hits' . ' FROM #__phocacart_hits AS a';
if (isset($user->id) && (int) $user->id > 0) {
$q .= ' WHERE a.product_id = ' . (int) $productId . ' AND a.user_id = ' . (int) $user->id;
} else {
if (isset($ip) && $ip != '') {
$q .= ' WHERE a.product_id = ' . (int) $productId . ' AND a.ip = ' . $db->quote($ip);
}
}
$q .= ' ORDER BY a.id' . ' LIMIT 1';
$db->setQuery($q);
$item = $db->loadAssoc();
$q = '';
if (empty($item)) {
if (isset($user->id) && (int) $user->id > 0) {
$q = 'INSERT INTO #__phocacart_hits (product_id, user_id, ip, hits, type, date) VALUES (' . (int) $productId . ', ' . (int) $user->id . ', ' . $db->quote($ip) . ', 1, 1, ' . $db->quote($date) . ')';
} else {
if (isset($ip) && $ip != '') {
$q = 'INSERT INTO #__phocacart_hits (product_id, ip, hits, type, date) VALUES (' . (int) $productId . ', ' . $db->quote($ip) . ', 1, 1, ' . $db->quote($date) . ')';
}
}
$db->setQuery($q);
$db->execute();
return true;
} else {
if (isset($item['id']) && (int) $item['id'] > 0) {
$hits = (int) $item['hits'] + 1;
if (isset($user->id) && (int) $user->id > 0) {
$q = 'UPDATE #__phocacart_hits SET hits = ' . (int) $hits . ', date = ' . $db->quote($date) . ' WHERE product_id = ' . (int) $productId . ' AND user_id = ' . (int) $user->id;
$db->setQuery($q);
$db->execute();
return true;
} else {
if (isset($ip) && $ip != '') {
$q = 'UPDATE #__phocacart_hits SET hits = ' . (int) $hits . ', date = ' . $db->quote($date) . ' WHERE product_id = ' . (int) $productId . ' AND ip = ' . $db->quote($ip);
$db->setQuery($q);
$db->execute();
return true;
}
}
}
}
return false;
}