diff --git a/app/Controller/AttributesController.php b/app/Controller/AttributesController.php index 653f50130..734cb28c3 100644 --- a/app/Controller/AttributesController.php +++ b/app/Controller/AttributesController.php @@ -881,10 +881,16 @@ class AttributesController extends AppController throw new NotFoundException(__('Invalid Event.')); } if ($existingAttribute['Attribute']['object_id']) { - $result = $this->Attribute->save($this->request->data, array('fieldList' => $this->Attribute->editableFieds)); + $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->save($this->request->data, array('fieldList' => $this->Attribute->editableFieds)); + $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) { @@ -908,20 +914,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 d88a365bc..c46e723bd 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 convertObject($object, $isSiteAdmin = false, $raw = false) { $toRearrange = array('SharingGroup', 'Attribute', 'ShadowAttribute', 'Event'); diff --git a/app/Model/Attribute.php b/app/Model/Attribute.php index 43e7e60f5..0721c02e0 100644 --- a/app/Model/Attribute.php +++ b/app/Model/Attribute.php @@ -4303,8 +4303,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 - $this->AttributeTag->attachTagToAttribute($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 0dbe4a8c2..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( @@ -103,6 +127,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; + } + // This function help mirroring the tags at attribute level. It will delete tags that are not present on the remote attribute public function pruneOutdatedAttributeTagsFromSync($newerTags, $originalAttributeTags) { diff --git a/app/Model/Event.php b/app/Model/Event.php index 8d7494ef8..b931ee44f 100755 --- a/app/Model/Event.php +++ b/app/Model/Event.php @@ -3942,7 +3942,8 @@ class Event extends AppModel $tag_id = $this->EventTag->Tag->captureTag($tag, $user); if ($tag_id) { $nothingToChange = false; - $result = $this->EventTag->attachTagToEvent($this->id, $tag_id, $nothingToChange); + $tag['id'] = $tag_id; + $result = $this->EventTag->handleEventTag($this->id, $tag, $nothingToChange); if ($result && !$nothingToChange) { $changed = true; } diff --git a/app/Model/EventTag.php b/app/Model/EventTag.php index 4e0d38cbf..509b7dd59 100644 --- a/app/Model/EventTag.php +++ b/app/Model/EventTag.php @@ -110,6 +110,16 @@ class EventTag extends AppModel return $eventIDs; } + public function handleEventTag($event_id, $tag, &$nothingToChange = false) + { + if (empty($tag['deleted'])) { + $result = $this->attachTagToEvent($event_id, $tag['id'], $nothingToChange); + } else { + $result = $this->detachTagFromEvent($event_id, $tag['id'], $nothingToChange); + } + return $result; + } + public function attachTagToEvent($event_id, $tag_id, &$nothingToChange = false) { $existingAssociation = $this->find('first', array( @@ -130,6 +140,27 @@ class EventTag extends AppModel return true; } + public function detachTagFromEvent($event_id, $tag_id, &$nothingToChange = false) + { + $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; + } + } else { + $nothingToChange = true; + } + return false; + } + public function getSortedTagList($context = false) { $conditions = array();