From d096b0d5ab2dd6a313668061166146420fe7735a Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Wed, 25 Jun 2014 22:48:19 +1000 Subject: [PATCH] Fixed logic in room allocation form --- booking.rooms.inc | 153 ++++++++++++++++++++++++++++++--------------- booking.tokens.inc | 54 ++++++++-------- 2 files changed, 130 insertions(+), 77 deletions(-) diff --git a/booking.rooms.inc b/booking.rooms.inc index 06330ff..e438ce2 100644 --- a/booking.rooms.inc +++ b/booking.rooms.inc @@ -763,7 +763,7 @@ function booking_rooms_allocate_form($node, &$form_state, $location_id) { } - watchdog('booking', "
Loading existing room allocations:\n@info
", array('@info' => print_r( $room_mapping, true))); + //watchdog('booking', "
Loading existing room allocations:\n@info
", 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', "
Room assignment submission:\n@info
", array('@info' => print_r( $singlebed_ids, true))); + //watchdog('booking', "
Room assignment submission form state:\n@info
", 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 - } /** diff --git a/booking.tokens.inc b/booking.tokens.inc index 2d9256f..01040b1 100644 --- a/booking.tokens.inc +++ b/booking.tokens.inc @@ -323,7 +323,7 @@ $booking_registration_intro_text = variable_get('booking_registration_intro_text /*Text for emails*/ $form['emails'] = array( '#type' => 'fieldset', - '#title' => 'Built-In Email Text Definitions', + '#title' => 'Built-In Workflow Email Definitions', '#collapsible' => TRUE, '#collapsed' => TRUE, ); @@ -355,32 +355,6 @@ $booking_registration_intro_text = variable_get('booking_registration_intro_text '#description' => t(''), '#default_value' => variable_get('booking_email_waitinglist_text', $booking_email_waitinglist_text), ); - $form['emails']['booking_email_travel_required_subject'] = array ( - '#type' => 'textfield', - '#title' => t('Subject line for email requesting attendee to complete the travel form'), - '#size' => 150, - '#maxlength' => 300, - '#default_value' => variable_get('booking_email_travel_required_subject','[booking:eventname] Travel Details Required'), - ); - $form['emails']['booking_email_travel_required_text'] = array( - '#title' => t('Email text requesting attendee to complete the travel form.'), - '#type' => 'textarea', - '#description' => t(''), - '#default_value' => variable_get('booking_email_travel_required_text', ''), - ); - $form['emails']['booking_email_travel_complete_subject'] = array ( - '#type' => 'textfield', - '#title' => t('Subject line for email indicating a person has completed the travel form'), - '#size' => 150, - '#maxlength' => 300, - '#default_value' => variable_get('booking_email_travel_complete_subject','[booking:eventname] Travel Details Received'), - ); - $form['emails']['booking_email_travel_complete_text'] = array( - '#title' => t('Email text to indicate a person has completed the travel form.'), - '#type' => 'textarea', - '#description' => t(''), - '#default_value' => variable_get('booking_email_travel_complete_text', ''), - ); $form['emails']['booking_email_paymentoutstanding_text'] = array( '#title' => t('Email text to send a person reminding them of how much they owe'), '#type' => 'textarea', @@ -427,7 +401,33 @@ $booking_registration_intro_text = variable_get('booking_registration_intro_text '#type' => 'textarea', '#description' => t(''), '#default_value' => variable_get('booking_email_missedpayment', ''), + ); + $form['emails']['booking_email_travel_required_subject'] = array ( + '#type' => 'textfield', + '#title' => t('Subject line for email requesting attendee to complete the travel form'), + '#size' => 150, + '#maxlength' => 300, + '#default_value' => variable_get('booking_email_travel_required_subject','[booking:eventname] Travel Details Required'), ); + $form['emails']['booking_email_travel_required_text'] = array( + '#title' => t('Email text requesting attendee to complete the travel form.'), + '#description' => t('This email will be sent from the !email email address', array('!email' => variable_get('booking_logistics_email'))), + '#type' => 'textarea', + '#default_value' => variable_get('booking_email_travel_required_text', ''), + ); + $form['emails']['booking_email_travel_complete_subject'] = array ( + '#type' => 'textfield', + '#title' => t('Subject line for email indicating a person has completed the travel form'), + '#size' => 150, + '#maxlength' => 300, + '#default_value' => variable_get('booking_email_travel_complete_subject','[booking:eventname] Travel Details Received'), + ); + $form['emails']['booking_email_travel_complete_text'] = array( + '#title' => t('Email text to indicate a person has completed the travel form.'), + '#description' => t('This email will be sent from the !email email address', array('!email' => variable_get('booking_logistics_email'))), + '#type' => 'textarea', + '#default_value' => variable_get('booking_email_travel_complete_text', ''), + ); /*Text for emails*/ $form['custom-emails'] = array(