Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
hello ... I've searched and can't find an answer to this one. I have an event listing which shows upcoming and previous events. For upcoming:
Code: Select all
 <cms:pages masterpage='diary-dates.php' order='asc'  show_future_entries='1' start_on="<cms:date format='Y-m-d' gmt='1' />" >
and for previous
Code: Select all
<cms:pages masterpage='diary-dates.php' order='desc'  stop_before="<cms:date format='Y-m-d' gmt='1' />" >


However as well as a standard single date event I need to allow the client to set up an event which has several dates or a date range (e.g. 26, 28, 30 September 2014). I would want this type of event to be ordered in the listing by the first/earliest date and to remain 'upcoming' until the last/latest date has past.

So I need 2 dates to work with - one for deciding upcoming/previous and another for ordering in date order. I'll use k_page_date to determine upcoming/previous - so will ask the client to set this to the last/latest date of a date range. And I've set up an extra editable field
Code: Select all
<cms:editable type='datetime' group='date_group' name='date_range_start' format='dmy' allow_time='1' minute_steps='5' default_time='2000-01-01 00:00:00' label='First/earliest date of a date range or several dates' order='15' /> 
for the client to set up with the first/earliest date of a date range. And I'll use this for sorting the events.

Most of the events will be single dates and it would be nice if I don't have to ask the client to set up the same date in two places - e.g. Published date and my extra date_range_start date.

What I'm after is something along these lines: if nothing is entered for date_range_start set date_range_start to k_page_date. I'm not sure how to do this?

Thanks!
Hi potato,

What I'm after is something along these lines: if nothing is entered for date_range_start set date_range_start to k_page_date. I'm not sure how to do this?

There is a similar thread (viewtopic.php?f=2&t=7672) which does the reverse (i.e. set k_page_date to the editable region's value) but we can adapt it to suit this use-case.

Please copy and paste the following PHP code in your site's (or the one found in 'couch/addons' folder) kfunctions.php.
Code: Select all
class MyEvent{
    function set_default_date( $field ){
        global $FUNCS;
       
        $start_date = trim($field->get_data());
        if( $start_date == '2000-01-01 00:00:00' ){// if date not set
           
            // get publish date ..
            $f = &$field->siblings[3];
            $publish_date = trim( $f->get_data() );
            if( $publish_date != '0000-00-00 00:00:00' ){

                // .. and set it as the default date
                $field->data = $publish_date;
                $field->modified = 1;
            }
            unset( $f );
           
        }
       
        return true;
    }
}

Add the code hilighted below to the definition of the datetime region
<cms:editable type='datetime' validator='MyEvent::set_default_date' group='date_group' ..

As you can see, we have set our PHP function as a validator for the datetime region.
Which means that every time the page is saved the PHP function kicks in. In the routine we check if the date set in the region is '2000-01-01 00:00:00' (that is the default date that you have set in the definition). If it is, it means the user has not explicitly set a date and so we find the publish_date and set that as the new value.

Hope this helps.
... in the end I did it slightly differently as I thought it was a good idea to avoid the client needing to input anything into Published Date. There are now editable fields for event date and optionally, end date.

So event date is always used to set published date and if an end date is entered that is then used to override published date. I adapted your code somewhat blindly but it works! Many thanks!

Code: Select all
  
class MyEvent{
    function set_default_date( $field ){
        global $FUNCS;
       
        $date_range_start = trim($field->get_data());
       
        // Set publish_date to value of this field
        $f = &$field->siblings[3];
        $publish_date = trim( $f->get_data() );
        if( $publish_date != '0000-00-00 00:00:00' ){
            $date_range_start = $FUNCS->make_date( $date_range_start );
            if( $publish_date != $date_range_start ){
                $f->store_posted_changes( $date_range_start );
            }
        }
       
        unset( $f );
       
        return true;
    }
}


class MyEvent2{
    function set_end_date( $field ){
        global $FUNCS;
       
        $date_range_end = trim($field->get_data());
        if( $date_range_end != '2000-01-01 00:00:00' ){// if end date set
           
        // Set publish_date to value of this field
        $f = &$field->siblings[3];
        $publish_date = trim( $f->get_data() );
        if( $publish_date != '0000-00-00 00:00:00' ){
            $date_range_end = $FUNCS->make_date( $date_range_end );
            if( $publish_date != $date_range_end ){
                $f->store_posted_changes( $date_range_end );
            }
        }
       
            unset( $f );
           
        }
       
        return true;
    }
}
3 posts Page 1 of 1
cron