Generalising room capacity validation

This commit is contained in:
2014-06-03 22:52:41 +10:00
parent 399b0fa29f
commit a04fa3d4e0

View File

@@ -300,6 +300,25 @@ function booking_room_edit_form_validate($form, &$form_state) {
3 => 'booking_room_queenbeds', 3 => 'booking_room_queenbeds',
); );
//watchdog('booking', "<pre>Room Number Form State:\n@info</pre>", 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 //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", $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(); 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;
} }