diff --git a/booking.test_rooms.inc b/booking.test_rooms.inc index 166cf1d..3f79415 100644 --- a/booking.test_rooms.inc +++ b/booking.test_rooms.inc @@ -53,12 +53,6 @@ function booking_rooms_allocate_test_form($node, &$form_state, $location_id) { //query for room definitions $room_query = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid ORDER BY CAST(booking_room_number as SIGNED INTEGER) ASC", array(':lid' => $location_id)); - - //query for attendees - $query = db_query("SELECT nid, booking_firstname, booking_lastname, booking_gender, booking_dob, booking_partner_id " . - " FROM {booking_person} " . - " WHERE booking_event_id = :eid and (booking_status=1 or booking_status=5) order by booking_lastname, booking_firstname", - array(':eid' => $event->eid)); //define the table header $header = array ( @@ -151,11 +145,14 @@ function booking_rooms_allocate_test_form_submit($form, &$form_state) { global $event; $values = $form_state['input']; - watchdog('booking_debug', "
Room assignment test submission form state:\n@info", array('@info' => print_r( $values, true))); - + watchdog('booking_debug', "
Room assignment test submission form :\n@info", array('@info' => print_r( $form, true))); + //query for existing room allocations - $room_mapping_query = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid", array(':eid' => $event->eid)); - $room_mapping = $room_mapping_query->fetchAllAssoc('booking_nodeid'); + $query = db_select('booking_person', 'p'); + $query->leftJoin('booking_room_mapping', 'm', 'm.booking_nodeid = p.nid'); + $query->condition('p.booking_event_id', $event->eid, '='); + $query->fields('p', array('booking_lastname', 'booking_firstname'))->fields('m'); + $room_mapping = $query->execute()->fetchAllAssoc('booking_nodeid'); $bed_inputs = array( 'booking_room_singlebed' => 1, @@ -173,7 +170,7 @@ function booking_rooms_allocate_test_form_submit($form, &$form_state) { if (empty($values[$type])) continue; - watchdog('booking_debug', "
Room assignment submission for !type:\n@info", array('!type' => $type, '@info' => print_r( $values[$type], true))); + //watchdog('booking_debug', "
Room assignment submission for !type:\n@info", array('!type' => $type, '@info' => print_r( $values[$type], true))); //go through each room foreach($values[$type] as $room => $data) @@ -182,21 +179,142 @@ function booking_rooms_allocate_test_form_submit($form, &$form_state) { foreach ($data as $bed_index => $person) { if (! empty($person)) { - watchdog('booking_debug', "
Processing room assignment for person:\n@info", array('@info' => print_r( $person, true))); - //TODO: //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); + + } //parsed node id successfully - //run query to see if someone was previously assigned to this bed - //if someone was, then remove the mapping if it was a different person - //add a new mapping for this person to this bed + } //empty bed check + else + { + //TODO: Check if this bed used to have someone allocated + //and if so, remove their mapping } + } //next bed + } //next room + } //next bed type +} //end function + + +/** + * look through the previous form data and return the matching element + */ +function _booking_rooms_allocate_get_previous_value(&$form, $element) { + + foreach($form['rooms']['#rows'] as $key => $value) + { + if ((! empty($value['data'])) && $value['data']['#id'] == $element){ + return $value['data']['#value']; + } + } +} + +function _booking_rooms_allocate_test_form_submit_helper(&$room_mapping, $room, $type_id, $nid) +{ + global $event; + $message = ""; + + //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)) + { + $message = t('Assigning person id !id to a type !type bed in room id !room.', + array('!id' => $nid, '!room' => $room, '!type' => $type_id)); - } //next bed - } //next room - } //next bed type -} //end function + //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(); + } + //this person has already been inserted during this form submission + //so don't add them in twice + else + { + $message .= t(' Except this person already exists.'); + } + + } + //no capacity available in this room + else + { + $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) + { + $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, + 'booking_room_bedtype' => $type_id, + )) + ->condition('booking_eventid', $event->eid) + ->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) + { + $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, + 'booking_room_bedtype' => $type_id, + )) + ->condition('booking_eventid', $event->eid) + ->condition('booking_nodeid', $nid) + ->execute(); + + } + + + //log the result if there was one + if ($message !== "") + { + watchdog('booking', $message); + drupal_set_message($message); + } + +} + /** * function to generate table rows for each single bed defined in this room