Show a structured field depending on date and time settings


#1

I would like to show the text stored in a text field of a structured field depending on the date/time settings of the other fields, for example only on mondays from 9:00 to 18:00 and on thursdays from 10:00 to 20:00. It should also be possible to define a startdate and enddate, so we could have seperate settings for a winter and a summer period.

I have an example code which compares a startdate and enddate with the current time, which i found here:
https://getkirby.com/docs/cookbook/filtering

$events = page('events')
->children()
->visible()
->filter(function($child) {
return $child->date(null, 'startdate') < time() && $child->date(null, 'enddate') > time();
});

But i have no idea how to integrate the other fields. Here are the fields in my structure field inside the blueprint:

text (type: text)

startdate (type: datetime)
enddate (type: datetime)

monday (type: toggle)
monday_start (type: time)
monday_end (type: time)

tuesday (type: toggle)
tuesday_start (type: time)
tuesday_end (type: time)

wednesday (type: toggle)
wednesday_start (type: time)
wednesday_end (type: time)

thursday (type: toggle)
thursday_start (type: time)
thursday_end (type: time)

friday (type: toggle)
friday_start (type: time)
friday_end (type: time)

saturday (type: toggle)
saturday_start (type: time)
saturday_end (type: time)

sunday (type: toggle)
sunday_start (type: time)
sunday_end (type: time)

Thanks in advance!


#2

Can you explain a little more about the intended logic? I can only see one text field in the blue print, so I assume from that, if the current day and time falls within the matching day and within the time range set in the fields, you wish to display that one, single, text field?

I think I would do it with two structure fields, one for winter, one for summer. I think I would rip though the structure field and put all the days and time ranges it into an array, then look for todays day and time in the array and see if the time falls within that range.

The example you show from the cookbook is not really useful, because that is meant for filtering pages based on a start date field, but here we are dealing with a structure field.


#3

Hi jimbobrjames,

Can you explain a little more about the intended logic? I can only see one text field in the blue print, so I assume from that, if the current day and time falls within the matching day and within the time range set in the fields, you wish to display that one, single, text field?

Yes, exactly.

I think I would do it with two structure fields, one for winter, one for summer. I think I would rip though the structure field and put all the days and time ranges it into an array, then look for todays day and time in the array and see if the time falls within that range.

The winter and summer period was only an example. There should be more entries with different time frames possible. But the main questions remains: what does the logic look like, that checks, if it is a monday or tuesday etc.?

The example you show from the cookbook is not really useful, because that is meant for filtering pages based on a start date field, but here we are dealing with a structure field.

Right, but i adopted it for my structured field like this:

<?php
$events = page('mainpage/subpage')->name_of_structured_field()->toStructure();
$filtered_events = $events->filter(function($child) {
return $child->date(null, 'startdate') <= time() && $child->date(null, 'enddate') >= time() && $child->onoff() == 'true';
});
foreach($filtered_events as $event): ?>
<?php echo $event->text()->kirbytext(); ?>
<?php endforeach ?>

Thanks!


#4

Is this solved now? …If not, I’d like to see the blueprint, because the structure above looks a bit strange.

Anyway, currently you only check a date, but not the time, so that doesn’t seem to be the final result you are looking for?

Currently, if you want to check if something takes place on Monday and on Thursday, you would have to check if each of these two fields in the structure field is set to true (and if the time is correct). I somehow have the feeling that the structure is not really ideal for what you are trying to do here.

I’d probably rather solve this with subpages, where the main page has the text and start- and end date, and either a single subpage that contains a structure field with the days/hours (but as generic fields, not a named fields) or a subpage for each relevant day.

# daterange-page
fields:
  text:
    label: Text
    type: text
  startdate:
    type: date
  enddate:
   type: date
# subpage
fields:
  timetable:
    label: Timetable
    type: structure
    fields:
      day:
        label: Select a day
        type: select
        options:
          monday: Monday
          # ect.
      startdate:
      enddate:

#5

I think checking the day is irrelevant, since you only have one text field to display. All you actually care about is what time it is :slight_smile: If you had a string of text for each day, then what day it is would be important… or have i missed something?

The function you posted above looks like it should do it, because it will check each entry in the structure field. So the fact that each entry is a different range, or a different day shouldn’t matter.

Can you maybe show the expected result on the front end? How would you want this displayed on the actual webpage… i think knowing that might lead to the cleanest solution.


#6

Sorry for the confusion. I mixed up two different things here. I should not write posts when distracted by my ill child. I will try to make it clear in my next post, which might take a day or two. Thanks and good night.


#8

@krl Sure, no problem. Hope your child gets well soon.