@BobRay - I am trying out the snippet you posted and I have a few questions:
The 90-day-old pages are available to anyone. So, I really only need to show this when anonymous users visit the page. Instead of the isMember, how would I say "if user anonymous"? I'm guessing this will also affect the hasSessionContext.
Also, instead of returning a string or sending to a URL, is it possible to call a chunk from within a snippet?
does something like that, what you are after?
<?php
$validTpl = $modx->getOption('validTpl',$scriptProperties,'');
$unvalidTpl = $modx->getOption('unvalidTpl',$scriptProperties,'');
$groupdays = array(
'group_0'=>'0',
'group_30'=>'30',
'group_60'=>'60',
'group_90'=>'90',
'everybody'=>'120',
);
$publishedon = strtotime($modx->resource->get('publishedon'));
foreach ($groupdays as $group=>$days){
if ( ($group == 'everybody' && $publishedon < (time() - ($days * 24 * 60 * 60))) || ($modx->user->isMember($group) && $publishedon < (time() - ($days * 24 * 60 * 60))) ){
return $modx->getChunk($validTpl);
}
}
return $modx->getChunk($unvalidTpl);
Thanks Bruno, I'll have to give that a try. I did get everything working as expected with Bob's code, though. Got conditional chunks and also pulled out the hasSessionContext because not all of my users will be logged in. Didn't realize that has replaced 'isLoggedIn'.
I'm still refining things a bit, but these ideas were key in getting this moving.
I'll keep you all posted with the progress.
With my code, the users shouldn't have to be logged in for it to work. Not-logged-in users should get the "unauthorized" message or get sent to the upgrade page. The hassSessionContext() call is only there to avoid PHP errors and to avoid doing the calculations if the user isn't logged in.
The only way I can think of to do it is to set a $_SESSION variable in your snippet and use sendRedirect() to redirect to the current page. The snippet would have to detect and remove the $_SESSION variable and skip the redirection if it's set. I'm not sure if it will work or not.
sendRedirect() can specify a response code:
$modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 403 Forbidden'));
$modx->sendRedirect($url, array('responseCode' => 'HTTP/1.1 401 Unauthorized'));
Nah, that didn't work. It returns a 200. Looks like it doesn't do anything if there is no actual redirect taking place. I allowed it to redirect to a separate url and it ends, but it picks up the 302 even though 401 is defined here. So, aside from what I'm doing here that still seems off.
I'm trying to get more information about modResponse, to see if that might be what I need.
The only other thing I can think of is to tell Google not to index it in robots.txt or put a noindex meta tag on the page -- Google will respect that.