Add limits to hourly booking reservations as well as number of future reservations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

<?php
/**
* @file
* Primary module hooks for roblib_bee_limits module.
*
*/
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
/**
* Implements hook_form_FORM_ID_alter.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function roblib_bee_limits_form_bee_add_reservation_form_alter(&$form, FormStateInterface $form_state, $form_id){
$form['#validate'][] = 'roblib_bee_limits_validate_reservation';
}
/**
* Additional form validation for bee_add_reservation_form.
*
* Limits hourly bookings.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @throws \Exception
*/
function roblib_bee_limits_validate_reservation($form, FormStateInterface $form_state) {
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$roles = $user->getRoles();
debug($roles, 'pp-roles', TRUE);
$values = $form_state->getValues();
$node = Node::load($values['node']);
$config_factory = \Drupal::configFactory();
$bee_settings = $config_factory->get('node.type.' . $node->bundle())->get('bee');
// we only want to limit hourly reservations
if($bee_settings['bookable_type'] != 'hourly') {
return;
}
$start_date = $values['start_date'];
$end_date = $values['end_date'];
$start_timestamp = new \DateTime($start_date->format('Y-m-d H:i'));
$end_timestamp = new \DateTime($end_date->format('Y-m-d H:i'));
$minutes_diff = abs($end_timestamp->getTimestamp() - $start_timestamp->getTimestamp()) / 60;
debug($minutes_diff, 'pp-minutesdiff', TRUE);
if($minutes_diff > 120) {
$form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.'));
}
}