From ebef5a388ca0a72b9af013c8692348143e3e2f31 Mon Sep 17 00:00:00 2001 From: iglocska Date: Wed, 3 Apr 2024 12:06:17 +0200 Subject: [PATCH] chg: [UI] event view now only load analyst data for objects/attributes actually shown via pagination --- app/Controller/EventsController.php | 4 +- app/Model/Behavior/AnalystDataBehavior.php | 2 +- .../Behavior/AnalystDataParentBehavior.php | 23 ++++++++++- app/Model/Event.php | 39 +++++++++++++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 61ae6b162..8c0a4df4b 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -1490,7 +1490,6 @@ class EventsController extends AppController $containsProposals = true; } } - foreach ($event['Object'] as $k => $object) { $modDate = date("Y-m-d", $object['timestamp']); $modificationMap[$modDate] = !isset($modificationMap[$modDate])? 1 : $modificationMap[$modDate] + 1; @@ -1522,7 +1521,6 @@ class EventsController extends AppController } } } - if ($containsProposals && $this->__canPublishEvent($event, $user)) { $mess = $this->Session->read('Message'); if (empty($mess)) { @@ -1696,8 +1694,8 @@ class EventsController extends AppController } $namedParams = $this->request->params['named']; - $conditions['includeAnalystData'] = true; if ($this->_isRest()) { + $conditions['includeAnalystData'] = true; $conditions['includeAttachments'] = isset($namedParams['includeAttachments']) ? $namedParams['includeAttachments'] : true; } else { $conditions['includeAllTags'] = true; diff --git a/app/Model/Behavior/AnalystDataBehavior.php b/app/Model/Behavior/AnalystDataBehavior.php index 60cb00f18..069b80ac2 100644 --- a/app/Model/Behavior/AnalystDataBehavior.php +++ b/app/Model/Behavior/AnalystDataBehavior.php @@ -76,7 +76,7 @@ class AnalystDataBehavior extends ModelBehavior ]); $results = []; foreach ($temp as $result) { - $results[$result[$type]['object_uuid']] = $result; + $results[$result[$type]['object_uuid']][$type][] = $result[$type]; } return $results; } diff --git a/app/Model/Behavior/AnalystDataParentBehavior.php b/app/Model/Behavior/AnalystDataParentBehavior.php index b3bc419c8..f1f336834 100644 --- a/app/Model/Behavior/AnalystDataParentBehavior.php +++ b/app/Model/Behavior/AnalystDataParentBehavior.php @@ -38,10 +38,31 @@ class AnalystDataParentBehavior extends ModelBehavior return $data; } + public function fetchAnalystDataBulk(Model $model, array $uuids, array $types = ['Note', 'Opinion', 'Relationship']) { + $uuids = array_chunk($uuids, 10000); + if (empty($this->__currentUser)) { + $user_id = Configure::read('CurrentUserId'); + $this->User = ClassRegistry::init('User'); + if ($user_id) { + $this->__currentUser = $this->User->getAuthUser($user_id); + } + } + $results = []; + foreach ($uuids as $uuid_chunk) { + foreach ($types as $type) { + $this->{$type} = ClassRegistry::init($type); + $this->{$type}->fetchRecursive = !empty($model->includeAnalystDataRecursive); + $temp = $this->{$type}->fetchForUuids($uuid_chunk, $this->__currentUser); + $results = array_merge($results, $temp); + } + } + return $results; + } + public function attachAnalystDataBulk(Model $model, array $objects, array $types = ['Note', 'Opinion', 'Relationship']) { $uuids = []; - $objects = array_chunk($objects, 10000); + $objects = array_chunk($objects, 10000, true); if (empty($this->__currentUser)) { $user_id = Configure::read('CurrentUserId'); $this->User = ClassRegistry::init('User'); diff --git a/app/Model/Event.php b/app/Model/Event.php index 20b0fe886..2e4c47f19 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -5587,6 +5587,7 @@ class Event extends AppModel $passedArgs['page'] = 0; } $params = $customPagination->applyRulesOnArray($objects, $passedArgs, 'events', 'category'); + $objects = $this->attachAnalystDataToViewObjects($objects); foreach ($objects as $k => $object) { if (isset($referencedByArray[$object['uuid']])) { foreach ($referencedByArray[$object['uuid']] as $objectType => $references) { @@ -5599,6 +5600,44 @@ class Event extends AppModel return $params; } + // take a list of paginated, rearranged objects from the event view generation's viewUI() function + // collect all attribute and object uuids from the object list + // fetch the related analyst data and inject them back into the object list + public function attachAnalystDataToViewObjects($objects) + { + $attribute_notes = []; + $object_notes = []; + foreach ($objects as $k => $object) { + if ($object['objectType'] === 'object') { + $object_notes[] = $object['uuid']; + foreach ($object['Attribute'] as $a) { + $attribute_notes[] = $a['uuid']; + } + } else if ($object['objectType'] === 'attribute') { + $attribute_notes[] = $object['uuid']; + } + } + $attribute_notes = $this->Attribute->fetchAnalystDataBulk($attribute_notes); + $object_notes = $this->Object->fetchAnalystDataBulk($object_notes); + foreach ($objects as $k => $object) { + if ($object['objectType'] === 'object') { + if (!empty($object_notes[$object['uuid']])) { + $objects[$k] = array_merge($object, $object_notes[$object['uuid']]); + } + foreach ($object['Attribute'] as $k2 => $a) { + if (!empty($attribute_notes[$a['uuid']])) { + $objects[$k]['Attribute'][$k2] = array_merge($a, $attribute_notes[$a['uuid']]); + } + } + } else if ($object['objectType'] === 'attribute') { + if (!empty($attribute_notes[$object['uuid']])) { + $objects[$k] = array_merge($object, $attribute_notes[$object['uuid']]); + } + } + } + return $objects; + } + // pass along a json from the server filter rules // returns a conditions set to be merged into pagination / event fetch / etc public function filterRulesToConditions($rules)