firefly-iii/app/Rules/ValidJournals.php

58 lines
1.8 KiB
PHP
Raw Normal View History

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