Files
firefly-iii/app/Console/Commands/Correction/MigratePreferences.php

75 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/*
* MigratePreferences.php
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
namespace FireflyIII\Console\Commands\Correction;
use FireflyIII\Models\Preference;
use FireflyIII\User;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
class MigratePreferences extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'firefly-iii:migrate-preferences';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Give Firefly III preferences a user group ID so they can be made administration specific.';
/**
* Execute the console command.
*/
public function handle(): int
{
$items = ['frontpageAccounts'];
$users = User::get();
/** @var User $user */
foreach($users as $user) {
$count = 0;
foreach($items as $item) {
$preference = Preference::where('name', $item)->where('user_id', $user->id)->first();
if(null === $preference) {
continue;
}
if(null !== $preference->user_group_id) {
$preference->user_group_id = $user->user_group_id;
$preference->save();
$count++;
}
}
if($count > 0) {
$this->info(sprintf('Migrated %d preference(s) for user #%d ("%s").', $count, $user->id, $user->email));
}
}
return CommandAlias::SUCCESS;
}
}