Updating three date fields from current date using Rules

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
TechnoTim2010's picture

Hi

I think I am more or less there,

I have a Drupal 7 Commerce site that needs to calculate three dates based upon the event of a customer buying a membership subscription product.

The basic logic is that based upon the current date (as this will be triggered when payment is received in full for the transaction) is that three user account fields are updated.
Membership Starts a Date type field
Membership Ends a Date type field
Invoice Date

The logic for the calculation of these dates can be done using php5.3's date() and relative date methods so

<?php $startdate = date('Y-m-d h:m:s',strtotime('first day of next month'));
$expirydate = date('Y-m-d h:m:s',strtotime('first monday of next month +1 year'));
$invoicedate = date('Y-m-d h:m:s',strtotime('first monday of next month'));?>

works in pure php and returns a timestamp which I then formatted. But I can just use the timestamp

But how to use this in rules.
I used 'Add a variable' to add three variables to views: membership_starts, membership_ends and invoice_date
I created a hook_rules_action_info function as follows:

<?php
/<strong>
* @
file
Rules code: actions, conditions and events.
*/

/</
strong>
* Implements
hook_rules_action_info().
*
*
mymodule specific code
*/
function
mymodule_rules_action_info() {
 
$actions = array(
// declare component action to calculate first invoice date
// calls a function to calculate date
   
'mymodule_action_calculate_dates' => array(
     
'label' => t('Calculate membersip and invoice Dates for Customer'), // Name displayed to admins
     
'group' => t('mymodule Specific'), // Used for grouping actions in select lists
     
'parameter' => array(
         
'membership_starts' => array(
           
'type' => 'Date',
           
'label' => t('The date the users  membership will start'),
            ),
         
'membership_ends' => array(
           
'type' => 'Date',
           
'label' => t('The date the users membership ends'),
            ),
         
'invoice_date' => array(
           
'type' => 'Date',
           
'label' => t('The date the user can be invoiced')
            ),
        ),
        ),
     );
     return
$actions;
}
/**
* Function to calculate the first membership start date for a new member,
* the membership expiry date for the new member and the date the memver should
* first be invoiced.
*/
function mymodule_action_calculate_dates () {
// takes now as confirmed paid date as that is when action is run
$startdate = date(strtotime('first day of next month'));
$startdate = format_date($startdate, 'medium');
$expirydate = date(strtotime('first monday of next month +1 year'));
$expirydate = format_date($expirydate, 'medium');
$invoicedate = date(strtotime('first monday of next month'));
$invoicedate = format_date($invoicedate, 'medium');

return array(
'membership_starts' => $startdate,
'membership_ends' => $expirydate,
 
'invoice_date' => $invoicedate,
  );
}
?>

But this produces the message

The data type of the configured argument does not match the parameters membership starts requirement.

I have tried everything I can and spent hours on IRC #drupal-support trying for a solution to no avail

What do I need to get the results in the format that the variables in rules will accept.

Please advise.

Tim

Comments

Solved

TechnoTim2010's picture

The solution was simpler than I thought.

I set the three fields simply using "set a data value", set the value simply as the relative date format available from php5.3 and onwards and set the target as the field name.

So for membership_starts the data value was "first day of next month" whilst the target was the field for the user of membership_starts. The inline help for set a data value seems to just suggest you can use the strtotime() solutions of "+1 day" but actually happily accepts any of the relative date formats allowed in data() or strtotime() as per
http://php.net/manual/en/datetime.formats.relative.php

This of course makes the hook_rules_action_info code I wrote in my custom code redundant, but I am happy that means there is no extra overhead as a result.

Tim

Rules

Group organizers

Group categories

Categories

Group notifications

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