From bcefda63908a81a64f57ddc18a323e6c1ed0d19c Mon Sep 17 00:00:00 2001 From: Marco Germani Date: Mon, 26 Aug 2024 22:30:28 +0200 Subject: [PATCH] Fixed #75 --- CHANGELOG.md | 220 ++++++++++++++++++------------------ src/Models/Email.php | 258 +++++++++++++++++++++---------------------- 2 files changed, 239 insertions(+), 239 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31e7cd7..334d5cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,110 +1,110 @@ -# Changelog - -All notable changes to `filament-email` will be documented in this file. - -## v1.5.1 - 2024-07-27 - -### What's Changed - -* Restored InteractsWithActions for list view - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.5.0...v1.5.1 - -## v1.5.0 - 2024-07-26 - -### What's Changed - -* Added previous/next buttons into view page - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.9...v1.5.0 - -## v1.4.9 - 2024-07-24 - -### What's Changed - -* Add German translations by @hofmannsven in https://github.com/RickDBCN/filament-email/pull/70 -* Bump dependabot/fetch-metadata from 2.1.0 to 2.2.0 by @dependabot in https://github.com/RickDBCN/filament-email/pull/71 -* Added tr translation by @eneskomur in https://github.com/RickDBCN/filament-email/pull/72 - -### New Contributors - -* @hofmannsven made their first contribution in https://github.com/RickDBCN/filament-email/pull/70 -* @eneskomur made their first contribution in https://github.com/RickDBCN/filament-email/pull/72 - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.8...v1.4.9 - -## v1.4.8 - 2024-07-02 - -### What's Changed - -* feat: add resource lang to pt_BR. by @samuelterra22 in https://github.com/RickDBCN/filament-email/pull/67 - -### New Contributors - -* @samuelterra22 made their first contribution in https://github.com/RickDBCN/filament-email/pull/67 - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.7...v1.4.8 - -## v1.4.7 - 2024-06-19 - -### What's Changed - -* Fix missing storage option in resend and delete by @cooljet84 in https://github.com/RickDBCN/filament-email/pull/66 - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.6...v.1.4.7 - -## v1.4.6 - 2024-06-18 - -### What's Changed - -* Allow custom storage disk & config for attachment storage by @cooljet84 in https://github.com/RickDBCN/filament-email/pull/65 - -### New Contributors - -* @cooljet84 made their first contribution in https://github.com/RickDBCN/filament-email/pull/65 - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.5...v1.4.6 - -## v1.4.5 - 2024-05-27 - -### What's Changed - -* Allow disabling of scheduled prune by @ryanldy in https://github.com/RickDBCN/filament-email/pull/62+ -* Reviewed code from pull request #62 with minor changes - -### New Contributors - -* @ryanldy made their first contribution in https://github.com/RickDBCN/filament-email/pull/62 - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.4...v1.4.5 - -## v1.4.4 - 2024-05-19 - -### What's Changed - -* Fixed [#61](https://github.com/RickDBCN/filament-email/issues/61) - "Undefined array key attachments" for re-send e-mail action when there's no attachments and toggle button is disabled (thanks to @StaffCollab for reporting) - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.3...v1.4.4 - -## v1.4.3 - 2024-05-16 - -### What's Changed - -* Adding the option to configure pagination by @edwink75 in https://github.com/RickDBCN/filament-email/pull/60 -* Reviewed code from pull request #60 with minor changes - -### New Contributors - -* @edwink75 made their first contribution in https://github.com/RickDBCN/filament-email/pull/60 - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.2...v1.4.3 - -## v1.4.2 - 2024-05-13 - -### What's Changed - -* Enhanched view attachments layout. -* Fixed some config default. -* Updated README screenshot. - -**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.1...v1.4.2 +# Changelog + +All notable changes to `filament-email` will be documented in this file. + +## v1.5.1 - 2024-07-27 + +### What's Changed + +* Restored InteractsWithActions for list view + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.5.0...v1.5.1 + +## v1.5.0 - 2024-07-26 + +### What's Changed + +* Added previous/next buttons into view page + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.9...v1.5.0 + +## v1.4.9 - 2024-07-24 + +### What's Changed + +* Add German translations by @hofmannsven in https://github.com/RickDBCN/filament-email/pull/70 +* Bump dependabot/fetch-metadata from 2.1.0 to 2.2.0 by @dependabot in https://github.com/RickDBCN/filament-email/pull/71 +* Added tr translation by @eneskomur in https://github.com/RickDBCN/filament-email/pull/72 + +### New Contributors + +* @hofmannsven made their first contribution in https://github.com/RickDBCN/filament-email/pull/70 +* @eneskomur made their first contribution in https://github.com/RickDBCN/filament-email/pull/72 + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.8...v1.4.9 + +## v1.4.8 - 2024-07-02 + +### What's Changed + +* feat: add resource lang to pt_BR. by @samuelterra22 in https://github.com/RickDBCN/filament-email/pull/67 + +### New Contributors + +* @samuelterra22 made their first contribution in https://github.com/RickDBCN/filament-email/pull/67 + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.7...v1.4.8 + +## v1.4.7 - 2024-06-19 + +### What's Changed + +* Fix missing storage option in resend and delete by @cooljet84 in https://github.com/RickDBCN/filament-email/pull/66 + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.6...v.1.4.7 + +## v1.4.6 - 2024-06-18 + +### What's Changed + +* Allow custom storage disk & config for attachment storage by @cooljet84 in https://github.com/RickDBCN/filament-email/pull/65 + +### New Contributors + +* @cooljet84 made their first contribution in https://github.com/RickDBCN/filament-email/pull/65 + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.5...v1.4.6 + +## v1.4.5 - 2024-05-27 + +### What's Changed + +* Allow disabling of scheduled prune by @ryanldy in https://github.com/RickDBCN/filament-email/pull/62+ +* Reviewed code from pull request #62 with minor changes + +### New Contributors + +* @ryanldy made their first contribution in https://github.com/RickDBCN/filament-email/pull/62 + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.4...v1.4.5 + +## v1.4.4 - 2024-05-19 + +### What's Changed + +* Fixed [#61](https://github.com/RickDBCN/filament-email/issues/61) - "Undefined array key attachments" for re-send e-mail action when there's no attachments and toggle button is disabled (thanks to @StaffCollab for reporting) + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.3...v1.4.4 + +## v1.4.3 - 2024-05-16 + +### What's Changed + +* Adding the option to configure pagination by @edwink75 in https://github.com/RickDBCN/filament-email/pull/60 +* Reviewed code from pull request #60 with minor changes + +### New Contributors + +* @edwink75 made their first contribution in https://github.com/RickDBCN/filament-email/pull/60 + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.2...v1.4.3 + +## v1.4.2 - 2024-05-13 + +### What's Changed + +* Enhanched view attachments layout. +* Fixed some config default. +* Updated README screenshot. + +**Full Changelog**: https://github.com/RickDBCN/filament-email/compare/v1.4.1...v1.4.2 diff --git a/src/Models/Email.php b/src/Models/Email.php index c573a25..bb2e800 100644 --- a/src/Models/Email.php +++ b/src/Models/Email.php @@ -1,129 +1,129 @@ - 'json', - ]; - - public function team(): BelongsTo - { - return $this->belongsTo(config('filament-email.tenant_model'), 'team_id', 'id'); - } - - protected static function booted(): void - { - static::addGlobalScope('teams', function (Builder $query) { - if (auth()->check() && Filament::getTenant()) { - $query->whereBelongsTo(auth()->user()?->teams); - } else { - $query->whereTeamId(null); - } - }); - } - - public static function boot() - { - parent::boot(); - - self::deleting(function ($record) { - $storageDisk = config('filament-email.attachments_disk', 'local'); - $folderPath = null; - if (! empty($record->attachments)) { - foreach ($record->attachments as $attachment) { - $filePath = Storage::disk($storageDisk)->path($attachment['path']); - if (empty($folderPath)) { - $parts = explode(DIRECTORY_SEPARATOR, $attachment['path']); - array_pop($parts); - $folderPath = implode(DIRECTORY_SEPARATOR, $parts); - } - if (! Storage::directoryExists($folderPath) && Storage::disk($storageDisk)->exists($attachment['path'])) { - Storage::disk($storageDisk)->delete($attachment['path']); - } - } - } - - if (! empty($record->raw_body)) { - if (! Storage::disk($storageDisk)->directoryExists($record->raw_body) && Storage::disk($storageDisk)->exists($record->raw_body)) { - if (empty($folderPath)) { - $parts = explode(DIRECTORY_SEPARATOR, $record->raw_body); - array_pop($parts); - $folderPath = implode(DIRECTORY_SEPARATOR, $parts); - } - Storage::disk($storageDisk)->delete($record->raw_body); - } - if (Storage::disk($storageDisk)->directoryExists($folderPath)) { - Storage::disk($storageDisk)->deleteDirectory($folderPath); - } - } - }); - } - - public function prunable() - { - return static::where('created_at', '<=', now()->subDays(config('filament-email.keep_email_for_days', 60))); - } - - private function getTableColumns() - { - return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable()); - } - - private function getSearchableFields() - { - $columns = $this->getTableColumns(); - $fields = config('filament-email.resource.table_search_fields', $this->defaultSearchFields); - - return Arr::where($fields, function ($value) use ($columns) { - return in_array($value, $columns); - }); - } - - public function scopeFilter($query, array $filters) - { - $query->when($filters['search'] ?? null, function ($query, $search) { - foreach ($this->getSearchableFields() as $key => $field) { - $query->{$key > 0 ? 'orWhere' : 'where'}($field, 'LIKE', "%{$search}%"); - } - }); - } -} + 'json', + ]; + + public function team(): BelongsTo + { + return $this->belongsTo(config('filament-email.tenant_model'), 'team_id', 'id'); + } + + protected static function booted(): void + { + static::addGlobalScope('teams', function (Builder $query) { + if (auth()->check() && Filament::getTenant()) { + $query->whereBelongsTo(auth()->user()?->teams); + } else { + $query->whereTeamId(null); + } + }); + } + + public static function boot() + { + parent::boot(); + + self::deleting(function ($record) { + $storageDisk = config('filament-email.attachments_disk', 'local'); + $folderPath = null; + if (! empty($record->attachments)) { + foreach ($record->attachments as $attachment) { + $filePath = Storage::disk($storageDisk)->path($attachment['path']); + if (empty($folderPath)) { + $parts = explode(DIRECTORY_SEPARATOR, $attachment['path']); + array_pop($parts); + $folderPath = implode(DIRECTORY_SEPARATOR, $parts); + } + if (! Storage::directoryExists($folderPath) && Storage::disk($storageDisk)->exists($attachment['path'])) { + Storage::disk($storageDisk)->delete($attachment['path']); + } + } + } + + if (! empty($record->raw_body) && count(explode(DIRECTORY_SEPARATOR, $record->raw_body)) === 3) { + if (! Storage::disk($storageDisk)->directoryExists($record->raw_body) && Storage::disk($storageDisk)->exists($record->raw_body)) { + if (empty($folderPath)) { + $parts = explode(DIRECTORY_SEPARATOR, $record->raw_body); + array_pop($parts); + $folderPath = implode(DIRECTORY_SEPARATOR, $parts); + } + Storage::disk($storageDisk)->delete($record->raw_body); + } + if (Storage::disk($storageDisk)->directoryExists($folderPath)) { + Storage::disk($storageDisk)->deleteDirectory($folderPath); + } + } + }); + } + + public function prunable() + { + return static::where('created_at', '<=', now()->subDays(config('filament-email.keep_email_for_days', 60))); + } + + private function getTableColumns() + { + return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable()); + } + + private function getSearchableFields() + { + $columns = $this->getTableColumns(); + $fields = config('filament-email.resource.table_search_fields', $this->defaultSearchFields); + + return Arr::where($fields, function ($value) use ($columns) { + return in_array($value, $columns); + }); + } + + public function scopeFilter($query, array $filters) + { + $query->when($filters['search'] ?? null, function ($query, $search) { + foreach ($this->getSearchableFields() as $key => $field) { + $query->{$key > 0 ? 'orWhere' : 'where'}($field, 'LIKE', "%{$search}%"); + } + }); + } +}