It would be really great to be able to have an event for the Rules module for "after book this resource" or similar. Alternatively could someone give me an idea of what hook I would use to grab the submitted node after I have clicked the "Book this resource" button so that I can run some other custom code before it moves on to the "URL to continue the booking" node.

What I am trying to achieve is to merge Availability Field and Drupal Commerce together to create a rental site. Rooms isn't quite flexible enough for me but I can get the results I am after with AC and DC, I just need to be able to save from one to the other. I would like to be able to either click the book now button and hook the event to then save the product to the cart at the same time. Or click the add to cart button and then save the dates back to Availability Field.

I was originally trying to use Rules with the "after product added to cart" event, but I can't access the node from there, only the product entity. I think going from Availability Field back to save the product to the cart might be easier.

Any help greatly appreciated. TIA

Comments

fietserwin’s picture

The "book this resource" does not fire any hook or event, it initiates a POST from the user's browser to a URL configured by yourself. This URL will typically be a webform but that is no necessity. I tend to compare the "book this resource" button and its accompanying form with an add to cart button: not a complete form as not all information that is typically needed can be entered. To complete a booking you will have to create the "checkout" (web)form.

Perhaps you can configure the booking formlet field to POST to the "add to cart" handling functionality from Drupal Commerce and hook in at that moment. The information posted in the body of the POST contains everything to identify the calendar and the entity (in a multilingual situation this does not need to be a 1-1 relation).

Code snippets to retrieve the calendar and entity (based on the info that is POSTed are available via the README.txt documentation of the booking formlet sub-module.

So far I've never worked with rules, though I know that some issues in this module's issue queue can be solved by adding support for it. So I am interested in adding it. Can you develop a first version?

fietserwin’s picture

Note: I still would like to try to merge Availability Calendars and Rooms (#1229920: [AC] Possible integration of availability_calendars and rooms). Perhaps we should look at the inflexibilities of Rooms (compared with this module) instead of trying to move this module in the direction of Rooms. But as rooms is already in development for over a year, I did not have a serious look at that yet.

fietserwin’s picture

Version: 7.x-3.2 » 7.x-4.x-dev
Category: support » feature

Changing into a feature request.

yaelsho’s picture

In case someone still looking for that-I needed something similar and use workaround:
Goal: Verify the user is not trying to reserve fully booked dates:

Rule setup:

event: Content is viewed

conditions:
1. Data comparison:
"Data to compare: [node:nid], Data value: your booking webform nid"
2. Execute custom PHP code to check if the selected dates are in fully booked state (in this case fully booked state (sid) = 3)
"
/*collect booking data from webfor url parameters, I'm using webform 7.4 and therefore the use of $_GET, I belive that in previous webform version need to use $_POSt instead */
$to=$_GET['to_iso']; /*arrival date*/
$from=$_GET['from_iso']; /*departue date*/
$cid=$_GET['cid']; /*calendar id*/

/*collect availability data in the required dates*/
$to=date(AC_ISODATE, strtotime($to));
$from=date(AC_ISODATE, strtotime($from));
$availability = array();
if (!empty($cid)) {
$availability = db_select('availability_calendar_availability')
->fields('availability_calendar_availability', array('date', 'sid'))
->condition('cid', $cid)
->condition('date', array($from, $to), 'BETWEEN')
->execute()
->fetchAllKeyed();
}

/*reset the return parameter to false*/
$ret=0;

/*change the return parameter in case at lease one requested dates is fully booked*/
foreach($availability as $paramName => $value){
if($value==3){
$ret=1;
break;
}
}
/*return the condition result: 0=non of the requested dates is fully booked, 1=at lease one of them is fully booked*/
return $ret;

Actions:
1. Page redirect to previous page (to the booking calendar page)
" return $_SERVER['HTTP_REFERER']; "
2. Show a message on the site - "At least one of the selected dates are already fully booked. You may select again available dates (fully booked dates are marked in red)."

For reference you may import the rule export below and try to modify it per your needs.
Rules export:

{ "rules_check_availability_post_booking_request" : {
"LABEL" : "check availability post booking request",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "php" ],
"ON" : [ "node_view" ],
"IF" : [
{ "data_is" : { "data" : [ "node:nid" ], "value" : "9" } },
{ "php_eval" : { "code" : "\/*collect webform data*\/\r\n$to=$_GET[\u0027to_iso\u0027];\r\n$from=$_GET[\u0027from_iso\u0027]; \r\n$cid=$_GET[\u0027cid\u0027]; \r\n\r\n\/*collect availability data in the required dates*\/\r\n $to=date(AC_ISODATE, strtotime($to));\r\n $from=date(AC_ISODATE, strtotime($from));\r\n $availability = array();\r\n if (!empty($cid)) {\r\n $availability = db_select(\u0027availability_calendar_availability\u0027)\r\n -\u003Efields(\u0027availability_calendar_availability\u0027, array(\u0027date\u0027, \u0027sid\u0027))\r\n -\u003Econdition(\u0027cid\u0027, $cid)\r\n -\u003Econdition(\u0027date\u0027, array($from, $to), \u0027BETWEEN\u0027)\r\n -\u003Eexecute()\r\n -\u003EfetchAllKeyed();\r\n }\r\n$ret=0;\r\n\/*check how many days in the request are low, high, peak season*\/\r\n\r\nforeach($availability as $paramName =\u003E $value){\r\n if($value==3){\t\r\n $ret=1;\r\n break;\r\n }\r\n}\r\nreturn $ret;\r\n\r\n\t" } }
],
"DO" : [
{ "redirect" : { "url" : "\u003C?php \r\nreturn $_SERVER[\u0027HTTP_REFERER\u0027];\r\n?\u003E" } },
{ "drupal_message" : {
"message" : "At least one of the selected dates are already fully booked. You may select again available dates (fully booked dates are marked in red).\r\n",
"type" : "error"
}
}
]
}
}

Regards, Yael

Collins405’s picture

Issue summary: View changes

I created this sandbox for rules integration, I would appreciate some testing

https://www.drupal.org/sandbox/chriscollins405/2747947