Manaus Posted February 11, 2014 Share Posted February 11, 2014 Hello, I need to show only new articles to logged in users, I believe it's done with cookies, but really can't figure out how to proceed.. Any suggestion? Thanks! Link to comment Share on other sites More sharing options...
adrian Posted February 11, 2014 Share Posted February 11, 2014 You should just need this: if($user->isLoggedin()){ //show new articles here } Link to comment Share on other sites More sharing options...
Manaus Posted February 11, 2014 Author Share Posted February 11, 2014 Thanks Adrian, but how do I define 'new articles'? The user should see only those published between his last visit and now... is there any $user->lastLogin() ? Link to comment Share on other sites More sharing options...
adrian Posted February 11, 2014 Share Posted February 11, 2014 Ok, gotcha. I did this on a site a while back. I'll dig up the code and get back to you in a minute. Actually I did this on the very first PW site I built, so there might be a better way, but this still works. Create a new datetime field called last_page_load and add it to the user template (remember to "Show System Templates" so you have access to it). Put this code somewhere that is included on every page of the site: if ($user->isLoggedin()) { $user->last_page_load = date('Y-m-d H:i:s'); $user->of(false); $user->save(); } Then you can do this: foreach($articles as $article){ if($user->isLoggedin() && $article->modified > $user->last_page_load){ // echo article here } } Let me know how you go with that. I don't think I have missed any components in making this work, and maybe there is a better approach, but one advantage of this over cookies is that it won't matter whether the user clears their cookies or not, or if they are visiting from different devices. EDIT: Thinking through this - in my case I had already limited $articles with a find. You may want to do the same as foreach'ing through 100's/1000's of articles won't be a good idea, so you could use something like this: $new_articles = $pages->find("template=articles,modified>{$user->last_page_load}"); 7 Link to comment Share on other sites More sharing options...
Manaus Posted February 11, 2014 Author Share Posted February 11, 2014 Great Adrian, thanks!! [removed dumb question :] Link to comment Share on other sites More sharing options...
ryan Posted February 16, 2014 Share Posted February 16, 2014 Adrian, remember to restore the output formatting state of $user, i.e. $user->of(true); unless you sure you won't be outputting anything from user after that block of code. But safer just to restore it to of(true) after you've saved. 2 Link to comment Share on other sites More sharing options...
Manaus Posted March 21, 2014 Author Share Posted March 21, 2014 I stumbled upon this issue again, but it's getting curious developments... It might happen that posts are often modified [adding or removing users from them], so the posts are now appearing 'new' quite often. Is there another way to use a per-user state of the posts? Just thinking: I might add a list of this-user-has-read linked pages to each post, and everytime the user reads the post, flag the checkbox. Thanks for any suggestion... Link to comment Share on other sites More sharing options...
adrian Posted March 21, 2014 Share Posted March 21, 2014 Why not just change "modified" to "created"? Link to comment Share on other sites More sharing options...
Manaus Posted March 22, 2014 Author Share Posted March 22, 2014 Thanks Adrian, I thought about it, but each user has his view of the page, according to his last visit... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now