|
|
|
<?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';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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());
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
$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) {
|
|
|
|
$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 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();
|
|
|
|
$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;
|
|
|
|
}
|