-
☆ A M B ☆
- 205 Posts
Current Set-up / Situation
I have a simple TV (checkbox) to display a chunk when selected. This, in-turn, shows a module window upon visiting the site with an emergency notification from the company.
How To Question
How can I go about either: refactoring this checkbox TV or creating a new one TV /chunk display set up where when the user selects this checkbox, it's only for a set amount of time, i.g. It will only display for x hours or day(s)?
Sal Baldovinos
SEO & DIGITAL MARKETING
ARIEL DIGITAL | Maximize Your Potential
www.arieldigitalmarketing.com
discuss.answer
-
☆ A M B ☆
- 24,524 Posts
I think I would change that TV to be a date field, have the editor select the date/time he wants it to end, and in the place where you normally put the chunk tags use an output modifier:
[[[[*notification:strtotime:lte=`[[!now]]`:then=`$notification-chunk`:else=``]]]]
The "now" snippet would simply return a current timestamp.
-
☆ A M B ☆
- 24,524 Posts
Or you could do a bit more work on a snippet, and allow the editor to simply pick the current date/time, and have the snippet use that to calculate whether or not it's exceeded the time limit. Have the snippet use two properties, the TV's value (just use the TV tag for the property value to pass the date the TV set), and the time limit. Have the snippet output chunk tags or use getChunk('chunkname') if it's still within the time. Put the snippet where you normally put the chunk or TV tag - the duration is in seconds (in this example, it uses 86400 seconds = one day).
[[!showNotification? &starttime=`[[*theTV]]` &duration=`86400`]]
$now = time();
$start = $modx->getOption('starttime',$scriptProperties); // from the TV
$duration = $modx->getOption('duration',$scriptProperties);
if (empty($start) || empty($duration)) { return; } // do nothing if no useful properties
if((strtotime($start) + $duration) <= $now) {
return $modx->getChunk('chunkname');
// return '[[$chunkname]]';
}
return;
[ed. note: sottwell last edited this post 11 years, 8 months ago.]
I think line 6 should be:
return $modx->getChunk('chunkname');
Lines 6-9 could be replaced with this, though it's less readable:
return (strtotime($start) + $duration) <= $now ? $modx->getChunk('chunkname') : '';
Susan's version is probably plenty fast, but you can almost always speed things up by putting everything in the snippet and getting the raw value of the TV:
$resourceId = $modx->resource->get('id');
$tvId = 12; /* change this to the ID of your TV */
$duration = 86400;
/* Get the value of the modTemplateVarResource */
$query = $modx->newQuery('modTemplateVarResource', array(
'contentid' => $resourceId,
'tmplvarid' => $tvId,
));
$query->select('value');
$start = $modx->getValue($query->prepare());
return ((!empty($start)) && ($start + $duration <= $time()))
? $modx->getChunk('chunkname')
: '';
-
☆ A M B ☆
- 24,524 Posts
I knew something was wrong in there somewhere, I just couldn't put my finger on it.
I've done the same thing many times.
-
☆ A M B ☆
- 205 Posts
It's great having such a passionate community to get interesting solutions from! Thank you both.
Sal Baldovinos
SEO & DIGITAL MARKETING
ARIEL DIGITAL | Maximize Your Potential
www.arieldigitalmarketing.com
-
☆ A M B ☆
- 205 Posts
Quote from: BobRay at Jan 30, 2015, 12:56 AMI think line 6 should be:
return $modx->getChunk('chunkname');
Lines 6-9 could be replaced with this, though it's less readable:
return (strtotime($start) + $duration) <= $now ? $modx->getChunk('chunkname') : '';
Susan's version is probably plenty fast, but you can almost always speed things up by putting everything in the snippet and getting the raw value of the TV:
$resourceId = $modx->resource->get('id');
$tvId = 12; /* change this to the ID of your TV */
$duration = 86400;
/* Get the value of the modTemplateVarResource */
$query = $modx->newQuery('modTemplateVarResource', array(
'contentid' => $resourceId,
'tmplvarid' => $tvId,
));
$query->select('value');
$start = strtotime((modx->getValue($query->prepare()));
return ((!empty($start)) && ($start + $duration <= $time()))
? $modx->getChunk('chunkname')
: '';
Is this still assuming the TV is now a date picker?
[ed. note: BobRay last edited this post 11 years, 8 months ago.]
Sal Baldovinos
SEO & DIGITAL MARKETING
ARIEL DIGITAL | Maximize Your Potential
www.arieldigitalmarketing.com
Yes, and you should check the modx_site_tmplvar_contentvalues table in the DB to verify that the value stored is a single number rather than a human-readable date. If it's the latter, the code would have to be modified slightly.
-
☆ A M B ☆
- 24,524 Posts
I'm pretty sure it's It's definitely a human-readable date, so you'd need to add a strtotime() to convert it to a timestamp.
$start = strtotime($modx->getValue($query->prepare()));