From 1cedef106c2364fe6d7085ad8fdd051473da62bf Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Sat, 5 May 2018 20:23:28 +1000 Subject: [PATCH] more work on edit variety submit hook --- booking.variety_admin.inc | 89 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/booking.variety_admin.inc b/booking.variety_admin.inc index ebb2335..6fcb0a3 100644 --- a/booking.variety_admin.inc +++ b/booking.variety_admin.inc @@ -623,7 +623,6 @@ function booking_variety_regn_edit_form($node, &$form_state, $nid) $form = array(); $data = $node; $redirect_path = "admin/config/booking/variety/report"; - $prefix = "

Edit variety session registration for user

"; //verify that $variety_regn_id is a number if (! preg_match('/^[0-9]+$/', $nid)) { @@ -632,16 +631,17 @@ function booking_variety_regn_edit_form($node, &$form_state, $nid) return ""; } - $person_query = db_query("SELECT * FROM {booking_person_view} WHERE nid = :nid", + $person = db_query("SELECT * FROM {booking_person_view} WHERE nid = :nid", array(':nid' => $nid))->fetchObject(); - if (! $person_query) { + if (! $person) { drupal_set_message("Error: Could not find matching person. Unable to edit variety session registrations.", 'error', FALSE); drupal_goto($redirect_path); return ""; } - - $session_ids = drupal_json_decode($person_query->booking_variety_ids); + $session_ids = drupal_json_decode($person->booking_variety_ids); + $prefix = t("

Edit variety session registration for !first !last.

", + array('!first' => $person->booking_firstname, '!last' => $person->booking_lastname)); // Query the variety timeslot table so we know how many select elements to create $timeslot_query = db_select('booking_variety_timeslots', 'v'); @@ -650,7 +650,7 @@ function booking_variety_regn_edit_form($node, &$form_state, $nid) ->orderBy('v.booking_variety_start'); $timeslot_result = $timeslot_query->execute(); - + // --- Form starts here --- //define the form for variety session configuration if we're not deleting a session if(!isset($form_state['storage']['confirm'])) { $form[] = array ( @@ -704,5 +704,80 @@ function booking_variety_regn_edit_form($node, &$form_state, $nid) return confirm_form($form, "Are you sure you wish to delete all variety session registrations for id " . $variety_regn_id . "?", current_path(), NULL, "Delete Session"); } - +} + +/** + * Function submit data for editing a variety session registration for a person + */ +function booking_variety_regn_edit_form_submit($form, &$form_state) { + global $event; + $values = $form_state['input']; + $booking_variety_ids = array(); + $redirect_path = "admin/config/booking/variety"; + + //get a list of timeslot IDs from matching form values + $variety_timeslot_ids = preg_filter('/^select-variety-(\d+)/', '$1', array_keys( $values )); + //build an associative array based on selections + foreach ($variety_timeslot_ids as $id) { + $selected_session_id = $values['select-variety-' . $id]; + // Don't try and check availablity for a select element that is still on the default value + if ($selected_session_id == 0) { + continue; + } + //store the selected variety sessions in an array of IDs + $booking_variety_ids[$id] = $selected_session_id; + } + + //if we're deleting, add the confirmation to the form if it hasn't been defined yet + if($form_state['values']['op'] == 'Delete Session Registration' && (!isset($form_state['storage']['confirm']))) { + //watchdog('booking_debug', "
Variety session deletion confirmation being set:\n@info
", array('@info' => print_r( $form_state, true))); + $form_state['storage']['confirm'] = TRUE; + $form_state['rebuild'] = TRUE; + } + elseif ($form_state['values']['op'] == 'Delete Session Registration') { + //delete the variety session + watchdog('booking', "Deleting variety session registration for person ID !nid", array('!nid' => $values['nid'])); + + //@todo - decremement regn count for this person's variety sessions +/* + + //use an update query for the regncount field + //idea from https://api.drupal.org/comment/19374#comment-19374 + db_update('booking_variety_sessions') + ->expression('booking_variety_regncount', 'booking_variety_regncount + :count', array(':count' => 1)) + ->condition('vid', $selected_session_id) + ->execute(); +*/ + + db_delete('booking_variety_regn') + ->condition('booking_person_nid', $values['nid']) + ->execute(); + + drupal_set_message('Deleted variety session registration for person id ' . $values['booking_session_id'] ); + $form_state['redirect'] = $redirect_path; + } + elseif ($form_state['values']['op'] == 'Update Session Registration') { + // Get the previous variety session IDs and compare them + $person = db_query("SELECT * FROM {booking_person_view} WHERE nid = :nid", + array(':nid' => $nid))->fetchObject(); + $previous_variety_ids = drupal_json_decode($person->booking_variety_ids); + + foreach ($previous_variety_ids as $previous_tid => $previous_sid) { + if ($booking_variety_ids[$previous_tid] != $previous_sid) { + watchdog('booking_debug', 'Person @nid in timeslot @tid previously registered for session id @vid but new value is @new', array( + '@nid' => $values['nid'], '@tid' => $previous_tid, '@vid' => $previous_sid, '@new' => $booking_variety_ids[$previous_tid] + )); + } + } + /* + $result = db_update('booking_variety_regn') + ->fields(array( + 'booking_variety_ids' => drupal_json_encode(), + )) + ->condition('booking_person_nid', $values['nid']) + ->execute(); + watchdog('booking', "Updated variety session registration for person ID !nid", array('!nid' => $values['nid'])); + */ + $form_state['redirect'] = $redirect_path; + } }