Jump to content

Fieldtype Business Hours


thuijzer
 Share

Recommended Posts

https://github.com/thuijzer/processwire-FieldtypeBusinessHours

Fieldtype Business Hours

ProcessWire Fieldtype for entering business hours (opening hours)

 

Input format

Leave a day empty when closed on that day

Times are in 24 hour format.

9:00-12:00
9:00-12:00, 13:00-17:30
16:00-2:00

 

Usage in templates

Days are from 1 to 7 where 1 is Monday and 7 is Sunday (ISO-8601)

echo $page->field_name->isNowOpen() ? 'Now open' : 'Now closed';
if($page->field_name[1] == null) { echo 'Closed on Monday'; }
if($page->field_name[2]->inRange('11:00')) { echo 'Tuesday at 11:00 open'; }
echo $page->field_name[1];
echo $page->field_name[1]->getEntries()->getFrom()->format('H:i');

 

  • Like 14
  • Thanks 2
Link to comment
Share on other sites

Thanks for the module! I will have to give it a try this week. In the past I have just use the Pro Fields: Textarea module to create "fields" for each day, which was quite convenient. However, this looks promising and quite straight forward. 

 

** Just in case, I added a screenshot of the layout using dummy times.

Screen Shot 2018-01-18 at 2.48.24 PM.png

  • Like 1
Link to comment
Share on other sites

On 19-1-2018 at 8:57 PM, Juergen said:

Hello thuijzer, I always get this error message after calling


echo $page->field_name[1]->entries[0]->getFrom()->format('H:i');

in template:

Cannot access private property ProcessWire\BusinessHoursDay::$entries

Best regards

My bad. The documentation is wrong.

I like to use immutable objects so I created a getter function for the enries: getEntries()

The readme is now updated.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Hello @thuijzer,

I dont know if others agree with me, but I would find it useful if an extra input field for public holidays will be added after Sunday. Fe in the gastronomy there is often the scenario that the business is open on public holidays. Here in Austria most of the time restaurants are closed on Monday, but if it falls on a public holiday they are open. So an extra field will take care of this. If the field will be left blank business is closed on a public holiday.

Best regards

  • Like 1
Link to comment
Share on other sites

17 minutes ago, Juergen said:

Hello @thuijzer,

I dont know if others agree with me, but I would find it useful if an extra input field for public holidays will be added after Sunday. Fe in the gastronomy there is often the scenario that the business is open on public holidays. Here in Austria most of the time restaurants are closed on Monday, but if it falls on a public holiday they are open. So an extra field will take care of this. If the field will be left blank business is closed on a public holiday.

Best regards

That sounds like a great idea. I will think about a way to enter dates that override opening hours.

  • Like 1
Link to comment
Share on other sites

5 hours ago, Juergen said:

Most of the time I use this library for business hours. You can take a look at it: https://github.com/spatie/opening-hours

This will take care of exceptions (like public holidays)

 

 

I've never seen that library. That is looking very good. Maybe I will just use this library and create a fieldtype that creates the right settings for this lib. Looks like the license makes this possible.

Edit: looks like it needs PHP 7. I think a lot of ProcessWire users are still on 5.6 or even lower, so maybe that's not an option.
But I can always take a look at the source ;)

Link to comment
Share on other sites

Here is another library that I use to get public holidays for each country, but dont make it too complicated.

https://github.com/azuyalabs/yasumi

Its only a possibility to get holidays of each country without entering them inside an textarea input.

The big problem of holidays is that they can change from year to year (fe. Eastern)

So my thought is to include only an additional input field for the opening times on a public holiday (like the other days of the week).

Everyone can include the libraries I have mentioned if there is a need (like I have done with your input field). The new inputfield can be used to output the opening times for holidays like the other days of the week. Include a hint that holiday times will be not included in your output functions  like this

On 18.1.2018 at 2:46 PM, thuijzer said:

echo $page->field_name->isNowOpen() ? 'Now open' : 'Now closed';

Keep it as simple as possible.:)

Here is a screenshot of how I did it. The values of the opening times come from your field. If it is open today or not, comes from the Spatie library, so it will be no problem for developers to include it .

In addition a hint will be shown, when the business is open the next time (comes also from Spatie library)

Screenshot(2).thumb.png.81f3b067d8801e1ce72b707ea2c6e83f.png

In my case I only use your input field for entering the business hours. Everything on the frontend side (output) comes from the Spatie library.

Link to comment
Share on other sites

Just to clearify:

I think the first aim of this inputfield should be a clean UI for entering business hours in an easy way (and in this case you made a great job).:)

The next step is to provide the API calls to get the opening times for each day (like you have also done).:) So every opening time of each day can be indivdually outputted, formatted, styled or used for further PHP operations.

Offering some nice additional API-calls for getting some additional information (whether it is open or not) is great, but can be achived with other libraries or PHP coding too and should only be an addon (not really necessary).;)

So an additional input field for opening times on natural holidays is enough. We can use the value to make further operations (fe to check if this day is a nat. holiday or not by using fe. the Yasumi library) -> thats up to us as site developers and should not be an issue you have to deal with. Not everyone needs this feature.

Best regards

 

  • Like 2
Link to comment
Share on other sites

  • 4 weeks later...

On PW 3.0.92 on my production server with Maria DB 10.2.12 I get the following error when trying to save a page with the fieldtype, even though all the fields have been filled out.

 Error saving field "businessHours" - SQLSTATE[HY000]: General error: 1364 Field 'data' doesn't have a default value

On my WAMP setup with mySQL 5.7.14 it does work.

I managed to create a workaround by manually editing the table design in phpMyAdmin and setting a default value for the data field to 0.

 

  • Thanks 1
Link to comment
Share on other sites

1 hour ago, Kiwi Chris said:

On PW 3.0.92 on my production server with Maria DB 10.2.12 I get the following error when trying to save a page with the fieldtype, even though all the fields have been filled out.

 Error saving field "businessHours" - SQLSTATE[HY000]: General error: 1364 Field 'data' doesn't have a default value

Just wondering. All Fieldtypes need to have a column 'data' in their database table. This is defined in their getDatabaseSchema(). If you look at this method in the parent field type class, wire/core/Fieldtype.php, it has this comment each Fieldtype should override this in particular (with respect to the data column). I notice that this module does not define data in its schema but I would have thought that shouldn't be a problem since it first calls  the parent's parent::getDatabaseSchema() (which has data defined) and builds on that.

Just thinking out loud.

Link to comment
Share on other sites

  • 4 months later...

Hi,

I have PHP 5.4.45 and PW 3.0.106 and get this error:

Fehler: Class 'DateTimeImmutable' not found (Zeile 51 in /home/wwwapow/public_html/cms/site/modules/_FieldtypeBusinessHours/FieldtypeBusinessHours.module)

How can I solve this error?

Thanks for help!

Link to comment
Share on other sites

@guenter55

I've no idea, about rewriting to remove this, I'm not familiar with the code. However, if on a linux system (or mac) it's probably much easier to upgrade the installed version of PHP than it is to rewrite the code to remove its use of that class. You are also well behind the curve in terms of supported (and potentially more secure) versions of PHP

Edited to add: Or wait until @adrian finds a shim ?

@thuijzer sounds like you need to add a 'requires' =>'php>=5.5.0' line to your module config.

  • Like 1
Link to comment
Share on other sites

4 minutes ago, guenter55 said:

@adrian
Where should I implement this class? I tried it in FieldtypeBusinessHours.module without effect. 

I would think that including it in the __construct() method would work. Make sure the path is correct and it is actually being included.

Note that I haven't ever used this.

Link to comment
Share on other sites

12 minutes ago, adrian said:

I would think that including it in the __construct() method would work. Make sure the path is correct and it is actually being included.

Thanks for the help @adrian but that seems to exceed my php knowledge. I see the __construct() functions in the modul-files, but I don´t know what exactly to do.

Link to comment
Share on other sites

I would try this line:

include_once(__DIR__ . '/DateTimeImmutable_php5.4.php');

before:

include_once(__DIR__ . '/BusinessHours.php');

making sure that you have saved the contents of that new class into the BusinessHours module folder.

Link to comment
Share on other sites

7 minutes ago, adrian said:

I would try this line:


include_once(__DIR__ . '/DateTimeImmutable_php5.4.php');

You are Great adrian!!
No more error in admin, I´ll try it in template tomorrow - it´s time to catch some hours sleep ?

Greetings from Austria!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...