You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.6 KiB
86 lines
2.6 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Primary module hooks for roblib_bee_limits module. |
|
* |
|
*/ |
|
|
|
use Drupal\Core\Form\FormBase; |
|
use Drupal\Core\Form\FormStateInterface; |
|
use Drupal\node\Entity\Node; |
|
|
|
/** |
|
* Implements hook_form_FORM_ID_alter. |
|
* |
|
* @param $form |
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
|
* @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'; |
|
} |
|
|
|
/** |
|
* Additional form validation for bee_add_reservation_form. |
|
* |
|
* Limits hourly bookings. |
|
* |
|
* @param $form |
|
* @param \Drupal\Core\Form\FormStateInterface $form_state |
|
* |
|
* @throws \Exception |
|
*/ |
|
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(); |
|
$bee_settings = $config_factory->get('node.type.' . $node->bundle())->get('bee'); |
|
// we only want to limit hourly reservations |
|
if($bee_settings['bookable_type'] != 'hourly') { |
|
return; |
|
} |
|
$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.')); |
|
} |
|
} |
|
|
|
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; |
|
} |
|
}
|
|
|