|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\SystemTags\Command\Files; |
| 11 | + |
| 12 | +use OC\Core\Command\Info\FileUtils; |
| 13 | +use OCP\SystemTag\ISystemTagManager; |
| 14 | +use OCP\SystemTag\ISystemTagObjectMapper; |
| 15 | +use OCP\SystemTag\TagAlreadyExistsException; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class Add extends Command { |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private FileUtils $fileUtils, |
| 25 | + private ISystemTagManager $systemTagManager, |
| 26 | + private ISystemTagObjectMapper $systemTagObjectMapper, |
| 27 | + ) { |
| 28 | + parent::__construct(); |
| 29 | + } |
| 30 | + |
| 31 | + protected function configure(): void { |
| 32 | + $this->setName('tag:files:add') |
| 33 | + ->setDescription('Add a system-tag to a file or folder') |
| 34 | + ->addArgument('target', InputArgument::REQUIRED, 'file id or path') |
| 35 | + ->addArgument('tags', InputArgument::REQUIRED, 'Name of the tag(s) to add, comma separated') |
| 36 | + ->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); |
| 37 | + } |
| 38 | + |
| 39 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 40 | + $targetInput = $input->getArgument('target'); |
| 41 | + $tagsInput = $input->getArgument('tags'); |
| 42 | + |
| 43 | + if ($tagsInput === '') { |
| 44 | + $output->writeln('<error>`tags` can\'t be empty</error>'); |
| 45 | + return 3; |
| 46 | + } |
| 47 | + |
| 48 | + $tagNameArray = explode(',', $tagsInput); |
| 49 | + |
| 50 | + $access = $input->getArgument('access'); |
| 51 | + switch ($access) { |
| 52 | + case 'public': |
| 53 | + $userVisible = true; |
| 54 | + $userAssignable = true; |
| 55 | + break; |
| 56 | + case 'restricted': |
| 57 | + $userVisible = true; |
| 58 | + $userAssignable = false; |
| 59 | + break; |
| 60 | + case 'invisible': |
| 61 | + $userVisible = false; |
| 62 | + $userAssignable = false; |
| 63 | + break; |
| 64 | + default: |
| 65 | + $output->writeln('<error>`access` property is invalid</error>'); |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + $targetNode = $this->fileUtils->getNode($targetInput); |
| 70 | + |
| 71 | + if (! $targetNode) { |
| 72 | + $output->writeln("<error>file $targetInput not found</error>"); |
| 73 | + return 1; |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($tagNameArray as $tagName) { |
| 77 | + try { |
| 78 | + $tag = $this->systemTagManager->createTag($tagName, $userVisible, $userAssignable); |
| 79 | + $output->writeln("<info>$access</info> tag named <info>$tagName</info> created."); |
| 80 | + } catch (TagAlreadyExistsException $e) { |
| 81 | + $tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); |
| 82 | + } |
| 83 | + |
| 84 | + $this->systemTagObjectMapper->assignTags((string)$targetNode->getId(), 'files', $tag->getId()); |
| 85 | + $output->writeln("<info>$access</info> tag named <info>$tagName</info> added."); |
| 86 | + } |
| 87 | + |
| 88 | + return 0; |
| 89 | + } |
| 90 | +} |
0 commit comments