Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Multiple DateFiled or TimeFiled or DateTimeField


Go to End


4 Posts   1162 Views

Avatar
CHD

Community Member, 219 Posts

2 March 2012 at 6:49am

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.

Avatar
martimiz

Forum Moderator, 1391 Posts

2 March 2012 at 6:57am

Edited: 02/03/2012 6:57am

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 :-)

Avatar
CHD

Community Member, 219 Posts

2 March 2012 at 7:01am

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? :D

Avatar
martimiz

Forum Moderator, 1391 Posts

2 March 2012 at 9:18am

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);
	}
}