Okay, great. So now we have all parts set up. Could you also post your final *working* cron command? Maybe it will help those who come to this topic much later.
--
I am not sure on how to first, set the time and date a cron job should be fired
Your cron job, as you said, requests "notifications.php" every 5 minutes. So, every 5 minutes a script should look for pages that have the datetime editable set at this time (+/- 1 minute, maybe). It is important to understand, that we do not set any specific date to cron, instead we let it run periodically. (It will run even if you don't have any notifications yet). This helps to concentrate the efforts on the couch script.
The button "set notification" can be removed in my opinion, because it creates an illusion that notification is being set somewhere if pressed. Just choose the date and time in the editable and save the cloned page. Template should have the code to find pages that have the same time (or close to that) in editable. This task is handled via regular "cms:pages" with
custom_field parameter. For a notification scheduled to Apr 25, 2018 6:10 am the following code finds the page with that notification (my editable is named
my_datetime). I realize that script might have a delay of some kind or time sync issues, so to make sure I look for anything from 6:09 and 6:11.
- Code: Select all
<cms:pages masterpage='notifications.php' show_future_entries='1' custom_field="my_datetime >= '2018-04-25 06:09' | my_datetime <= '2018-04-25 06:11' " >
...
</cms:pages>
If you (or anyone else) use the publish_date, the same search could be done via "cms:pages" with parameters
start_on,
stop_before, there are samples in docs for that, so I won't repeat it now.
--
script sends the email containing the data related to the one clonable page
When you have this cloned page found - you can place whatever ("cms:send_mail", as you said) within the "cms:pages" block.
--
if I decide to set multiple notifications
If you mean to set several dates and times, then perhaps this could be handled via several datetime editables? Alternative solution is to move everything regarding notifications to a separate template, so a contract can have a relation editable set to that template with type="many" and therefore have an unlimited number of notifications, associated with the contract. Each notification becomes a separate cloned page. Fortunately, the method to find cloned pages is absolutely the same - with custom_field we look for pages which time falls in a desired (current) time period.
One final note to the sample above. You want to find the pages when cron calls the script, so the date in custom_field must be set programmatically to the date and time of request. Here is a sample modification (pay attention to quotes as we have an expression):
- Code: Select all
<cms:pages masterpage='notifications.php' show_future_entries='1' custom_field="my_datetime == <cms:date format='Y-m-d H:i:00' />" >
...
</cms:pages>
I really urge you to test things *not* programmatically and step by step, using some real page and type in your date in a static form, to make sure each and every step works fine before the last modification of the code.
Above I use "cms:date" to return the time when the page was accessed. It works just fine, and note that I typed "00" instead of "s" in time format, because seconds are always zeroed in a datetime editable. I think you can see that it is a deep caveat to fall into (with seconds) when using date set programmatically.
A very important consideration: datetime editable in database is stored in format like "2018-04-25 06:10:00" (Y-m-d H:i:s), so database will search for exactly this format in case you have "==" in custom_field (second sample).
Otherwise, if you use "<=" and ">=" as in my first sample, then it is possible to have a diminished format, like "<cms:date format='Y-m-d H:i' />, which is handy to skip seconds as well (though they can be zeroed as above). Finally, if you need to programmatically return time +/-1 minute, there is a topic on that
viewtopic.php?f=8&t=10559