Files
firefly-iii/app/Rules/ValidJournals.php

59 lines
1.8 KiB
PHP
Raw Normal View History

2019-06-22 13:09:11 +02:00
<?php
/**
* ValidJournals.php
2020-02-16 13:56:25 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-06-22 13:09:11 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-06-22 13:09:11 +02:00
*
* 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.
2019-06-22 13:09:11 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-06-22 13:09:11 +02:00
* 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.
2019-06-22 13:09:11 +02:00
*
* 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/>.
2019-06-22 13:09:11 +02:00
*/
declare(strict_types=1);
2019-06-22 13:09:11 +02:00
namespace FireflyIII\Rules;
use FireflyIII\Models\TransactionJournal;
2023-10-29 17:41:14 +01:00
use Illuminate\Contracts\Validation\ValidationRule;
use Closure;
2019-06-22 13:09:11 +02:00
/**
* Class ValidJournals
*/
2023-10-29 17:41:14 +01:00
class ValidJournals implements ValidationRule
2019-06-22 13:09:11 +02:00
{
/**
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2019-06-22 13:09:11 +02:00
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
2019-06-22 13:09:11 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug('In ValidJournals::passes');
2019-06-22 13:09:11 +02:00
if (!is_array($value)) {
2023-10-29 17:41:14 +01:00
return;
2019-06-22 13:09:11 +02:00
}
$userId = auth()->user()->id;
foreach ($value as $journalId) {
$count = TransactionJournal::where('id', $journalId)->where('user_id', $userId)->count();
if (0 === $count) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Count for transaction #%d and user #%d is zero! Return FALSE', $journalId, $userId));
2019-06-22 13:09:11 +02:00
2023-10-29 17:41:14 +01:00
$fail('validation.invalid_selection')->translate();
2023-12-20 19:35:52 +01:00
2023-10-29 17:41:14 +01:00
return;
2019-06-22 13:09:11 +02:00
}
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Return true!');
2019-06-22 13:09:11 +02:00
}
}