Update room location definition forms

This commit is contained in:
2014-11-24 21:08:58 +11:00
parent 9a61cb6744
commit f7a5856b2c

View File

@@ -53,64 +53,55 @@ function booking_room_view_summary() {
function booking_roomlocation_define_form($node, &$form_state, $create, $editid = 0) function booking_roomlocation_define_form($node, &$form_state, $create, $editid = 0)
{ {
$form = array (); $form = array ();
$prefix = "<p>Add a new study group definition</p>"; $prefix = "<p>Add a new room location definition</p>";
if ($create == true) if ($create == true)
{ {
drupal_set_title('Add Study Group'); drupal_set_title('Add Room Location');
$data = $node; $data = $node;
watchdog('booking', 'Creating new study group: @info', array ('@info' => var_export($node, TRUE))); watchdog('booking', 'Creating new room location: @info', array ('@info' => var_export($node, TRUE)));
} }
else else
{ {
drupal_set_title('Edit Study Group'); drupal_set_title('Edit Room Location');
//verify that $editid is a number //verify that $editid is a number
if (! preg_match('/^[0-9]+$/', $editid)) { if (! preg_match('/^[0-9]+$/', $editid)) {
drupal_set_message("Error: Invalid study group ID supplied. Unable to edit study group definition.", 'error', FALSE); drupal_set_message("Error: Invalid room location ID supplied. Unable to edit room location definition.", 'error', FALSE);
drupal_goto('admin/booking/config/studygroups'); drupal_goto('admin/config/booking/rooms');
return ""; return "";
} }
$data = db_query("SELECT * FROM {booking_studygroup_list} WHERE sid = :id", $data = db_query("SELECT * FROM {booking_room_locations} WHERE lid = :id",
array(':id' => $editid)) array(':id' => $editid))
->fetchObject(); ->fetchObject();
$prefix = t("<p>Update the &quot;!event &quot; study group definition.</p>", array('!event' => $data->booking_studygroup_descrip)); $prefix = t("<p>Update the &quot;!event &quot; room location definition.</p>", array('!event' => $data->booking_roomlocation_descrip));
//add this to the form in a hidden field so we can update the right price //add this to the form in a hidden field so we can update the right price
$form['booking_sid'] = array ( $form['booking_lid'] = array (
'#type' => 'hidden', '#type' => 'hidden',
'#value' => $editid, '#value' => $editid,
); );
watchdog('booking', 'Editing study group definition: @info', watchdog('booking', 'Editing room location definition: @info',
array ('@info' => var_export($data, TRUE))); array ('@info' => var_export($data, TRUE)));
} }
if(!isset($form_state['storage']['confirm'])) if(!isset($form_state['storage']['confirm']))
{ {
$form['booking_studygroup_descrip'] = array ( $form['booking_roomlocation_descrip'] = array (
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Description of this study group (eg Monday)'), '#title' => t('Description of this room location (eg Ramoth)'),
'#size' => 60, '#size' => 60,
'#maxlength' => 150, '#maxlength' => 150,
'#required' => TRUE, '#required' => TRUE,
'#default_value' => !empty($data->booking_studygroup_descrip) ? $data->booking_studygroup_descrip : '', '#default_value' => !empty($data->booking_roomlocation_descrip) ? $data->booking_roomlocation_descrip : '',
); );
$form['booking_num_group_sessions'] = array ( $form['booking_roomlocation_active'] = array (
'#type' => 'textfield',
'#title' => t('The number of sessions this study group will have'),
'#size' => 5,
'#maxlength' => 10,
'#required' => TRUE,
'#default_value' => !empty($data->booking_num_group_sessions) ? $data->booking_num_group_sessions : '',
);
$form['booking_is_readinggroup'] = array (
'#type' => 'radios', '#type' => 'radios',
'#title' => t('Reading group?'), '#title' => t('Location active?'),
'#description' => t('Select whether this study group definition is for a reading group. Leave as No unless you want team colours associated with this group'), '#description' => t('Select whether this room location definition is active'),
'#options' => array (0 => t('No'), t('Yes')), '#options' => array (0 => t('No'), t('Yes')),
'#default_value' => !empty($data->booking_is_readinggroup) ? ($data->booking_is_readinggroup == 'Y' ? 1 : 0) : 0, '#default_value' => !empty($data->booking_roomlocation_active) ? ($data->booking_roomlocation_active == 'Y' ? 1 : 0) : 0,
); );
if ($create == true) if ($create == true)
@@ -118,18 +109,18 @@ function booking_roomlocation_define_form($node, &$form_state, $create, $editid
$form['submit'] = array $form['submit'] = array
( (
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Create Study Group'), '#value' => t('Create'),
); );
} else { } else {
$form['Update'] = array $form['Update'] = array
( (
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Update Study Group'), '#value' => t('Updat'),
); );
$form['Delete'] = array $form['Delete'] = array
( (
'#type' => 'submit', '#type' => 'submit',
'#value' => t("Delete Study Group Definition"), '#value' => t("Delete"),
); );
} }
@@ -144,8 +135,8 @@ function booking_roomlocation_define_form($node, &$form_state, $create, $editid
//confirm delete //confirm delete
else else
{ {
return confirm_form($form, "Are you sure you wish to delete studygroup definition with id " . $editid . "?", return confirm_form($form, "Are you sure you wish to delete room location definition with id " . $editid . "?",
current_path(), NULL, "Delete Study Group Definition"); current_path(), NULL, "Delete");
} }
} }
@@ -155,79 +146,72 @@ function booking_roomlocation_define_form_submit($form, &$form_state) {
global $event; global $event;
$values = $form_state['input']; $values = $form_state['input'];
$redirect_path = array('admin/config/booking/studygroups'); $redirect_path = array('admin/config/booking/rooms');
$new_count = 0; $new_count = 0;
//get the number of study groups before making this update //get the number of study groups before making this update
$count = db_query("SELECT count(*) as num FROM {booking_studygroup_list} WHERE booking_eventid = :eid", //$count = db_query("SELECT count(*) as num FROM {booking_studygroup_list} WHERE booking_eventid = :eid",
array(':eid' => $event->eid))->fetchObject(); // array(':eid' => $event->eid))->fetchObject();
//watchdog('booking', 'Checkboxes when setting buttons: @info', array ('@info' => var_export($checkboxes, TRUE))); //watchdog('booking', 'Checkboxes when setting buttons: @info', array ('@info' => var_export($checkboxes, TRUE)));
if ($form_state['values']['op'] == 'Create Study Group') if ($form_state['values']['op'] == 'Create')
{ {
db_insert('booking_studygroup_list') db_insert('booking_room_locations')
->fields(array( ->fields(array(
'booking_eventid' => $event->eid, 'booking_roomlocation_descrip' => $values['booking_roomlocation_descrip'],
'booking_studygroup_descrip' => $values['booking_studygroup_descrip'], 'booking_roomlocation_active' => $values['booking_roomlocation_active'] == 1 ? 'Y' : 'N',
'booking_num_group_sessions' => $values['booking_num_group_sessions'],
'booking_is_readinggroup' => $values['booking_is_readinggroup'] == 1 ? 'Y' : 'N',
)) ))
->execute(); ->execute();
$new_count = $count->num + 1; //$new_count = $count->num + 1;
} }
//if we're deleting, add the confirmation to the form if it hasn't been defined yet //if we're deleting, add the confirmation to the form if it hasn't been defined yet
elseif($form_state['values']['op'] == "Delete Study Group Definition" && (!isset($form_state['storage']['confirm']))) elseif($form_state['values']['op'] == "Delete" && (!isset($form_state['storage']['confirm'])))
{ {
watchdog('booking', "<pre>Studygroup deletion confirmation being set:\n@info</pre>", array('@info' => print_r( $form_state, true))); watchdog('booking', "<pre>Room location definition deletion confirmation being set:\n@info</pre>", array('@info' => print_r( $form_state, true)));
$form_state['storage']['confirm'] = TRUE; $form_state['storage']['confirm'] = TRUE;
$form_state['rebuild'] = TRUE; $form_state['rebuild'] = TRUE;
$new_count = $count->num; //$new_count = $count->num;
} }
elseif ($form_state['values']['op'] == 'Delete Study Group Definition') elseif ($form_state['values']['op'] == 'Delete')
{ {
//verify that booking_pid is a number //verify that booking_pid is a number
if (! preg_match('/^[0-9]+$/', $values['booking_sid'])) { if (! preg_match('/^[0-9]+$/', $values['booking_lid'])) {
drupal_set_message("Error: Invalid studygroup ID supplied. Unable to delete entry.", 'error', FALSE); drupal_set_message("Error: Invalid room location ID supplied. Unable to delete entry.", 'error', FALSE);
return ""; return "";
} }
//TODO: Confirmation $num_deleted = db_delete('booking_room_locations')
//return confirm_form($form, "Really delete price?", 'admin/config/booking/prices'); ->condition('lid', $values['booking_lid'])
$num_deleted = db_delete('booking_studygroup_list')
->condition('sid', $values['booking_sid'])
->execute(); ->execute();
$new_count = $count->num - 1; //$new_count = $count->num - 1;
} }
elseif ($form_state['values']['op'] == 'Update Study Group') elseif ($form_state['values']['op'] == 'Update')
{ {
//verify that booking_sid is a number //verify that booking_sid is a number
if (! preg_match('/^[0-9]+$/', $values['booking_sid'])) { if (! preg_match('/^[0-9]+$/', $values['booking_lid'])) {
drupal_set_message("Error: Invalid studygroup ID supplied. Unable to update study group.", 'error', FALSE); drupal_set_message("Error: Invalid room location ID supplied. Unable to update room location definition.", 'error', FALSE);
return ""; return "";
} }
//update the study group //update the study group
db_update('booking_studygroup_list') db_update('booking_room_locations')
->fields(array ( ->fields(array (
'booking_eventid' => $event->eid, 'booking_roomlocation_descrip' => $values['booking_roomlocation_descrip'],
'booking_studygroup_descrip' => $values['booking_studygroup_descrip'], 'booking_roomlocation_active' => $values['booking_roomlocation_active'] == 1 ? 'Y' : 'N',
'booking_num_group_sessions' => $values['booking_num_group_sessions'],
'booking_is_readinggroup' => $values['booking_is_readinggroup'] == 1 ? 'Y' : 'N',
)) ))
->condition('sid', $values['booking_sid']) ->condition('lid', $values['booking_lid'])
->execute(); ->execute();
$new_count = $count->num; //$new_count = $count->num;
} }
//update the number of study groups now defined //update the number of study groups now defined
variable_set('booking_studygroup_count', $new_count); //variable_set('booking_studygroup_count', $new_count);
$form_state['redirect'] = $redirect_path; $form_state['redirect'] = $redirect_path;
} }