Commit 4c794bc6 authored by nicola85's avatar nicola85

Add new callback 'matching-units-calendar'

parent f93370c6
......@@ -199,6 +199,14 @@ function bat_api_services_resources() {
'source' => array('param' => 'event_type'),
'optional' => TRUE,
),
array(
'name' => 'children',
'type' => 'boolean',
'description' => t('Return children'),
'source' => array('param' => 'children'),
'optional' => TRUE,
'default value' => TRUE,
),
array(
'name' => 'offset',
'type' => 'string',
......@@ -350,6 +358,53 @@ function bat_api_services_resources() {
),
);
$resources['matching-units-calendar'] = array(
'operations' => array(
'index' => array(
'callback' => 'bat_api_services_matching_units_calendar',
'help' => t('Returns an object containing events information.'),
'access arguments' => array('access events calendar index service'),
'args' => array(
array(
'name' => 'unit_types',
'type' => 'string',
'description' => t('Unit Types'),
'source' => array('param' => 'unit_types'),
'optional' => TRUE,
),
array(
'name' => 'start',
'type' => 'string',
'description' => t('Start date'),
'source' => array('param' => 'start'),
'optional' => FALSE,
),
array(
'name' => 'end',
'type' => 'string',
'description' => t('End date'),
'source' => array('param' => 'end'),
'optional' => FALSE,
),
array(
'name' => 'event_type',
'type' => 'string',
'description' => t('Type'),
'source' => array('param' => 'event_type'),
'optional' => FALSE,
),
array(
'name' => 'event_states',
'type' => 'string',
'description' => t('Event States'),
'source' => array('param' => 'event_states'),
'optional' => FALSE,
),
),
),
),
);
return $resources;
}
......@@ -362,7 +417,7 @@ function bat_api_services_resources() {
* @param $offset
* @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, $return_children, $offset, $limit) {
$create_event_access = FALSE;
if (bat_event_access('create', bat_event_create(array('type' => $event_type)))) {
......@@ -412,11 +467,19 @@ function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_ty
foreach ($childrens as $type_id => $children) {
$unit_type = bat_type_load($type_id);
$units[] = array(
'id' => $unit_type->type_id,
'title' => $unit_type->name,
'children' => $children,
);
if ($return_children) {
$units[] = array(
'id' => $unit_type->type_id,
'title' => $unit_type->name,
'children' => $children,
);
}
else {
$units[] = array(
'id' => $unit_type->type_id,
'title' => $unit_type->name,
);
}
}
}
......@@ -823,6 +886,7 @@ function bat_api_services_edit_event($event_id, $bat_event_data) {
return $return;
}
/**
* Merge non blocking events so that we display them as a continuous single event.
*
......@@ -853,3 +917,111 @@ function _bat_api_merge_non_blocking_events($events) {
return $events;
}
/**
* Retrieve a list of events to use with Fullcalendar scheduler.
*
* @param $unit_types
* @param $start_date
* @param $end_date
* @param $event_types
* @param $event_states
*/
function bat_api_services_matching_units_calendar($unit_types, $start_date, $end_date, $event_type, $event_states) {
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));
}
$states = array_filter(explode(',', $event_states));
$events_json = array();
// Get the event type definition from Drupal
$bat_event_type = bat_event_type_load($event_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
$state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE);
$start_date_object = new DateTime($start_date);
$end_date_object = new DateTime($end_date);
$today = new DateTime();
if (!user_access('view past event information') && $today > $start_date_object) {
if ($today > $end_date_object) {
$return->events = array();
return $return;
}
$start_date_object = $today;
}
foreach ($unit_types as $unit_type) {
$entities = $controller->getReferencedIds($unit_type);
$childrens = array();
$units = array();
foreach ($entities as $entity) {
$target_entity = entity_load_single($target_entity_type, $entity['id']);
$units[] = new Unit($entity['id'], $target_entity->getEventDefaultValue($event_type));
}
if (!empty($units)) {
$dates = array(
$end_date_object->getTimestamp() => $end_date_object,
);
$calendar = new Calendar($units, $state_store);
$event_ids = $calendar->getEvents($start_date_object, $end_date_object);
foreach ($event_ids as $unit_id => $unit_events) {
foreach ($unit_events as $key => $event) {
$event_start_date = $event->getStartDate();
$dates[$event_start_date->getTimestamp()] = $event_start_date;
}
}
ksort($dates);
$dates = array_values($dates);
for ($i = 0; $i < (count($dates) - 1); $i++) {
$sd = $dates[$i];
$ed = clone($dates[$i + 1]);
$ed->sub(new DateInterval('PT1M'));
$response = $calendar->getMatchingUnits($sd, $ed, $states, array(), FALSE, FALSE);
if (count(array_keys($response->getIncluded()))) {
$color = 'green';
}
else {
$color = 'red';
}
$events_json[] = array(
'id' => $unit_type,
'resourceId' => $unit_type,
'start' => $sd->format('Y-m-d') . 'T' . $sd->format('H:i:00'),
'end' => $ed->format('Y-m-d') . 'T' . $ed->format('H:i:00'),
'color' => $color,
'rendering' => 'background',
'blocking' => 0,
'title' => '',
);
}
}
}
$events_json = _bat_api_merge_non_blocking_events($events_json);
return array_values($events_json);
}
......@@ -53,6 +53,13 @@ function bat_api_default_services_endpoint() {
),
),
),
'matching-units-calendar' => array(
'operations' => array(
'index' => array(
'enabled' => '1',
),
),
),
'units' => array(
'operations' => array(
'index' => array(
......
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