public static function trackViewed(EntityModel $entity)
{
$entityType = $entity->getEntityType();
$trackedTypes = [ENTITY_CLIENT, ENTITY_INVOICE, ENTITY_QUOTE, ENTITY_TASK, ENTITY_EXPENSE];
if (!in_array($entityType, $trackedTypes)) {
return;
}
$object = static::convertToObject($entity);
$history = Session::get(RECENTLY_VIEWED) ?: [];
$accountHistory = isset($history[$entity->account_id]) ? $history[$entity->account_id] : [];
$data = [];
// Add to the list and make sure to only show each item once
for ($i = 0; $i < count($accountHistory); $i++) {
$item = $accountHistory[$i];
if ($object->url == $item->url) {
continue;
}
array_push($data, $item);
if (isset($counts[$item->accountId])) {
$counts[$item->accountId]++;
} else {
$counts[$item->accountId] = 1;
}
}
array_unshift($data, $object);
if (isset($counts[$entity->account_id]) && $counts[$entity->account_id] > RECENTLY_VIEWED_LIMIT) {
array_pop($data);
}
$history[$entity->account_id] = $data;
Session::put(RECENTLY_VIEWED, $history);
}