Commit ebf5e34c authored by acrollet's avatar acrollet

Handle webforms with pages

parent 30346ddb
......@@ -12,7 +12,7 @@ use Drupal\node\Entity\Node;
function bee_webform_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['#webform_id'])) {
$webform = \Drupal\webform\Entity\Webform::load($form['#webform_id']);
foreach ($webform->getElementsDecodedAndFlattened() as $element) {
foreach ($webform->getElementsDecodedAndFlattened() as $element_id => $element) {
if ($element['#type'] == 'webform_bee_reservation_form') {
$form['actions']['submit']['#submit'][] = 'bee_webform_form_submit';
}
......@@ -20,140 +20,145 @@ function bee_webform_form_alter(&$form, FormStateInterface $form_state, $form_id
}
}
/**
* bee_webform submission handler.
*/
function bee_webform_form_submit($form, FormStateInterface $form_state) {
foreach ($form['elements'] as $id => $element) {
$webform = \Drupal\webform\Entity\Webform::load($form['#webform_id']);
foreach ($webform->getElementsDecodedAndFlattened() as $element_id => $element) {
if ($element['#type'] == 'webform_bee_reservation_form') {
$value = $form_state->getValue($id);
$webform = \Drupal\webform\Entity\Webform::load($form['#webform_id']);
$bee_element_id = $element_id;
}
}
if ($value['content_type'] && $value['start_date'] && $value['end_date'] && $value['capacity']) {
$webform_submission = $form_state->getFormObject()->getEntity();
$value = $form_state->getValue($bee_element_id);
$start_date = $value['start_date'];
$end_date = $value['end_date'];
if ($value['content_type'] && $value['start_date'] && $value['end_date'] && $value['capacity']) {
$webform_submission = $form_state->getFormObject()->getEntity();
$max_capacity = FALSE;
$start_date = $value['start_date'];
$end_date = $value['end_date'];
$available_units = [];
$max_capacity = FALSE;
$query = \Drupal::entityQuery('node')
->condition('type', $value['content_type']);
$available_units = [];
$nids = $query->execute();
foreach (Node::loadMultiple($nids) as $node) {
$values = [
'node' => $node,
'start_date' => $start_date,
'end_date' => $end_date,
];
$query = \Drupal::entityQuery('node')
->condition('type', $value['content_type']);
if ($node_available_units = bee_webform_get_available_units($values)) {
if (count($node_available_units) == $value['capacity']) {
$available_units = [$node->id() => $node_available_units];
break;
}
elseif (count($node_available_units) > $value['capacity']) {
$available_units[$node->id()] = $node_available_units;
$max_capacity = TRUE;
}
elseif (!$max_capacity) {
$available_units[$node->id()] = $node_available_units;
}
}
}
$nids = $query->execute();
foreach (Node::loadMultiple($nids) as $node) {
$values = [
'node' => $node,
'start_date' => $start_date,
'end_date' => $end_date,
];
uasort($available_units, function($a, $b) { return count($a) - count($b); });
$index = 0;
$events_created = [];
foreach ($available_units as $nid => $node_available_units) {
if ($max_capacity && count($node_available_units) < $value['capacity']) {
continue;
}
$node = Node::load($nid);
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
if ($bee_settings['bookable_type'] == 'daily') {
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
}
else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
}
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
foreach ($node_available_units as $unit) {
if ($bee_settings['bookable_type'] == 'daily') {
$event = bat_event_create(['type' => 'availability_daily']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
else {
$event = bat_event_create(['type' => 'availability_hourly']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
$event->set('field_event_webform_submission', $webform_submission->id());
$event->set('event_bat_unit_reference', $unit);
$event->save();
$events_created[] = $event->id();
if (++$index == $value['capacity']) {
break 2;
}
}
if ($node_available_units = bee_webform_get_available_units($values)) {
if (count($node_available_units) == $value['capacity']) {
$available_units = [$node->id() => $node_available_units];
break;
}
elseif (count($node_available_units) > $value['capacity']) {
$available_units[$node->id()] = $node_available_units;
$max_capacity = TRUE;
}
elseif (!$max_capacity) {
$available_units[$node->id()] = $node_available_units;
}
}
}
uasort($available_units, function($a, $b) { return count($a) - count($b); });
$index = 0;
$events_created = [];
foreach ($available_units as $nid => $node_available_units) {
if ($max_capacity && count($node_available_units) < $value['capacity']) {
continue;
}
$node = Node::load($nid);
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
// Send email with summary of any allocations made.
if (count($events_created) == 0) {
$message = t("No nodes with sufficient capacity were found, therefore no reservations were created.\nTo view the submission, please go to @submission", ['@submission' => $webform_submission->url('canonical', ['absolute' => TRUE])]);
if ($bee_settings['bookable_type'] == 'daily') {
$booked_state = bat_event_load_state_by_machine_name('bee_daily_booked');
}
else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
}
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
foreach ($node_available_units as $unit) {
if ($bee_settings['bookable_type'] == 'daily') {
$event = bat_event_create(['type' => 'availability_daily']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
else {
$events = bat_event_load_multiple($events_created);
$event_links = [];
foreach ($events as $event) {
$event_links[] = $event->url('canonical', ['absolute' => TRUE]);
}
$message = t("The following BEE reservations were created for a new webform submission:\n\n@event_links\n\nTo view the submission, please go to @submission", ['@event_links' => implode("\n", $event_links), '@submission' => $webform_submission->url('canonical', ['absolute' => TRUE])]);
$event = bat_event_create(['type' => 'availability_hourly']);
$event_dates = [
'value' => $start_date->format('Y-m-d\TH:i:00'),
'end_value' => $end_date->format('Y-m-d\TH:i:00'),
];
$event->set('event_dates', $event_dates);
$event->set('event_state_reference', $booked_state->id());
}
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'bee_webform';
$key = 'bee_webform_submission';
$to = $webform->getOwner()->getEmail();
$params['webform_title'] = $webform->get('title');
$params['message'] = $message;
$langcode = $webform->getOwner()->getPreferredLangcode();
$send = TRUE;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] !== true) {
$message = t('There was a problem sending your email notification to @email for creating a webform BEE reservation.', array('@email' => $to));
drupal_set_message($message, 'error');
\Drupal::logger('bee_webform')->error($message);
return;
$event->set('field_event_webform_submission', $webform_submission->id());
$event->set('event_bat_unit_reference', $unit);
$event->save();
$events_created[] = $event->id();
if (++$index == $value['capacity']) {
break 2;
}
}
}
$message = t('An email notification has been sent to @email for creating a webform BEE reservation.', array('@email' => $to));
drupal_set_message($message);
\Drupal::logger('bee_webform')->notice($message);
// Send email with summary of any allocations made.
if (count($events_created) == 0) {
$message = t("No nodes with sufficient capacity were found, therefore no reservations were created.\nTo view the submission, please go to @submission", ['@submission' => $webform_submission->url('canonical', ['absolute' => TRUE])]);
}
else {
$events = bat_event_load_multiple($events_created);
$event_links = [];
foreach ($events as $event) {
$event_links[] = $event->url('canonical', ['absolute' => TRUE]);
}
$message = t("The following BEE reservations were created for a new webform submission:\n\n@event_links\n\nTo view the submission, please go to @submission", ['@event_links' => implode("\n", $event_links), '@submission' => $webform_submission->url('canonical', ['absolute' => TRUE])]);
}
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'bee_webform';
$key = 'bee_webform_submission';
$to = $webform->getOwner()->getEmail();
$params['webform_title'] = $webform->get('title');
$params['message'] = $message;
$langcode = $webform->getOwner()->getPreferredLangcode();
$send = TRUE;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] !== true) {
$message = t('There was a problem sending your email notification to @email for creating a webform BEE reservation.', array('@email' => $to));
drupal_set_message($message, 'error');
\Drupal::logger('bee_webform')->error($message);
return;
}
$message = t('An email notification has been sent to @email for creating a webform BEE reservation.', array('@email' => $to));
drupal_set_message($message);
\Drupal::logger('bee_webform')->notice($message);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment