Can Rules do this?

Events happening in the community are now at Drupal community events on www.drupal.org.
mlittle's picture

I have a node with a date field. For this discussion let's assume that the value in the date field is 1/1/2010. I would like to check all the nodes (of a particular content type) and, once that date is equal to or greater then x number of days from today, send an email to someone. Obviously, each node would have a different date as populated at the time the node was created. I want to iterate through the nodes and if the node date is equal to or greater then x number of days from today, do something like send an email. Is this possible with rules or do I need to do something with cron, or both.

Thanks for your help

Comments

Rules or Rules + VBO

itangalo's picture

Hya

What you describe should be a good case for Rules.
I see two ways of doing it:

1) You use Rules Scheduler (included in the Rules project) to schedule an e-mail when each node is created. I haven't tried it myself, but it should be possible to use a CCK date field and add (say) 20 days to it. The actual e-mailing is done on a cron run, but Rules takes care of queueing all the e-mails to be sent.

2) The other way is to use Views Bulk Operations, and list all the nodes that match the date criteria you want. Then you can use Rules to run the VBO once every day (or so). Sending e-mail with VBO is pretty straight forward, but it becomes trickier when trying to configure the VBO fields through Rules.

The first solution is good if you want one e-mail per node. The second is good if you want one e-mail per day, and each node can occur in several of them. Either way you will have use of Rules Scheduler.

Some more comments:
* The first solution could send repeated e-mails, but it will probably be trickier than the second one.
* If you use the first one you should make Rules keep track of queued e-mail and change them whenever dates are changed in the nodes.
* The second one will be tricky. Sending e-mails with VBO is pretty straight forward, but configuring VBO fields through Rules is not.

I just wrote a blog post on making Rules and Views Bulk Operations work together: http://nodeone.se/drupal-planet/make-rules-dance-with-views-bulk-operations. It might be some help if you want to get started on that track.

Good luck!
//Johan Falk, NodeOne, Sweden

Just so I understand...

mlittle's picture

Johan,

First, thanks for the reply and the information. Are you suggesting that, when the node is created to queue the email and just have it wait until the predetermined date? Just for clarification... My site has date fields in the my node (created with cck). The date is always in the past, about 3 months prior. Basically it's the date of an event that happened in court. Each node would most likely have a different date, although it is possible that some could have the same date. I need to send someone an email 160 days after that date. So, say the node is created today and that date field has 1/1/2010 in it. I need the email to be sent on 6/10/2010 (160 days later). Are you suggesting to just create the email when the node is created and hold it until 06/10/2010? And this could be done with rules and cron? If I understand your response, I believe the first solution matches my requirements the best.

Thanks again for your help.

Mike

You got it all right!

itangalo's picture

If that is your needs, then solution one is the right way to go. One node, one e-mail.

The procedure to create the function you're looking for is roughly as follows:

  1. Create a rule set "send the 160 days e-mail". Make sure to have an argument "node" to be passed into the rule set.
  2. Add a rule (action) in the rule set that sends an e-mail with some pre-defined content. You will have great use of the Token module for including stuff like node body, link to the node, et cetera. (If you're not sending the e-mail to the node owner, or an e-mail address specified in the node, you'll have some extra work to get a dynamic address it into Rules. I've tried. A static e-mail address should be a piece of cake.)
  3. Create a triggered rule, activated on "new node created" (or perhaps node save). Make sure to add any important conditions (such as correct node type).
  4. Add a rule (action) to the triggered rule that schedules "send the 160 days e-mail". Pass along the saved node as argument.
  5. Schedule for the correct date. Here's the tricky part, 'cause you need to find a way to take field_your_date_field and add 160 days. It could just work right away with Tokens (like "[field_date_xyz] + 160 days"), but otherwise I would go for a line of PHP code.
  6. Give every scheduled e-mail task a unique ID, so you can identify and possibly modify it later on. A good idea would be "e-mail NID on day YY-MM-DD".

Save. Try it out.

If you want to go further, consider adding functionality to adjust the scheduled e-mail if the date changes. That would require a new rule set (delete-old-and-make-new-scheduling) and a new triggered rule (activate-on-content-changed).

You might also want to spend some time thinking about edge cases, such as someone typing a date three years ago. Should it send an e-mail right away, or none at all?

That's it, I think.
Rules, Rules Scheduler, Token. And cron.

Good luck!
//Johan Falk, NodeOne, Sweden

PS: If step 5 turns out to be difficult you might want to add another CCK field and have it automatically set to the previous date field + 160 days. It could be done with Computed Field, but also with Rules if you activate the forms support. Using the value in the new field for scheduling would be easy.

They have some solution here.

gagarine's picture

They have some solution here. http://groups.drupal.org/node/21377

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: