Jump to content

Recommended Posts

Posted

Hey,

I'm wanting to build a selector where you can filter the day (Friday, Saturday, Sunday). However, it doesn't matter the date as long as it falls on a Friday, Saturday or Sunday. 

$pages->find("template=act, showing_on=Friday");

I'm using a dateTime field because it will show the actual date, the only thing I can think of is have a date select and a options field to select the day. I was just wondering if there is a better way of doing it avoiding the user selecting twice.

Before I was looping through each act and converting the dateTime to day and checking if it doesn't equal the day !="Friday" and removing it from the array if it doesn't. However this crashes the server when traffic is high. 

Posted
11 minutes ago, BitPoet said:

You could try my DatetimeAdvanced field type and filter by showing_on.day_of_week.

Exactly what I'm looking for, thank you very much!

 

Edit:

@BitPoet

Not working with 3.0 -
Error: Class 'ProcessWire\WireDT' not found (line 109 of /home/peterand/public_html/pwtramlines/site/modules/WireDT/FieldtypeDatetimeAdvanced.module) 

Posted

Thanks for the feedback. I hadn't tried it with 3.x yet. Since WireDT is a PW module anyway, I'm going to switch to using PW's $modules/Module API instead of "new" and "instanceof".

  • Like 1
Posted
2 hours ago, BitPoet said:

Thanks for the feedback. I hadn't tried it with 3.x yet. Since WireDT is a PW module anyway, I'm going to switch to using PW's $modules/Module API instead of "new" and "instanceof".

@BitPoet sadly this has broken the website. All date format fields when using the time field also get the following error - All the data has reset to 1970. It also breaks the ability to do $pages->find("timeon<=". time() .");

  •  Error saving field "timeon" - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '' for column 'data' at row 1
Posted

Sorry, it's apparently been too long since I tested the module with newer PW versions (turned out I was using an early 3.x release, but too much has changed since then), but I'm working on it. It's giving me a bit of a headache still, but I'll try to have it fixed over the weekend.

  • Like 2
Posted

@Tom., I think BitPoet's module is the way to go, but in case you can't use it (e.g. your MySQL doesn't have timezone support enabled) you could use a saveReady hook to populate a hidden field with the day of the week of the entered date.

// in /site/ready.php
$this->pages->addHookAfter('saveReady', function($event) {
    $page = $event->arguments('page');
    if($page->template->name === 'act' && $page->date_field) {
        $page->day_of_week = date('l', $page->date_field);
    }
});

// find pages
$pages->find("template=act, day_of_week=Friday"); 

 

  • Like 1
Posted

Actually, @Robin S' approach isn't a bad one either.

I've just pushed 0.0.8 to GitHub that overrides some native methods of FieldtypeDatetime and adds InputfieldDatetimeAdvanced to work around some explicit integer casts by the core WireDatetime class. I haven't been able to test it extensively yet, of course, but it does look like everything works on 3.0.34 (high time to upgrade my 3.x test system now). I'm not completely sure if the module upgrade will run smoothly. In the worst case, switch the field back to a regular Datetime field, uninstall the module and WireDT, then re-install the Fieldtype module.

  • Like 1

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.
×
×
  • Create New...