chg: [internal] Optimise Event::__attachReferences method

pull/6288/head
Jakub Onderka 2020-09-04 21:21:14 +02:00
parent b8663ebb28
commit 3153b45539
1 changed files with 30 additions and 24 deletions

View File

@ -1931,7 +1931,6 @@ class Event extends AppModel
}
$conditionsAttributes = array();
$conditionsObjects = array();
$conditionsObjectReferences = array();
if (isset($options['flatten']) && $options['flatten']) {
$flatten = true;
@ -2084,7 +2083,6 @@ class Event extends AppModel
$fieldsObj = array('*');
$fieldsShadowAtt = array('ShadowAttribute.id', 'ShadowAttribute.type', 'ShadowAttribute.category', 'ShadowAttribute.value', 'ShadowAttribute.to_ids', 'ShadowAttribute.uuid', 'ShadowAttribute.event_uuid', 'ShadowAttribute.event_id', 'ShadowAttribute.old_id', 'ShadowAttribute.comment', 'ShadowAttribute.org_id', 'ShadowAttribute.proposal_to_delete', 'ShadowAttribute.timestamp', 'ShadowAttribute.first_seen', 'ShadowAttribute.last_seen');
$fieldsOrg = array('id', 'name', 'uuid', 'local');
$fieldsServer = array('id', 'url', 'name');
if (!$options['includeAllTags']) {
$tagConditions = array('exportable' => 1);
} else {
@ -2112,7 +2110,6 @@ class Event extends AppModel
'conditions' => $conditionsObjects,
'order' => false,
'ObjectReference' => array(
'conditions' => $conditionsObjectReferences,
'order' => false
)
),
@ -2178,7 +2175,7 @@ class Event extends AppModel
unset($results[$eventKey]); // Current user cannot access sharing_group associated to this event
continue;
}
$this->__attachReferences($user, $event, $sgids, $fields);
$this->__attachReferences($event, $fields);
$this->__attachTags($user, $event, $tagConditions);
$event = $this->Orgc->attachOrgsToEvent($event, $fieldsOrg);
if (!$options['sgReferenceOnly'] && $event['Event']['sharing_group_id']) {
@ -6642,29 +6639,38 @@ class Event extends AppModel
return true;
}
private function __attachReferences($user, &$event, $sgids, $fields)
/**
* Attach referenced object to ObjectReference. Since reference can be just to attribute or object in the same event,
* we just find proper element in event.
*
* @param array $event
* @param array $fields
*/
private function __attachReferences(array &$event, array $fields)
{
if (!empty($event['Object'])) {
foreach ($event['Object'] as $k => $object) {
if (!empty($object['ObjectReference'])) {
foreach ($object['ObjectReference'] as $k2 => $reference) {
$type = array('Attribute', 'Object')[$reference['referenced_type']];
$temp = $this->{$type}->find('first', array(
'recursive' => -1,
'fields' => array_merge($fields['common'], $fields[array('Attribute', 'Object')[$reference['referenced_type']]]),
'conditions' => array('id' => $reference['referenced_id'])
));
if (!empty($temp)) {
if (!$user['Role']['perm_site_admin'] && $user['org_id'] != $event['Event']['orgc_id']) {
if ($temp[$type]['distribution'] == 0 || ($temp[$type]['distribution'] == 4 && !in_array($temp[$type]['sharing_group_id'], $sgids))) {
unset($object['ObjectReference'][$k2]);
continue;
}
}
$event['Object'][$k]['ObjectReference'][$k2][$type] = $temp[$type];
}
if (!isset($event['Object'])) {
return;
}
foreach ($event['Object'] as $k => $object) {
foreach ($object['ObjectReference'] as $k2 => $reference) {
// find referenced object in current event
$type = $reference['referenced_type'] == 0 ? 'Attribute' : 'Object';
$found = null;
foreach ($event[$type] as $o) {
if ($o['id'] == $reference['referenced_id']) {
$found = $o;
break;
}
}
if ($found) {
// copy requested fields
$reference = [];
foreach (array_merge($fields['common'], $fields[$type]) as $field) {
$reference[$field] = $found[$field];
}
$event['Object'][$k]['ObjectReference'][$k2][$type] = $reference;
}
}
}
}