From cc64e015e4881e9924855cb63139d3ea0c9c1c8a Mon Sep 17 00:00:00 2001 From: ppound Date: Fri, 30 Oct 2020 10:43:32 -0300 Subject: [PATCH] pushing event validation to a rule action to see if it makes more sense than multiple validation paths --- roblib_bee_limits.module | 3 +- src/Plugin/RulesAction/BatEventValidation.php | 117 ++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/Plugin/RulesAction/BatEventValidation.php diff --git a/roblib_bee_limits.module b/roblib_bee_limits.module index a003a30..534c3c7 100644 --- a/roblib_bee_limits.module +++ b/roblib_bee_limits.module @@ -18,7 +18,8 @@ use Drupal\node\Entity\Node; * @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'; + // TESTING validation in rules, we should be able to validate both webform and normal forms in rules. + //$form['#validate'][] = 'roblib_bee_limits_validate_reservation'; } /** diff --git a/src/Plugin/RulesAction/BatEventValidation.php b/src/Plugin/RulesAction/BatEventValidation.php new file mode 100644 index 0000000..05ee8ac --- /dev/null +++ b/src/Plugin/RulesAction/BatEventValidation.php @@ -0,0 +1,117 @@ +id()); + $event_type = bat_event_type_load($event->bundle()); + // we only want to limit hourly reservations + if($event_type->id() != 'availability_hourly') { + return; + } + $this->checkReservations($user); + $start_date = $event->getStartDate(); + $end_date = $event->getEndDate(); + $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; + $max_time = $this->getMaxTime($user); + if($minutes_diff > $max_time) { + drupal_set_message(t('Rule hit Reservation exceeded your max + booking time of @max_time minutes.', array('@max_time' => $max_time)), 'error'); + + $response = new RedirectResponse(\Drupal::request()->getRequestUri()); + $response->send(); + exit; + } + } + + function getMaxTime($user){ + $roles = $user->getRoles(); + $config = \Drupal::config('roblib_bee_limits.settings'); + $max_time = 0; + foreach($roles as $role){ + $tmp = $config->get($role . '_max_booking_duration'); + $tmp = empty($tmp) ? 0 : $tmp; + $max_time = $max_time > $tmp ? $max_time : $tmp; + } + return $max_time; + } + + function getMaxBookingsAllowed($user) { + $roles = $user->getRoles(); + $config = \Drupal::config('roblib_bee_limits.settings'); + $max_active_bookings = 0; + foreach($roles as $role){ + $tmp = $config->get($role . '_active_bookings_allowed'); + $tmp = empty($tmp) ? 0 : $tmp; + $max_active_bookings = $max_active_bookings > $tmp ? $max_active_bookings : $tmp; + } + //$is_admin = array_search('administrator', $roles); + return $max_active_bookings; + } + + + function checkReservations($user) { + $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 = $this->getMaxBookingsAllowed($user); + if($max_bookings_allowed != -1 && $active_bookings >= $max_bookings_allowed){ + drupal_set_message(t('Rule Hit Reservation exceeded your maximum number + of active bookings allowed. You currently have @active_bookings active reservations, + You are allowed @max_bookings .', array('@active_bookings' => $active_bookings, + '@max_bookings' => $max_bookings_allowed)),'error'); + + $response = new RedirectResponse(\Drupal::request()->getRequestUri()); + $response->send(); + exit; + } + } + +}