From 1fca4151f198c2a4b751087802921fd4a9403f5b Mon Sep 17 00:00:00 2001 From: ppound Date: Fri, 16 Oct 2020 10:47:10 -0300 Subject: [PATCH] initial commit --- roblib_bee_limits.info.yml | 8 +++ roblib_bee_limits.install | 38 +++++++++++++ roblib_bee_limits.libraries.yml | 37 ++++++++++++ roblib_bee_limits.links.menu.yml | 6 ++ roblib_bee_limits.module | 55 ++++++++++++++++++ roblib_bee_limits.permissions.yml | 4 ++ roblib_bee_limits.routing.yml | 7 +++ src/Controller/RoblibBeeLimitsController.php | 25 +++++++++ src/Form/SettingsForm.php | 59 ++++++++++++++++++++ 9 files changed, 239 insertions(+) create mode 100644 roblib_bee_limits.info.yml create mode 100644 roblib_bee_limits.install create mode 100644 roblib_bee_limits.libraries.yml create mode 100644 roblib_bee_limits.links.menu.yml create mode 100644 roblib_bee_limits.module create mode 100644 roblib_bee_limits.permissions.yml create mode 100644 roblib_bee_limits.routing.yml create mode 100644 src/Controller/RoblibBeeLimitsController.php create mode 100644 src/Form/SettingsForm.php diff --git a/roblib_bee_limits.info.yml b/roblib_bee_limits.info.yml new file mode 100644 index 0000000..5362da1 --- /dev/null +++ b/roblib_bee_limits.info.yml @@ -0,0 +1,8 @@ +name: roblib_bee_limits +type: module +description: Adds validation to limit both the number of active bookings as well as the max duration of bookings a user can have based on their roles. +package: Custom +core: 8.x +core_version_requirement: ^8 || ^9 +dependencies: + - bee diff --git a/roblib_bee_limits.install b/roblib_bee_limits.install new file mode 100644 index 0000000..b8d563e --- /dev/null +++ b/roblib_bee_limits.install @@ -0,0 +1,38 @@ +addStatus(__FUNCTION__); +} + +/** + * Implements hook_uninstall(). + */ +function roblib_bee_limits_uninstall() { + \Drupal::messenger()->addStatus(__FUNCTION__); +} + +/** + * Implements hook_requirements(). + */ +function roblib_bee_limits_requirements($phase) { + $requirements = []; + + if ($phase == 'runtime') { + $value = mt_rand(0, 100); + $requirements['roblib_bee_limits_status'] = [ + 'title' => t('roblib_bee_limits status'), + 'value' => t('roblib_bee_limits value: @value', ['@value' => $value]), + 'severity' => $value > 50 ? REQUIREMENT_INFO : REQUIREMENT_WARNING, + ]; + } + + return $requirements; +} diff --git a/roblib_bee_limits.libraries.yml b/roblib_bee_limits.libraries.yml new file mode 100644 index 0000000..b5d752b --- /dev/null +++ b/roblib_bee_limits.libraries.yml @@ -0,0 +1,37 @@ +# Custom module library for general purposes. +roblib_bee_limits: + js: + js/roblib-bee-limits.js: {} + css: + component: + css/roblib-bee-limits.css: {} + dependencies: + - core/drupalSettings + - roblib_bee_limits/jquery-labelauty + +# Third-party library (self hosted). +jquery-labelauty: + remote: https://github.com/fntneves/jquery-labelauty + version: 1.1.0 + license: + name: MIT + url: https://github.com/fntneves/jquery-labelauty/blob/v1.1.0/LICENSE + gpl-compatible: true + js: + /libraries/jquery-labelauty/source/jquery-labelauty.js: {} + css: + component: + /libraries/jquery-labelauty/source/jquery-labelauty.css: {} + dependencies: + - core/jquery + +# Third-party library (CDN). +vuejs: + remote: https://vuejs.org + version: 2.0.5 + license: + name: MIT + url: https://github.com/vuejs/vue/blob/dev/LICENSE + gpl-compatible: true + js: + https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js: {type: external, minified: true} diff --git a/roblib_bee_limits.links.menu.yml b/roblib_bee_limits.links.menu.yml new file mode 100644 index 0000000..a7a4772 --- /dev/null +++ b/roblib_bee_limits.links.menu.yml @@ -0,0 +1,6 @@ +roblib_bee_limits.settings_form: + title: roblib_bee_limits + description: Configure roblib_bee_limits. + parent: system.admin_config_system + route_name: roblib_bee_limits.settings_form + weight: 10 diff --git a/roblib_bee_limits.module b/roblib_bee_limits.module new file mode 100644 index 0000000..3c2454f --- /dev/null +++ b/roblib_bee_limits.module @@ -0,0 +1,55 @@ +id()); + $roles = $user->getRoles(); + debug($roles, 'pp-roles', TRUE); + $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; + debug($minutes_diff, 'pp-minutesdiff', TRUE); + if($minutes_diff > 120) { + $form_state->setError($form, t('Reservation exceeded max booking time of 2 hours.')); + } +} diff --git a/roblib_bee_limits.permissions.yml b/roblib_bee_limits.permissions.yml new file mode 100644 index 0000000..fa4fea7 --- /dev/null +++ b/roblib_bee_limits.permissions.yml @@ -0,0 +1,4 @@ +administer roblib_bee_limits configuration: + title: 'Administer roblib_bee_limits configuration' + description: 'Allow a user to configure limits on Bee reservation bookings.' + restrict access: true diff --git a/roblib_bee_limits.routing.yml b/roblib_bee_limits.routing.yml new file mode 100644 index 0000000..945920c --- /dev/null +++ b/roblib_bee_limits.routing.yml @@ -0,0 +1,7 @@ +roblib_bee_limits.settings_form: + path: '/admin/config/system/roblib-bee-limits' + defaults: + _title: 'roblib_bee_limits settings' + _form: 'Drupal\roblib_bee_limits\Form\SettingsForm' + requirements: + _permission: 'administer roblib_bee_limits configuration' diff --git a/src/Controller/RoblibBeeLimitsController.php b/src/Controller/RoblibBeeLimitsController.php new file mode 100644 index 0000000..7c8433f --- /dev/null +++ b/src/Controller/RoblibBeeLimitsController.php @@ -0,0 +1,25 @@ + 'item', + '#markup' => $this->t('It works!'), + ]; + + return $build; + } + +} diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php new file mode 100644 index 0000000..b54769f --- /dev/null +++ b/src/Form/SettingsForm.php @@ -0,0 +1,59 @@ + 'textfield', + '#title' => $this->t('Example'), + '#default_value' => $this->config('roblib_bee_limits.settings')->get('example'), + ]; + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + if ($form_state->getValue('example') != 'example') { + $form_state->setErrorByName('example', $this->t('The value is not correct.')); + } + parent::validateForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->config('roblib_bee_limits.settings') + ->set('example', $form_state->getValue('example')) + ->save(); + parent::submitForm($form, $form_state); + } + +}