You are here

function bat_event_create_event_type_schema in Booking and Availability Management Tools for Drupal 8

Same name and namespace in other branches
  1. 7 modules/bat_event/bat_event.module \bat_event_create_event_type_schema()

Create 6 tables for store events of type $machine_name.

Parameters

string $machine_name:

1 call to bat_event_create_event_type_schema()
EventType::save in modules/bat_event/src/Entity/EventType.php
Saves an entity permanently.

File

modules/bat_event/bat_event.module, line 523
Manage Events - Events store the EventValue of a Unit over a period of time.

Code

function bat_event_create_event_type_schema($machine_name) {
  $schema = [];
  $schema['bat_event_' . $machine_name . '_day_state'] = [
    'description' => 'Holds the state of each unit for a given day',
    'fields' => bat_event_generate_day_schema_fields(),
    'indexes' => [
      'unit_id' => [
        'unit_id',
      ],
      'year' => [
        'year',
      ],
      'month' => [
        'month',
      ],
    ],
    'unique keys' => [
      'month_key' => [
        'unit_id',
        'year',
        'month',
      ],
    ],
    'foreign keys' => [
      'unit_id' => [
        'table' => 'unit',
        'columns' => [
          'unit_id' => 'id',
        ],
      ],
    ],
  ];
  $schema['bat_event_' . $machine_name . '_day_event'] = [
    'description' => 'Holds the event that determined the state of a unit',
    'fields' => bat_event_generate_day_schema_fields(),
    'indexes' => [
      'unit_id' => [
        'unit_id',
      ],
      'year' => [
        'year',
      ],
      'month' => [
        'month',
      ],
    ],
    'unique keys' => [
      'month_key' => [
        'unit_id',
        'year',
        'month',
      ],
    ],
    'foreign keys' => [
      'unit_id' => [
        'table' => 'unit',
        'columns' => [
          'unit_id' => 'id',
        ],
      ],
    ],
  ];
  $schema['bat_event_' . $machine_name . '_hour_state'] = [
    'description' => 'Holds the state of the unit',
    'fields' => bat_event_generate_hour_schema_fields(),
    'indexes' => [
      'unit_id' => [
        'unit_id',
      ],
      'year' => [
        'year',
      ],
      'month' => [
        'month',
      ],
    ],
    'unique keys' => [
      'day_key' => [
        'unit_id',
        'year',
        'month',
        'day',
      ],
    ],
    'foreign keys' => [
      'unit_id' => [
        'table' => 'unit',
        'columns' => [
          'unit_id' => 'id',
        ],
      ],
    ],
  ];
  $schema['bat_event_' . $machine_name . '_hour_event'] = [
    'description' => 'Holds the event that determined the state of a unit',
    'fields' => bat_event_generate_hour_schema_fields(),
    'indexes' => [
      'unit_id' => [
        'unit_id',
      ],
      'year' => [
        'year',
      ],
      'month' => [
        'month',
      ],
    ],
    'unique keys' => [
      'day_key' => [
        'unit_id',
        'year',
        'month',
        'day',
      ],
    ],
    'foreign keys' => [
      'unit_id' => [
        'table' => 'unit',
        'columns' => [
          'unit_id' => 'id',
        ],
      ],
    ],
  ];
  $schema['bat_event_' . $machine_name . '_minute_state'] = [
    'description' => 'Holds the state of the unit',
    'fields' => bat_event_generate_minute_schema_fields(),
    'indexes' => [
      'unit_id' => [
        'unit_id',
      ],
      'year' => [
        'year',
      ],
      'month' => [
        'month',
      ],
    ],
    'unique keys' => [
      'day_key' => [
        'unit_id',
        'year',
        'month',
        'day',
        'hour',
      ],
    ],
    'foreign keys' => [
      'unit_id' => [
        'table' => 'unit',
        'columns' => [
          'unit_id' => 'id',
        ],
      ],
    ],
  ];
  $schema['bat_event_' . $machine_name . '_minute_event'] = [
    'description' => 'Holds the event that determined the state of a unit',
    'fields' => bat_event_generate_minute_schema_fields(),
    'indexes' => [
      'unit_id' => [
        'unit_id',
      ],
      'year' => [
        'year',
      ],
      'month' => [
        'month',
      ],
    ],
    'unique keys' => [
      'day_key' => [
        'unit_id',
        'year',
        'month',
        'day',
        'hour',
      ],
    ],
    'foreign keys' => [
      'unit_id' => [
        'table' => 'unit',
        'columns' => [
          'unit_id' => 'id',
        ],
      ],
    ],
  ];
  foreach ($schema as $name => $table) {
    Database::getConnection()
      ->schema()
      ->createTable($name, $table);
  }
}