diff --git a/roblib_bee_limits.module b/roblib_bee_limits.module index 3c2454f..adfda95 100644 --- a/roblib_bee_limits.module +++ b/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) { $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id()); $roles = $user->getRoles(); - debug($roles, 'pp-roles', TRUE); + roblib_bee_limits_check_reservations($user, $form, $form_state); $values = $form_state->getValues(); $node = Node::load($values['node']); $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')); $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.')); } } + +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; + } +}