thuijzer

Fieldtype Business Hours

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

hi thuijzer,

would you mind adding a screenshot to get an instant first impression of how it looks like?

  • Like 5

Share this post


Link to post
Share on other sites

@louisstephens Thank you for the screenshot! Note that a 24 hour input is used. So on Saturday you will be open from 3:00-6:00AM and 10:30-12:00AM.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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 ;)

Share this post


Link to post
Share on other sites
4 minutes ago, thuijzer said:

Looks like the license makes this possible.

Yeah. MIT license is pretty permissive.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
1 minute ago, netcarver said:

You need to upgrade your PHP installation - that class was only added in version 5.5.0 (ref: PHP page for the class)

Thanks for your quick reply!

Is there no way to rewrite the code for earlier PHP version?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By BitPoet
      I'm really in love with FormBuilder, but the one thing missing to match all my end users' expectations were repeatable field groups. Think repeaters, in ProcessWire terms. Our primary application of PW is our corporate intranet, so "lines" of fields are quite common in the forms I build. We have all kinds of request forms where the information for a varying number of colleagues needs to be entered (from meal order to flight booking request) and where it is simply impractical to send a form for each, and I don't want to clutter my forms with multiple instances of fields that may only get used ten percent of the time.
      That's why I started to build FormBuilderMultiplier (link to GitHub).
      What it does:
      Adds an option to make a regular Fieldgroup repeatable Lets you limit the number of instances of a Fieldgroup on the form Adds an "Add row" button the form that adds another instance of the Fieldgroup's fields Adds a counter suffix at the end of every affected field's label Stores the entered values just like regular fields Makes the entered values available in preview and email notifications Supports most text based fields, textareas and selects (really, I haven't had enough time to test all the available choices yet) What it doesn't do (yet):
      Support saving to ProcessWire pages (i.e. real Repeaters) I haven't tested all the validation stuff, Date/Time inputs etc. yet, but since I'm utterly swamped with other stuff at work, I didn't want to wait until I have it polished. Any feedback is welcome. There might also be some issues with different output frameworks that I haven't encountered yet. The forms I work with mostly use UIKit.
      Status:
      Still alpha, so test well before using it in the field.
      Known issues:
      When rows are added, the form's iframe needs to be resized, which isn't completely clean yet.
      How it works:
      The Fieldgroup settings are added through regular hooks, as is the logic that adds the necessary field copies for processing the form and displaying previews.
      "Multiplied" field instances are suffixed with _NUM, where NUM is an incremental integer starting from 1. So if you have add two fields named "surname" and "givenname" to a fieldgroup and check the "multiply" checkbox, the form will initially have "surname_1" and "givenname_1" field (I'm still considering changing that to make the risk to shoot oneself into the foot by having a regular "surname_1" field somewhere else in the form less likely).
      When a "row" is added, the first row is cloned through JS and the counter in the fields' IDs, names and "for" attributes as well as the counter in the label are incremented before appending the copies to the Fieldset container in the form.
      To keep backend and frontend in sync, a hidden field named [name of the fieldset]__multiplier_rows is added to the form. Both the backend and the frontend script use this to store and retrieve the number of "rows".
      ToDo:
      Naturally, add the option to store the data in real repeaters when saving to pages. Do a lot of testing (and likely fixing). Make a few things (like the "Add row" button label etc.) configurable in field(set) context. Add a smooth API to retrieve the multiplied values as WireArrays. The mandatory moving screenshot:

    • By MoritzLost
      Hello there,
      I'm working on a tiny textformatter module that searches the text for titles of other pages on your site and creates hyperlinks to them. I'm not sure if something like this exists already, but I haven't found anything in the module directory, so I wrote my own solution 🙂
      It's not properly tested yet and is still missing some functionality I would like to implement, so at the moment it should be considered in BETA. Features include limiting the pages that will get searched by template, and adding a custom CSS class to the generated hyperlinks. As I'm writing this I noticed that it will probably include unpublished and hidden pages at the moment, so yeah ... it's still in development alright 😅
      You can download the module from Github:
      https://github.com/MoritzLost/TextformatterPageTitleLinks
      There's some more information in the readme as well.
      Anyway, let me know what you think! I'm happy about any feedback, possible improvements or ideas on how to improve the module. Cheers.
    • By adrian
      This module provides a way to rapidly generate Page fields and the required templates and pages for use as a drop down select (or any other Page field type).
      This module will let you create a full page field setup in literally a few seconds 
      To use, run Page Field Select Creator from the Setup Menu
      Enter a Field Title, eg: Room Types Select Options - These will become the child pages that will populate the page field select options. There are two different options.
       
      Option 1. TITLE FIELD ONLY - enter one option per line, eg:
       
      Single
      Double
      Suite
       
       
      Option 2. MULTIPLE FIELDS - the first line is used for the field names and the first field must be 'Title'. Subsequent lines are the values for the fields, eg:
       
      Title, Number of Beds, Number of People, Kitchen Facilities
      Single, 1, 1, Fridge Only
      Double, 2, 2, Fridge Only
      Suite, 3, 6, Full Kitchen
        Choose the parent where the page tree of options will be created, eg a hidden "Options" parent page Select a "Deference in API as" option depending on your needs Choose the input field type Check whether "Allow new pages to be created from field?" should be enabled. As an example, if you entered "Room Types" as the field title, you would end up with all of the following automatically created:
      a fully configured page field called: room_types MULTIPLE FIELDS OPTION - 3 additional fields - number_of_beds, number_of_people, kitchen a parent template called: room_types a child template called: room_types_items (with either just a title field, or with the 3 additional fields as well) a parent page called: Room Types a series of child pages named and titled based on the per line entries in the Select Options textarea The templates are configured such that the "room_types_items" child template can only have the main "room_types" template as a parent, and vice versa.

      Then all you have to do is add the newly created page field to any template you want and you're ready to go!
       
      You can grab it from:
       
      Modules directory: http://modules.processwire.com/modules/process-page-field-select-creator/
      Github: https://github.com/adrianbj/ProcessPageFieldSelectCreator
       

    • By bernhard
      WHY?
      This module was built to fill the gap between simple $pages->find() operations and complex SQL queries.
      The problem with $pages->find() is that it loads all pages into memory and that can be a problem when querying multiple thousands of pages. Even $pages->findMany() loads all pages into memory and therefore is a lot slower than regular SQL.
      The problem with SQL on the other hand is, that the queries are quite complex to build. All fields are separate tables, some repeatable fields use multiple rows for their content that belong to only one single page, you always need to check for the page status (which is not necessary on regular find() operations and therefore nobody is used to that).
      In short: It is far too much work to efficiently and easily get an array of data based on PW pages and fields and I need that a lot for my RockGrid module to build all kinds of tabular data.

      Basic Usage

       
      Docs & Download
      https://modules.processwire.com/modules/rock-finder/
      https://github.com/BernhardBaumrock/RockFinder
       
      Changelog
      180817, v1.0.6, support for joining multiple finders 180810, v1.0.5, basic support for options fields 180528, v1.0.4, add custom select statement option 180516, change sql query method, bump version to 1.0.0 180515, multilang bugfix 180513, beta release <180513, preview/discussion took place here: https://processwire.com/talk/topic/18983-rocksqlfinder-highly-efficient-and-flexible-sql-finder-module/
    • By BitPoet
      MediaLibrary
      Update: MediaLibrary can now be found in the official module list.
      Out of necessity, I've started to implement a simple media library module.
      The basic mechanism is that it adds a MediaLibrary template with file and image fields. Pages of this type can be added anywhere in the page tree.
      The link and image pickers in CKEditor are extended to allow quick selection of library pages from dropdowns. In the link picker this happens in the MediaLibrary tab, where you can also see a preview of the selected image. In the image picker, simply select a library from the dropdown at the top, everything else is handled by standard functionality.
      I've put the code onto github. This module is compatible with ProcessWire 3.
      Steps to usage:
      Download the module's zip from github (switch to the pw3 branche beforehand if you want to test on PW 3.x) and unpack it into site/modules Click "Modules" -> "Refresh" in the admin Click "Install" for MediaLibrary For testing, create a page with the MediaLibrary template under home (give it an expressive title like 'Global Media') and add some images and files Edit a differnt page with a CKEditor field and add a link and an image to see the MediaLibrary features in action (see the screencap for details) Optionally, go into the module settings for MediaLibrary Note: this module is far from being as elaborate as Kongondo's Media Manager (and doesn't plan to be). If you need a feature-rich solution for integrated media management, give it a look.
      Feel free to change the settings for MediaFiles and MediaImages fields, just keep the type as multiple.
      There are some not-so-pretty hacks for creating and inserting the correct markup, which could probably be changed to use standard input fields, though I'm a bit at a loss right now how to get it to work. I've also still got to take a look at error handling before I can call it fit for production. All feedback and pointers are appreciated (that's also why I post this in the development section).

      Edit 09.03.2016 / version 0.0.4: there's now also a "Media" admin page with a shortcut to quickly add a new library.

      Edit 01.05.2016:
      Version 0.0.8:
      - The module now supports nested media libraries (all descendants of eligible media libraries are also selectable in link/image picker).
      - There's a MediaLibrary::getPageMediaLibraries method you can hook after to modify the array of available libraries.
      - You can switch between (default) select dropdowns or radio boxes in the module configuration of MediaLIbrary to choose libraries.
      Edit 10.10.2018:
      Version 0.1.3:
      - Dropped compatibility for ProcessWire legacy versions by adding namespaces
      - Allow deletion of libraries from the Media overview admin page
      - Added an option to hide media libraries from the page tree (optionally also for superusers)