summaryrefslogtreecommitdiffstats
path: root/apps/files/service
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-11-18 18:53:45 +0100
committerVincent Petry <pvince81@owncloud.com>2014-12-15 12:10:54 +0100
commita5bb66f4a723bce5c5fbe919a48cd5133204ef62 (patch)
treeb2e067bde8aaa1de6973adc7760fafb1e37e9084 /apps/files/service
parentc6be491a89a4eebe15bcb20f6e0b01f23a093761 (diff)
downloadnextcloud-server-a5bb66f4a723bce5c5fbe919a48cd5133204ef62.tar.gz
nextcloud-server-a5bb66f4a723bce5c5fbe919a48cd5133204ef62.zip
Added favorites feature to the files app
Diffstat (limited to 'apps/files/service')
-rw-r--r--apps/files/service/tagservice.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/apps/files/service/tagservice.php b/apps/files/service/tagservice.php
new file mode 100644
index 00000000000..86885e38ddd
--- /dev/null
+++ b/apps/files/service/tagservice.php
@@ -0,0 +1,94 @@
+<?php
+/**
+ * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\Files\Service;
+
+/**
+ * Service class to manage tags on files.
+ */
+class TagService {
+
+ /**
+ * @var \OCP\IUserSession
+ */
+ private $userSession;
+
+ /**
+ * @var \OCP\ITags
+ */
+ private $tagger;
+
+ /**
+ * @var \OCP\Files\Folder
+ */
+ private $homeFolder;
+
+ public function __construct(
+ \OCP\IUserSession $userSession,
+ \OCP\ITags $tagger,
+ \OCP\Files\Folder $homeFolder
+ ) {
+ $this->userSession = $userSession;
+ $this->tagger = $tagger;
+ $this->homeFolder = $homeFolder;
+ }
+
+ /**
+ * Updates the tags of the specified file path.
+ * The passed tags are absolute, which means they will
+ * replace the actual tag selection.
+ *
+ * @param string $path path
+ * @param array $tags array of tags
+ * @return array list of tags
+ * @throws \OCP\NotFoundException if the file does not exist
+ */
+ public function updateFileTags($path, $tags) {
+ $fileId = $this->homeFolder->get($path)->getId();
+
+ $currentTags = $this->tagger->getTagsForObjects(array($fileId));
+
+ if (!empty($currentTags)) {
+ $currentTags = current($currentTags);
+ }
+
+ $newTags = array_diff($tags, $currentTags);
+ foreach ($newTags as $tag) {
+ $this->tagger->tagAs($fileId, $tag);
+ }
+ $deletedTags = array_diff($currentTags, $tags);
+ foreach ($deletedTags as $tag) {
+ $this->tagger->unTag($fileId, $tag);
+ }
+
+ // TODO: re-read from tagger to make sure the
+ // list is up to date, in case of concurrent changes ?
+ return $tags;
+ }
+
+ /**
+ * Updates the tags of the specified file path.
+ * The passed tags are absolute, which means they will
+ * replace the actual tag selection.
+ *
+ * @param array $tagName tag name to filter by
+ * @return FileInfo[] list of matching files
+ * @throws \Exception if the tag does not exist
+ */
+ public function getFilesByTag($tagName) {
+ $nodes = $this->homeFolder->searchByTag(
+ $tagName, $this->userSession->getUser()->getUId()
+ );
+ foreach ($nodes as &$node) {
+ $node = $node->getFileInfo();
+ }
+
+ return $nodes;
+ }
+}
+