Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Nov 27, 2023
1 parent a13ce60 commit fed52b2
Show file tree
Hide file tree
Showing 26 changed files with 1,176 additions and 150 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ _ide_helper.php
.php_cs.cache
.php-cs-fixer.cache
.php-cs-fixer.php
.phpactor.json
.phpstorm.meta.php
.phpunit.result.cache
auth.json
Expand Down
33 changes: 33 additions & 0 deletions app/Concerns/HasLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Models\City;
use App\Models\County;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait HasLocation
{
public function initializeHasLocation(): void
{
$this->fillable = array_merge($this->fillable, ['county_id', 'city_id']);
}

public function county(): BelongsTo
{
return $this->belongsTo(County::class);
}

public function city(): BelongsTo
{
return $this->belongsTo(City::class);
}

public function scopeWithLocation(Builder $query): Builder
{
return $query->with('county', 'city');
}
}
35 changes: 35 additions & 0 deletions app/Concerns/HasUlid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

trait HasUlid
{
protected $ulidColumn = 'ulid';

public static function bootHasUlid()
{
// Generate ULID if none provided
static::creating(function (Model $model) {
if (! $model->{$model->ulidColumn}) {
$model->{$model->ulidColumn} = (string) Str::ulid();
}
});

// Make sure ULIDs can't be changed
static::updating(function (Model $model) {
$originalUlid = $model->getOriginal($model->ulidColumn);

if (
! \is_null($originalUlid) &&
$originalUlid !== $model->{$model->ulidColumn}
) {
$model->{$model->ulidColumn} = $originalUlid;
}
});
}
}
68 changes: 68 additions & 0 deletions app/Filament/Forms/Components/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace App\Filament\Forms\Components;

use App\Models\City;
use App\Models\County;
use Filament\Forms\Components\Concerns\CanBeValidated;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Select;
use Illuminate\Support\Facades\Cache;

class Location extends Grid
{
use CanBeValidated;

protected bool $withCity = true;

public function withoutCity(): self
{
$this->withCity = false;

return $this;
}

public function getChildComponents(): array
{
return [
Select::make('county_id')
->label(__('field.county'))
->options(function () {
return Cache::driver('array')
->rememberForever(
'counties',
fn () => County::pluck('name', 'id')
);
})
->searchable()
->preload()
->reactive()
->required($this->isRequired())
->afterStateUpdated(fn (callable $set) => $set('city_id', null))
->when(! $this->withCity, fn ($component) => $component->columnSpanFull()),

Select::make('city_id')
->label(__('field.city'))
->searchable()
->required($this->isRequired())
->getSearchResultsUsing(function (string $search, callable $get) {
$countyId = (int) $get('county_id');

if (! $countyId) {
return [];
}

return City::query()
->where('county_id', $countyId)
->search($search)
->limit(100)
->get()
->pluck('name', 'id');
})
->getOptionLabelUsing(fn ($value) => City::find($value)?->name)
->visible($this->withCity),
];
}
}
21 changes: 21 additions & 0 deletions app/Filament/MediaLibrary/TenantPathGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Filament\MediaLibrary;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator;

class TenantPathGenerator extends DefaultPathGenerator
{
protected function getBasePath(Media $media): string
{
return collect([
filament()->getTenant()?->ulid,
$media->getKey(),
])
->filter()
->join(\DIRECTORY_SEPARATOR);
}
}
45 changes: 45 additions & 0 deletions app/Filament/Pages/Profile/UserPersonalInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Filament\Pages\Profile;

use Filament\Forms\Components\Group;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TextInput;
use Jeffgreco13\FilamentBreezy\Livewire\PersonalInfo as BasePersonalInfo;

class UserPersonalInfo extends BasePersonalInfo
{
public array $only = [
'first_name',
'last_name',
'email',
];

protected function getProfileFormSchema(): array
{
$groupFields = [];

if ($this->hasAvatars) {
$groupFields[] = SpatieMediaLibraryFileUpload::make('avatar')
->label(__('filament-breezy::default.fields.avatar'))
->avatar()
->collection('avatars')
->conversion('large');
}

$groupFields[] = Group::make()
->columnSpan(2)
->columns(2)
->schema([
TextInput::make('first_name'),
TextInput::make('last_name'),

$this->getEmailComponent()
->columnSpanFull(),
]);

return $groupFields;
}
}
70 changes: 67 additions & 3 deletions app/Filament/Pages/Tenancy/EditOrganizationProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace App\Filament\Pages\Tenancy;

use App\Filament\Forms\Components\Location;
use App\Rules\ValidCIF;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Tenancy\EditTenantProfile;
Expand All @@ -12,15 +17,74 @@ class EditOrganizationProfile extends EditTenantProfile
{
public static function getLabel(): string
{
return 'Team profile';
return __('organization.profile');
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name'),
// ...
Section::make()
->columns(2)
->schema([
TextInput::make('name')
->label(__('organization.field.name')),

TextInput::make('short_name')
->label(__('organization.field.short_name')),

Select::make('type')
->label(__('organization.field.type')),

TextInput::make('cif')
->label(__('organization.field.cif'))
->rule(new ValidCIF),

TextInput::make('main_activity')
->label(__('organization.field.main_activity')),

TextInput::make('phone')
->label(__('organization.field.phone'))
->tel(),

TextInput::make('website')
->label(__('organization.field.website'))
->url(),
]),

Section::make(__('organization.section.location'))
->columns(2)
->schema([
TextInput::make('address')
->label(__('organization.field.address'))
->maxLength(200)
->columnSpanFull()
->required(),

Location::make()
->required(),
]),

Section::make(__('organization.section.reprezentative'))
->columns(2)
->schema([
TextInput::make('reprezentative_name')
->label(__('organization.field.reprezentative_name')),

TextInput::make('reprezentative_email')
->label(__('organization.field.reprezentative_email')),
]),

Section::make(__('organization.field.logo'))
->schema([
SpatieMediaLibraryFileUpload::make('logo')
->label(__('organization.field.logo'))
->hiddenLabel()
->image()
->collection('logo')
->conversion('large')
->columnSpanFull(),
]),
]);
}
}
9 changes: 5 additions & 4 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('first_name'),
TextColumn::make('last_name'),
TextColumn::make('first_name')
->searchable(),
TextColumn::make('last_name')
->searchable(),
TextColumn::make('roles'),
TextColumn::make('account_status'),
TextColumn::make('last_login_at'),

])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\ViewAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Expand Down
39 changes: 35 additions & 4 deletions app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,59 @@

namespace App\Models;

use App\Concerns\HasLocation;
use App\Concerns\HasUlid;
use Filament\Models\Contracts\HasAvatar;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Facades\Storage;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class Organization extends Model implements HasAvatar
class Organization extends Model implements HasAvatar, HasMedia
{
use HasFactory;
use HasLocation;
use HasUlid;
use InteractsWithMedia;

protected $fillable = [
'name',
'avatar_url',
'slug',
'short_name',
'type',
'cif',
'main_activity',
'address',
'reprezentative_name',
'reprezentative_email',
'phone',
'website',
];

public function users(): MorphToMany
{
return $this->morphedByMany(User::class, 'model', 'model_has_organizations');
}

public function registerMediaCollections(): void
{
$this->addMediaCollection('logo')
->singleFile()
->registerMediaConversions(function () {
$this->addMediaConversion('thumb')
->fit(Manipulations::FIT_CONTAIN, 64, 64)
->optimize();

$this->addMediaConversion('large')
->fit(Manipulations::FIT_CONTAIN, 256, 256)
->optimize();
});
}

public function getFilamentAvatarUrl(): ?string
{
return $this->avatar_url ? Storage::url($this->avatar_url) : null;
return $this->getFirstMediaUrl('logo', 'thumb');
}
}
Loading

0 comments on commit fed52b2

Please sign in to comment.