From 34c5570692b1715001b624a366793b8872e6b030 Mon Sep 17 00:00:00 2001 From: Tom King Date: Tue, 26 Nov 2019 09:12:07 +0000 Subject: [PATCH 1/3] new: Allow for attribute tag deletion via Event or Attribute edit. Clean and return the attribute tags on response from editing an attribute, update code to remove legacy --- app/Controller/AttributesController.php | 32 +++++++++++++------------ app/Lib/Tools/JSONConverterTool.php | 18 ++++++++++++++ app/Model/Attribute.php | 9 +++++-- app/Model/AttributeTag.php | 20 ++++++++++++++++ 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/app/Controller/AttributesController.php b/app/Controller/AttributesController.php index b3ae81fca..e5af5e8fa 100644 --- a/app/Controller/AttributesController.php +++ b/app/Controller/AttributesController.php @@ -893,7 +893,7 @@ class AttributesController extends AppController $result = $this->Attribute->save($this->request->data, array('Attribute.category', 'Attribute.value', 'Attribute.to_ids', 'Attribute.comment', 'Attribute.distribution', 'Attribute.sharing_group_id')); $this->Attribute->Object->updateTimestamp($existingAttribute['Attribute']['object_id']); } else { - $result = $this->Attribute->save($this->request->data); + $result = $this->Attribute->editAttribute($this->request->data['Attribute'], $event['Event']['id'], $this->Auth->user(), 0); if ($this->request->is('ajax')) { $this->autoRender = false; if ($result) { @@ -917,20 +917,22 @@ class AttributesController extends AppController $this->Attribute->Object->save($object); } } - if ($this->_isRest() || $this->response->type() === 'application/json') { - $saved_attribute = $this->Attribute->find('first', array( - 'conditions' => array('id' => $this->Attribute->id), - 'recursive' => -1, - 'fields' => $this->Attribute->defaultFields - )); - $response = array('response' => array('Attribute' => $saved_attribute['Attribute'])); - $this->set('response', $response); - if ($this->response->type() === 'application/json') { - $this->render('/Attributes/json/view'); - } else { - $this->render('view'); - } - return; + if ($this->_isRest()) { + $saved_attribute = $this->Attribute->find('first', array( + 'conditions' => array('id' => $this->Attribute->id), + 'recursive' => -1, + 'contain' => array('AttributeTag' => array('Tag')) + )); + if ($this->response->type() === 'application/json') { + $type = 'json'; + } else { + $type = 'xml'; + } + App::uses(strtoupper($type) . 'ConverterTool', 'Tools'); + $tool = strtoupper($type) . 'ConverterTool'; + $converter = new $tool(); + $saved_attribute = $converter->convertAttribute($saved_attribute, true); + return $this->RestResponse->viewData($saved_attribute, $type); } else { $this->redirect(array('controller' => 'events', 'action' => 'view', $eventId)); } diff --git a/app/Lib/Tools/JSONConverterTool.php b/app/Lib/Tools/JSONConverterTool.php index 256a24d5b..412c71ef6 100644 --- a/app/Lib/Tools/JSONConverterTool.php +++ b/app/Lib/Tools/JSONConverterTool.php @@ -11,6 +11,24 @@ class JSONConverterTool return ']}' . PHP_EOL; } + public function convertAttribute($attribute, $raw = false) + { + $toRearrange = array('AttributeTag'); + foreach ($toRearrange as $object) { + if (isset($attribute[$object])) { + $attribute['Attribute'][$object] = $attribute[$object]; + unset($attribute[$object]); + } + } + + // Submit as list to the attribute cleaner but obtain the only attribute + $attribute['Attribute'] = $this->__cleanAttributes(array($attribute['Attribute']))[0]; + if ($raw) { + return $attribute; + } + return json_encode($attribute, JSON_PRETTY_PRINT); + } + public function convert($event, $isSiteAdmin=false, $raw = false) { $toRearrange = array('Org', 'Orgc', 'SharingGroup', 'Attribute', 'ShadowAttribute', 'RelatedAttribute', 'RelatedEvent', 'Galaxy', 'Object'); diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index ee97379a4..c1d86bce4 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -4076,8 +4076,13 @@ class Attribute extends AppModel foreach ($attribute['Tag'] as $tag) { $tag_id = $this->AttributeTag->Tag->captureTag($tag, $user); if ($tag_id) { - // fix the IDs here - $this->AttributeTag->attachTagToAttribute($this->id, $attribute['event_id'], $tag_id); + if (!isset($tag['deleted'])) { + // fix the IDs here + $this->AttributeTag->attachTagToAttribute($this->id, $attribute['event_id'], $tag_id); + } else { + // Delete the tag + $this->AttributeTag->detachTagFromAttribute($this->id, $attribute['event_id'], $tag_id); + } } else { // If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons // However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor diff --git a/app/Model/AttributeTag.php b/app/Model/AttributeTag.php index 28eed6969..37e277734 100644 --- a/app/Model/AttributeTag.php +++ b/app/Model/AttributeTag.php @@ -103,6 +103,26 @@ class AttributeTag extends AppModel return true; } + public function detachTagFromAttribute($attribute_id, $event_id, $tag_id) + { + $existingAssociation = $this->find('first', array( + 'recursive' => -1, + 'conditions' => array( + 'tag_id' => $tag_id, + 'event_id' => $event_id, + 'attribute_id' => $attribute_id + ) + )); + + if (!empty($existingAssociation)) { + $result = $this->delete($existingAssociation['AttributeTag']['id']); + if ($result) { + return true; + } + } + return false; + } + public function countForTag($tag_id, $user) { return $this->find('count', array( From 9008b565ddce5b0cb6bef7fbdf2f9cfeb1332adf Mon Sep 17 00:00:00 2001 From: Tom King Date: Tue, 26 Nov 2019 12:40:15 +0000 Subject: [PATCH 2/3] new: Allow tag deletion for an event on update --- app/Model/Event.php | 8 +++++++- app/Model/EventTag.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/Model/Event.php b/app/Model/Event.php index 96c705058..6640aa242 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -3864,7 +3864,13 @@ class Event extends AppModel foreach ($data['Event']['Tag'] as $tag) { $tag_id = $this->EventTag->Tag->captureTag($tag, $user); if ($tag_id) { - $this->EventTag->attachTagToEvent($this->id, $tag_id); + if (!isset($tag['deleted'])) { + // fix the IDs here + $this->EventTag->attachTagToEvent($this->id, $tag_id); + } else { + // Delete the tag + $this->EventTag->detachTagFromEvent($this->id, $tag_id); + } } else { // If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons // However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor diff --git a/app/Model/EventTag.php b/app/Model/EventTag.php index 7fde8cde8..542fa6435 100644 --- a/app/Model/EventTag.php +++ b/app/Model/EventTag.php @@ -128,6 +128,25 @@ class EventTag extends AppModel return true; } + public function detachTagFromEvent($event_id, $tag_id) + { + $existingAssociation = $this->find('first', array( + 'recursive' => -1, + 'conditions' => array( + 'tag_id' => $tag_id, + 'event_id' => $event_id + ) + )); + + if (!empty($existingAssociation)) { + $result = $this->delete($existingAssociation['EventTag']['id']); + if ($result) { + return true; + } + } + return false; + } + public function getSortedTagList($context = false) { $conditions = array(); From 8d11600e2bec77129d1fd4b3a881fcb9c7549c3e Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 27 Feb 2020 11:07:17 +0100 Subject: [PATCH 3/3] chg: [tag] Support of untagging in Object's Attribute and other fixes - deleted: 0 is correctly handled - stopped usage of `editAttribute` from Attribute Controller --- app/Controller/AttributesController.php | 8 +++++++- app/Model/Attribute.php | 9 ++------- app/Model/AttributeTag.php | 24 ++++++++++++++++++++++++ app/Model/Event.php | 9 ++------- app/Model/EventTag.php | 9 +++++++++ 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/Controller/AttributesController.php b/app/Controller/AttributesController.php index c001a80b7..3df23634a 100644 --- a/app/Controller/AttributesController.php +++ b/app/Controller/AttributesController.php @@ -893,9 +893,15 @@ class AttributesController extends AppController } if ($existingAttribute['Attribute']['object_id']) { $result = $this->Attribute->save($this->request->data, array('Attribute.category', 'Attribute.value', 'Attribute.to_ids', 'Attribute.comment', 'Attribute.distribution', 'Attribute.sharing_group_id')); + if ($result) { + $this->Attribute->AttributeTag->handleAttributeTags($this->request->data['Attribute'], $event['Event']['id']); + } $this->Attribute->Object->updateTimestamp($existingAttribute['Attribute']['object_id']); } else { - $result = $this->Attribute->editAttribute($this->request->data['Attribute'], $event['Event']['id'], $this->Auth->user(), 0); + $result = $this->Attribute->save($this->request->data); + if ($result) { + $this->Attribute->AttributeTag->handleAttributeTags($this->request->data['Attribute'], $event['Event']['id']); + } if ($this->request->is('ajax')) { $this->autoRender = false; if ($result) { diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index 1f5a6300e..f14ecfcca 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -4257,14 +4257,9 @@ class Attribute extends AppModel foreach ($attribute['Tag'] as $tag) { $tag_id = $this->AttributeTag->Tag->captureTag($tag, $user); if ($tag_id) { + $tag['id'] = $tag_id; // fix the IDs here - if (!isset($tag['deleted'])) { - // fix the IDs here - $this->AttributeTag->attachTagToAttribute($this->id, $attribute['event_id'], $tag_id); - } else { - // Delete the tag - $this->AttributeTag->detachTagFromAttribute($this->id, $attribute['event_id'], $tag_id); - } + $this->AttributeTag->handleAttributeTag($this->id, $attribute['event_id'], $tag); } else { // If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons // However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor diff --git a/app/Model/AttributeTag.php b/app/Model/AttributeTag.php index 2cb052959..a39ca3126 100644 --- a/app/Model/AttributeTag.php +++ b/app/Model/AttributeTag.php @@ -85,6 +85,30 @@ class AttributeTag extends AppModel $this->delete($id); } + public function handleAttributeTags($attribute, $event_id) + { + if (isset($attribute['Tag'])) { + foreach ($attribute['Tag'] as $tag) { + if (!isset($tag['id'])) { + $tag_id = $this->Tag->lookupTagIdFromName($tag['name']); + $tag['id'] = $tag_id; + } + if ($tag['id'] > 0) { + $this->handleAttributeTag($attribute['id'], $event_id, $tag); + } + } + } + } + + public function handleAttributeTag($attribute_id, $event_id, $tag) + { + if (empty($tag['deleted'])) { + $this->attachTagToAttribute($attribute_id, $event_id, $tag['id']); + } else { + $this->detachTagFromAttribute($attribute_id, $event_id, $tag['id']); + } + } + public function attachTagToAttribute($attribute_id, $event_id, $tag_id) { $existingAssociation = $this->find('first', array( diff --git a/app/Model/Event.php b/app/Model/Event.php index f6ed61464..0e13c89f0 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -3738,13 +3738,8 @@ class Event extends AppModel foreach ($data['Event']['Tag'] as $tag) { $tag_id = $this->EventTag->Tag->captureTag($tag, $user); if ($tag_id) { - if (!isset($tag['deleted'])) { - // fix the IDs here - $this->EventTag->attachTagToEvent($this->id, $tag_id); - } else { - // Delete the tag - $this->EventTag->detachTagFromEvent($this->id, $tag_id); - } + $tag['id'] = $tag_id; + $this->EventTag->handleEventTag($this->id, $tag); } else { // If we couldn't attach the tag it is most likely because we couldn't create it - which could have many reasons // However, if a tag couldn't be added, it could also be that the user is a tagger but not a tag editor diff --git a/app/Model/EventTag.php b/app/Model/EventTag.php index 542fa6435..579a1d967 100644 --- a/app/Model/EventTag.php +++ b/app/Model/EventTag.php @@ -110,6 +110,15 @@ class EventTag extends AppModel return $eventIDs; } + public function handleEventTag($event_id, $tag) + { + if (empty($tag['deleted'])) { + $this->attachTagToEvent($event_id, $tag['id']); + } else { + $this->detachTagFromEvent($event_id, $tag['id']); + } + } + public function attachTagToEvent($event_id, $tag_id) { $existingAssociation = $this->find('first', array(