From 0ab577578f0752c2e2dcf50db6f21b70f004b62f Mon Sep 17 00:00:00 2001 From: ppound Date: Tue, 20 Oct 2020 14:43:48 -0300 Subject: [PATCH] add limit configuration per role by hooking role_edit_form --- config/install/roblib_bee_limits.settings.yml | 0 roblib_bee_limits.module | 102 ++++++++++++++++-- 2 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 config/install/roblib_bee_limits.settings.yml diff --git a/config/install/roblib_bee_limits.settings.yml b/config/install/roblib_bee_limits.settings.yml new file mode 100644 index 0000000..e69de29 diff --git a/roblib_bee_limits.module b/roblib_bee_limits.module index adfda95..ddbb76f 100644 --- a/roblib_bee_limits.module +++ b/roblib_bee_limits.module @@ -21,6 +21,69 @@ function roblib_bee_limits_form_bee_add_reservation_form_alter(&$form, FormState $form['#validate'][] = 'roblib_bee_limits_validate_reservation'; } +/** + * Implements hook_form_FORM_ID_alter. + * + * @param $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + * @param $form_id + */ +function roblib_bee_limits_form_user_role_form_alter(&$form, FormStateInterface $form_state, $form_id){ + $config = \Drupal::config('roblib_bee_limits.settings'); + $form['#validate'][] = 'roblib_bee_limits_submit_user_role_form'; + $form['#submit'][] = 'roblib_bee_limits_validate_user_role_form'; + $form['roblib_bee_limits_fieldset'] = array( + '#type' => 'details', + '#title' => t('BEE Hourly Booking Limits'), + '#open' => FALSE, + ); + $role_machine_name = $form['id']['#default_value']; + $form['roblib_bee_limits_fieldset']['active_bookings_allowed'] = array( + '#type' => 'number', + '#default_value' => $config->get($role_machine_name . '_active_bookings_allowed'), + '#title' => t('Active Bookings Allowed'), + '#min' => 0, + '#max' => 1000, + '#size' => 4, + ); + $form['roblib_bee_limits_fieldset']['max_booking_duration'] = array( + '#type' => 'number', + '#default_value' => $config->get($role_machine_name . '_max_booking_duration'), + '#title' => t('Max Booking Duration (in minutes)'), + '#min' => 0, + '#max' => 1440, + '#size' => 4, + ); +} + +/** + * Additional form validation for role_edit_form. + * + * + * @param $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + * + * @throws \Exception + */ +function roblib_bee_limits_validate_user_role_form($form, FormStateInterface $form_state) { + //for now validation is handled by the FAPI number element +} + +/** + * Additional submit handler for role edit form. + * + * @param $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + */ +function roblib_bee_limits_submit_user_role_form($form, FormStateInterface $form_state) { + $config_factory = \Drupal::configFactory(); + $role_machine_name = $form['id']['#default_value']; + $config_factory->getEditable('roblib_bee_limits.settings') + ->set($role_machine_name . '_active_bookings_allowed', $form_state->getValue('active_bookings_allowed')) + ->set($role_machine_name . '_max_booking_duration', $form_state->getValue('max_booking_duration')) + ->save(); +} + /** * Additional form validation for bee_add_reservation_form. * @@ -33,8 +96,6 @@ 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(); - roblib_bee_limits_check_reservations($user, $form, $form_state); $values = $form_state->getValues(); $node = Node::load($values['node']); $config_factory = \Drupal::configFactory(); @@ -43,14 +104,29 @@ function roblib_bee_limits_validate_reservation($form, FormStateInterface $form_ if($bee_settings['bookable_type'] != 'hourly') { return; } + roblib_bee_limits_check_reservations($user, $form, $form_state); $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; - if($minutes_diff > 120) { - $form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.')); + $max_time = roblib_bee_limits_get_max_time($user); + if($minutes_diff > $max_time) { + $form_state->setError($form, t('Reservation exceeded your max + booking time of @max_time minutes.', array('@max_time' => $max_time))); + } +} + +function roblib_bee_limits_get_max_time($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 roblib_bee_limits_check_reservations($user, $form, &$form_state) { @@ -71,16 +147,22 @@ function roblib_bee_limits_check_reservations($user, $form, &$form_state) { } $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.')); + $form_state->setError($form, t('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))); } } 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; + $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; }