366 lines
12 KiB
PHP
366 lines
12 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Functions to handle room allocation and administration for the booking module
|
|
*/
|
|
|
|
/**
|
|
* Function to define the form for configuring room definitions and also display existing room definitions
|
|
*/
|
|
function booking_rooms_define_form() {
|
|
global $event;
|
|
$form = array();
|
|
$bedcount_options = array();
|
|
|
|
//create the options array for bed counts
|
|
for ($i = 0; $i <= 15; $i++)
|
|
$bedcount_options[$i] = $i;
|
|
|
|
//define the form for adding to the room definitions
|
|
|
|
$form[] = array (
|
|
'first_heading' => array (
|
|
'#type' => 'markup',
|
|
'#markup' => "<h2>Add Room</h2>",
|
|
),
|
|
);
|
|
|
|
$form['booking_room_location_id'] = array(
|
|
'#type' => 'select',
|
|
'#title' => t('Choose room location'),
|
|
'#options' => _booking_room_location_lookup(),
|
|
);
|
|
$form['booking_room_number'] = array (
|
|
'#type' => 'textfield',
|
|
'#title' => t('Specify room number'),
|
|
'#size' => 5,
|
|
'#maxlength' => 10,
|
|
'#default_value' => !empty($data->booking_room_number) ? $data->booking_room_number : '',
|
|
);
|
|
$form['booking_room_singlebeds'] = array (
|
|
'#type' => 'select',
|
|
'#title' => t('Specify number of single beds'),
|
|
'#options' => $bedcount_options,
|
|
'#default_value' => !empty($data->booking_room_singlebeds) ? $data->booking_room_singlebeds : '',
|
|
);
|
|
$form['booking_room_doublebeds'] = array (
|
|
'#type' => 'select',
|
|
'#title' => t('Specify number of double beds'),
|
|
'#options' => $bedcount_options,
|
|
'#default_value' => !empty($data->booking_room_doublebeds) ? $data->booking_room_doublebeds : '',
|
|
);
|
|
$form['booking_room_queenbeds'] = array (
|
|
'#type' => 'select',
|
|
'#title' => t('Specify number of queen beds'),
|
|
'#options' => $bedcount_options,
|
|
'#default_value' => !empty($data->booking_room_queenbeds) ? $data->booking_room_queenbeds : '',
|
|
);
|
|
$form['booking_room_ensuite'] = array(
|
|
'#type' => 'checkbox',
|
|
'#title' => t('Tick if this room has an ensuite'),
|
|
'#default_value' => (!empty($data->booking_room_ensuite) && $data->booking_room_ensuite == 'Y') ? 1 : 0,
|
|
);
|
|
$form['submit'] = array
|
|
(
|
|
'#type' => 'submit',
|
|
'#value' => t('Add Room'),
|
|
);
|
|
|
|
//display the existing room definitions
|
|
$form[] = array (
|
|
'first_heading' => array (
|
|
'#type' => 'markup',
|
|
'#markup' => "<h2>Room Definitions</h2>",
|
|
),
|
|
);
|
|
|
|
$header = array (
|
|
'booking_room_location_description' => t('Room Location'),
|
|
'booking_room_number' => t('Room Number'),
|
|
'booking_room_singlebeds' => t('Number Single Beds'),
|
|
'booking_room_doublebeds' => t('Number Double Beds'),
|
|
'booking_room_queenbeds' => t('Number Queen Beds'),
|
|
'booking_room_ensuite' => t('Ensuite?'),
|
|
);
|
|
|
|
$result = db_query("SELECT * from {booking_room_definition}");
|
|
|
|
foreach($result as $data)
|
|
{
|
|
$options[$data->rid] = array
|
|
(
|
|
'booking_room_location_description' => _booking_room_location_lookup($data->booking_room_location_id),
|
|
'booking_room_number' => $data->booking_room_number,
|
|
'booking_room_singlebeds' => $data->booking_room_singlebeds,
|
|
'booking_room_doublebeds' => $data->booking_room_doublebeds,
|
|
'booking_room_queenbeds' => $data->booking_room_queenbeds,
|
|
'booking_room_ensuite' => $data->booking_room_ensuite == 'Y' ? 'Yes' : 'No',
|
|
);
|
|
}
|
|
|
|
$form['table'] = array (
|
|
'#type' => 'tableselect',
|
|
'#header' => $header,
|
|
'#options' => $options,
|
|
'#multiple' => false,
|
|
);
|
|
|
|
/*
|
|
$form['Delete'] = array
|
|
(
|
|
'#type' => 'submit',
|
|
'#value' => t('Delete'),
|
|
);
|
|
*/
|
|
|
|
return array (
|
|
'form' => $form,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Process the form for adding room definitions
|
|
*/
|
|
function booking_rooms_define_form_submit($form, &$form_state) {
|
|
global $event;
|
|
$values = $form_state['input'];
|
|
|
|
if ($form_state['values']['op'] == 'Add Room')
|
|
{
|
|
db_insert('booking_room_definition')
|
|
->fields(array(
|
|
'booking_room_location_id' => $values['booking_room_location_id'],
|
|
'booking_room_number' => $values['booking_room_number'],
|
|
'booking_room_singlebeds' => $values['booking_room_singlebeds'],
|
|
'booking_room_doublebeds' => $values['booking_room_doublebeds'],
|
|
'booking_room_queenbeds' => $values['booking_room_queenbeds'],
|
|
'booking_room_ensuite' => $values['booking_room_ensuite'] == 1 ? 'Y' : 'N',
|
|
))
|
|
->execute();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function for allocating rooms
|
|
*/
|
|
function booking_rooms_allocate_form($node, &$form_state) {
|
|
global $event;
|
|
$form = array();
|
|
$attendee_select = array();
|
|
$options = array();
|
|
$counter = 0;
|
|
|
|
//make a list of all attendees
|
|
$attendee_select[] = '';
|
|
|
|
$query = db_query("SELECT nid, booking_firstname, booking_lastname, booking_gender FROM {booking_person} " .
|
|
"where booking_event_id = :eid and booking_status=1 order by booking_lastname, booking_firstname",
|
|
array(':eid' => $event->eid));
|
|
|
|
foreach($query as $row)
|
|
$attendee_select[$row->nid] = $row->booking_firstname . ' ' . $row->booking_lastname . ' ['. $row->booking_gender . ']';
|
|
|
|
//query for room definitions
|
|
$room_query = db_query("SELECT * FROM {booking_room_definition}");
|
|
|
|
//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->fetchAll();
|
|
|
|
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(
|
|
drupal_get_path('module', 'booking') . '/booking.css',
|
|
);
|
|
|
|
//define the header
|
|
$header = array (
|
|
'booking_room_location' => array('data' => t('Room Location'), 'field' => 'booking_room_location_id'),
|
|
'booking_room_number' => array('data' => t('Room Number')),
|
|
'booking_room_singlebed' => array('data' => t('Single Bed')),
|
|
'booking_room_doublebed_p1' => array('data' => t('Double Bed Person 1')),
|
|
'booking_room_doublebed_p2' => array('data' => t('Double Bed Person 2')),
|
|
'booking_room_queenbed_p1' => array('data' => t('Queen Bed Person 1')),
|
|
'booking_room_queenbed_p2' => array('data' => t('Queen Bed Person 2')),
|
|
);
|
|
|
|
$default_row = array();
|
|
$default_row['booking_room_location'] = "";
|
|
$default_row['booking_room_number'] = "";
|
|
$default_row['booking_room_singlebed'] = "";
|
|
$default_row['booking_room_doublebed_p1'] = "";
|
|
$default_row['booking_room_doublebed_p2'] = "";
|
|
$default_row['booking_room_queenbed_p1'] = "";
|
|
$default_row['booking_room_queenbed_p2'] = "";
|
|
|
|
foreach ($room_query as $data)
|
|
{
|
|
//load the existing bed mappings for this room
|
|
$existing_beds = array();
|
|
for ($i = 1; $i <= 3; $i++)
|
|
{
|
|
foreach ($room_mapping as $mapping)
|
|
{
|
|
if ($mapping->booking_roomid == $data->rid && $mapping->booking_room_bedtype == $i)
|
|
{
|
|
$existing_beds[$i][] = $mapping->booking_nodeid;
|
|
}
|
|
}
|
|
}
|
|
|
|
watchdog('booking', "<pre>Existing bed mappings:\n@info</pre>", array('@info' => print_r( $existing_beds, true)));
|
|
|
|
|
|
//create a row that contains just the room location and number
|
|
$row = _booking_clone_array($default_row);
|
|
$row['booking_room_location'] = _booking_room_location_lookup($data->booking_room_location_id);
|
|
$row['booking_room_number'] = $data->booking_room_number;
|
|
$row['#attributes'] = array('id' => array("new-group-row"));
|
|
$options[$counter++] = $row;
|
|
|
|
//create an additional row for each single bed
|
|
for ($i = 0; $i < $data->booking_room_singlebeds; $i++)
|
|
{
|
|
//retrieve the default value if one exists
|
|
$default = (!empty($existing_beds[1][$i])) ? $existing_beds[1][$i] : 0;
|
|
|
|
$row = _booking_clone_array($default_row);
|
|
$row['booking_room_singlebed'] = array('data' => array(
|
|
'#type' => 'select',
|
|
'#options' => $attendee_select,
|
|
'#name' => 'booking_room_singlebed[' . $data->rid . '][' . $i . ']',
|
|
'#value' => $default,
|
|
));
|
|
$options[$counter++] = $row;
|
|
}
|
|
|
|
//create an additional row for each double bed
|
|
//$j is our counter that increments twice as fast as $i to cater for both beds
|
|
$j = 0;
|
|
for ($i = 0; $i < $data->booking_room_doublebeds; $i++)
|
|
{
|
|
$row = _booking_clone_array($default_row);
|
|
$row['booking_room_doublebed_p1'] = array('data' => array(
|
|
'#type' => 'select',
|
|
'#options' => $attendee_select,
|
|
'#name' => 'booking_room_doublebed_p1[' . $data->rid . '][' . $i . ']',
|
|
'#value' => (!empty($existing_beds[2][$j])) ? $existing_beds[2][$j++] : 0,
|
|
));
|
|
$row['booking_room_doublebed_p2'] = array('data' => array(
|
|
'#type' => 'select',
|
|
'#options' => $attendee_select,
|
|
'#name' => 'booking_room_doublebed_p2[' . $data->rid . '][' . $i . ']',
|
|
'#value' => (!empty($existing_beds[2][$j])) ? $existing_beds[2][$j++] : 0,
|
|
));
|
|
$options[$counter++] = $row;
|
|
}
|
|
|
|
//create an additional row for each queen bed
|
|
//$j is our counter that increments twice as fast as $i to cater for both beds
|
|
$j = 0;
|
|
for ($i = 1; $i <= $data->booking_room_queenbeds; $i++)
|
|
{
|
|
$row = _booking_clone_array($default_row);
|
|
$row['booking_room_queenbed_p1'] = array('data' => array(
|
|
'#type' => 'select',
|
|
'#options' => $attendee_select,
|
|
'#name' => 'booking_room_queenbed_p1[' . $data->rid . '][' . $i . ']',
|
|
'#value' => (!empty($existing_beds[3][$j])) ? $existing_beds[3][$j++] : 0,
|
|
));
|
|
$row['booking_room_queenbed_p2'] = array('data' => array(
|
|
'#type' => 'select',
|
|
'#options' => $attendee_select,
|
|
'#name' => 'booking_room_queenbed_p2[' . $data->rid . '][' . $i . ']',
|
|
'#value' => (!empty($existing_beds[3][$j])) ? $existing_beds[3][$j++] : 0,
|
|
));
|
|
$options[$counter++] = $row;
|
|
}
|
|
|
|
}
|
|
|
|
$form['table'] = array (
|
|
'#type' => 'tableselect',
|
|
'#header' => $header,
|
|
'#options' => $options,
|
|
'#empty' => t('No attendees found.'),
|
|
);
|
|
|
|
//so we can access the dropdown elements
|
|
$form['booking_room_singlebed'] = array( '#type' => 'value', );
|
|
$form['booking_room_queenbed_p1'] = array( '#type' => 'value', );
|
|
$form['booking_room_queenbed_p2'] = array( '#type' => 'value', );
|
|
$form['booking_room_doublebed_p1'] = array( '#type' => 'value', );
|
|
$form['booking_room_doublebed_p2'] = array( '#type' => 'value', );
|
|
|
|
$form['submit'] = array (
|
|
'#type' => 'submit',
|
|
'#value' => t('Submit'),
|
|
);
|
|
|
|
return array (
|
|
'form' => $form,
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* Process the submission for room assignment
|
|
*/
|
|
function booking_rooms_allocate_form_submit($form, &$form_state) {
|
|
global $event;
|
|
$counter = 0;
|
|
$checkboxes = $form_state['values']['table'];
|
|
//$singlebed_ids = $form_state['values']['booking_room_singlebed'];
|
|
$values = $form_state['input'];
|
|
|
|
$bed_inputs = array(
|
|
'booking_room_singlebed' => 1,
|
|
'booking_room_doublebed_p1' => 2,
|
|
'booking_room_doublebed_p2' => 2,
|
|
'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)));
|
|
|
|
//go through the different bed types
|
|
foreach ($bed_inputs as $type => $type_id)
|
|
{
|
|
//watchdog('booking', "Bed type !type with id !id", array('!type' => $type, '!id' => $type_id));
|
|
//watchdog('booking', "<pre>Room assignment submission:\n@info</pre>", array('@info' => print_r( $form_state['values'][$type], true)));
|
|
|
|
//if this bed type wasn't defined in the form, skip it
|
|
if (empty($form_state['values'][$type]))
|
|
continue;
|
|
|
|
//go through each room
|
|
foreach($form_state['values'][$type] as $key => $value)
|
|
{
|
|
//go through each bed
|
|
foreach ($value as $index => $nid)
|
|
{
|
|
//if this is actually a person to process
|
|
if ($nid > 0)
|
|
{
|
|
drupal_set_message(t('Assigning person id !id to a !type bed in room id !room.',
|
|
array('!id' => $nid, '!room' => $key, '!type' => $type_id)));
|
|
|
|
//TODO: Check there isn't already a mapping for this person
|
|
|
|
$result = db_insert('booking_room_mapping')
|
|
->fields(array(
|
|
'booking_roomid' => $key,
|
|
'booking_eventid' => $event->eid,
|
|
'booking_nodeid' => $nid,
|
|
'booking_room_bedtype' => $type_id,
|
|
))
|
|
->execute();
|
|
|
|
}
|
|
} //each bed
|
|
} //each room
|
|
} //each bed type
|
|
|
|
} |