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 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://gitlab.com/baumrock/RockFinder/tree/master
       
      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 OLSA
      Hello for all,
      ConfigurationForm fieldtype module is one my experiment from 2016.
      Main target to build this module was to store multiple setup and configuration values in just 1 field and avoid to use 1 db table to store just single "number of items on page", or another db table to store "layout type" etc. Thanks to JSON formatted storage this module can help you to reduce number of PW native fields in project, save DB space, and reduce number of queries at front-end.
      Install and setup:
      Download (at the bottom ), unzip and install like any other PW module (site/modules/...). Create some filed using this type of field (ConfigurationForm Fieldtype) Go to field setup Input tab and drag some subfields to container area (demo). Set "Name" and other params for subfields Save and place field to templates ("Action tab") How to use it:
      In my case, I use it to store setup and configurations values, but also for contact details, small content blocks... (eg. "widgets").
      Basic usage example:
      ConfigForm fieldtype "setup" has subfields:
      "limit", type select, option values: 5, 10, 15, 20
      "sort", type select, option values: "-date", "date",  "-sort", "sort"
      // get page children (items) $limit = isset($page->setup->limit) ? $page->setup->limit : 10; $sort = isset($page->setup->sort) ? $page->setup->sort : '-sort'; $items = $page->children("limit=$limit, sort=$sort");  
      Screenshots:


       
      Notes:
      Provide option to search inside subfields Provide multilanguage inputs for text and textarea field types Provide option for different field layout per-template basis Do not place/use field type "Button" or "File input" because it won't works. Please read README file for more details and examples Module use JSON format to store values. Text and textarea field types are multilanguage compatible, but please note that main target for this module was to store setup values and small content blocks and save DB space. Search part inside JSON is still a relatively new in MySQL (>=5.77) and that's on you how and for what to use this module.
      Thanks:
      Initial point for this fieldtype was jQuery plugin FormBuiled and thanks to Kevin Chappel for this plugin.
      In field type "link" I use javascript part from @marcostoll module and thanks to him for that part.
      Download:
      FieldtypeConfigForm.zip
      Edit: 14. August 2018. please delete/uninstall previously downloaded zip
      Regards.
         
    • By bernhard
      @Sergio asked about the pdf creation process in the showcase thread about my 360° feedback/survey tool and so I went ahead and set my little pdf helper module to public.
      Description from PW Weekly:
       
      Modules Directory: https://modules.processwire.com/modules/rock-pdf/
      Download & Docs: https://gitlab.com/baumrock/RockPdf
       
      You can combine it easily with RockReplacer: 
      See also a little showcase of the RockPdf module in this thread:
       
    • By Thomas Diroll
      Hi guys I'm relatively new to PW and just finished developing a page for a client. I was able to include all necessary functionality using the core fieldtypes but now I it seems that I need to extend them with a custom one. What I need is a simple button, that copies the absolute url (frontend not PW-backend) of the page which is currently edited to the clipboard. As this feature is only needed inside a specific template, I tend to use a custom fieldtype which provides this feature. I've been looking inside the core modules code (eg. FieldtypeCheckbox.module) but I don't really get the structure of it and how its rendered to the admin page. I also didn't find a lot of tutorials covering custom fieldtypes.
      Maybe some of you could give me some tips on how to write a basic custom fieldtype that renders a button which copies the value of
      page->httpUrl() to the clipboard using JS. Thanks!
    • By bernhard
      Some of you might have followed the development of this module here: https://processwire.com/talk/topic/15524-previewdiscussion-rockdatatables/ . It is the successor of "RockDataTables" and requires RockFinder to get the data for the grid easily and efficiently. It uses the open source part of agGrid for grid rendering.
       
      WHY?
      ProcessWire is awesome for creating all kinds of custom backend applications, but where it is not so awesome in my opinion is when it comes to listing this data. Of course we have the built in page lister and we have ListerPro, but none of that solutions is capable of properly displaying large amounts of data, for example lists of revenues, aggregations, quick and easy sorts by the user, instant filter and those kind of features. RockGrid to the rescue 😉 
       
      Features/Highlights:
      100k+ rows Instant (client side) filter, search, sort (different sort based on data type, eg "lower/greater than" for numbers, "contains" for strings) extendable via plugins (available plugins at the moment: fullscreen, csv export, reload, batch-processing of data, column sum/statistics, row selection) all the agGrid features (cell renderers, cell styling, pagination, column grouping etc) vanilla javascript, backend and frontend support (though not all plugins are working on the frontend yet and I don't plan to support it as long as I don't need it myself)  
      Limitations:
      While there is an option to retrieve data via AJAX the actual processing of the grid (displaying, filtering, sorting) is done on the client side, meaning that you can get into troubles when handling really large datasets of several thousands of rows. agGrid should be one of the most performant grid options in the world (see the official example page with a 100k row example) and does a lot to prevent problems (such as virtual row rendering), but you should always have this limitation in mind as this is a major difference to the available lister options that do not have this limitation.
      Currently it only supports AdminThemeUikit and I don't plan to support any other admin theme.
       
      Download: https://gitlab.com/baumrock/RockGrid
      Installation: https://gitlab.com/baumrock/RockGrid/wikis/Installation
      Quikckstart: https://gitlab.com/baumrock/RockGrid/wikis/quickstart
      Further instructions: https://gitlab.com/baumrock/RockGrid/wikis/quickstart#further-instructions
       
      Module status: alpha, License: MIT
      Note that every installation and uninstallation sends an anonymous google analytics event to my google analytics account. If you don't want that feel free to remove the appropriate lines of code before installation/uninstallation.
       
      Contribute:
      You can contribute to the development of this and other modules or just say thank you by
      testing, reporting issues and making PRs at gitlab liking this post buying me a drink: paypal.me/baumrock/5 liking my facebook page: facebook.com/baumrock hiring me for pw work: baumrock.com  
      Support: Please note that this module might not be as easy and plug&play as many other modules. It needs a good understanding of agGrid (and JavaScript in general) and it likely needs some looks into the code to get all the options. Please understand that I can not provide free support for every request here in the forum. I try to answer all questions that might also help others or that might improve the module but for individual requests I offer paid support for 60€ per hour.
       
      Changelog
      180730 support subdir installations 180711 bugfix (naming issue) 180630 alpha realease  
      Use Cases / Examples:
      Colored grid cells, Icons, Links etc. The Grid also has a "batcher" feature built in that helps communicating with the server via AJAX and managing resource intensive tasks in batches:

      Filters, PW panel links and instant reload on panel close:

      You can combine the grid with a chart library like I did with the (outdated) RockDataTables module: