Anyone got a snippet or package that will allow me to display the last n viewed pages - probably have to specify viewed below a specific parent(s)
Thanks
Mark
I'm also looking for a "recently viewed resources" which can be templates via chunks to use TVs from viewed resources. Also params to customise the amount of history output (default would be 5 or 10 resources).
Is this for logged in users? Than its easy to log the history in a custom-table.
Otherwise you will need to work with sessions and cookies, perhaps also in addition with a custom-table.
What to choose depends on your requirements.
That's a good question. For the site I'm currently creating it would be good or both situations. It's a property rental website so it would be great if it remembers what the user has viewed before & after login.
-
☆ A M B ☆
- 3,141 Posts
When I see stuff like that on sites I always wonder if that's really true or if they're just grabbing random items...
Wouldn't it make more sense (and get more people to click through) if you'd include a listing of related items to the one currently on screen instead? There's getRelated for that which you can integrate, I don't think there's any Extra currently available that'll get the latest viewed for you and list those. Like Bruno says it wouldn't be too shocking to develop, but my gut says you're not a PHP dev
You're right I'm not a PHP dev at all!
Actually, I think that there is a good usage of this. If you filter articles from a number of containers, the breadcrumb does not show you how to get back. Secondly, if you are looking at items for purchase, it will show the ones that you have just looked at for reference.
"Remember what Bilbo used to say: It's a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there's no knowing where you might be swept off to."
-J.R.R. Tolkien from The Lord of the Rings
-
☆ A M B ☆
- 3,141 Posts
Hmm I think I read the request differently (reading back now, probably not the way it was intended) when I made my post...
The use case you describe makes sense
Here's a quick thing to help you get started:
Plugin to trigger on OnWebPageComplete (though could also try OnWebPageLoad, haven't tested what event is best in this case):
<?php
if (!isset($_SESSION['recent.' . $modx->context->get('key') ])) {
$s = $_SESSION['recent.' . $modx->context->get('key') ] = array();
} else {
$s = $_SESSION['recent.' . $modx->context->get('key') ];
}
$id = $modx->resource->get('id');
// Prevent getting the same page in the list. Unsetting it here & re-adding it later makes it top of the list again.
if (in_array($id, $s)) {
unset ($s[array_search($id, $s)]);
}
array_unshift($s, $id);
$_SESSION['recent.' . $modx->context->get('key') ] = $s;
Snippet to get the list of IDs as comma separated list (name it getRecentViewed):
if (!isset($_SESSION['recent.' . $modx->context->get('key') ])) {
$s = $_SESSION['recent.' . $modx->context->get('key') ] = array();
} else {
$s = $_SESSION['recent.' . $modx->context->get('key') ];
}
return implode(',',$s);
Example usage:
[[!getResources? &resources=`[[!getRecentViewed]]` &parents=`1,2,3` &tpl=`customChunkToDisplayWith`]]
Untested