Commit 01a68756 authored by nicola85's avatar nicola85

Add Webform integration

parent 6eda4a04
name: Bookable Entities Everywhere Webform
type: module
description: Bookable Entities Everywhere - easily make content types bookable with the BAT module
core: 8.x
package: bat
dependencies:
- bee
- webform
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
/**
* Implements hook_form_alter().
*/
function bee_webform_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['#webform_id'])) {
foreach ($form['elements'] as $element) {
if ($element['#type'] == 'webform_bee_reservation_form') {
$form['actions']['submit']['#submit'][] = 'bee_webform_form_submit';
}
}
}
}
function bee_webform_form_submit($form, FormStateInterface $form_state) {
foreach ($form['elements'] as $id => $element) {
if ($element['#type'] == 'webform_bee_reservation_form') {
$value = $form_state->getValue($id);
if ($value['content_type'] && $value['start_date'] && $value['end_date'] && $value['capacity']) {
$start_date = $value['start_date'];
$end_date = $value['end_date'];
$max_capacity = FALSE;
$available_units = [];
$query = \Drupal::entityQuery('node')
->condition('type', $value['content_type']);
$nids = $query->execute();
foreach (Node::loadMultiple($nids) as $node) {
$values = [
'node' => $node,
'start_date' => $start_date,
'end_date' => $end_date,
];
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;
}
}
}
$index = 0;
foreach ($available_units as $nid => $node_available_units) {
$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');
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
}
else {
$booked_state = bat_event_load_state_by_machine_name('bee_hourly_booked');
$start_date = new \DateTime($start_date->format('Y-m-d H:i'));
$end_date = new \DateTime($end_date->format('Y-m-d H:i'));
}
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('event_bat_unit_reference', $unit);
$event->save();
if (++$index == $value['capacity']) {
break 2;
}
}
}
}
}
}
}
/**
* Get available Units.
*
* @param $values
*
* return array
*/
function bee_webform_get_available_units($values) {
$node = $values['node'];
$start_date = $values['start_date'];
$end_date = $values['end_date'];
$bee_settings = \Drupal::config('node.type.' . $node->bundle())->get('bee');
$units_ids = [];
foreach ($node->get('field_availability_' . $bee_settings['bookable_type']) as $unit) {
$units_ids[] = $unit->entity->id();
}
if ($bee_settings['bookable_type'] == 'daily') {
$start_date = new \DateTime($start_date);
$end_date = new \DateTime($end_date);
$end_date->sub(new \DateInterval('PT1M'));
$available_units_ids = bat_event_get_matching_units($start_date, $end_date, ['bee_daily_available'], $bee_settings['type_id'], 'availability_daily');
}
else {
$start_date = new \DateTime($start_date->format('Y-m-d H:i'));
$end_date = new \DateTime($end_date->format('Y-m-d H:i'));
$end_date->sub(new \DateInterval('PT1M'));
$available_units_ids = bat_event_get_matching_units($start_date, $end_date, ['bee_hourly_available'], $bee_settings['type_id'], 'availability_hourly');
}
return array_intersect($units_ids, $available_units_ids);
}
<?php
namespace Drupal\bee_webform\Element;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\webform\Element\WebformCompositeBase;
/**
* BEE reservation form
*
* @FormElement("webform_bee_reservation_form")
*/
class WebformBeeReservationForm extends WebformCompositeBase {
/**
* {@inheritdoc}
*/
public static function getCompositeElements(array $element) {
$elements = [];
$bookable_type = FALSE;
$content_type_options = [];
foreach (array_filter($element['#content_types']) as $node_type) {
$node_type = NodeType::load($node_type);
$content_type_options[$node_type->id()] = $node_type->label();
$bee_settings = \Drupal::config('node.type.' . $node_type->id())->get('bee');
if (isset($bee_settings['bookable_type'])) {
$bookable_type = $bee_settings['bookable_type'];
}
}
if ($content_type_options) {
if (count($content_type_options) > 1) {
$elements['content_type'] = [
'#type' => 'radios',
'#title' => t('Content type'),
'#options' => $content_type_options,
];
}
else {
$elements['content_type'] = [
'#type' => 'hidden',
'#value' => key($content_type_options),
];
}
$elements['start_date'] = [
'#type' => ($bookable_type == 'daily') ? 'date' : 'datetime',
'#title' => t('Start date'),
];
$elements['end_date'] = [
'#type' => ($bookable_type == 'daily') ? 'date' : 'datetime',
'#title' => t('End date'),
];
$elements['capacity'] = [
'#type' => 'number',
'#title' => t('Capacity'),
];
}
return $elements;
}
/**
* {@inheritdoc}
*/
public static function validateWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
$value = NestedArray::getValue($form_state->getValues(), $element['#parents']);
if ($value['content_type'] && $value['start_date'] && $value['end_date'] && $value['capacity']) {
$available_units = 0;
$query = \Drupal::entityQuery('node')
->condition('type', $value['content_type']);
$nids = $query->execute();
foreach (Node::loadMultiple($nids) as $node) {
$values = [
'node' => $node,
'start_date' => $value['start_date'],
'end_date' => $value['end_date'],
];
$available_units += count(bee_webform_get_available_units($values));
}
if ($available_units < $value['capacity']) {
$form_state->setError($element, t('Unfortunately, not enough units of this type are available'));
}
}
}
}
<?php
namespace Drupal\bee_webform\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
use Drupal\webform\Plugin\WebformElement\WebformCompositeBase;
/**
* BEE reservation form
*
* @WebformElement(
* id = "webform_bee_reservation_form",
* label = @Translation("BEE reservation form"),
* description = @Translation("BEE reservation form."),
* category = @Translation("Advanced elements"),
* )
*/
class WebformBeeReservationForm extends WebformCompositeBase {
/**
* {@inheritdoc}
*/
public function getDefaultProperties() {
return [
'content_types' => [],
] + parent::getDefaultProperties();
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$node_types = NodeType::loadMultiple();
$options = [];
foreach ($node_types as $node_type) {
$bee_settings = \Drupal::config('node.type.' . $node_type->id())->get('bee');
if (!empty($bee_settings['bookable'])) {
$options[$node_type->id()] = $node_type->label();
}
}
$form['composite']['content_types'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Content type(s)'),
'#options' => $options,
'#element_validate' => [[get_class($this), 'validateContentTypes']],
];
return $form;
}
/**
* Webform element validation handler.
*/
public static function validateContentTypes(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value'];
$bookable_type = FALSE;
if (count($value) > 1) {
foreach ($value as $node_type) {
$bee_settings = \Drupal::config('node.type.' . $node_type)->get('bee');
if (isset($bee_settings['bookable_type'])) {
if ($bookable_type) {
if ($bookable_type != $bee_settings['bookable_type']) {
$form_state->setError($element, t('Daily and Hourly content types cannot both be selected.'));
}
}
else {
$bookable_type = $bee_settings['bookable_type'];
}
}
}
}
}
}
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