|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | +namespace OCA\Deck\Command; |
| 8 | + |
| 9 | +use OCP\IConfig; |
| 10 | +use OCP\IUserManager; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | + |
| 16 | +class CalendarToggle extends Command { |
| 17 | + private IUserManager $userManager; |
| 18 | + private IConfig $config; |
| 19 | + |
| 20 | + public function __construct(IUserManager $userManager, IConfig $config) { |
| 21 | + parent::__construct(); |
| 22 | + $this->userManager = $userManager; |
| 23 | + $this->config = $config; |
| 24 | + } |
| 25 | + |
| 26 | + protected function configure() { |
| 27 | + $this |
| 28 | + ->setName('deck:calendar-toggle') |
| 29 | + ->setDescription('Enable or disable Deck calendar/tasks integration for all existing users. Users can still change their own setting afterwards. Only affects users that already exist at the time of execution.') |
| 30 | + ->addOption( |
| 31 | + 'on', |
| 32 | + null, |
| 33 | + InputOption::VALUE_NONE, |
| 34 | + 'Enable calendar/tasks integration for all existing users (users can opt-out later)' |
| 35 | + ) |
| 36 | + ->addOption( |
| 37 | + 'off', |
| 38 | + null, |
| 39 | + InputOption::VALUE_NONE, |
| 40 | + 'Disable calendar/tasks integration for all existing users (users can opt-in later)' |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 45 | + $enable = $input->getOption('on'); |
| 46 | + $disable = $input->getOption('off'); |
| 47 | + if ($enable && $disable) { |
| 48 | + $output->writeln('<error>Cannot use --on and --off together.</error>'); |
| 49 | + return 1; |
| 50 | + } |
| 51 | + if (!$enable && !$disable) { |
| 52 | + $output->writeln('<error>Please specify either --on or --off.</error>'); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + $value = $enable ? 'yes' : ''; |
| 56 | + $users = $this->userManager->search(''); |
| 57 | + $count = 0; |
| 58 | + foreach ($users as $user) { |
| 59 | + $uid = $user->getUID(); |
| 60 | + $this->config->setUserValue($uid, 'deck', 'calendar', $value); |
| 61 | + $output->writeln("Set calendar integration to '" . ($enable ? 'on' : 'off') . "' for user: $uid"); |
| 62 | + $count++; |
| 63 | + } |
| 64 | + $output->writeln("Done. Updated $count existing users."); |
| 65 | + return 0; |
| 66 | + } |
| 67 | +} |
0 commit comments