Commit 93ed3190 authored by Nicolò Caruso's avatar Nicolò Caruso
Browse files

Handle generic event target_entity_type

parent 7b22e8a7
...@@ -363,36 +363,41 @@ function bat_api_services_resources() { ...@@ -363,36 +363,41 @@ function bat_api_services_resources() {
* @param $limit * @param $limit
*/ */
function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_type, $offset, $limit) { function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_type, $offset, $limit) {
$create_event_access = FALSE;
if (bat_event_access('create', bat_event_create(array('type' => $event_type)))) {
$create_event_access = TRUE;
}
$ids = array_filter(explode(',', $unit_ids)); $ids = array_filter(explode(',', $unit_ids));
$types = ($unit_types == 'all') ? array() : array_filter(explode(',', $unit_types));
$query = db_select('bat_units', 'n') if ($unit_types == 'all') {
->fields('n', array('unit_id', 'type_id', 'type', 'name')); $types = array();
if (!empty($ids)) { foreach (bat_unit_get_types() as $type => $info) {
$query->condition('unit_id', $ids, 'IN'); $types[] = $type;
} }
if (!empty($types)) {
$query->condition('type_id', $types, 'IN');
} }
$query->orderBy('type'); else {
$query->range($offset, $limit); $types = array_filter(explode(',', $unit_types));
$bat_units = $query->execute()->fetchAll(); }
$units = array(); $bat_event_type = bat_event_type_load($event_type);
$childrens = array(); $target_entity_type = $bat_event_type->target_entity_type;
$create_event_access = FALSE; $controller = entity_get_controller($target_entity_type);
if (bat_event_access('create', bat_event_create(array('type' => $event_type)))) { $units = array();
$create_event_access = TRUE;
}
foreach ($bat_units as $unit) { foreach ($types as $type) {
$entities = $controller->getReferencedIds($type, $unit_ids);
$childrens = array();
$childrens[$unit->type_id][] = array( foreach ($entities as $entity) {
'id' => 'S' . $unit->unit_id, $childrens[$entity['type_id']][] = array(
'title' => $unit->name, 'id' => 'S' . $entity['id'],
'title' => $entity['name'],
'create_event' => $create_event_access, 'create_event' => $create_event_access,
); );
} }
...@@ -406,6 +411,7 @@ function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_ty ...@@ -406,6 +411,7 @@ function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_ty
'children' => $children, 'children' => $children,
); );
} }
}
return $units; return $units;
} }
...@@ -622,7 +628,15 @@ function bat_api_services_events_index($target_ids, $target_types, $target_entit ...@@ -622,7 +628,15 @@ function bat_api_services_events_index($target_ids, $target_types, $target_entit
* @param $event_types * @param $event_types
*/ */
function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_date, $end_date, $event_types, $background) { function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_date, $end_date, $event_types, $background) {
$unit_types = ($unit_types == 'all') ? array() : array_filter(explode(',', $unit_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') { if ($event_types == 'all') {
$types = array(); $types = array();
...@@ -640,6 +654,10 @@ function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_d ...@@ -640,6 +654,10 @@ function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_d
// Get the event type definition from Drupal // Get the event type definition from Drupal
$bat_event_type = bat_event_type_load($type); $bat_event_type = bat_event_type_load($type);
$target_entity_type = $bat_event_type->target_entity_type;
$controller = entity_get_controller($target_entity_type);
// For each type of event create a state store and an event store // For each type of event create a state store and an event store
$event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT); $event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT);
...@@ -657,26 +675,17 @@ function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_d ...@@ -657,26 +675,17 @@ function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_d
$ids = array_filter(explode(',', $unit_ids)); $ids = array_filter(explode(',', $unit_ids));
// Get all units for a given type foreach ($unit_types as $unit_type) {
$query = db_select('bat_units', 'n') $entities = $controller->getReferencedIds($unit_type, $unit_ids);
->fields('n', array('unit_id', 'type', 'name'));
// and optionally limit to specific ids if defined in API call $childrens = array();
if (!empty($ids)) {
$query->condition('unit_id', $ids, 'IN');
}
// and types
if (!empty($unit_types)) {
$query->condition('type_id', $unit_types, 'IN');
}
$query->orderBy('unit_id');
$bat_units = $query->execute()->fetchAll();
// Create an array of unit objects - the default value is set to 0 since we want // 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 // 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) // which events are represented by events in the database (i.e. have a value different to 0)
$units = array(); $units = array();
foreach ($bat_units as $unit) { foreach ($entities as $entity) {
$units[] = new Unit($unit->unit_id, 0); $units[] = new Unit($entity['id'], 0);
} }
if (!empty($units)) { if (!empty($units)) {
...@@ -703,6 +712,7 @@ function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_d ...@@ -703,6 +712,7 @@ function bat_api_services_events_index_calendar($unit_ids, $unit_types, $start_d
} }
} }
} }
}
$events_json = _bat_api_merge_non_blocking_events($events_json); $events_json = _bat_api_merge_non_blocking_events($events_json);
......
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