diff --git a/booking.module b/booking.module index 38cef3e..2b534c9 100644 --- a/booking.module +++ b/booking.module @@ -588,21 +588,21 @@ function booking_menu() { 'type' => MENU_LOCAL_ACTION, ); - $items['admin/booking/rooms/%/assign'] = array( + $items['admin/booking/rooms/old/%/assign'] = array( 'title' => 'Assign Rooms', 'description' => 'Assign attendees to rooms', 'page callback' => 'drupal_get_form', - 'page arguments' => array('booking_rooms_allocate_form', 3), + 'page arguments' => array('booking_rooms_allocate_form', 4), 'access arguments' => array('edit room allocations'), //'type' => MENU_NORMAL_ITEM, ); //use this for testing the autocomplete fields - $items['admin/booking/rooms/test/%/assign'] = array( - 'title' => 'Assign Rooms', - 'description' => 'Assign attendees to rooms', + $items['admin/booking/rooms/%/assign'] = array( + 'title' => 'Assign Attendees to Rooms', + 'description' => 'Assign attendees to rooms via autocomplete fields', 'page callback' => 'drupal_get_form', - 'page arguments' => array('booking_rooms_allocate_test_form', 4), + 'page arguments' => array('booking_rooms_allocate_test_form', 3), 'access arguments' => array('edit room allocations'), //'type' => MENU_NORMAL_ITEM, ); diff --git a/booking.test_rooms.inc b/booking.test_rooms.inc index 3f79415..2f106d9 100644 --- a/booking.test_rooms.inc +++ b/booking.test_rooms.inc @@ -178,26 +178,38 @@ function booking_rooms_allocate_test_form_submit($form, &$form_state) { //go through each bed foreach ($data as $bed_index => $person) { + //check for previous value + $previous_nid = 0; + $previous_nid = _booking_rooms_allocate_get_previous_value(&$form, $type, $room, $type . '[' . $room . '][' . $bed_index . ']'); + if (! empty($person)) { - //extract nid from $person using regex if (preg_match('/[\s\w,]+\s\[(\d+)\]/i', $person, $matches)) { //watchdog('booking_debug', "Processing room assignment for ID !id belonging to person !person ", array('!id' => $matches[1], '!person' => $person)); $nid = $matches[1]; - - //TODO: - //see if someone was previously assigned to this bed - //if someone was, then remove the mapping if it was a different person - //see if this person was already assigned to a different bed - //if true, then update their mapping to point to this bed - //otherwise, insert a new mapping for this person to this bed - _booking_rooms_allocate_test_form_submit_helper(&$room_mapping, $room, $type_id, $nid); + //handle the insert/update/deletes required + _booking_rooms_allocate_test_form_submit_helper(&$room_mapping, $room, $type_id, $bed_index, $nid, $previous_nid); } //parsed node id successfully } //empty bed check + elseif (empty($person) && $previous_nid > 0) + { + $message = t('Removing person !person previously in room id !room with bed index !index and bed type !type.', + array('!room' => $room, '!index' => $bed_index, '!person' => $previous_nid, '!type' => $type_id)); + watchdog('booking', $message); + drupal_set_message($message); + + db_delete('booking_room_mapping') + ->condition('booking_eventid', $event->eid) + ->condition('booking_nodeid', $previous_nid) + ->condition('booking_room_bedtype', $type_id) + ->condition('booking_roomid', $room) + ->execute(); + + } else { //TODO: Check if this bed used to have someone allocated @@ -212,21 +224,53 @@ function booking_rooms_allocate_test_form_submit($form, &$form_state) { /** * look through the previous form data and return the matching element */ -function _booking_rooms_allocate_get_previous_value(&$form, $element) { +function _booking_rooms_allocate_get_previous_value(&$form, $type, $room, $name) { - foreach($form['rooms']['#rows'] as $key => $value) + foreach($form['form']['rooms']['#rows'] as $key => $value) { - if ((! empty($value['data'])) && $value['data']['#id'] == $element){ - return $value['data']['#value']; + //watchdog('booking_debug', "
Room assignment checker for type !type in room !room:\n@info", array('!room' => $room, '!type' => $type, '@info' => print_r( $value, true))); + //return; + if ((!empty($value[$type]['data']['#value'])) && ($value[$type]['data']['#name'] == $name)) { + //watchdog('booking_debug', "Found correct room with room number !num and type !type", array('!num' => $room, '!type' => $type)); + + //found the correct element, extract the node id + $person = $value[$type]['data']['#value']; + if (preg_match('/[\s\w,]+\s\[(\d+)\]/i', $person, $matches)) { + return $matches[1]; + } } } + + //in case there was no matching value, return an empty string + return 0; } -function _booking_rooms_allocate_test_form_submit_helper(&$room_mapping, $room, $type_id, $nid) +/** + * function to update person with correct bed allocation and remove any previous allocation if necessary + */ +function _booking_rooms_allocate_test_form_submit_helper(&$room_mapping, $room, $type_id, $bed_index, $nid, $previous_nid) { global $event; $message = ""; + //remove any person previously defined for this bed 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 previous !person from this location.', + array('!room' => $room, '!index' => $bed_index, '!person' => $previous_nid)); + watchdog('booking', $message); + drupal_set_message($message); + + //look for an exact match, + //in case this person has moved to a different bed type during this form submission + db_delete('booking_room_mapping') + ->condition('booking_eventid', $event->eid) + ->condition('booking_nodeid', $previous_nid) + ->condition('booking_room_bedtype', $type_id) + ->condition('booking_roomid', $room) + ->execute(); + } + //if this person didn't previously have a room/bed mapping if (empty($room_mapping[$nid])) {