Browse Source

initial commit

master
ppound 4 years ago
commit
1fca4151f1
  1. 8
      roblib_bee_limits.info.yml
  2. 38
      roblib_bee_limits.install
  3. 37
      roblib_bee_limits.libraries.yml
  4. 6
      roblib_bee_limits.links.menu.yml
  5. 55
      roblib_bee_limits.module
  6. 4
      roblib_bee_limits.permissions.yml
  7. 7
      roblib_bee_limits.routing.yml
  8. 25
      src/Controller/RoblibBeeLimitsController.php
  9. 59
      src/Form/SettingsForm.php

8
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

38
roblib_bee_limits.install

@ -0,0 +1,38 @@
<?php
/**
* @file
* Install, update and uninstall functions for the roblib_bee_limits module.
*/
/**
* Implements hook_install().
*/
function roblib_bee_limits_install() {
\Drupal::messenger()->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;
}

37
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}

6
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

55
roblib_bee_limits.module

@ -0,0 +1,55 @@
<?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();
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.'));
}
}

4
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

7
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'

25
src/Controller/RoblibBeeLimitsController.php

@ -0,0 +1,25 @@
<?php
namespace Drupal\roblib_bee_limits\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Returns responses for roblib_bee_limits routes.
*/
class RoblibBeeLimitsController extends ControllerBase {
/**
* Builds the response.
*/
public function build() {
$build['content'] = [
'#type' => 'item',
'#markup' => $this->t('It works!'),
];
return $build;
}
}

59
src/Form/SettingsForm.php

@ -0,0 +1,59 @@
<?php
namespace Drupal\roblib_bee_limits\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure roblib_bee_limits settings for this site.
*/
class SettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'roblib_bee_limits_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['roblib_bee_limits.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['example'] = [
'#type' => '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);
}
}
Loading…
Cancel
Save