Compare commits

...

5 Commits

  1. 46
      reservation.inc
  2. 14
      reserve.inc
  3. 2
      reserve.info.yml
  4. 6
      reserve.module
  5. 2
      reserve.series.inc
  6. 7
      src/Controller/CalendarController.php
  7. 1
      src/Form/ReserveDailyHoursForm.php
  8. 5
      src/Form/ReserveDefaultHoursForm.php
  9. 2
      src/Form/ReserveSettingsForm.php
  10. 3
      src/Plugin/Field/FieldFormatter/ReserveCategoryFormatter.php
  11. 3
      src/Plugin/Field/FieldType/ReserveCategory.php
  12. 6
      src/Plugin/Field/FieldWidget/ReserveCategorySelect.php
  13. 46
      src/Tests/LoadTest.php

46
reservation.inc

@ -8,6 +8,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\StringTranslation\TranslatableMarkup;
function reserve_form_reserve_reservation_form_alter(array &$form, FormStateInterface $form_state) { function reserve_form_reserve_reservation_form_alter(array &$form, FormStateInterface $form_state) {
// params either passed in on url - CREATE // params either passed in on url - CREATE
@ -79,33 +80,15 @@ function reserve_form_reserve_reservation_form_alter(array &$form, FormStateInte
$d = $yyyymmdd . ' 00:00:00'; $d = $yyyymmdd . ' 00:00:00';
if (reserve_daily_max_exceeded($yyyymmdd) && !$book_extended) { if (reserve_daily_max_exceeded($yyyymmdd) && !$book_extended) {
$form = array(); $daily_exceeded_message = t('You have exceeded the max number of daily bookings. Unable to add reservation.');
$form['message'] = [ reserve_set_modal_message($form, $daily_exceeded_message);
'#type' => 'markup',
'#weight' => -25,
'#markup' => '
<div class="messages__wrapper layout-container">
<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">' .
t('You have exceeded the max number of daily bookings. Unable to add reservation.') .
'</div></div>',
];
return;
} }
$config = \Drupal::config('reserve.settings'); $config = \Drupal::config('reserve.settings');
$max_per_user = $config->get('reservations_per_user'); $max_per_user = $config->get('reservations_per_user');
if (count(reserve_user_reservations()) > $max_per_user if (count(reserve_user_reservations()) > $max_per_user
&& !$book_extended) { && !$book_extended) {
$form = array(); $max_reservations_exceeded_message = t('You have exceeded your max number of open bookings. Unable to add reservation.');
$form['message'] = [ reserve_set_modal_message($form, $max_reservations_exceeded_message);
'#type' => 'markup',
'#weight' => -25,
'#markup' => '
<div class="messages__wrapper layout-container">
<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">' .
t('You have exceeded your max number of open bookings. Unable to add reservation.') .
'</div></div>',
];
return;
} }
} }
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($eid); $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($eid);
@ -117,9 +100,9 @@ function reserve_form_reserve_reservation_form_alter(array &$form, FormStateInte
// set default Group Name as name of current user // set default Group Name as name of current user
$form['name']['widget'][0]['value']['#required'] = TRUE; $form['name']['widget'][0]['value']['#required'] = TRUE;
$form['name']['widget'][0]['value']['#default_value'] = $form['name']['widget'][0]['value']['#default_value'] ? /*$form['name']['widget'][0]['value']['#default_value'] = $form['name']['widget'][0]['value']['#default_value'] ?
$form['name']['widget'][0]['value']['#default_value'] : $form['name']['widget'][0]['value']['#default_value'] :
\Drupal::currentUser()->getDisplayName(); \Drupal::currentUser()->getDisplayName();*/
// set values taken from URL // set values taken from URL
$form['reservation_date']['widget'][0]['value']['#default_value'] = DrupalDateTime::createFromTimestamp(strtotime($yyyymmdd)); $form['reservation_date']['widget'][0]['value']['#default_value'] = DrupalDateTime::createFromTimestamp(strtotime($yyyymmdd));
@ -296,6 +279,21 @@ function reserve_return_to_home_page($form, FormStateInterface $form_state) {
$form_state->setRedirectUrl($url); $form_state->setRedirectUrl($url);
} }
function reserve_set_modal_message(&$form, $message) {
$form['message'] = [
'#type' => 'markup',
'#weight' => -25,
'#markup' => '
<div class="messages__wrapper layout-container">
<div role="contentinfo" aria-label="Warning message" class="messages messages--warning">' .
$message .
'</div></div>',
];
$form['message']['#errors'] = TRUE;
$form['actions']['submit']['#attributes']['disabled'] = 'disabled';
$form['actions']['submit']['#access'] = FALSE;
}
/** /**
* Custom submit handler for login form. * Custom submit handler for login form.
*/ */

14
reserve.inc

@ -1,4 +1,5 @@
<?php <?php
use Drupal\field\Entity\FieldConfig;
/** /**
* @file * @file
* Classes and general Room Reservations helper functions. * Classes and general Room Reservations helper functions.
@ -21,13 +22,13 @@ function reserve_categories($ebundle = null) {
$entity_type = ebundle_split($ebundle, 'type'); $entity_type = ebundle_split($ebundle, 'type');
$bundle = ebundle_split($ebundle, 'bundle'); $bundle = ebundle_split($ebundle, 'bundle');
$field = reserve_category_fields($ebundle); $field = reserve_category_fields($ebundle);
$fconfig = \Drupal\field\Entity\FieldConfig::loadByName($entity_type, $bundle, $field)->getSettings(); $fconfig = FieldConfig::loadByName($entity_type, $bundle, $field)->getSettings();
$ids = array_filter($fconfig['categories']); $ids = array_filter($fconfig['categories']);
} }
else { else {
$query = \Drupal::service('entity_type.manager') $query = \Drupal::service('entity_type.manager')
->getStorage('reserve_category')->getQuery(); ->getStorage('reserve_category')->getQuery();
$ids = $query->condition('status', TRUE)->execute(); $ids = $query->condition('status', TRUE)->accessCheck(FALSE)->execute();
} }
$cats = \Drupal::entityTypeManager()->getStorage('reserve_category')->loadMultiple($ids); $cats = \Drupal::entityTypeManager()->getStorage('reserve_category')->loadMultiple($ids);
@ -66,7 +67,7 @@ function reserve_entities($ebundle) {
} }
//->sort('reservations_display_order', 'ASC') //->sort('reservations_display_order', 'ASC')
$ids = $query->execute(); $ids = $query->accessCheck(FALSE)->execute();
$entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple($ids); $entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple($ids);
@ -696,7 +697,7 @@ function reserve_start_conflicts($room, $yyyy_mmdd, $time) {
) )
"; ";
$conflicts_found = FALSE; $conflicts_found = FALSE;
$conflicts_found = db_query($sql, array( $conflicts_found = \Drupal::database()->query($sql, array(
':room' => $room, ':room' => $room,
':date0' => $search_items[0]['date'], ':time0' => $search_items[0]['start_time'], ':length0' => $search_items[0]['length'], ':date0' => $search_items[0]['date'], ':time0' => $search_items[0]['start_time'], ':length0' => $search_items[0]['length'],
':date1' => $search_items[1]['date'], ':time1' => $search_items[1]['start_time'], ':length1' => $search_items[1]['length'], ':date1' => $search_items[1]['date'], ':time1' => $search_items[1]['start_time'], ':length1' => $search_items[1]['length'],
@ -705,8 +706,7 @@ function reserve_start_conflicts($room, $yyyy_mmdd, $time) {
':date4' => $search_items[4]['date'], ':time4' => $search_items[4]['start_time'], ':length4' => $search_items[4]['length'], ':date4' => $search_items[4]['date'], ':time4' => $search_items[4]['start_time'], ':length4' => $search_items[4]['length'],
':date5' => $search_items[5]['date'], ':time5' => $search_items[5]['start_time'], ':length5' => $search_items[5]['length'], ':date5' => $search_items[5]['date'], ':time5' => $search_items[5]['start_time'], ':length5' => $search_items[5]['length'],
':date6' => $search_items[6]['date'], ':time6' => $search_items[6]['start_time'], ':length6' => $search_items[6]['length'], ':date6' => $search_items[6]['date'], ':time6' => $search_items[6]['start_time'], ':length6' => $search_items[6]['length'],
':date7' => $search_items[7]['date'], ':time7' => $search_items[7]['start_time'], ':length7' => $search_items[7]['length']) ':date7' => $search_items[7]['date'], ':time7' => $search_items[7]['start_time'], ':length7' => $search_items[7]['length']))->rowCount();
)->rowCount();
return $conflicts_found; return $conflicts_found;
} }
@ -933,6 +933,7 @@ function reserve_daily_max_exceeded($yyyy_mmdd) {
if ($user->id()) { if ($user->id()) {
$ids = \Drupal::service('entity_type.manager') $ids = \Drupal::service('entity_type.manager')
->getStorage('reserve_reservation')->getQuery() ->getStorage('reserve_reservation')->getQuery()
->accessCheck(FALSE)
->condition('user_id', $user->id()) ->condition('user_id', $user->id())
->condition('reservation_date', $yyyy_mmdd . '%', 'like') ->condition('reservation_date', $yyyy_mmdd . '%', 'like')
->execute(); ->execute();
@ -966,6 +967,7 @@ function reserve_user_reservations() {
$latest_date = date('Y-m-d', strtotime("now +13 days")); $latest_date = date('Y-m-d', strtotime("now +13 days"));
$ids = \Drupal::service('entity_type.manager') $ids = \Drupal::service('entity_type.manager')
->getStorage('reserve_reservation')->getQuery() ->getStorage('reserve_reservation')->getQuery()
->accessCheck(TRUE)
->condition('user_id', $user->id()) ->condition('user_id', $user->id())
->condition('reservation_date', $earliest_date, '>=') ->condition('reservation_date', $earliest_date, '>=')
->condition('reservation_date', $latest_date, '<=') ->condition('reservation_date', $latest_date, '<=')

2
reserve.info.yml

@ -2,7 +2,7 @@ name: Reserve
type: module type: module
description: Reservation system. description: Reservation system.
package: Reserve package: Reserve
core_version_requirement: ^8 || ^9 core_version_requirement: ^9.5 || ^10
dependencies: dependencies:
- drupal:views - drupal:views
- drupal:options - drupal:options

6
reserve.module

@ -16,9 +16,9 @@ use Drupal\Core\Form\FormStateInterface;
define ('RESERVE_SAVE_CONFIRMATION_MSG', t('Configuration settings have been saved')); define ('RESERVE_SAVE_CONFIRMATION_MSG', t('Configuration settings have been saved'));
define ('RESERVE_RESET_CONFIRMATION_MSG', t('Configuration settings have been reset to their default values')); define ('RESERVE_RESET_CONFIRMATION_MSG', t('Configuration settings have been reset to their default values'));
module_load_include('inc', 'reserve', 'reserve'); include(dirname(__FILE__) . '/reserve.inc');
module_load_include('inc', 'reserve', 'reserve.series'); include(dirname(__FILE__) . '/reserve.series.inc');
module_load_include('inc', 'reserve', 'reservation'); include(dirname(__FILE__) . '/reservation.inc');
$GLOBALS['debug'] = false; $GLOBALS['debug'] = false;

2
reserve.series.inc

@ -130,6 +130,7 @@ function reserve_reserve_reservation_update($entity) {
// grab all reservations in this series except the one being submitted // grab all reservations in this series except the one being submitted
$ids = \Drupal::service('entity_type.manager') $ids = \Drupal::service('entity_type.manager')
->getStorage('reserve_reservation')->getQuery() ->getStorage('reserve_reservation')->getQuery()
->accessCheck(TRUE)
->condition('status', TRUE) ->condition('status', TRUE)
->condition('reservation_series_id', $sid) ->condition('reservation_series_id', $sid)
->condition('id', $entity->id(), '!=') ->condition('id', $entity->id(), '!=')
@ -183,6 +184,7 @@ function _reserve_series_delete($entity) {
// grab all reservations in this series // grab all reservations in this series
$ids = \Drupal::service('entity_type.manager') $ids = \Drupal::service('entity_type.manager')
->getStorage('reserve_reservation')->getQuery() ->getStorage('reserve_reservation')->getQuery()
->accessCheck(TRUE)
->condition('reservation_series_id', $sid) ->condition('reservation_series_id', $sid)
->execute(); ->execute();
$results = \Drupal::entityTypeManager()->getStorage('reserve_reservation')->loadMultiple($ids); $results = \Drupal::entityTypeManager()->getStorage('reserve_reservation')->loadMultiple($ids);

7
src/Controller/CalendarController.php

@ -1,6 +1,7 @@
<?php <?php
namespace Drupal\reserve\Controller; namespace Drupal\reserve\Controller;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Link; use Drupal\Core\Link;
@ -334,7 +335,7 @@ class CalendarController extends ControllerBase {
$extended = \Drupal::currentUser()->hasPermission('add reservations extended'); $extended = \Drupal::currentUser()->hasPermission('add reservations extended');
$config = \Drupal::config('reserve.settings'); $config = \Drupal::config('reserve.settings');
$category_field = reserve_category_fields($ebundle); $category_field = reserve_category_fields($ebundle);
$clearimg = '<img src="' . base_path() . drupal_get_path('module', 'reserve') . '/images/clear.png" />'; $clearimg = '<img src="' . base_path() . \Drupal::service('extension.list.module')->getPath('reserve') . '/images/clear.png" />';
$modal = ['attributes' => [ $modal = ['attributes' => [
'class' => ['use-ajax'], 'class' => ['use-ajax'],
'data-dialog-type' => 'modal', 'data-dialog-type' => 'modal',
@ -371,12 +372,12 @@ class CalendarController extends ControllerBase {
$variables['#calendar-text'] = $config->get('calendar_text'); $variables['#calendar-text'] = $config->get('calendar_text');
$variables['#reserve_room_instructions_text'] = $config->get('reserve_instructions') ? $config->get('reserve_instructions') : $variables['#reserve_room_instructions_text'] = $config->get('reserve_instructions') ? $config->get('reserve_instructions') :
t('To make a reservation, click on the desired time/day in the calendar below. You will be asked to login.'); t('To make a reservation, click on the desired time/day in the calendar below. You will be asked to login.');
$variables['#arrow'] = base_path() . drupal_get_path('module', 'reserve') . '/images/arrow-icon.png'; $variables['#arrow'] = base_path() . \Drupal::service('extension.list.module')->getPath('reserve') . '/images/arrow-icon.png';
$variables['#date'] = \Drupal::service('date.formatter')->format(strtotime($month . ' ' . $xday . ', ' . $year), 'custom', 'l, F d, Y'); $variables['#date'] = \Drupal::service('date.formatter')->format(strtotime($month . ' ' . $xday . ', ' . $year), 'custom', 'l, F d, Y');
$variables['#date_picker'] = \Drupal::formBuilder()->getForm('Drupal\reserve\Form\CalendarDatePicker'); $variables['#date_picker'] = \Drupal::formBuilder()->getForm('Drupal\reserve\Form\CalendarDatePicker');
$field = reserve_category_fields($ebundle); $field = reserve_category_fields($ebundle);
$fconfig = \Drupal\field\Entity\FieldConfig::loadByName($entity_type, $bundle, $field)->getSettings(); $fconfig = FieldConfig::loadByName($entity_type, $bundle, $field)->getSettings();
$variables['#calendar_header'] = $fconfig['calendar_header']; $variables['#calendar_header'] = $fconfig['calendar_header'];
$variables['#reservation_instructions'] = $fconfig['reservation_instructions']; $variables['#reservation_instructions'] = $fconfig['reservation_instructions'];

1
src/Form/ReserveDailyHoursForm.php

@ -5,7 +5,6 @@ namespace Drupal\reserve\Form;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
module_load_include('inc', 'reserve', 'reserve.admin');
/** /**
* Class ReserveCategorySettingsForm. * Class ReserveCategorySettingsForm.

5
src/Form/ReserveDefaultHoursForm.php

@ -5,8 +5,6 @@ namespace Drupal\reserve\Form;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
module_load_include('inc', 'reserve', 'reserve.admin');
/** /**
* Class ReserveCategorySettingsForm. * Class ReserveCategorySettingsForm.
* *
@ -235,7 +233,6 @@ class ReserveDefaultHoursForm extends ConfigFormBase {
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues(); $values = $form_state->getValues();
$default_hours = array(); $default_hours = array();
if ($form_state->getTriggeringElement()['#name'] == 'save') { if ($form_state->getTriggeringElement()['#name'] == 'save') {
for ($day = 0; $day < 7; $day++) { for ($day = 0; $day < 7; $day++) {
@ -244,7 +241,7 @@ class ReserveDefaultHoursForm extends ConfigFormBase {
$default_hours[] = $values['day_' . $day]['second_shift_open_' . $day]; $default_hours[] = $values['day_' . $day]['second_shift_open_' . $day];
$default_hours[] = $values['day_' . $day]['second_shift_close_' . $day]; $default_hours[] = $values['day_' . $day]['second_shift_close_' . $day];
} }
$confirmation = t(RESERVE_SAVE_CONFIRMATION_MSG); $confirmation = RESERVE_SAVE_CONFIRMATION_MSG;
} }
if ($form_state->getTriggeringElement()['#name'] == 'reset') { if ($form_state->getTriggeringElement()['#name'] == 'reset') {
for ($day = 0; $day < 7; $day++) { for ($day = 0; $day < 7; $day++) {

2
src/Form/ReserveSettingsForm.php

@ -114,7 +114,7 @@ class ReserveSettingsForm extends ConfigFormBase {
'#title' => t('Time to show on calendar before/after open slots'), '#title' => t('Time to show on calendar before/after open slots'),
'#type' => 'select', '#type' => 'select',
'#options' => array(0 => '0 hours', 1 => t('1 hour'), 2 => t('2 hours'), '3' => t('All day')), '#options' => array(0 => '0 hours', 1 => t('1 hour'), 2 => t('2 hours'), '3' => t('All day')),
'#default_value' => $config->get('show_before_after_hours') ? $config->get('advanced_booking_admin') : 3, '#default_value' => $config->get('show_before_after_hours') ? $config->get('show_before_after_hours') : 3,
'#description' => t('The number of hours before the first and last open slots for the day that are shown on the calendar. The default is to display the entire day.'), '#description' => t('The number of hours before the first and last open slots for the day that are shown on the calendar. The default is to display the entire day.'),
); );

3
src/Plugin/Field/FieldFormatter/ReserveCategoryFormatter.php

@ -2,6 +2,7 @@
namespace Drupal\reserve\Plugin\Field\FieldFormatter; namespace Drupal\reserve\Plugin\Field\FieldFormatter;
use Drupal\reserve\Entity\ReserveCategory;
use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
@ -35,7 +36,7 @@ class ReserveCategoryFormatter extends FormatterBase {
$element = []; $element = [];
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
$category = \Drupal\reserve\Entity\ReserveCategory::load($item->cid); $category = ReserveCategory::load($item->cid);
// Render each element as markup. // Render each element as markup.
$element[$delta] = [ $element[$delta] = [
'#type' => 'markup', '#type' => 'markup',

3
src/Plugin/Field/FieldType/ReserveCategory.php

@ -64,7 +64,8 @@ class ReserveCategory extends FieldItemBase implements FieldItemInterface {
*/ */
public function fieldSettingsForm(array $form, FormStateInterface $form_state) { public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
// get a list of all Reserve Categories // get a list of all Reserve Categories
$ids = \Drupal::entityQuery('reserve_category')->sort('name', 'ASC')->execute(); $ids = \Drupal::entityQuery('reserve_category')->accessCheck(TRUE)
->sort('name', 'ASC')->execute();
$categories = \Drupal\reserve\Entity\ReserveCategory::loadMultiple($ids); $categories = \Drupal\reserve\Entity\ReserveCategory::loadMultiple($ids);
$options = array(); $options = array();
foreach ($categories as $cat) { foreach ($categories as $cat) {

6
src/Plugin/Field/FieldWidget/ReserveCategorySelect.php

@ -2,6 +2,7 @@
namespace Drupal\reserve\Plugin\Field\FieldWidget; namespace Drupal\reserve\Plugin\Field\FieldWidget;
use Drupal\reserve\Entity\ReserveCategory;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase; use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
@ -29,8 +30,9 @@ class ReserveCategorySelect extends WidgetBase {
// get a list of all Reserve Categories for this bundle // get a list of all Reserve Categories for this bundle
$set = $items->getSettings()['categories']; $set = $items->getSettings()['categories'];
$ids = \Drupal::entityQuery('reserve_category')->sort('name', 'ASC')->execute(); $ids = \Drupal::entityQuery('reserve_category')->accessCheck(TRUE)
$categories = \Drupal\reserve\Entity\ReserveCategory::loadMultiple($ids); ->sort('name', 'ASC')->execute();
$categories = ReserveCategory::loadMultiple($ids);
$options = array(); $options = array();
foreach ($categories as $key => $cat) { foreach ($categories as $key => $cat) {
if (!in_array($key, $set)) continue; if (!in_array($key, $set)) continue;

46
src/Tests/LoadTest.php

@ -1,46 +0,0 @@
<?php
namespace Drupal\reserve\Tests;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group reserve
*/
class LoadTest extends WebTestBase{
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['reserve'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertResponse(200);
}
}
Loading…
Cancel
Save