array('data' => t('First Name'), 'field' => 'p.booking_firstname'), 'booking_lastname' => array('data' => t('Last Name'), 'field' => 'p.booking_lastname', 'sort' => 'asc'), 'booking_gender' => array('data' => t('Gender'), 'field' => 'p.booking_gender'), 'booking_age' => array('data' => t('Age'), 'field' => 'p.booking_dob'), 'booking_married' => array('data' => t('Married?'), 'field' => 'p.booking_married'), 'booking_room_request' => array('data' => t('Requested Roommates'), 'field' => 'p.booking_room_mate1'), 'booking_roomlocation' => array('data' => t('Room Location'), 'field' => 'r.booking_room_location_id'), 'booking_room_num' => array('data' => t('Room Number'), 'field' => 'r.booking_room_number'), 'booking_room_bedtype' => array('data' => t('Bed Type'), 'field' => 'm.booking_room_bedtype'), 'booking_room_edit' => array('data' => t('Edit')), ); $query = db_select('booking_person', 'p'); $query->leftJoin('booking_room_mapping', 'm', 'm.booking_nodeid = p.nid'); $query->leftJoin('booking_room_definition', 'r', 'r.rid = m.booking_roomid'); $query->leftJoin('booking_room_locations', 'l', 'l.lid = r.booking_room_location_id'); $db_and = db_and(); $db_and->condition('p.booking_eventid', $event->eid, '='); $db_and->condition('p.booking_status', 1, '='); $query->condition($db_and); $query->fields('p')->fields('m')->fields('r')->fields('l'); $table_sort = $query->extend('TableSort')->orderbyHeader($header); $result = $table_sort->execute(); foreach($result as $data) { $location_link = $data->booking_roomlocation_descrip . l( t(' (Edit)'), t('admin/booking/rooms/!id/assign', array('!id' => $data->booking_room_location_id)), array('query' => drupal_get_destination()) ); $rows[] = array( 'data' => array( $data->booking_firstname, $data->booking_lastname, $data->booking_gender == 'M' ? 'Male' : 'Female', _booking_get_age_years($data->booking_dob), $data->booking_married == 'Y' ? 'Yes' : 'No', $data->booking_room_mate1, $data->booking_room_location_id > 0 ? $location_link : '', $data->booking_room_number, _booking_room_bedtype_lookup($data->booking_room_bedtype), l( t('Change Room'), t('admin/booking/!id/edit-room', array('!id' => $data->nid)), array('query' => drupal_get_destination()) ), ), ); } $prefix = t("
Manually assign/update room allocation for !first !last.
Room Edit selected room location:\n@info", array('@info' => print_r( $selected_room_location, true))); $selected_room_num = isset($form_state['values']['booking_room_number']) ? $form_state['values']['booking_room_number'] : $person->booking_room_number; //watchdog('booking', "
Room Edit selected room number:\n@info", array('@info' => print_r( $selected_room_num, true))); $form['booking_room_location_id'] = array( '#type' => 'select', '#title' => t('Room Location'), '#options' => $location_options, '#default_value' => $person->booking_room_location_id, '#ajax' => array( 'event' => 'change', 'wrapper' => 'booking_roomnum_wrapper', 'callback' => 'booking_roomnum_ajax_callback', ), ); $form['booking_room_number'] = array( '#type' => 'select', '#title' => t('Room Number'), //'#description' => t(''), '#prefix' => '
Room Edit ajax callback:\n@info", array('@info' => print_r( $form, true))); return $form['form']['booking_room_number']; } /** * Function to return the updated form element booking_room_bedtype for booking_room_edit_form() */ function booking_bedtype_ajax_callback($form, $form_state) { //watchdog('booking', "
Room Edit ajax callback:\n@info", array('@info' => print_r( $form, true))); return $form['form']['booking_room_bedtype']; } /** * Function to calculate appropriate range of room numbers for ajax enabled form booking_room_edit_form() * @param $location_id - the room location id to look at * @return array containing the room numbers for this location */ function _booking_get_roomedit_roomnum_options($selected) { $room_options = array(); $room_options[] = "--"; $room_query = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid", array(':lid' => $selected)); foreach($room_query as $room) { $room_options[$room->booking_room_number] = $room->booking_room_number; } //watchdog('booking', "
Room Number Options:\n@info", array('@info' => print_r( $room_options, true))); return $room_options; } /** * Function to calculate appropriate bed options for ajax enabled form booking_room_edit_form() * @param $location_id - the room location id to look at * @param $room_num - the room number to get the bed details from * @return array containing the bed options for this specific room */ function _booking_get_roomedit_bedtype_options($location_id, $room_num) { $bed_options = array(); $bed_options[] = "--"; $details = db_query("SELECT * FROM {booking_room_definition} WHERE booking_room_location_id = :lid AND booking_room_number = :num", array(':lid' => $location_id, ':num' => $room_num))->fetchObject(); //check we got a response from the query if ($details) { //go through the bed options if ($details->booking_room_singlebeds > 0) { $bed_options[1] = "Single"; } if ($details->booking_room_doublebeds > 0) { $bed_options[2] = "Double"; } if ($details->booking_room_queenbeds > 0) { $bed_options[3] = "Queen"; } } //watchdog('booking', "
Room Number Options:\n@info", array('@info' => print_r( $bed_options, true))); return $bed_options; } /** * Validate the submission to update allocated room and bed for a person */ function booking_room_edit_form_validate($form, &$form_state) { //TODO: turn this into a generalised helper function, and use it for validating booking_rooms_allocate_form() also global $event; $values = $form_state['input']; //watchdog('booking', "
Room Number Form State:\n@info", 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 (!isset($values['booking_room_location_id']) || !isset($values['booking_room_number']) || !isset($values['booking_room_bedtype'])) { form_set_error('form', t('Invalid configuration detected. Please ensure a room location, room number and bed type are all set.') ); return; } //check if the location but not room number has been set if ($values['booking_room_location_id'] > 0 && $values['booking_room_number'] == 0) { //return here, since there is nothing we need to validate yet return; } //get the room definition from the database so we can verify there is capacity $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(); //perform a check to see if this person is already allocated to this bed $check = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid AND booking_nodeid = :nid", array(':eid' => $event->eid, ':nid' => $values['personid']))->fetchObject(); if ($check && ($check->booking_roomid == $details->rid && $check->booking_room_bedtype == $values['booking_room_bedtype'])) { //drupal_set_message('Not validating room capacity because this person is already allocated this bed in this room.'); } //there was no existing mapping for this person, so check that there's capacity in this room elseif (! _booking_room_capacity_check($details->rid, $values['booking_room_bedtype'], $details)) { form_set_error('booking_room_number', t('Unfortunately there are no beds available of the type specified in the room.') ); } } } /** * Process the submission to update allocated room and bed for a person */ function booking_room_edit_form_submit($form, &$form_state) { global $event; $values = $form_state['input']; $redirect_path = "admin/config/booking/rooms"; //check if we should remove the room allocation if ($form_state['values']['op'] == 'Remove') { $message = t("Removing id !nid from room number !number in location !location", array('!nid' => $values['personid'], '!location' => $values['booking_room_location_id'], '!number' => $values['booking_room_number']) ); watchdog('booking', $message); drupal_set_message($message, 'status', FALSE); db_delete('booking_room_mapping') ->condition('booking_eventid', $event->eid) ->condition('booking_nodeid', $values['personid']) ->execute(); //return; } //otherwise, continue with adding/updating the room allocation else { $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(); $check = db_query("SELECT * FROM {booking_room_mapping} WHERE booking_eventid = :eid AND booking_nodeid = :nid", array(':eid' => $event->eid, ':nid' => $values['personid']))->fetchObject(); //this person already exists in mapping table if ($check) { $message = t("Updating person id !nid to room id !id with bed type !type.", array('!nid' => $values['personid'], '!id' => $details->rid, '!type' => _booking_room_bedtype_lookup($values['booking_room_bedtype']), '!number' => $values['booking_room_number']) ); //there is an existing mapping to update $result = db_update('booking_room_mapping') ->fields(array( 'booking_roomid' => $details->rid, 'booking_room_bedtype' => $values['booking_room_bedtype'], )) ->condition('mid', $check->mid) //->condition('booking_eventid', $event->eid) //->condition('booking_nodeid', $values['personid']) ->execute(); } //create a new record in the mapping table else { $message = t("Allocating person id !nid to room id !id with bed type !type.", array('!nid' => $values['personid'], '!id' => $details->rid, '!type' => _booking_room_bedtype_lookup($values['booking_room_bedtype']), '!number' => $values['booking_room_number']) ); //create a new room mapping for this person $result = db_insert('booking_room_mapping') ->fields(array( 'booking_roomid' => $details->rid, 'booking_eventid' => $event->eid, 'booking_nodeid' => $values['personid'], 'booking_room_bedtype' => $values['booking_room_bedtype'], )) ->execute(); } watchdog('booking', $message); drupal_set_message($message, 'status', FALSE); } //end operation check $form_state['redirect'] = $redirect_path; } /** * Function for generating report of room allocations in a specific location */ function booking_rooms_view_form($node, &$form_state, $location_id) { global $event; $rows = array(); $form = array(); //verify that $location_id is a number if (! preg_match('/^[0-9]+$/', $location_id)) { drupal_set_message("Error: Invalid room location ID '" . $location_id . "' supplied. Unable to allocate rooms.", 'error', FALSE); drupal_goto('admin/booking/rooms'); return ""; } //@todo reduce the number of queries by using some joins $location_description = db_query("SELECT booking_roomlocation_descrip FROM {booking_room_locations} where lid = :id", array(':id' => $location_id)) ->fetchObject(); //use the query result to include a human friendly group name $prefix = t("
Room assignment report rows:\n@info", array('@info' => print_r( $rows, true))); $result = array( '#attached' => array( 'css' => array(drupal_get_path('module', 'booking') . '/booking.css') ), 'first_para' => array( '#type' => 'markup', '#markup' => $prefix, ), 'table' => array( '#theme' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('id' => 'sort-table'), '#empty' => t('No room allocations found for this location.'), ) ); return $result; } /** * Function for generating a name relating to a node id * @param $attendees - an associative array containing the possible attendees * @param $nid - the node id of the person for which to return the formatted string * @return string containing first name and last name relating to node id */ function _booking_rooms_view_formatperson(&$attendees, $nid) { $output = "Empty"; if ($nid > 0 && !empty($attendees[$nid])) { $output = $attendees[$nid]->booking_firstname . " " . $attendees[$nid]->booking_lastname; } return $output; }