diff --git a/booking.rooms.inc b/booking.rooms.inc index 2a62386..905d87f 100644 --- a/booking.rooms.inc +++ b/booking.rooms.inc @@ -300,6 +300,25 @@ function booking_room_edit_form_validate($form, &$form_state) { 3 => 'booking_room_queenbeds', ); + //watchdog('booking', "
Room Number Form State:\n@info
", array('@info' => print_r( $form_state['values'], true))); + + //no need to validate if we're just removing the mapping + //op won't be defined in the form if it's just receiving the ajax callback, so check that is defined first + if (isset($form_state['values']['op']) && $form_state['values']['op'] == 'Remove') + { + return; + } + else + { + if (! _booking_room_capacity_check($values['booking_room_location_id'], $values['booking_room_number'], $values['booking_room_bedtype'])) + { + form_set_error('booking_room_number', + t('Unfortunately there are no beds available of the type specified in the room.') + ); + } + } + + /* //get the specific room definition from the database $details = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid AND booking_room_number = :num", array(':lid' => $values['booking_room_location_id'], ':num' => $values['booking_room_number']))->fetchObject(); @@ -323,7 +342,48 @@ function booking_room_edit_form_validate($form, &$form_state) { ); } } + */ +} +/** + * Validate there is available capacity for the specified room and bed type + * @return TRUE if there is sufficient capacity, otherwise FALSE + */ +function _booking_room_capacity_check($room_location_id, $room_number, $room_bedtype) { + global $event; + + $bed_inputs = array( + 1 => 'booking_room_singlebeds', + 2 => 'booking_room_doublebeds', + 3 => 'booking_room_queenbeds', + ); + + //make sure the value exists before validating it + if (!empty($room_bedtype)) + { + //get the specific room definition from the database + $details = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid AND booking_room_number = :num", + array(':lid' => $room_location_id, ':num' => $room_number))->fetchObject(); + + //get all person-to-room mappings relating to this room and bed type + $mappings = db_query("SELECT count(*) as num FROM {booking_room_mapping} " . + "WHERE booking_eventid = :eid AND booking_roomid = :rid AND booking_room_bedtype = :type", + array(':eid' => $event->eid, ':rid' => $details->rid, ':type' => $room_bedtype))->fetchObject(); + + $db_field = $bed_inputs[$room_bedtype]; + $max_beds = $details->$db_field; + //check that there is sufficient capacity to allocate another person to this room and bed type + if ($mappings->num < $max_beds) + { + watchdog('booking','Sufficient capacity is available in location !id, room !room, with bed type !type. !count beds remaining of this type', + array('!id' => $room_location_id, '!room' => $room_number, '!type' => $room_bedtype, '!count' => $max_beds - $mappings->num)); + + return true; + } + } + + return false; + }