/** * Creates a new tag * * @param string $data JSON encoded string containing the properties of the tag to create * @param string $contentType content type of the data * @return ISystemTag newly created system tag * * @throws BadRequest if a field was missing * @throws Conflict if a tag with the same properties already exists * @throws UnsupportedMediaType if the content type is not supported */ private function createTag($data, $contentType = 'application/json') { if (explode(';', $contentType)[0] === 'application/json') { $data = json_decode($data, true); } else { throw new UnsupportedMediaType(); } if (!isset($data['name'])) { throw new BadRequest('Missing "name" attribute'); } $tagName = $data['name']; $userVisible = true; $userAssignable = true; if (isset($data['userVisible'])) { $userVisible = (bool) $data['userVisible']; } if (isset($data['userAssignable'])) { $userAssignable = (bool) $data['userAssignable']; } try { return $this->tagManager->createTag($tagName, $userVisible, $userAssignable); } catch (TagAlreadyExistsException $e) { throw new Conflict('Tag already exists', 0, $e); } }
/** * Creates a new tag * * @param string $data JSON encoded string containing the properties of the tag to create * @param string $contentType content type of the data * @return ISystemTag newly created system tag * * @throws BadRequest if a field was missing * @throws Conflict if a tag with the same properties already exists * @throws UnsupportedMediaType if the content type is not supported */ private function createTag($data, $contentType = 'application/json') { if (explode(';', $contentType)[0] === 'application/json') { $data = json_decode($data, true); } else { throw new UnsupportedMediaType(); } if (!isset($data['name'])) { throw new BadRequest('Missing "name" attribute'); } $tagName = $data['name']; $userVisible = true; $userAssignable = true; if (isset($data['userVisible'])) { $userVisible = (bool) $data['userVisible']; } if (isset($data['userAssignable'])) { $userAssignable = (bool) $data['userAssignable']; } if ($userVisible === false || $userAssignable === false) { if (!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) { throw new BadRequest('Not sufficient permissions'); } } try { return $this->tagManager->createTag($tagName, $userVisible, $userAssignable); } catch (TagAlreadyExistsException $e) { throw new Conflict('Tag already exists', 0, $e); } }
public function testDeleteTagRemovesRelations() { $tag1 = $this->tagManager->createTag('one', true, false); $tag2 = $this->tagManager->createTag('two', true, true); $tagMapper = new SystemTagObjectMapper($this->connection, $this->tagManager); $tagMapper->assignTags(1, 'testtype', $tag1->getId()); $tagMapper->assignTags(1, 'testtype', $tag2->getId()); $tagMapper->assignTags(2, 'testtype', $tag1->getId()); $this->tagManager->deleteTags($tag1->getId()); $tagIdMapping = $tagMapper->getTagIdsForObjects([1, 2], 'testtype'); $this->assertEquals([1 => [$tag2->getId()], 2 => []], $tagIdMapping); }
public function testTagGroups() { $tag1 = $this->tagManager->createTag('tag1', true, false); $tag2 = $this->tagManager->createTag('tag2', true, false); $this->tagManager->setTagGroups($tag1, ['group1', 'group2']); $this->tagManager->setTagGroups($tag2, ['group2', 'group3']); $this->assertEquals(['group1', 'group2'], $this->tagManager->getTagGroups($tag1)); $this->assertEquals(['group2', 'group3'], $this->tagManager->getTagGroups($tag2)); // change groups $this->tagManager->setTagGroups($tag1, ['group3', 'group4']); $this->tagManager->setTagGroups($tag2, []); $this->assertEquals(['group3', 'group4'], $this->tagManager->getTagGroups($tag1)); $this->assertEquals([], $this->tagManager->getTagGroups($tag2)); }