Browse Source

pushing event validation to a rule action to see if it makes more sense than multiple validation paths

master
ppound 4 years ago
parent
commit
cc64e015e4
  1. 3
      roblib_bee_limits.module
  2. 117
      src/Plugin/RulesAction/BatEventValidation.php

3
roblib_bee_limits.module

@ -18,7 +18,8 @@ use Drupal\node\Entity\Node;
* @param $form_id * @param $form_id
*/ */
function roblib_bee_limits_form_bee_add_reservation_form_alter(&$form, FormStateInterface $form_state, $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';
} }
/** /**

117
src/Plugin/RulesAction/BatEventValidation.php

@ -0,0 +1,117 @@
<?php
/**
* @file
* Contains \Drupal\custom_pub\Plugin\RulesAction\SetCustomPublishingOption.
*/
namespace Drupal\roblib_bee_limits\Plugin\RulesAction;
use Drupal\rules\Core\RulesActionBase;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Provides an action to trigger a custom publishing option.
*
* @RulesAction(
* id = "rules_roblib_bee_limits_validate",
* label = @Translation("Additional validation on BAT Events"),
* category = @Translation("Event"),
* context = {
* "entity" = @ContextDefinition("entity:bat_event",
* label = @Translation("Event"),
* description = @Translation("Specifies the event, which should be validated.")
* )
* }
* )
*/
class BatEventValidation extends RulesActionBase {
/**
* Sets the custom publishing option on a given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $event
* The entity to be saved.
*/
protected function doExecute(EntityInterface $event) {
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->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;
}
}
}
Loading…
Cancel
Save