diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 5c0bdb42a..f10074626 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -1208,20 +1208,6 @@ class EventsController extends AppController $event = $this->Sightingdb->attachToEvent($event, $this->Auth->user()); } $this->params->params['paging'] = array($this->modelClass => $params); - // workaround to get the event dates in to the attribute relations - $relatedDates = array(); - if (isset($event['RelatedEvent'])) { - foreach ($event['RelatedEvent'] as $relation) { - $relatedDates[$relation['Event']['id']] = $relation['Event']['date']; - } - if (isset($event['RelatedAttribute'])) { - foreach ($event['RelatedAttribute'] as $key => $relatedAttribute) { - foreach ($relatedAttribute as $key2 => $relation) { - $event['RelatedAttribute'][$key][$key2]['date'] = $relatedDates[$relation['id']]; - } - } - } - } $this->set('event', $event); $dataForView = array( 'Attribute' => array('attrDescriptions' => 'fieldDescriptions', 'distributionDescriptions' => 'distributionDescriptions', 'distributionLevels' => 'distributionLevels', 'shortDist' => 'shortDist'), @@ -1363,21 +1349,12 @@ class EventsController extends AppController 'Event' => array('eventDescriptions' => 'fieldDescriptions', 'analysisDescriptions' => 'analysisDescriptions', 'analysisLevels' => 'analysisLevels') ); - // workaround to get the event dates in to the attribute relations and number of correlation per related event - $relatedDates = array(); + // workaround to get number of correlation per related event $relatedEventCorrelationCount = array(); - if (!empty($event['RelatedEvent'])) { - foreach ($event['RelatedEvent'] as $relation) { - $relatedDates[$relation['Event']['id']] = $relation['Event']['date']; - } - if (!empty($event['RelatedAttribute'])) { - foreach ($event['RelatedAttribute'] as $key => $relatedAttribute) { - foreach ($relatedAttribute as $key2 => $relation) { - if (!empty($relatedDates[$relation['id']])) { - $event['RelatedAttribute'][$key][$key2]['date'] = $relatedDates[$relation['id']]; - } - $relatedEventCorrelationCount[$relation['id']][$relation['value']] = 1; - } + if (!empty($event['RelatedAttribute'])) { + foreach ($event['RelatedAttribute'] as $relatedAttribute) { + foreach ($relatedAttribute as $relation) { + $relatedEventCorrelationCount[$relation['id']][$relation['value']] = true; } } } diff --git a/app/Model/Event.php b/app/Model/Event.php index 5b45507d1..cb25daf6e 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -993,34 +993,47 @@ class Event extends AppModel 'order' => false, 'limit' => $max_correlations )); + if (empty($correlations)) { + return array(); + } + + $eventIds = []; + foreach ($correlations as $correlation) { + $eventIds[] = $correlation[$settings[$context]['correlationModel']]['event_id']; + } + + $conditions = $this->createEventConditions($user); + $conditions['AND']['Event.id'] = $eventIds; + $events = $this->find('all', array( + 'recursive' => -1, + 'conditions' => $conditions, + 'fields' => array('Event.id', 'Event.orgc_id', 'Event.info', 'Event.date'), + )); + + $eventInfos = array(); + foreach ($events as $event) { + $eventInfos[$event['Event']['id']] = $event['Event']; + } + $relatedAttributes = array(); - $orgc_ids = array(); - foreach ($correlations as $k => $correlation) { - if (empty($orgc_ids[$correlation[$settings[$context]['correlationModel']]['event_id']])) { - $temp = $this->find('first', array( - 'recursive' => -1, - 'conditions' => array('Event.id' => $correlation[$settings[$context]['correlationModel']]['event_id']), - 'fields' => array('Event.orgc_id') - )); - if (!empty($temp)) { - $orgc_ids[$correlation[$settings[$context]['correlationModel']]['event_id']] = $temp['Event']['orgc_id']; - } + foreach ($correlations as $correlation) { + $correlation = $correlation[$settings[$context]['correlationModel']]; + // User don't have access to correlated attribute event, skip. + if (!isset($eventInfos[$correlation['event_id']])) { + continue; } + + $eventInfo = $eventInfos[$correlation['event_id']]; $current = array( - 'id' => $correlation[$settings[$context]['correlationModel']]['event_id'], - 'attribute_id' => $correlation[$settings[$context]['correlationModel']]['attribute_id'], - 'info' => $correlation[$settings[$context]['correlationModel']]['info'], - 'value' => $correlation[$settings[$context]['correlationModel']]['value'], + 'id' => $correlation['event_id'], + 'attribute_id' => $correlation['attribute_id'], + 'value' => $correlation['value'], + 'org_id' => $eventInfo['orgc_id'], + 'info' => $eventInfo['info'], + 'date' => $eventInfo['date'], ); - if (!empty($orgc_ids[$correlation[$settings[$context]['correlationModel']]['event_id']])) { - $current['org_id'] = $orgc_ids[$correlation[$settings[$context]['correlationModel']]['event_id']]; - } else { - $current['org_id'] = 'unknown'; - } - if (empty($relatedAttributes[$correlation[$settings[$context]['correlationModel']][$settings[$context]['parentIdField']]]) || !in_array($current, $relatedAttributes[$correlation[$settings[$context]['correlationModel']][$settings[$context]['parentIdField']]])) { - $relatedAttributes[$correlation[$settings[$context]['correlationModel']][$settings[$context]['parentIdField']]][] = $current; - } - unset($correlations[$k]); + $parentId = $correlation[$settings[$context]['parentIdField']]; + $relatedAttributes[$parentId][] = $current; } return $relatedAttributes; }