2022-08-23 05:43:40 +02:00
< ? php
2022-10-16 19:29:53 +02:00
2022-08-23 05:43:40 +02:00
/*
* UpdateGroupInformation . php
* Copyright ( c ) 2022 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 />.
*/
2022-10-16 19:29:53 +02:00
declare ( strict_types = 1 );
2022-08-23 05:43:40 +02:00
namespace FireflyIII\Console\Commands\Integrity ;
2023-06-20 07:16:56 +02:00
use FireflyIII\Console\Commands\ShowsFriendlyMessages ;
2022-08-23 05:43:40 +02:00
use FireflyIII\Models\Account ;
use FireflyIII\Models\Attachment ;
use FireflyIII\Models\AvailableBudget ;
use FireflyIII\Models\Bill ;
use FireflyIII\Models\Budget ;
use FireflyIII\Models\Category ;
use FireflyIII\Models\CurrencyExchangeRate ;
2023-08-11 19:37:28 +02:00
use FireflyIII\Models\ObjectGroup ;
2022-08-23 05:43:40 +02:00
use FireflyIII\Models\Recurrence ;
use FireflyIII\Models\Rule ;
use FireflyIII\Models\RuleGroup ;
use FireflyIII\Models\Tag ;
use FireflyIII\Models\TransactionGroup ;
use FireflyIII\Models\TransactionJournal ;
use FireflyIII\Models\UserGroup ;
use FireflyIII\Models\Webhook ;
use FireflyIII\User ;
use Illuminate\Console\Command ;
2022-12-11 10:32:25 +01:00
use Illuminate\Database\QueryException ;
2022-08-23 05:43:40 +02:00
/**
* Class UpdateGroupInformation
*/
class UpdateGroupInformation extends Command
{
2023-06-20 07:16:56 +02:00
use ShowsFriendlyMessages ;
2022-10-30 12:24:51 +01:00
protected $description = 'Makes sure that every object is linked to a group' ;
2023-06-02 07:36:17 +02:00
protected $signature = 'firefly-iii:upgrade-group-information' ;
2022-08-23 05:43:40 +02:00
/**
* Execute the console command .
*
* @ return int
*/
public function handle ()
{
// objects: accounts, attachments, available budgets, bills, budgets, categories, currency_exchange_rates
// recurrences, rule groups, rules, tags, transaction groups, transaction journals, webhooks
$users = User :: get ();
2023-12-20 19:35:52 +01:00
2022-08-23 05:43:40 +02:00
/** @var User $user */
foreach ( $users as $user ) {
$this -> updateGroupInfo ( $user );
}
return 0 ;
}
private function updateGroupInfo ( User $user ) : void
{
$group = $user -> userGroup ;
if ( null === $group ) {
2023-06-20 07:16:56 +02:00
$this -> friendlyWarning ( sprintf ( 'User "%s" has no group.' , $user -> email ));
2023-05-29 13:56:55 +02:00
2022-08-23 05:43:40 +02:00
return ;
}
2024-01-01 14:43:56 +01:00
$set = [
2022-10-30 12:24:51 +01:00
Account :: class ,
Attachment :: class ,
AvailableBudget :: class ,
Bill :: class ,
Budget :: class ,
Category :: class ,
2023-08-11 19:37:28 +02:00
ObjectGroup :: class ,
2022-10-30 12:24:51 +01:00
CurrencyExchangeRate :: class ,
Recurrence :: class ,
RuleGroup :: class ,
Rule :: class ,
Tag :: class ,
TransactionGroup :: class ,
TransactionJournal :: class ,
Webhook :: class ,
];
2022-08-23 05:43:40 +02:00
foreach ( $set as $className ) {
$this -> updateGroupInfoForObject ( $user , $group , $className );
}
}
private function updateGroupInfoForObject ( User $user , UserGroup $group , string $className ) : void
{
2022-12-11 10:32:25 +01:00
try {
2022-12-11 10:43:52 +01:00
$result = $className :: where ( 'user_id' , $user -> id ) -> where ( 'user_group_id' , null ) -> update ([ 'user_group_id' => $group -> id ]);
2022-12-29 19:41:57 +01:00
} catch ( QueryException $e ) {
2023-06-20 07:16:56 +02:00
$this -> friendlyError ( sprintf ( 'Could not update group information for "%s" because of error "%s"' , $className , $e -> getMessage ()));
2023-05-29 13:56:55 +02:00
2022-12-11 10:32:25 +01:00
return ;
}
2022-08-23 05:43:40 +02:00
if ( 0 !== $result ) {
2024-06-15 16:16:40 +02:00
$this -> friendlyPositive ( sprintf ( 'User #%d: Moved %d %s objects to the correct group.' , $user -> id , $result , str_replace ( 'FireflyIII\Models\\' , '' , $className )));
2022-08-23 05:43:40 +02:00
}
}
}