creating duplicate records for a date range

9 posts by 2 authors in: Forums > CMS Builder
Last Post: September 16, 2010   (RSS)

By Deborah - December 9, 2009

I have a calendar that displays CMSB records from an xml.php file. Each event is displayed on the calendar based on what is entered with a date field.

For events that occur weekly, this would mean that one would need to create 52 records for a year's worth of this event. Using two date fields (start date + stop date), and making only one entry, would it be possible with PHP to have the same information for a record print for each date within the start and stop range?

Hope that makes sense.

Deborah

Re: [Deborah] creating duplicate records for a date range

By Chris - December 10, 2009

Hi Deborah,

That's a very custom configuration you've got there. I'm afraid we can't provide free support for this.

It would be possible to set something up so you can have "repeating events", but we'd have to help you through our consulting service. Please let me know if you're interested in this.
All the best,
Chris

Re: [Deborah] creating duplicate records for a date range

By Chris - January 25, 2010

Hi Deborah,

I've been thinking about calendars and schedules recently, and, on second reading, your request doesn't seem so complicated. If you'd still like help with it, please post your xml.php code. :)
All the best,
Chris

Re: [chris] creating duplicate records for a date range

By Deborah - January 26, 2010

Hi, Chris.

Thanks for your kind offer. Here is the xml.php code:

<?php header('Content-type: application/xml; charset=utf-8'); ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<?php require_once "/var/www/vhost/80/p/www.peabody.lib.me.us/cmspeabody/lib/viewer_functions.php";
list($calendarRecords, $calendarMetaData) = getRecords(array(
'tableName' => 'calendar',
));
?>
<calendar>
<?php foreach ($calendarRecords as $record): ?>
<event>
<date><?php echo date("Y-m-d", strtotime($record['date'])) ?></date>
<title><?php echo htmlspecialchars ($record['title']) ?></title>
<url><?php echo $record['_link'] ?></url>
</event>
<?php endforeach; ?>
</calendar>


As an example, this client wanted to be able to enter a standard Saturday event for multiple blocks of Saturdays (such as 3 months).

I look forward to anything you come up with. And if you need a beta tester, I'm happy to help out.

Deborah

Re: [Deborah] creating duplicate records for a date range

By Chris - January 26, 2010 - edited: January 26, 2010

Hi Deborah,

How about this idea:

Add 7 new checkbox fields to your calendar section, named sunday, monday, tuesday, wednesday, thursday, friday, and saturday. Add a second date field (let's call it 'end_date') and set its default to "None / Blank".

Finally, replace your <calendar /> with:

<calendar>
<?php
foreach ($calendarRecords as $record) {
if (strtotime($record['end_date'])) {
$last_day = null;
$current_time = strtotime($record['date']);
$end_time = strtotime($record['end_date']);
while ($current_time < $end_time || date("Y-m-d", $current_time) == date("Y-m-d", $end_time)) {
$skip = false;

$current_day = date("Y-m-d", $current_time);
// skip if we've already seen this day (necessary because we're skipping forward by 23 hours)
if ($current_day == $last_day) { $skip = true; }

// skip if relevant weekday checkbox isn't checked
$current_weekday = strtolower(date('l', $current_time));
if (!$record[$current_weekday]) { $skip = true; }

// output <event> for this day!
if (!$skip) { ?>

<event>
<date><?php echo date("Y-m-d", $current_time) ?></date>
<title><?php echo htmlspecialchars ($record['title']) ?></title>
<url><?php echo $record['_link'] ?></url>
</event>
<?php }

// advance current_time (but not by 24 hours, lest we skip over a springtime daylight-savings day)
$current_time += 23*60*60;
$last_day = $current_day;
}
}
else {
// no end_date, so this is a single-day event
?>

<event>
<date><?php echo date("Y-m-d", strtotime($record['date'])) ?></date>
<title><?php echo htmlspecialchars ($record['title']) ?></title>
<url><?php echo $record['_link'] ?></url>
</event>
<?php
}
}
?>

</calendar>


Not quite as simple as I was hoping for. I had a much cleaner solution in mind, but it only works for PHP 5+.

Note that if end_date is blank, the weekday checkbox fields will be ignored and the event will be for only one day.

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] creating duplicate records for a date range

By Deborah - January 26, 2010

Hi, Chris.

I understand the concept, but will need a few days to work with the code you supplied, because I'm on deadline with something right now.

All of my sites run PHP 5+ which would allow for the "easier option", but I understand if this is to become a built-in feature in CMSB, it may need to be available for older PHP versions.

Thanks and I'll get back to you to let you know how it worked out.
Deborah

Re: [Deborah] creating duplicate records for a date range

By Deborah - September 16, 2010

Chris and Forum Go-ers,

Sorry for the nine-month delay in replying to Chris' post. The client I wanted to accomplish this for entered the events manually before I had a chance to test this out.... then I got busy.

Today I tested out Chris' solution and it worked perfectly for me. Thought others here might want to know.

Chris, a very belated "Thank You"!

~ Deborah

Re: [Deborah] creating duplicate records for a date range

By Chris - September 16, 2010

Hi Deborah,

Glad that worked out for you! :)
All the best,
Chris