|
|
@ -21,6 +21,69 @@ function roblib_bee_limits_form_bee_add_reservation_form_alter(&$form, FormState |
|
|
|
$form['#validate'][] = 'roblib_bee_limits_validate_reservation'; |
|
|
|
$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. |
|
|
|
* 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) { |
|
|
|
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(); |
|
|
|
|
|
|
|
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(); |
|
|
@ -43,14 +104,29 @@ function roblib_bee_limits_validate_reservation($form, FormStateInterface $form_ |
|
|
|
if($bee_settings['bookable_type'] != 'hourly') { |
|
|
|
if($bee_settings['bookable_type'] != 'hourly') { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
roblib_bee_limits_check_reservations($user, $form, $form_state); |
|
|
|
$start_date = $values['start_date']; |
|
|
|
$start_date = $values['start_date']; |
|
|
|
$end_date = $values['end_date']; |
|
|
|
$end_date = $values['end_date']; |
|
|
|
$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; |
|
|
|
if($minutes_diff > 120) { |
|
|
|
$max_time = roblib_bee_limits_get_max_time($user); |
|
|
|
$form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.')); |
|
|
|
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) { |
|
|
|
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); |
|
|
|
$max_bookings_allowed = roblib_bee_limits_get_max_bookings_allowed($user); |
|
|
|
if($max_bookings_allowed != -1 && $active_bookings > $max_bookings_allowed){ |
|
|
|
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) { |
|
|
|
function roblib_bee_limits_get_max_bookings_allowed($user) { |
|
|
|
$roles = $user->getRoles(); |
|
|
|
$roles = $user->getRoles(); |
|
|
|
$is_admin = array_search('administrator', $roles); |
|
|
|
$config = \Drupal::config('roblib_bee_limits.settings'); |
|
|
|
if($is_admin) { |
|
|
|
$max_active_bookings = 0; |
|
|
|
return -1; |
|
|
|
foreach($roles as $role){ |
|
|
|
}else { |
|
|
|
$tmp = $config->get($role . '_active_bookings_allowed'); |
|
|
|
return 3; |
|
|
|
$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; |
|
|
|
} |
|
|
|
} |
|
|
|