fix: Fixed the editing of tags using the rest API

pull/1857/head
iglocska 2017-01-16 16:05:19 +01:00
parent 44fcf5dfdb
commit a5663dfc07
3 changed files with 45 additions and 1 deletions

View File

@ -1008,7 +1008,9 @@ class EventsController extends AppController {
$sgs = $this->Event->SharingGroup->fetchAllAuthorised($this->Auth->user(), 'name', 1);
if ($this->request->is('post')) {
if ($this->_isRest()) {
if (empty($this->data)) {
throw new MethodNotAllowedException('No valid event data received.');
}
// rearrange the response if the event came from an export
if (isset($this->request->data['response'])) $this->request->data = $this->request->data['response'];

View File

@ -27,4 +27,19 @@ class AttributeTag extends AppModel {
),
);
public function attachTagToAttribute($attribute_id, $event_id, $tag_id) {
$existingAssociation = $this->find('first', array(
'recursive' => -1,
'conditions' => array(
'tag_id' => $tag_id,
'attribute_id' => $attribute_id
)
));
if (empty($existingAssociation)) {
$this->create();
if (!$this->save(array('attribute_id' => $attribute_id, 'event_id' => $event_id, 'tag_id' => $tag_id))) return false;
}
return true;
}
}

View File

@ -2025,6 +2025,7 @@ class Event extends AppModel {
foreach ($attribute['AttributeTag'] as $at) {
$this->Attribute->AttributeTag->create();
$at['attribute_id'] = $this->Attribute->id;
$at['event_id'] = $this->id;
$this->Attribute->AttributeTag->save($at);
}
}
@ -2182,6 +2183,32 @@ class Event extends AppModel {
'title' => 'Attribute dropped due to validation for Event ' . $this->id . ' failed: ' . $attribute_short,
'change' => json_encode($this->Attribute->validationErrors),
));
} else {
if (isset($data['Event']['Attribute'][$k]['Tag']) && $user['Role']['perm_tagger']) {
foreach ($data['Event']['Attribute'][$k]['Tag'] as $tag) {
$tag_id = $this->Attribute->AttributeTag->Tag->captureTag($tag, $user);
if ($tag_id) {
$this->Attribute->AttributeTag->attachTagToAttribute($this->Attribute->id, $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
// In which case if no matching tag is found, no tag ID is returned. Logging these is pointless as it is the correct behaviour.
if ($user['Role']['perm_tag_editor']) {
$this->Log->create();
$this->Log->save(array(
'org' => $user['Organisation']['name'],
'model' => 'Attrubute',
'model_id' => $this->Attribute->id,
'email' => $user['email'],
'action' => 'edit',
'user_id' => $user['id'],
'title' => 'Failed create or attach Tag ' . $tag['name'] . ' to the attribute.',
'change' => ''
));
}
}
}
}
}
}
}