Fixed logic in room allocation form
This commit is contained in:
@@ -763,7 +763,7 @@ function booking_rooms_allocate_form($node, &$form_state, $location_id) {
|
||||
|
||||
}
|
||||
|
||||
watchdog('booking', "<pre>Loading existing room allocations:\n@info</pre>", array('@info' => print_r( $room_mapping, true)));
|
||||
//watchdog('booking', "<pre>Loading existing room allocations:\n@info</pre>", array('@info' => print_r( $room_mapping, true)));
|
||||
|
||||
//attach the custom css
|
||||
$form['#attached']['css'] = array(
|
||||
@@ -926,10 +926,8 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
'booking_room_queenbed_p1' => 3,
|
||||
'booking_room_queenbed_p2' => 3,
|
||||
);
|
||||
|
||||
|
||||
|
||||
//watchdog('booking', "<pre>Room assignment submission:\n@info</pre>", array('@info' => print_r( $singlebed_ids, true)));
|
||||
//watchdog('booking', "<pre>Room assignment submission form state:\n@info</pre>", array('@info' => print_r( $form_state, true)));
|
||||
|
||||
//go through the different bed types
|
||||
foreach ($bed_inputs as $type => $type_id)
|
||||
@@ -947,46 +945,91 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
//go through each bed
|
||||
foreach ($value as $index => $nid)
|
||||
{
|
||||
//if this is actually a person to process
|
||||
//calculate the name of the field for the bed
|
||||
$bed_lookup = $type . '[' . $room . '][' . $index . ']';
|
||||
$previous_nid = 0;
|
||||
$message = "";
|
||||
|
||||
//firstly check if there was a different nid here before
|
||||
|
||||
//iterate the option input-types in the form array that made up the original form
|
||||
foreach ($form['form']['table']['#options'] as $option)
|
||||
{
|
||||
//if this one was defined and matches the bed we're looking at now
|
||||
if (!empty($option[$type]) && $option[$type]['data']['#name'] == $bed_lookup)
|
||||
{
|
||||
$previous_nid = $option[$type]['data']['#value'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//if there is a person now selected for this bed
|
||||
if ($nid > 0)
|
||||
{
|
||||
//this person didn't previously have a room/bed mapping
|
||||
//remove any person previously defined here that doesn't match what is now defined
|
||||
if ($previous_nid > 0 && $nid != $previous_nid)
|
||||
{
|
||||
$message = t('Bed allocation for room !room and bed index !index has changed. Removing !person from this location.',
|
||||
array('!room' => $room, '!index' => $index, '!person' => $previous_nid));
|
||||
watchdog('booking', $message);
|
||||
drupal_set_message($message);
|
||||
|
||||
db_delete('booking_room_mapping')
|
||||
->condition('booking_eventid', $event->eid)
|
||||
->condition('booking_nodeid', $previous_nid)
|
||||
->execute();
|
||||
|
||||
}
|
||||
|
||||
//if this person didn't previously have a room/bed mapping
|
||||
if (empty($room_mapping[$nid]))
|
||||
{
|
||||
|
||||
//Validate that there is capacity for the person to be allocated to this room
|
||||
if (_booking_room_capacity_check($room, $type_id))
|
||||
{
|
||||
drupal_set_message(t('Assigning person id !id to a type !type bed in room id !room.',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id)));
|
||||
|
||||
$result = db_insert('booking_room_mapping')
|
||||
->fields(array(
|
||||
'booking_roomid' => $room,
|
||||
'booking_eventid' => $event->eid,
|
||||
'booking_nodeid' => $nid,
|
||||
'booking_room_bedtype' => $type_id,
|
||||
))
|
||||
->execute();
|
||||
$message = t('Assigning person id !id to a type !type bed in room id !room.',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id));
|
||||
|
||||
//double check we haven't already allocated a bed during this submission
|
||||
$check = db_query("SELECT * FROM {booking_room_mapping} " .
|
||||
" WHERE booking_eventid = :eid AND booking_roomid = :rid " .
|
||||
" AND booking_room_bedtype = :type AND booking_nodeid = :nid",
|
||||
array(':eid' => $event->eid, ':rid' => $room, ':type' => $type_id,
|
||||
':nid' => $nid,
|
||||
))->fetchObject();
|
||||
|
||||
if (! $check)
|
||||
{
|
||||
$result = db_insert('booking_room_mapping')
|
||||
->fields(array(
|
||||
'booking_roomid' => $room,
|
||||
'booking_eventid' => $event->eid,
|
||||
'booking_nodeid' => $nid,
|
||||
'booking_room_bedtype' => $type_id,
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
else
|
||||
{
|
||||
$message .= t(' Except this person already exists.');
|
||||
}
|
||||
|
||||
}
|
||||
//no capacity available in this room
|
||||
else
|
||||
{
|
||||
drupal_set_message(
|
||||
t('Insufficient capacity to assign person id !id to a type !type bed in room id !room.',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id)
|
||||
), 'error'
|
||||
);
|
||||
//, 'error', FALSE);
|
||||
$message = t('No capacity to assign person id !id to a type !type bed in room id !room.',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//this person previously had a room mapping but to a different room
|
||||
elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_roomid != $room)
|
||||
{
|
||||
drupal_set_message(t('Changing person id !id from old room !oldroom to new room !room with type !type bed.',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id, '!oldroom' => $room_mapping[$nid]->booking_roomid)));
|
||||
|
||||
}
|
||||
//this person previously had a room mapping but to a different room
|
||||
elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_roomid != $room)
|
||||
{
|
||||
$message = t('Changing person id !id from old room !oldroom to new room !room with type !type bed.',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id,
|
||||
'!oldroom' => $room_mapping[$nid]->booking_roomid));
|
||||
|
||||
db_update('booking_room_mapping')
|
||||
->fields(array(
|
||||
'booking_roomid' => $room,
|
||||
@@ -996,13 +1039,14 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
->condition('booking_nodeid', $nid)
|
||||
->execute();
|
||||
|
||||
}
|
||||
//this person previously had a room mapping but to a different bed type in the same room
|
||||
elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_room_bedtype != $type_id)
|
||||
{
|
||||
drupal_set_message(t('Changing person id !id in room !room to new bed type type !type .',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id, '!oldroom' => $room_mapping[$nid]->booking_roomid)));
|
||||
|
||||
}
|
||||
//this person previously had a room mapping but to a different bed type in the same room
|
||||
elseif ((!empty($room_mapping[$nid])) && $room_mapping[$nid]->booking_room_bedtype != $type_id)
|
||||
{
|
||||
$message = t('Changing person id !id in room !room to new bed type type !type .',
|
||||
array('!id' => $nid, '!room' => $room, '!type' => $type_id,
|
||||
'!oldroom' => $room_mapping[$nid]->booking_roomid));
|
||||
|
||||
db_update('booking_room_mapping')
|
||||
->fields(array(
|
||||
'booking_roomid' => $room,
|
||||
@@ -1012,19 +1056,28 @@ function booking_rooms_allocate_form_submit($form, &$form_state) {
|
||||
->condition('booking_nodeid', $nid)
|
||||
->execute();
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
//drupal_set_message(t('Person id !id already has some other room allocation.',
|
||||
// array('!id' => $nid, '!room' => $room, '!type' => $type_id)));
|
||||
}
|
||||
|
||||
} //valid node id check
|
||||
}
|
||||
//log the result
|
||||
watchdog('booking', $message);
|
||||
drupal_set_message($message);
|
||||
}
|
||||
//this bed has no ID assigned now, but did it have something before?
|
||||
elseif ($nid == 0 && $previous_nid > 0)
|
||||
{
|
||||
$message = t('Removing person !person previously in room id !room with bed index !index.',
|
||||
array('!room' => $room, '!index' => $index, '!person' => $option[$type]['data']['#value']));
|
||||
watchdog('booking', $message);
|
||||
drupal_set_message($message);
|
||||
|
||||
db_delete('booking_room_mapping')
|
||||
->condition('booking_eventid', $event->eid)
|
||||
->condition('booking_nodeid', $previous_nid)
|
||||
->execute();
|
||||
|
||||
} //end node checking
|
||||
} //each bed
|
||||
} //each room
|
||||
} //each bed type
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user