You are here

bat_booking.module in Booking and Availability Management Tools for Drupal 8

Same filename and directory in other branches
  1. 7 modules/bat_booking/bat_booking.module

File

modules/bat_booking/bat_booking.module
View source
<?php

/**
 * @file
 */
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\bat_booking\Entity\Booking;
use Drupal\bat_booking\Entity\BookingBundle;

/**
 * Determines whether the given user has access to a unit.
 *
 * @param string $op
 *   The operation being performed. One of 'view', 'update', 'create', 'delete'
 *   or just 'edit' (being the same as 'create' or 'update').
 * @param \Drupal\bat_booking\Entity\Booking $booking
 *   Optionally a booking to check access for. If nothing is
 *   given, access for all bookings is determined.
 * @param object $account
 *   The user to check for. Leave it to NULL to check for the global user.
 *
 * @return \Drupal\Core\Access\AccessResult
 */
function bat_booking_access(EntityInterface $entity, $operation, AccountInterface $account) {
  return bat_entity_access($entity, $operation, $account);
}

/**
 * Access callback for the entity API.
 */
function bat_booking_type_access($op, $unit = NULL, $account = NULL) {
  return \Drupal::currentUser()
    ->hasPermission('administer bat_booking_bundle entities', $account);
}

/**
 * Create a booking object.
 */
function bat_booking_create($values = []) {
  return Booking::create($values);
}

/**
 * Create a booking type object.
 */
function bat_booking_type_create($values = []) {
  return BookingBundle::create($values);
}

/**
 * Menu argument loader; Load a booking bundle by string.
 *
 * @param $bundle
 *   The machine-readable name of a booking bundle to load.
 * @param bool $reset
 *   A boolean indicating whether the internal cache should be reset.
 *
 * @return array|false
 *   A booking bundle array or FALSE if $bundle does not exist.
 */
function bat_booking_type_load($bundle, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityTypeManager()
      ->getStorage('bat_booking_bundle')
      ->resetCache();
  }
  return BookingBundle::load($bundle);
}

/**
 * Gets an array of all booking types, keyed by the type name.
 *
 * @param string $type_name
 *   If set, the type with the given name is returned.
 * @param bool $reset
 *   A boolean indicating that the internal cache should be reset.
 *
 * @return \Drupal\bat_booking\Entity\BookingBundle[]
 *   Depending whether $type isset, an array of booking types or a single one.
 */
function bat_booking_get_bundles($type_name = NULL, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityTypeManager()
      ->getStorage('bat_booking_bundle')
      ->resetCache();
  }
  $types = BookingBundle::loadMultiple();
  return isset($type_name) ? $types[$type_name] : $types;
}

/**
 * Saves a booking type to the db.
 *
 * @param \Drupal\bat_booking\Entity\BookingBundle $booking_type
 *   The booking type to save.
 */
function bat_booking_type_save(BookingBundle $booking_type) {
  $booking_type
    ->save();
}

/**
 * Deletes a booking type from the db.
 */
function bat_booking_type_delete(BookingBundle $bundle) {
  $bundle
    ->delete();
}

/**
 * Fetches a booking object.
 *
 * @param int $booking_id
 *   Integer specifying the booking id.
 * @param bool $reset
 *   A boolean indicating whether the internal cache should be reset.
 * @return \Drupal\bat_booking\Entity\Booking|false
 *   A fully-loaded $booking object or FALSE if it cannot be loaded.
 *
 * @see bat_booking_load_multiple()
 */
function bat_booking_load($booking_id, $reset = FALSE) {
  $bookings = bat_booking_load_multiple([
    $booking_id,
  ], [], $reset);
  return reset($bookings);
}

/**
 * Loads multiple bookings based on certain conditions.
 *
 * @param array $booking_ids
 *   An array of booking IDs.
 * @param array $conditions
 *   An array of conditions to match against the {bat_bookings} table.
 * @param bool $reset
 *   A boolean indicating that the internal cache should be reset.
 *
 * @return array
 *   An array of booking objects, indexed by booking_id.
 *
 * @see bat_booking_load()
 */
function bat_booking_load_multiple($booking_ids = [], $conditions = [], $reset = FALSE) {
  if ($reset) {
    \Drupal::entityTypeManager()
      ->getStorage('bat_booking')
      ->resetCache();
  }
  if (!empty($conditions)) {
    $query = \Drupal::entityQuery('bat_booking');
    if (!empty($booking_ids)) {
      $query
        ->condition('id', $booking_ids, 'IN');
    }
    foreach ($conditions as $key => $value) {
      $query
        ->condition($key, $value);
    }
    $booking_ids = $query
      ->execute();
  }
  return Booking::loadMultiple($booking_ids);
}

/**
 * Deletes a Bat Booking.
 *
 * @param \Drupal\bat_booking\Entity\Booking $booking
 *   The Booking object that represents the booking to delete.
 */
function bat_booking_delete(Booking $booking) {
  $booking
    ->delete();
}

/**
 * Implements hook_theme().
 */
function bat_booking_theme() {
  return [
    'bat_booking_add_list' => [
      'variables' => [
        'content' => NULL,
      ],
    ],
  ];
}

/**
 * Prepares variables for list of available unit type bundles templates.
 *
 * Default template: bat-type-add-list.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - content: An array of unit type bundles.
 */
function template_preprocess_bat_booking_add_list(&$variables) {
  $variables['types'] = [];
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
      $variables['types'][$type
        ->id()] = [
        'type' => $type
          ->id(),
        'add_link' => Link::fromTextAndUrl($type
          ->label(), new Url('entity.bat_booking.add', [
          'booking_bundle' => $type
            ->id(),
        ])),
      ];
    }
  }
}

/**
 * Add "Start Date" field.
 */
function bat_booking_add_start_date_field($bundle) {
  $field_name = 'booking_start_date';
  $field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
  $field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
  if (empty($field_storage)) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'type' => 'datetime',
      'cardinality' => 1,
      'locked' => 1,
      'settings' => [
        'cache_count' => 4,
        'cache_enabled' => 0,
        'granularity' => [
          'day' => 'day',
          'hour' => 'hour',
          'minute' => 'minute',
          'month' => 'month',
          'second' => 0,
          'year' => 'year',
        ],
        'timezone_db' => '',
        'todate' => '',
        'tz_handling' => 'none',
      ],
    ]);
    $field_storage
      ->save();
  }
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'label' => 'Start Date',
      'bundle' => $bundle,
      'required' => FALSE,
      'settings' => [
        'default_value' => 'blank',
        'default_value2' => 'same',
        'default_value_code' => '',
        'default_value_code2' => '',
        'user_register_form' => FALSE,
      ],
    ]);
    $field
      ->save();
  }
}

/**
 * Add "End Date" field.
 */
function bat_booking_add_end_date_field($bundle) {
  $field_name = 'booking_end_date';
  $field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
  $field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
  if (empty($field_storage)) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'type' => 'datetime',
      'cardinality' => 1,
      'locked' => 1,
      'settings' => [
        'cache_count' => 4,
        'cache_enabled' => 0,
        'granularity' => [
          'day' => 'day',
          'hour' => 'hour',
          'minute' => 'minute',
          'month' => 'month',
          'second' => 0,
          'year' => 'year',
        ],
        'timezone_db' => '',
        'todate' => '',
        'tz_handling' => 'none',
      ],
    ]);
    $field_storage
      ->save();
  }
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'label' => 'End Date',
      'bundle' => $bundle,
      'required' => FALSE,
      'settings' => [
        'default_value' => 'blank',
        'default_value2' => 'same',
        'default_value_code' => '',
        'default_value_code2' => '',
        'user_register_form' => FALSE,
      ],
    ]);
    $field
      ->save();
  }
}

/**
 * Add "Event" reference field.
 */
function bat_booking_add_event_reference_field($bundle) {
  $field_name = 'booking_event_reference';
  $field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
  $field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
  if (empty($field_storage)) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'type' => 'entity_reference',
      'cardinality' => 1,
      'locked' => 1,
      'settings' => [
        'target_type' => 'bat_event',
      ],
    ]);
    $field_storage
      ->save();
  }
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'label' => 'Event',
      'bundle' => $bundle,
      'required' => FALSE,
    ]);
    $field
      ->save();
  }
}

/**
 * Implements hook_entity_insert().
 */
function bat_booking_entity_insert(EntityInterface $entity) {
  if ($entity
    ->bundle() == 'bat_booking_bundle') {
    bat_booking_add_start_date_field($entity
      ->id());
    bat_booking_add_end_date_field($entity
      ->id());
    bat_booking_add_event_reference_field($entity
      ->id());
  }
}

Functions

Namesort descending Description
bat_booking_access Determines whether the given user has access to a unit.
bat_booking_add_end_date_field Add "End Date" field.
bat_booking_add_event_reference_field Add "Event" reference field.
bat_booking_add_start_date_field Add "Start Date" field.
bat_booking_create Create a booking object.
bat_booking_delete Deletes a Bat Booking.
bat_booking_entity_insert Implements hook_entity_insert().
bat_booking_get_bundles Gets an array of all booking types, keyed by the type name.
bat_booking_load Fetches a booking object.
bat_booking_load_multiple Loads multiple bookings based on certain conditions.
bat_booking_theme Implements hook_theme().
bat_booking_type_access Access callback for the entity API.
bat_booking_type_create Create a booking type object.
bat_booking_type_delete Deletes a booking type from the db.
bat_booking_type_load Menu argument loader; Load a booking bundle by string.
bat_booking_type_save Saves a booking type to the db.
template_preprocess_bat_booking_add_list Prepares variables for list of available unit type bundles templates.