Browse Source

added a simple limit on the number of active bookings allowed

master
ppound 4 years ago
parent
commit
ee00f0d8af
  1. 35
      roblib_bee_limits.module

35
roblib_bee_limits.module

@ -34,7 +34,7 @@ function roblib_bee_limits_form_bee_add_reservation_form_alter(&$form, FormState
function roblib_bee_limits_validate_reservation($form, FormStateInterface $form_state) { function roblib_bee_limits_validate_reservation($form, FormStateInterface $form_state) {
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id()); $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$roles = $user->getRoles(); $roles = $user->getRoles();
debug($roles, 'pp-roles', TRUE); roblib_bee_limits_check_reservations($user, $form, $form_state);
$values = $form_state->getValues(); $values = $form_state->getValues();
$node = Node::load($values['node']); $node = Node::load($values['node']);
$config_factory = \Drupal::configFactory(); $config_factory = \Drupal::configFactory();
@ -48,8 +48,39 @@ function roblib_bee_limits_validate_reservation($form, FormStateInterface $form_
$start_timestamp = new \DateTime($start_date->format('Y-m-d H:i')); $start_timestamp = new \DateTime($start_date->format('Y-m-d H:i'));
$end_timestamp = new \DateTime($end_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; $minutes_diff = abs($end_timestamp->getTimestamp() - $start_timestamp->getTimestamp()) / 60;
debug($minutes_diff, 'pp-minutesdiff', TRUE);
if($minutes_diff > 120) { if($minutes_diff > 120) {
$form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.')); $form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.'));
} }
} }
function roblib_bee_limits_check_reservations($user, $form, &$form_state) {
$uid = $user->id();
$results = \Drupal::entityQuery('bat_event')
->condition('uid', $uid, '=')
->condition('type', 'availability_hourly', '=')
->execute();
$active_bookings = 0;
$now = new DateTime("now");
foreach($results as $result){
$event = bat_event_load($result);
if($event->getEndDate() > $now){
$active_bookings++;
}
$event = NULL;
}
$max_bookings_allowed = roblib_bee_limits_get_max_bookings_allowed($user);
if($max_bookings_allowed != -1 && $active_bookings > $max_bookings_allowed){
$form_state->setError($form, t('Reservation exceeded max number of active bookings allowed.'));
}
}
function roblib_bee_limits_get_max_bookings_allowed($user) {
$roles = $user->getRoles();
$is_admin = array_search('administrator', $roles);
if($is_admin) {
return -1;
}else {
return 3;
}
}

Loading…
Cancel
Save