diff --git a/app/Controller/AnalystDataController.php b/app/Controller/AnalystDataController.php index c1fc94324..d9791777b 100644 --- a/app/Controller/AnalystDataController.php +++ b/app/Controller/AnalystDataController.php @@ -116,6 +116,13 @@ class AnalystDataController extends AppController $this->_setViewElements(); } + public function getRelatedElement($type, $uuid) + { + $this->__typeSelector('Relationship'); + $data = $this->AnalystData->getRelatedElement($this->Auth->user(), $type, $uuid); + return $this->RestResponse->viewData($data, 'json'); + } + private function __typeSelector($type) { foreach ($this->__valid_types as $vt) { if ($type === $vt) { diff --git a/app/Model/AppModel.php b/app/Model/AppModel.php index c104112fe..682518ec0 100644 --- a/app/Model/AppModel.php +++ b/app/Model/AppModel.php @@ -2070,6 +2070,7 @@ class AppModel extends Model `modified` datetime ON UPDATE CURRENT_TIMESTAMP, `distribution` tinyint(4) NOT NULL, `sharing_group_id` int(10) unsigned, + `relationship_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci, `related_object_uuid` varchar(40) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL, `related_object_type` varchar(80) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL, PRIMARY KEY (`id`), @@ -2080,6 +2081,7 @@ class AppModel extends Model KEY `orgc_uuid` (`orgc_uuid`), KEY `distribution` (`distribution`), KEY `sharing_group_id` (`sharing_group_id`), + KEY `relationship_type` (`relationship_type`), KEY `related_object_uuid` (`related_object_uuid`), KEY `related_object_type` (`related_object_type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; diff --git a/app/Model/Relationship.php b/app/Model/Relationship.php index 2163db5ec..ca864c86d 100644 --- a/app/Model/Relationship.php +++ b/app/Model/Relationship.php @@ -16,4 +16,108 @@ class Relationship extends AnalystData public $validate = array( ); + + /** @var object|null */ + protected $Event; + /** @var object|null */ + protected $Attribute; + /** @var object|null */ + protected $Object; + /** @var object|null */ + protected $Note; + /** @var object|null */ + protected $Opinion; + /** @var object|null */ + protected $Relationship; + /** @var object|null */ + protected $User; + /** @var array|null */ + private $__currentUser; + + public function afterFind($results, $primary = false) + { + $results = parent::afterFind($results, $primary); + if (empty($this->__currentUser)) { + $user_id = Configure::read('CurrentUserId'); + $this->User = ClassRegistry::init('User'); + if ($user_id) { + $this->__currentUser = $this->User->getAuthUser($user_id); + } + } + foreach ($results as $i => $v) { + $results[$i][$this->alias]['related_object'] = $this->getRelatedElement($this->__currentUser, $v[$this->alias]['related_object_type'], $v[$this->alias]['related_object_uuid']); + } + return $results; + } + + public function getRelatedElement(array $user, $type, $uuid): array + { + $data = []; + if ($type == 'Event') { + $this->Event = ClassRegistry::init('Event'); + $params = [ + ]; + $data = $this->Event->fetchSimpleEvent($user, $uuid, $params); + } else if ($type == 'Attribute') { + $this->Attribute = ClassRegistry::init('Attribute'); + $params = [ + 'conditions' => [ + ['Attribute.uuid' => $uuid], + ], + 'contain' => ['Event' => 'Orgc',] + ]; + $data = $this->Attribute->fetchAttributeSimple($user, $params); + $data = $this->rearrangeData($data, 'Attribute'); + $data['Attribute']['Organisation'] = $data['Attribute']['Event']['Orgc']; + $data['Attribute']['orgc_uuid'] = $data['Attribute']['Event']['Orgc']['uuid']; + unset($data['Attribute']['Event']['Orgc']); + } else if ($type == 'Object') { + $this->Object = ClassRegistry::init('MispObject'); + $params = [ + 'conditions' => [ + ['Object.uuid' => $uuid], + ] + ]; + $data = $this->Object->fetchObjectSimple($user, $params); + if (!empty($data)) { + $data = $data[0]; + } + } else if ($type == 'Note') { + $this->Note = ClassRegistry::init('Note'); + $params = [ + + ]; + $data = $this->Note->fetchNote(); + } else if ($type == 'Opinion') { + $this->Opinion = ClassRegistry::init('Opinion'); + $params = [ + + ]; + $data = $this->Opinion->fetchOpinion(); + } else if ($type == 'Relationship') { + $this->Relationship = ClassRegistry::init('Relationship'); + $params = [ + + ]; + $data = $this->Relationship->fetchRelationship(); + } + return $data; + } + + private function rearrangeData(array $data, $objectType): array + { + $models = ['Event', 'Attribute', 'Object', 'Organisation', ]; + if (!empty($data)) { + foreach ($models as $model) { + if ($model == $objectType) { + continue; + } + if (isset($data[$model])) { + $data[$objectType][$model] = $data[$model]; + unset($data[$model]); + } + } + } + return $data; + } } diff --git a/app/View/AnalystData/add.ctp b/app/View/AnalystData/add.ctp index 97833100f..b471c9923 100644 --- a/app/View/AnalystData/add.ctp +++ b/app/View/AnalystData/add.ctp @@ -62,9 +62,27 @@ if ($modelSelection === 'Note') { ] ] ); - } else if ($modelSelection === 'Relationship') { - + $fields = array_merge($fields, + [ + [ + 'field' => 'relationship_type', + 'class' => 'span4', + ], + [ + 'field' => 'related_object_type', + 'class' => 'span2', + 'options' => $dropdownData['valid_targets'], + 'type' => 'dropdown', + 'stayInLine' => 1 + ], + [ + 'field' => 'related_object_uuid', + 'class' => 'span4', + ], + sprintf('
', __('Related Object'), __('- No UUID provided -')) + ] + ); } echo $this->element('genericElements/Form/genericForm', [ 'data' => [ @@ -98,8 +116,13 @@ if (!$ajax) { var noteHTMLID = '#' + data[noteType].note_type_name + '-' + data[noteType].id var $noteToReplace = $(noteHTMLID) if ($noteToReplace.length == 1) { - console.log(data); - var compiledUpdatedNote = renderNote(data[noteType]) + var relatedObjects = {} + if (noteType == 'Relationship') { + var relationship = data[noteType] + relatedObjects[relationship['object_type']] = {} + relatedObjects[relationship['object_type']][relationship['related_object_uuid']] = relationship['related_object'][relationship['object_type']] + } + var compiledUpdatedNote = renderNote(data[noteType], relatedObjects) $noteToReplace[0].outerHTML = compiledUpdatedNote $(noteHTMLID).css({'opacity': 0}) setTimeout(() => { @@ -111,4 +134,52 @@ if (!$ajax) { function addNoteInUI(data) { location.reload() } - \ No newline at end of file + + function displayRelatedObject(data) { + if (Object.keys(data).length == 0) { + $('#related-object-container').html('') + } else { + var parsed = syntaxHighlightJson(data) + $('#related-object-container').html(parsed) + } + } + + function fetchAndDisplayRelatedObject(type, uuid) { + var url = baseurl + '/analystData/getRelatedElement/' + type + '/' + uuid + $.ajax({ + type: "get", + url: url, + headers: { Accept: "application/json" }, + success: function (data) { + console.log(data); + displayRelatedObject(data) + }, + error: function (data, textStatus, errorThrown) { + showMessage('fail', textStatus + ": " + errorThrown); + } + }); + } + + $(document).ready(function() { + $('#RelationshipRelatedObjectType').change(function(e) { + if ($('#RelationshipRelatedObjectUuid').val().length == 36) { + fetchAndDisplayRelatedObject($('#RelationshipRelatedObjectType').val(),$('#RelationshipRelatedObjectUuid').val()) + } + }) + $('#RelationshipRelatedObjectUuid').on('input', function(e) { + if ($('#RelationshipRelatedObjectUuid').val().length == 36) { + fetchAndDisplayRelatedObject($('#RelationshipRelatedObjectType').val(),$('#RelationshipRelatedObjectUuid').val()) + } + }) + }) + + + \ No newline at end of file diff --git a/app/View/AnalystData/index.ctp b/app/View/AnalystData/index.ctp index a0baa8d0d..60bbfb338 100644 --- a/app/View/AnalystData/index.ctp +++ b/app/View/AnalystData/index.ctp @@ -62,7 +62,20 @@ ); } else if ($modelSelection === 'Relationship') { - + $fields = array_merge($fields, + [ + [ + 'name' => __('Related Object Type'), + 'sort' => $modelSelection . '.related_object_type', + 'data_path' => $modelSelection . '.related_object_type' + ], + [ + 'name' => __('Related Object UUID'), + 'sort' => $modelSelection . '.related_object_uuid', + 'data_path' => $modelSelection . '.related_object_uuid' + ], + ] + ); } echo $this->element('genericElements/IndexTable/scaffold', [ diff --git a/app/View/Elements/genericElements/Analyst_data/generic.ctp b/app/View/Elements/genericElements/Analyst_data/generic.ctp index 762fdbe10..a7a47cbc3 100644 --- a/app/View/Elements/genericElements/Analyst_data/generic.ctp +++ b/app/View/Elements/genericElements/Analyst_data/generic.ctp @@ -183,11 +183,7 @@ $notes2 = [ ], ]; -$notes = $analyst_data['notes'] ?? []; -$opinions = $analyst_data['opinions'] ?? []; -$relationships = $analyst_data['relationships'] ?? []; - -$related_objects = [ +$related_objects2 = [ 'Event' => [ 'f80a0db2-24bd-4148-929e-7c803ade7ca1' => [ 'uuid' => 'f80a0db2-24bd-4148-929e-7c803ade7ca1', @@ -240,38 +236,56 @@ $related_objects = [ ], ]; -$notes = array_merge($notes, $opinions); +$notes = $analyst_data['notes'] ?? []; +$opinions = $analyst_data['opinions'] ?? []; +$relationships = $analyst_data['relationships'] ?? []; +$related_objects = [ + 'Attribute' => [], + 'Event' => [], + 'Object' => [], + 'Organisation' => [], + 'GalaxyCluster' => [], + 'Galaxy' => [], + 'Note' => [], + 'Opinion' => [], + 'SharingGroup' => [], +]; +foreach ($relationships as $relationship) { + $related_objects[$relationship['object_type']][$relationship['related_object_uuid']] = $relationship['related_object'][$relationship['object_type']]; +} + +$notesOpinions = array_merge($notes, $opinions); if(!function_exists("countNotes")) { - function countNotes($notes) { - $notesTotalCount = count($notes); + function countNotes($notesOpinions) { + $notesTotalCount = count($notesOpinions); $notesCount = 0; $relationsCount = 0; - foreach ($notes as $note) { - if ($note['note_type'] == 2) { // relationship + foreach ($notesOpinions as $notesOpinion) { + if ($notesOpinion['note_type'] == 2) { // relationship $relationsCount += 1; } else { $notesCount += 1; } - if (!empty($note['Note'])) { - $nestedCounts = countNotes($note['Note']); + if (!empty($notesOpinion['Note'])) { + $nestedCounts = countNotes($notesOpinion['Note']); $notesTotalCount += $nestedCounts['total']; - $notesCount += $nestedCounts['notes']; + $notesCount += $nestedCounts['notesOpinions']; $relationsCount += $nestedCounts['relations']; } - if (!empty($note['Opinion'])) { - $nestedCounts = countNotes($note['Opinion']); + if (!empty($notesOpinion['Opinion'])) { + $nestedCounts = countNotes($notesOpinion['Opinion']); $notesTotalCount += $nestedCounts['total']; - $notesCount += $nestedCounts['notes']; + $notesCount += $nestedCounts['notesOpinions']; $relationsCount += $nestedCounts['relations']; } } - return ['total' => $notesTotalCount, 'notes' => $notesCount, 'relations' => $relationsCount]; + return ['total' => $notesTotalCount, 'notesOpinions' => $notesCount, 'relations' => $relationsCount]; } } -$counts = countNotes($notes); -$notesCount = $counts['notes']; -$relationshipsCount = $counts['relations']; +$counts = countNotes($notesOpinions); +$notesOpinionCount = $counts['notesOpinions']; +$relationshipsCount = count($relationships); ?> @@ -279,7 +293,7 @@ $relationshipsCount = $counts['relations']; - + @@ -288,7 +302,7 @@ $relationshipsCount = $counts['relations'];