Jump to content

Hook pages::saved no created timestamp


spiroue
 Share

Recommended Posts

Hi,

I want to send an email if a page (a booking) is saved with a status field set to booked (Multiple Select). I got this to work with a $pages::saved hook in ready.php, initially it only worked when I updated (and saved) existing pages. When a new booking was created emails weren't sent. After some minor headaches I realised that new pages didn't have a 'created' timestamp set yet (1970-01-01) and I had an If statement to check that pages were newer than 7 days. This was ment to be some kind of fail safe to not send thousands and thousands of emails if for some reason those booking pages got "re-saved" by system or accident.

Anyone who has any ideas on how to make sure emails aren't sent by mistake and only when a new booking is created with status booked or when an existing booking is saved with status booked?

 

Link to comment
Share on other sites

Thanks kongondo. That would work if I didn't need an email being sent on existing pages as well. But when a page, a booking, for example changes status from 'offered' to 'booked' then I need an email to be sent. With Pages::added only new pages will trigger the hook right?

It's working right now but I'm afraid something will trigger emails being sent out for all bookings ever made. I don't know how that would happen but it feels like a possibility ?. For example moving bookings to trash generated emails but that's taken care of.

Link to comment
Share on other sites

7 hours ago, spiroue said:

with a status field set to booked (Multiple Select)

Why does this have to be Multi Select? Shouldn't it be either checked or not (i.e a single select?) 

4 hours ago, spiroue said:

But when a page, a booking, for example changes status from 'offered' to 'booked'

Aah, I see, it seems you have different status? 

4 hours ago, spiroue said:

With Pages::added only new pages will trigger the hook right?

Correct.

4 hours ago, spiroue said:

But when a page, a booking, for example changes status from 'offered' to 'booked' then I need an email to be sent.

If that's the case, it is better to use Pages:saved. However, please further explain your scenario, i.e.

  1. If a page is already set to booked, but it is edited, should an email be resent?
  2. What is booked? Is it the name of a page?
  3. What if a booking is cancelled, is an email to be sent?
Link to comment
Share on other sites

You can also use a conditional hook https://processwire.com/blog/posts/new-ajax-driven-inputs-conditional-hooks-template-family-settings-and-more/#new-conditional-hooks

These fire only, when a specified field value has changed. So your potential problem of accidentally sending multiple emails can be eliminated. You just watch for changes in your status field and act accordingly.

Edit: here is an excellent tutorial about how to watch for field changes: 

It uses a different hook.

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...