Commit aff25978 authored by nicola85's avatar nicola85

Add Events index

parent 64fdc09d
......@@ -5,4 +5,5 @@ core: 8.x
package: bat
dependencies:
- bat
- hal
- services
......@@ -6,7 +6,8 @@ _core:
id: bat_api
label: 'Bat api'
service_providers:
'events_index:calendar-events': 'events_index:calendar-events'
'calendar_events_index:calendar-events': 'calendar_events_index:calendar-events'
'events_index:events': 'events_index:events'
'unit_index:calendar-units': 'unit_index:calendar-units'
'entity_delete:bat_type_group': 'entity_delete:bat_type_group'
'entity_delete:bat_type_group_bundle': 'entity_delete:bat_type_group_bundle'
......
......@@ -6,43 +6,43 @@ Module port of Bat API for Drupal 8
### URL Operations
- Delete (DELETE)
bat_api/event/{event}
bat_api/bat_event/{bat_event}
bat_api/state/{state}
bat_api/property/{property}
bat_api/unit/{unit}
bat_api/unit_type/{unit_type}
bat_api/bat_type_group/{bat_type_group}
bat_api/bat_unit/{bat_unit}
bat_api/bat_unit_type/{bat_unit_type}
- Retrieve (GET)
bat_api/event/{event}
bat_api/bat_event/{bat_event}
bat_api/state/{state}
bat_api/property/{property}
bat_api/unit/{unit}
bat_api/unit_type/{unit_type}
bat_api/bat_type_group/{bat_type_group}
bat_api/bat_unit/{bat_unit}
bat_api/bat_unit_type/{bat_unit_type}
- Index (GET)
bat_api/event
bat_api/bat_event
bat_api/state
bat_api/property
bat_api/unit
bat_api/unit_type
bat_api/bat_type_group
bat_api/bat_unit
bat_api/bat_unit_type
- Create (POST)
bat_api/event
bat_api/bat_event
bat_api/state
bat_api/property
bat_api/unit
bat_api/unit_type
bat_api/bat_type_group
bat_api/bat_unit
bat_api/bat_unit_type
- Update (PATCH)
bat_api/event/{event}
bat_api/bat_event/{bat_event}
bat_api/state/{state}
bat_api/property/{property}
bat_api/unit/{unit}
bat_api/unit_type/{unit_type}
bat_api/bat_type_group/{bat_type_group}
bat_api/bat_unit/{bat_unit}
bat_api/bat_unit_type/{bat_unit_type}
- View (GET)
bat_api/event/{event}/view
bat_api/bat_event/{bat_event}/view
bat_api/state/{state}/view
bat_api/property/{property}/view
bat_api/unit/{unit}/view
bat_api/unit_type/{unit_type}/view
bat_api/bat_type_group/{bat_type_group}/view
bat_api/bat_unit/{bat_unit}/view
bat_api/bat_unit_type/{bat_unit_type}/view
<?php
/**
* @file
* Contains \Drupal\bat_api\Plugin\Deriver\CalendarEventsIndex.php
*/
namespace Drupal\bat_api\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CalendarEventsIndex extends DeriverBase implements ContainerDeriverInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('router.route_provider'),
$container->get('entity.manager')
);
}
public function getDerivativeDefinitions($base_plugin_definition) {
$entity_type_id = 'calendar-events';
$this->derivatives[$entity_type_id] = $base_plugin_definition;
$this->derivatives[$entity_type_id]['title'] = t('Calendar events');
$this->derivatives[$entity_type_id]['description'] = t('Index of events objects.');
$this->derivatives[$entity_type_id]['category'] = t('Calendar events');
$this->derivatives[$entity_type_id]['path'] = "$entity_type_id";
return $this->derivatives;
}
}
......@@ -7,7 +7,6 @@
namespace Drupal\bat_api\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -24,12 +23,12 @@ class EventsIndex extends DeriverBase implements ContainerDeriverInterface {
}
public function getDerivativeDefinitions($base_plugin_definition) {
$entity_type_id = 'calendar-events';
$entity_type_id = 'events';
$this->derivatives[$entity_type_id] = $base_plugin_definition;
$this->derivatives[$entity_type_id]['title'] = t('Calendar events');
$this->derivatives[$entity_type_id]['title'] = t('Events');
$this->derivatives[$entity_type_id]['description'] = t('Index of events objects.');
$this->derivatives[$entity_type_id]['category'] = t('Calendar events');
$this->derivatives[$entity_type_id]['category'] = t('Events');
$this->derivatives[$entity_type_id]['path'] = "$entity_type_id";
return $this->derivatives;
......
<?php
/**
* @file
* Contains \Drupal\bat_api\Plugin\ServiceDefinition\CalendarEventsIndex.php
*/
namespace Drupal\bat_api\Plugin\ServiceDefinition;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\services\ServiceDefinitionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Database\Database;
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;
use Drupal\bat_fullcalendar\FullCalendarOpenStateEventFormatter;
use Drupal\bat_fullcalendar\FullCalendarFixedStateEventFormatter;
/**
* @ServiceDefinition(
* id = "calendar_events_index",
* methods = {
* "GET"
* },
* translatable = true,
* deriver = "\Drupal\bat_api\Plugin\Deriver\CalendarEventsIndex"
* )
*/
class CalendarEventsIndex extends ServiceDefinitionBase implements ContainerFactoryPluginInterface {
/**
* The entity query factory.
*
* @var \Drupal\Core\Entity\Query\QueryFactoryInterface
*/
protected $queryFactory;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.query'),
$container->get('entity_type.manager')
);
}
/**
* @param array $configuration
* @param string $plugin_id
* @param mixed $plugin_definition
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryFactory $query_factory, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queryFactory = $query_factory;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 0;
}
/**
* {@inheritdoc}
*/
public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
$unit_types = $request->query->get('unit_types');
$event_types = $request->query->get('event_types');
$background = $request->query->get('background');
$unit_ids = $request->query->get('unit_ids');
$start_date = $request->query->get('start');
$end_date = $request->query->get('end');
if ($unit_types == 'all') {
$unit_types = array();
foreach (bat_unit_get_types() as $type => $info) {
$unit_types[] = $type;
}
}
else {
$unit_types = array_filter(explode(',', $unit_types));
}
if ($event_types == 'all') {
$types = array();
foreach (bat_event_get_types() as $type => $info) {
$types[] = $type;
}
}
else {
$types = array_filter(explode(',', $event_types));
}
$events_json = array();
foreach ($types as $type) {
// Check if user has permission to view calendar data for this event type.
if (!\Drupal::currentUser()->hasPermission('view calendar data for any ' . $type . ' event')) {
continue;
}
// Get the event type definition from Drupal
$bat_event_type = bat_event_type_load($type);
$target_entity_type = $bat_event_type->target_entity_type;
// For each type of event create a state store and an event store
$database = Database::getConnectionInfo('default');
$prefix = (isset($database['default']['prefix']['default'])) ? $database['default']['prefix']['default'] : '';
$event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT, $prefix);
$start_date_object = new \DateTime($start_date);
$end_date_object = new \DateTime($end_date);
$today = new \DateTime();
if (!\Drupal::currentUser()->hasPermission('view past event information') && $today > $start_date_object) {
if ($today > $end_date_object) {
$return->events = array();
return $return;
}
$start_date_object = $today;
}
$ids = array_filter(explode(',', $unit_ids));
foreach ($unit_types as $unit_type) {
$entities = $this->getReferencedIds($unit_type, $ids);
$childrens = array();
// Create an array of unit objects - the default value is set to 0 since we want
// to know if the value in the database is actually 0. This will allow us to identify
// which events are represented by events in the database (i.e. have a value different to 0)
$units = array();
foreach ($entities as $entity) {
$units[] = new Unit($entity['id'], 0);
}
if (!empty($units)) {
$event_calendar = new Calendar($units, $event_store);
$event_ids = $event_calendar->getEvents($start_date_object, $end_date_object);
if ($bat_event_type->getFixedEventStates()) {
$event_formatter = new FullCalendarFixedStateEventFormatter($bat_event_type, $background);
}
else {
$event_formatter = new FullCalendarOpenStateEventFormatter($bat_event_type, $background);
}
foreach ($event_ids as $unit_id => $unit_events) {
foreach ($unit_events as $key => $event) {
$events_json[] = array(
'id' => (string)$key . $unit_id,
'bat_id' => $event->getValue(),
'resourceId' => 'S' . $unit_id,
) + $event->toJson($event_formatter);
}
}
}
}
}
return $events_json;
}
public function getReferencedIds($unit_type, $ids = array()) {
$query = db_select('unit', 'n')
->fields('n', array('id', 'unit_type_id', 'type', 'name'));
if (!empty($ids)) {
$query->condition('id', $ids, 'IN');
}
$query->condition('unit_type_id', $unit_type);
$bat_units = $query->execute()->fetchAll();
$units = array();
foreach ($bat_units as $unit) {
$units[] = array(
'id' => $unit->id,
'name' => $unit->name,
'type_id' => $unit_type,
);
}
return $units;
}
}
......@@ -14,6 +14,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Database\Database;
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Store\DrupalDBStore;
......@@ -84,49 +85,23 @@ class EventsIndex extends ServiceDefinitionBase implements ContainerFactoryPlugi
* {@inheritdoc}
*/
public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
$unit_types = $request->query->get('unit_types');
$target_ids = $request->query->get('target_ids');
$target_types = $request->query->get('target_types');
$target_entity_type = $request->query->get('target_entity_type');
$start_date = $request->query->get('start_date');
$end_date = $request->query->get('end_date');
$event_types = $request->query->get('event_types');
$background = $request->query->get('background');
$unit_ids = $request->query->get('unit_ids');
$start_date = $request->query->get('start');
$end_date = $request->query->get('end');
$target_types = array_filter(explode(',', $target_types));
if ($unit_types == 'all') {
$unit_types = array();
foreach (bat_unit_get_types() as $type => $info) {
$unit_types[] = $type;
}
}
else {
$unit_types = array_filter(explode(',', $unit_types));
}
if ($event_types == 'all') {
$types = array();
foreach (bat_event_get_types() as $type => $info) {
$types[] = $type;
}
}
else {
$types = array_filter(explode(',', $event_types));
}
$types = array_filter(explode(',', $event_types));
$events_json = array();
foreach ($types as $type) {
// Check if user has permission to view calendar data for this event type.
if (!\Drupal::currentUser()->hasPermission('view calendar data for any ' . $type . ' event')) {
continue;
}
// Get the event type definition from Drupal
$bat_event_type = bat_event_type_load($type);
$target_entity_type = $bat_event_type->target_entity_type;
$database = Database::getConnectionInfo('default');
$prefix = (isset($database['default']['prefix']['default'])) ? $database['default']['prefix']['default'] : '';
// For each type of event create a state store and an event store
$prefix = (isset($databases['default']['default']['prefix'])) ? $databases['default']['default']['prefix'] : '';
$event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT, $prefix);
$start_date_object = new \DateTime($start_date);
......@@ -135,47 +110,44 @@ class EventsIndex extends ServiceDefinitionBase implements ContainerFactoryPlugi
$today = new \DateTime();
if (!\Drupal::currentUser()->hasPermission('view past event information') && $today > $start_date_object) {
if ($today > $end_date_object) {
$return->events = array();
return $return;
return array();
}
$start_date_object = $today;
}
$ids = array_filter(explode(',', $unit_ids));
foreach ($unit_types as $unit_type) {
$entities = $this->getReferencedIds($unit_type, $ids);
$childrens = array();
$ids = explode(',', $target_ids);
// Create an array of unit objects - the default value is set to 0 since we want
// to know if the value in the database is actually 0. This will allow us to identify
// which events are represented by events in the database (i.e. have a value different to 0)
$units = array();
foreach ($entities as $entity) {
$units[] = new Unit($entity['id'], 0);
$units = array();
foreach ($ids as $id) {
if ($target_entity = entity_load($target_entity_type, $id)) {
if (in_array($target_entity->type, $target_types) || empty($target_types)) {
// Setting the default value to 0 since we are dealing with the events array
// so getting event IDs.
$units[] = new Unit($id, 0);
}
}
}
if (!empty($units)) {
$event_calendar = new Calendar($units, $event_store);
$event_ids = $event_calendar->getEvents($start_date_object, $end_date_object);
if (!empty($units)) {
$event_calendar = new Calendar($units, $event_store);
$event_ids = $event_calendar->getEvents($start_date_object, $end_date_object);
if ($bat_event_type->getFixedEventStates()) {
$event_formatter = new FullCalendarFixedStateEventFormatter($bat_event_type, $background);
}
else {
$event_formatter = new FullCalendarOpenStateEventFormatter($bat_event_type, $background);
}
$bat_event_type = bat_event_type_load($type);
if ($bat_event_type->getFixedEventStates()) {
$event_formatter = new FullCalendarFixedStateEventFormatter($bat_event_type);
}
else {
$event_formatter = new FullCalendarOpenStateEventFormatter($bat_event_type);
}
foreach ($event_ids as $unit_id => $unit_events) {
foreach ($unit_events as $key => $event) {
$events_json[] = array(
'id' => (string)$key . $unit_id,
'bat_id' => $event->getValue(),
'resourceId' => 'S' . $unit_id,
) + $event->toJson($event_formatter);
}
foreach ($event_ids as $unit_id => $unit_events) {
foreach ($unit_events as $key => $event) {
$events_json[] = array(
'id' => (string)$key . $unit_id,
'bat_id' => $event->getValue(),
'resourceId' => 'S' . $unit_id,
) + $event->toJson($event_formatter);
}
}
}
......@@ -184,25 +156,4 @@ class EventsIndex extends ServiceDefinitionBase implements ContainerFactoryPlugi
return $events_json;
}
public function getReferencedIds($unit_type, $ids = array()) {
$query = db_select('unit', 'n')
->fields('n', array('id', 'unit_type_id', 'type', 'name'));
if (!empty($ids)) {
$query->condition('id', $ids, 'IN');
}
$query->condition('unit_type_id', $unit_type);
$bat_units = $query->execute()->fetchAll();
$units = array();
foreach ($bat_units as $unit) {
$units[] = array(
'id' => $unit->id,
'name' => $unit->name,
'type_id' => $unit_type,
);
}
return $units;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment