Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Multiple DateFiled or TimeFiled or DateTimeField

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 447 Views
  • CHD
    Avatar
    Community Member
    217 Posts

    Multiple DateFiled or TimeFiled or DateTimeField Link to this post

    hi guys,

    I'm doing weekyl opening hours on a directory site, so have 14 time fields required:

    Monday opening time, Monday closing time
    Tuesday opening... etc

    I wanna add the dropwdown to each field, but cant seem to work out how to do that without repeating LOTS of code?

    here's what I have:

    Statc $db = array (

    'MonStart' => 'Time',   
          'MonFinish' => 'Time',   
          'MonClosed' => 'Boolean',   
          
          'TueStart' => 'Time',   
          'TueFinish' => 'Time',   
          'TueClosed' => 'Boolean',   

    ............

    $TimeField = new Timefield('MonStart', 'Monday Opening time');
          $TimeField->setConfig('showdropdown', true);
       $TimeField->setConfig('timeformat', 'h:m a');
       $TimeField->setLocale('en_GB');

    new $TimeField

    any ideas?

    thanks.

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Multiple DateFiled or TimeFiled or DateTimeField Link to this post

    you could create a DaylyOpeningHours object, then do a CTF with a record for each day. That would get rid of the code repetition.

    Seems like a bit of an overkill - unless of course you want to do something special with these data beside showing them on the page... Else you could just use a table in the content area - but I guess you thought of that

  • CHD
    Avatar
    Community Member
    217 Posts

    Re: Multiple DateFiled or TimeFiled or DateTimeField Link to this post

    yeah well the idea is, business owners will be entering their own opening hours, so i will be using the "dropdown" on the front end, rather than having to type in lots of times manually, plus this ensures all date formats are the same. (although I guess I could do front end validating....)

    sorry I have no idea what you mean by CTF, any chance of an example?

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Multiple DateFiled or TimeFiled or DateTimeField Link to this post

    OK, sorry, a CTF isa complexTableField...

    But I was thinking: since a week only ever has the 7 days, you could just as well create the fields all at once, then use some sort of loop to keep from duplicating code, like the following (haven't tested, but might give you an idea:

    Suppose you call the fields:

    MondayOpenAt,
    MondayCloseAt
    MondayClosed
    TuesdayOpenAt
    ...

    Then you could do something like (just copied your fieldsettings):

    $arrayWeek = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' );
    $arraySettings = array('OpenAt' => 'open at', 'CloseAt' => 'close at', 'Closed' => 'closed all day');

    foreach ($arrayWeek as $day) {
       foreach ($arraySettings as $setting => $label) {

          $fieldName = $day . $setting;

          if ($setting != 'Closed') {
             $field = new Timefield($fieldName, _t("OpeningHours.$label", "$day $label"));
             $field->setConfig('showdropdown', true);
             $field->setConfig('timeformat', 'h:m a');
             $field->setLocale('en_GB');
          }
          else {
             $field = new CheckboxField($fieldName, _t("OpeningHours.$label", "$day $label"));
          }
          $fields->push($field);
       }
    }

    447 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.