Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete reservations warning #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 123 additions & 16 deletions ding_reservation.module
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ function ding_reservation_menu() {
'access callback' => 'ding_reservation_access',
'access arguments' => array(1),
);


$items['user/%user/status/reservations/delete/%'] = array(
'title' => 'Delete reservations',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_reservation_delete_reservations_form', 1, 5),
'access callback' => 'ding_reservation_access',
'access arguments' => array(1),
);

return $items;
}

Expand Down Expand Up @@ -304,7 +312,7 @@ function ding_reservation_reservations_form($form, &$form_state, $items = array(
'#prefix' => '<div class="delete-reservation">',
'#suffix' => '</div>',
'#type' => 'submit',
'#submit' => array('ding_reservation_reservations_delete_submit'),
'#submit' => array('ding_reservation_reservation_delete_submit'),
'#reservation_id' => $item->id,
'#name' => 'delete-' . preg_replace('/\W/', '-', $item->id), // Need this for formAPI can tell buttons apart
'#value' => t('Delete'),
Expand Down Expand Up @@ -363,7 +371,7 @@ function ding_reservation_reservations_form($form, &$form_state, $items = array(
'#prefix' => '<div class="delete-reservation">',
'#suffix' => '</div>',
'#type' => 'submit',
'#submit' => array('ding_reservation_reservations_delete_submit'),
'#submit' => array('ding_reservation_reservation_delete_submit'),
'#reservation_id' => $item->id,
'#name' => 'delete-' . preg_replace('/\W/', '-', $item->id), // Need this for formAPI can tell buttons apart
'#value' => t('Delete'),
Expand Down Expand Up @@ -392,12 +400,16 @@ function ding_reservation_reservations_form($form, &$form_state, $items = array(
}
}

$form['submit'] = array(
$form['delete'] = array(
'#prefix' => '<div class="delete-reservations">',
'#suffix' => '</div>',
'#submit' => array('ding_reservation_reservations_delete_submit'),
'#submit' => array('ding_reservation_deletes_form_submit'),
'#type' => 'submit',
'#value' => t('Delete reservations'),
'#ajax' => array(
'callback' => 'ding_reservation_deletes_form_callback',
'wrapper' => 'ding-reservation-reservations-form',
),
);

if ($type == 'not_ready_for_pickup') {
Expand All @@ -406,19 +418,22 @@ function ding_reservation_reservations_form($form, &$form_state, $items = array(
'#suffix' => '</div>',
'#type' => 'submit',
/* '#submit' => array('ding_reservation_update_reservation_form'), */
'#submit' => array('ding_reservation_reservations_form_submit'),
'#submit' => array('ding_reservation_updates_form_submit'),
'#value' => t('Update reservations'),
'#ajax' => array(
'callback' => 'ding_reservation_reservations_form_callback',
'wrapper' => 'ding-reservation-reservations-form',
'callback' => 'ding_reservation_updates_form_callback',
'wrapper' => 'ding-reservation-updates-form',
),
);
}

return $form;
}

function ding_reservation_reservations_delete_submit($form, &$form_state) {
/**
* Submit handler for the reservations form.
*/
function ding_reservation_reservation_delete_submit($form, &$form_state) {
global $user;
if (!empty($form_state['triggering_element']['#reservation_id'])) {
$reservations = array($form_state['triggering_element']['#reservation_id']);
Expand All @@ -432,19 +447,48 @@ function ding_reservation_reservations_delete_submit($form, &$form_state) {
}

/**
* Submit handler for the form.
* Submit handler for the delete form.
*/
function ding_reservation_reservations_form_submit($form, &$form_state) {
function ding_reservation_deletes_form_submit($form, &$form_state) {
global $user;
$ids = join(',', array_map('rawurlencode', array_filter($form_state['values']['reservations'],'is_string')));
$form_state['encoded_reservations'] = $ids;
$form_state['redirect'] = array('user/' . $user->uid . '/status/reservations/delete/' . $ids, array('query' => drupal_get_destination()));
}

/**
* Submit handler for the update form.
*/
function ding_reservation_updates_form_submit($form, &$form_state) {
global $user;
$ids = join(',', array_map('rawurlencode', array_filter($form_state['values']['reservations'],'is_string')));
$form_state['encoded_reservations'] = $ids;
$form_state['redirect'] = array('user/' . $user->uid . '/status/reservations/update/' . $ids, array('query' => drupal_get_destination()));
}

/**
* Ajax callback.
* Ajax callback for the delete form.
*/
function ding_reservation_deletes_form_callback($form, &$form_state) {
global $user;
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);

$html = theme('status_messages');
$html .= drupal_render(drupal_get_form('ding_reservation_delete_reservations_form', $user, $form_state['encoded_reservations']));

if ($html) {
$response['#commands'][] = ajax_command_ding_popup('ding_reservation', t('Delete reservations'), $html, array('refresh' => TRUE));
}
return $response;
}

/**
* Ajax callback for the update form.
*/
function ding_reservation_reservations_form_callback($form, &$form_state) {
function ding_reservation_updates_form_callback($form, &$form_state) {
global $user;
$response = array(
'#type' => 'ajax',
Expand All @@ -460,6 +504,45 @@ function ding_reservation_reservations_form_callback($form, &$form_state) {
return $response;
}

/**
* Delete reservations form.
*/
function ding_reservation_delete_reservations_form($form, $form_state, $account, $reservation_ids) {
global $user;
$ids = array_map('rawurldecode', explode(',', $reservation_ids));

$form_state['cache'] = TRUE;

$form['reservations'] = array(
'#type' => 'value',
'#value' => $ids,
);

$form['confirm_text'] = array(
'#markup' => '<div>' . t('Are you sure you want to delete these reservations?') . '</div>',
);

$form['submit'] = array(
'#type' => 'submit',
'#submit' => array('ding_reservation_delete_reservations_form_submit'),
'#ajax' => array(
'callback' => 'ding_reservation_delete_reservations_form_callback',
'wrapper' => 'ding-reservation-delete-reservation-form',
),
'#value' => t('Yes'),
'#name' => 'delete_reservations',
);

$form['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => 'user/' . $user->uid . '/status/reservations',
'#value' => t('Cancel'),
);

return $form;
}

/**
* Update reservations form.
*/
Expand Down Expand Up @@ -528,18 +611,42 @@ function ding_reservation_update_reservations_form_validate($form, &$form_state)
}

/**
* Submit handler.
* Submit handlers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this change, I would prefer having the comment above each function.

*
* Updates selected reservations.
*/
function ding_reservation_delete_reservations_form_submit($form, &$form_state) {
global $user;
if (!empty($form_state['triggering_element']['#reservation_id'])) {
$reservations = array($form_state['triggering_element']['#reservation_id']);
}
else {
$reservations = array_filter($form_state['values']['reservations']);
}
foreach ($reservations as $entity_id) {
ding_provider_invoke('reservation', 'delete', $user, $entity_id);
}
}

function ding_reservation_update_reservations_form_submit($form, &$form_state) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still lacking a comment

global $user;
ding_provider_invoke('reservation', 'update', $user, $form_state['values']['reservations'], $form_state['values']['provider_options']);
}

/**
* Ajax callback function.
* Ajax callback functions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this change, I would prefer having the comment above each function.

*/
function ding_reservation_delete_reservations_form_callback($form, &$form_state) {
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);

$html = theme('status_messages') . t('Your reservations have been deleted.');
$response['#commands'][] = ajax_command_ding_popup('ding_reservation', t('Delete reservations'), $html, array('refresh' => TRUE));

return $response;
}

function ding_reservation_update_reservations_form_callback($form, &$form_state) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still lacking a comment

$response = array(
'#type' => 'ajax',
Expand Down