Files
firefly-iii/app/Helpers/Collector/Extensions/AttachmentCollection.php

521 lines
18 KiB
PHP
Raw Permalink Normal View History

<?php
2022-03-29 14:55:51 +02:00
/*
* AttachmentCollection.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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Helpers\Collector\Extensions;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2025-11-02 14:48:36 +01:00
use Illuminate\Support\Facades\Log;
/**
* Trait AttachmentCollection
*/
trait AttachmentCollection
{
public function attachmentNameContains(string $name): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
2023-12-20 19:35:52 +01:00
2023-11-28 18:57:10 +01:00
/**
* @param array $object
*
* @return bool
*/
$filter = static function (array $object) use ($name): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result
= str_contains(strtolower((string) $attachment['filename']), strtolower($name))
2026-01-23 15:09:50 +01:00
|| str_contains(strtolower((string) $attachment['title']), strtolower($name));
2025-11-09 09:07:14 +01:00
if ($result) {
2022-09-28 07:35:57 +02:00
return true;
}
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
/**
* Has attachments
*/
public function hasAttachments(): GroupCollectorInterface
{
2025-11-02 14:48:36 +01:00
Log::debug('Add filter on attachment ID.');
$this->joinAttachmentTables();
$this->query->whereNotNull('attachments.attachable_id');
$this->query->whereNull('attachments.deleted_at');
return $this;
}
/**
* Join table to get attachment information.
*/
private function joinAttachmentTables(): void
{
if (false === $this->hasJoinedAttTables) {
// join some extra tables:
$this->hasJoinedAttTables = true;
2026-01-23 15:09:50 +01:00
$this->query
->leftJoin('attachments', 'attachments.attachable_id', '=', 'transaction_journals.id')
->where(static function (EloquentBuilder $q1): void { // @phpstan-ignore-line
$q1->where('attachments.attachable_type', TransactionJournal::class);
// $q1->where('attachments.uploaded', true);
// $q1->whereNull('attachments.deleted_at');
$q1->orWhereNull('attachments.attachable_type');
})
;
}
}
public function withAttachmentInformation(): GroupCollectorInterface
{
$this->fields[] = 'attachments.id as attachment_id';
$this->fields[] = 'attachments.filename as attachment_filename';
$this->fields[] = 'attachments.title as attachment_title';
2025-09-13 07:30:30 +02:00
$this->fields[] = 'attachments.deleted_at as attachment_deleted_at';
$this->fields[] = 'attachments.uploaded as attachment_uploaded';
$this->joinAttachmentTables();
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNameDoesNotContain(string $name): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
2023-12-20 19:35:52 +01:00
2023-11-28 18:57:10 +01:00
/**
* @param array $object
*
* @return bool
2023-12-20 19:35:52 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2023-11-28 18:57:10 +01:00
*/
$filter = static function (array $object) use ($name): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result
= !str_contains(strtolower((string) $attachment['filename']), strtolower($name))
2026-01-23 15:09:50 +01:00
&& !str_contains(strtolower((string) $attachment['title']), strtolower($name));
2025-11-09 09:07:14 +01:00
if ($result) {
return true;
}
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-09-28 07:35:57 +02:00
public function attachmentNameDoesNotEnd(string $name): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
2023-12-20 19:35:52 +01:00
2023-11-28 18:57:10 +01:00
/**
* @param array $object
*
* @return bool
2023-12-20 19:35:52 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2023-11-28 18:57:10 +01:00
*/
$filter = static function (array $object) use ($name): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result
= !str_ends_with(strtolower((string) $attachment['filename']), strtolower($name))
2026-01-23 15:09:50 +01:00
&& !str_ends_with(strtolower((string) $attachment['title']), strtolower($name));
2025-11-09 09:07:14 +01:00
if ($result) {
2022-09-28 07:35:57 +02:00
return true;
}
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNameDoesNotStart(string $name): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
2023-12-20 19:35:52 +01:00
2023-11-28 18:57:10 +01:00
/**
* @param array $object
*
* @return bool
2023-12-20 19:35:52 +01:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2023-11-28 18:57:10 +01:00
*/
$filter = static function (array $object) use ($name): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result
= !str_starts_with(strtolower((string) $attachment['filename']), strtolower($name))
2026-01-23 15:09:50 +01:00
&& !str_starts_with(strtolower((string) $attachment['title']), strtolower($name));
2025-11-09 09:07:14 +01:00
if ($result) {
return true;
}
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNameEnds(string $name): GroupCollectorInterface
2022-09-28 07:35:57 +02:00
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($name): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result
= str_ends_with(strtolower((string) $attachment['filename']), strtolower($name))
2026-01-23 15:09:50 +01:00
|| str_ends_with(strtolower((string) $attachment['title']), strtolower($name));
2025-11-09 09:07:14 +01:00
if ($result) {
2022-09-28 07:35:57 +02:00
return true;
}
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNameIs(string $name): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($name): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2022-12-29 19:41:57 +01:00
$result = $attachment['filename'] === $name || $attachment['title'] === $name;
2025-11-09 09:07:14 +01:00
if ($result) {
return true;
}
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNameIsNot(string $name): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($name): bool {
2022-12-29 19:41:57 +01:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result = $attachment['filename'] !== $name && $attachment['title'] !== $name;
2025-11-09 09:07:14 +01:00
if ($result) {
2022-12-29 19:41:57 +01:00
return true;
}
}
}
2023-12-20 19:35:52 +01:00
2022-12-29 19:41:57 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
public function attachmentNameStarts(string $name): GroupCollectorInterface
2022-09-28 07:35:57 +02:00
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($name): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
$result
= str_starts_with(strtolower((string) $attachment['filename']), strtolower($name))
2026-01-23 15:09:50 +01:00
|| str_starts_with(strtolower((string) $attachment['title']), strtolower($name));
2025-11-09 09:07:14 +01:00
if ($result) {
2022-09-28 07:35:57 +02:00
return true;
}
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
public function attachmentNotesAre(string $value): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && $notes === $value;
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-09-28 07:35:57 +02:00
public function attachmentNotesAreNot(string $value): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
2022-09-28 07:35:57 +02:00
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && $notes !== $value;
2022-09-28 07:35:57 +02:00
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
public function attachmentNotesContains(string $value): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && str_contains(strtolower($notes), strtolower($value));
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-09-28 07:35:57 +02:00
public function attachmentNotesDoNotContain(string $value): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
2022-09-28 07:35:57 +02:00
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && !str_contains(strtolower($notes), strtolower($value));
2022-09-28 07:35:57 +02:00
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNotesDoNotEnd(string $value): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && !str_ends_with(strtolower($notes), strtolower($value));
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNotesDoNotStart(string $value): GroupCollectorInterface
2022-09-28 07:35:57 +02:00
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
2022-09-28 07:35:57 +02:00
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && !str_starts_with(strtolower($notes), strtolower($value));
2022-09-28 07:35:57 +02:00
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNotesEnds(string $value): GroupCollectorInterface
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && str_ends_with(strtolower($notes), strtolower($value));
}
}
2023-12-20 19:35:52 +01:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2022-12-29 19:41:57 +01:00
public function attachmentNotesStarts(string $value): GroupCollectorInterface
2022-09-28 07:35:57 +02:00
{
$this->hasAttachments();
$this->withAttachmentInformation();
$filter = static function (array $object) use ($value): bool {
2022-09-28 07:35:57 +02:00
/** @var array $transaction */
foreach ($object['transactions'] as $transaction) {
/** @var array $attachment */
foreach ($transaction['attachments'] as $attachment) {
2023-12-20 19:35:52 +01:00
/** @var null|Attachment $object */
2022-09-28 07:35:57 +02:00
$object = auth()->user()->attachments()->find($attachment['id']);
2024-12-22 08:43:12 +01:00
$notes = (string) $object?->notes()->first()?->text;
2023-12-20 19:35:52 +01:00
return '' !== $notes && str_starts_with(strtolower($notes), strtolower($value));
2022-09-28 07:35:57 +02:00
}
}
2023-12-20 19:35:52 +01:00
2022-09-28 07:35:57 +02:00
return false;
};
$this->postFilters[] = $filter;
return $this;
}
/**
* Has attachments
*/
public function hasNoAttachments(): GroupCollectorInterface
{
2025-11-02 14:48:36 +01:00
Log::debug('Add filter on no attachments.');
$this->joinAttachmentTables();
2025-11-09 09:07:14 +01:00
$this->query->where(static function (EloquentBuilder $q1): void { // @phpstan-ignore-line
2026-01-23 15:09:50 +01:00
$q1->whereNull('attachments.attachable_id')->orWhere(static function (EloquentBuilder $q2): void {
$q2->whereNotNull('attachments.attachable_id')->whereNotNull('attachments.deleted_at');
// id is not null
// deleted at is not null.
});
});
return $this;
}
2022-03-29 14:55:51 +02:00
}