Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions database/factories/AddressFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Eclipse\Core\Database\Factories;

use Eclipse\Core\Enums\AddressType;
use Eclipse\Core\Models\User;
use Eclipse\Core\Models\User\Address;
use Eclipse\World\Models\Country;
use Illuminate\Database\Eloquent\Factories\Factory;

class AddressFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Address::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$type = match (fake()->numberBetween(1, 3)) {
1 => [AddressType::COMPANY_ADDRESS->value],
2 => [AddressType::DEFAULT_ADDRESS->value],
3 => [AddressType::COMPANY_ADDRESS->value, AddressType::DEFAULT_ADDRESS->value],
};

$hasCompanyAddress = in_array(AddressType::COMPANY_ADDRESS->value, $type);

return [
'recipient' => fake()->name(),
'company_name' => $hasCompanyAddress ? fake()->company() : null,
'company_vat_id' => $hasCompanyAddress ? fake()->numerify('##########') : null,
'street_address' => [
fake()->streetAddress(),
fake()->streetAddress(),
],
'postal_code' => fake()->postcode(),
'city' => fake()->city(),
'type' => $type,
'country_id' => Country::inRandomOrder()->first()?->id ?? Country::factory()->create()->id,
'user_id' => User::inRandomOrder()->first()?->id ?? User::factory()->create()->id,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('user_addresses', function (Blueprint $table) {
$table->id();
$table->string('recipient', 100);
$table->string('company_name', 100)
->nullable();
$table->string('company_vat_id', 50)
->nullable();
$table->json('street_address');
$table->string('postal_code', 50);
$table->string('city', 100);
$table->json('type');
$table->string('country_id', 2);
Comment thread
SlimDeluxe marked this conversation as resolved.
$table->foreignId('user_id')
->constrained('users')
->cascadeOnDelete();
$table->softDeletes();
$table->timestamps();
$table->foreign('country_id')
->references('id')
->on('world_countries');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('user_addresses');
}
};
5 changes: 5 additions & 0 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Eclipse\Core\Models\Site;
use Eclipse\Core\Models\User;
use Eclipse\Core\Models\User\Address;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

Expand Down Expand Up @@ -41,6 +42,10 @@ public function run(): void
foreach (Site::all() as $site) {
$user->sites()->attach($site);

Address::factory()->create([
'user_id' => $user->id,
]);

if (isset($preset['role'])) {
setPermissionsTeamId($site->id);
$user->assignRole($preset['role'])->save();
Expand Down
28 changes: 28 additions & 0 deletions src/Enums/AddressType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Eclipse\Core\Enums;

use Filament\Support\Contracts\HasDescription;
use Filament\Support\Contracts\HasLabel;

enum AddressType: string implements HasDescription, HasLabel
{
case DEFAULT_ADDRESS = 'default_address';
case COMPANY_ADDRESS = 'company_address';

public function getLabel(): ?string
{
return match ($this) {
self::DEFAULT_ADDRESS => 'Default',
self::COMPANY_ADDRESS => 'Company'
};
}

public function getDescription(): ?string
{
return match ($this) {
self::DEFAULT_ADDRESS => 'This is the default address',
self::COMPANY_ADDRESS => 'This is a company address'
};
}
}
3 changes: 2 additions & 1 deletion src/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
use Eclipse\Core\Filament\Exports\TableExport;
use Eclipse\Core\Filament\Resources;
use Eclipse\Core\Filament\Resources\UserResource\RelationManagers\AddressesRelationManager;
use Eclipse\Core\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
Expand Down Expand Up @@ -240,7 +241,7 @@ public static function infolist(Infolist $infolist): Infolist
public static function getRelations(): array
{
return [
//
AddressesRelationManager::class,
];
}

Expand Down
10 changes: 10 additions & 0 deletions src/Filament/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class EditUser extends EditRecord
{
protected static string $resource = UserResource::class;

public function hasCombinedRelationManagerTabsWithContent(): bool
{
return true;
}

public function getContentTabLabel(): ?string
{
return __('Edit User');
}

protected function getHeaderActions(): array
{
return [
Expand Down
10 changes: 10 additions & 0 deletions src/Filament/Resources/UserResource/Pages/ViewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class ViewUser extends ViewRecord
{
protected static string $resource = UserResource::class;

public function hasCombinedRelationManagerTabsWithContent(): bool
{
return true;
}

public function getContentTabLabel(): ?string
{
return __('View User');
}

protected function getHeaderActions(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

declare(strict_types=1);

namespace Eclipse\Core\Filament\Resources\UserResource\RelationManagers;

use Eclipse\Core\Enums\AddressType;
use Filament\Forms;
use Filament\Forms\Get;
use Filament\Infolists;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Support\Enums\FontWeight;
use Filament\Tables;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\HtmlString;

class AddressesRelationManager extends RelationManager
{
protected static string $relationship = 'addresses';

public static function getAddressForm(): array
{
return [
Forms\Components\CheckboxList::make('type')
->live()
->default([AddressType::DEFAULT_ADDRESS->value])
->required()
->options(AddressType::class)
->columns(2),
Forms\Components\TextInput::make('recipient')
->maxLength(100)
->label('Full name')
->required(),
Forms\Components\TextInput::make('company_name')
->visible(fn (Get $get): bool => in_array(AddressType::COMPANY_ADDRESS->value, $get('type') ?? []))
->required()
->maxLength(100),
Forms\Components\TextInput::make('company_vat_id')
->visible(fn (Get $get): bool => in_array(AddressType::COMPANY_ADDRESS->value, $get('type') ?? []))
->label('Company VAT ID')
->maxLength(50),
Forms\Components\Repeater::make('street_address')
->minItems(1)
->maxItems(3)
->required()
->simple(
Forms\Components\TextInput::make('street_address')
->maxLength(255)
->required()
)
->addActionLabel(__('Add address line')),
Forms\Components\Split::make([
Forms\Components\TextInput::make('postal_code')
->required()
->maxLength(50),
Forms\Components\TextInput::make('city')
->required()
->maxLength(100),
]),
Forms\Components\Select::make('country_id')
->required()
->relationship('country', 'name'),
];
}

public static function getAddressInfolist(): array
{
return [
Infolists\Components\Grid::make()->schema([
Infolists\Components\TextEntry::make('type')
->badge()
->formatStateUsing(fn ($state) => self::formatAddressTypeLabels($state)),
Infolists\Components\TextEntry::make('recipient'),
Infolists\Components\TextEntry::make('company_name')
->visible(fn ($record): bool => self::hasCompanyAddress($record->type)),
Infolists\Components\TextEntry::make('company_vat_id')
->visible(fn ($record): bool => self::hasCompanyAddress($record->type))
->default('-')
->label('Company VAT ID'),
Infolists\Components\TextEntry::make('street_address')
->listWithLineBreaks(),
Infolists\Components\TextEntry::make('country')
->formatStateUsing(fn ($state) => "{$state->name} {$state->flag}"),
Infolists\Components\Split::make([
Infolists\Components\TextEntry::make('postal_code')
->badge()
->color('warning'),
Infolists\Components\TextEntry::make('city'),
])->columnSpanFull(),
]),
];
}

private static function hasCompanyAddress($types): bool
{
if (! is_array($types)) {
return false;
}

return collect($types)->contains(function ($type) {
return ($type instanceof AddressType && $type === AddressType::COMPANY_ADDRESS) ||
$type === AddressType::COMPANY_ADDRESS->value;
});
}

private static function formatAddressTypeLabels($state): string
{
if (is_array($state)) {
return collect($state)->map(function ($type) {
if (is_string($type)) {
return AddressType::from($type)->getLabel();
}

return $type->getLabel();
})->join(', ');
}

if (is_string($state)) {
return AddressType::from($state)->getLabel();
}

return $state->getLabel();
}

public function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn (Builder $query) => $query->with(['country']))
->columns([
Tables\Columns\TextColumn::make('recipient')
->weight(FontWeight::Bold)
->description(function ($record) {
$streetAddresses = implode('<br/> ', $record->street_address);
$countryInfo = "{$record->country->flag} {$record->country->name}";

return new HtmlString("{$streetAddresses} <br/> {$countryInfo}");
})
->searchable(['recipient', 'street_address', 'country_id']),
Tables\Columns\TextColumn::make('type')
->badge()
->formatStateUsing(fn ($state) => self::formatAddressTypeLabels($state)),
])
->filters([
Tables\Filters\SelectFilter::make('type')
->options(AddressType::class)
->query(function (Builder $query, array $data): Builder {
if (filled($data['value'])) {
return $query->whereJsonContains('type', $data['value']);
}

return $query;
}),
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\ViewAction::make()
->infolist(self::getAddressInfolist()),
Tables\Actions\EditAction::make()
->form(self::getAddressForm()),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->headerActions([
Tables\Actions\CreateAction::make()
->label('Add new address')
->icon('heroicon-o-plus-circle')
->form(self::getAddressForm()),
]);
}
}
7 changes: 7 additions & 0 deletions src/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Eclipse\Core\Database\Factories\SiteFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Site extends Model
{
Expand Down Expand Up @@ -36,4 +37,10 @@ protected static function newFactory(): SiteFactory
{
return SiteFactory::new();
}

/** @return HasMany<\Eclipse\Core\Models\User\Role, self> */
public function roles(): HasMany
{
return $this->hasMany(\Eclipse\Core\Models\User\Role::class);
}
}
Loading