by
KK » Sat Oct 10, 2015 3:00 am
Ok, thanks.
Limiting the pages to a certain time-period will not require any additional editable-region.
While listing all pages, we just need to calculate the date 30 days (e.g.) before the current date and then ask cms:pages to fetch pages starting from that date.
To do the calculation we'll fall back on a little PHP (you can find many more examples here -
viewtopic.php?f=8&t=8316) -
- Code: Select all
<cms:php>
global $CTX;
// calculate start date that is minus 30 days from now
$ts_now = strtotime( "<cms:date format='Y-m-d H:i:s' />" );
$CTX->set( 'my_start_date', date('Y-m-d H:i:s', strtotime("-30 days", $ts_now)), 'global' );
</cms:php>
The 'my_start_date' variable will now always contain the date from one month back.
Use it with cms:pages as follows -
<cms:pages masterpage='whatever.php' start_on=my_start_date >
..
</cms:pages>
And now you'll never list any pages older than 30 days.
That takes care of the list-view.
Let us tackle the page-view next so that if someone (other than the admins) directly visits an expired page she should get a 404 Not found error.
To do that use the following in the page_view -
- Code: Select all
<cms:if k_page_date lt my_start_date >
<cms:if k_user_access_level lt '7'>
<cms:abort is_404='1' />
</cms:if>
</cms:if>
All the examples above had a period of 30 days hardcoded into them.
To make that period user-configurable, define an editable region in a global template (
http://docs.couchcms.com/tutorials/port ... bal-values) as follows -
- Code: Select all
<cms:editable
type='text'
name='expiry_duration'
label='Expiry Duration'
desc='set the number of days after which a submitted news item expires e.g. 30'
validator='non_negative_integer'
required='1'
>30</cms:editable>
And now modify our original PHP code calculating the date as follows to use the global region -
- Code: Select all
<cms:php>
global $CTX;
// calculate start date that is minus user-configured days from now
$ts_now = strtotime( "<cms:date format='Y-m-d H:i:s' />" );
$CTX->set( 'my_start_date', date('Y-m-d H:i:s', strtotime("-<cms:get_custom_field 'expiry_duration' masterpage='globals.php' /> days", $ts_now)), 'global' );
</cms:php>
Hope it helps.