Skip to content

Commit

Permalink
Merge pull request #39 from noxoua/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
noxoua authored Nov 5, 2023
2 parents 59b14d4 + eca5b17 commit 5f0d3b2
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 57 deletions.
44 changes: 40 additions & 4 deletions docs/004-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ____
```php
$logger->fields([
Field::make('meta')
->view('key-value')
->keyValue(differenceOnly: true)
->label('Attributes'),
])
```
Expand All @@ -118,13 +118,49 @@ ____
```php
$logger->fields([
// 'categories' => 'relation:name',
Field::make('categories')
->relation('name') // get names only
->badge('info')
Field::make('categories.name') // hasMany
->relation()
->label('Categories'),

// 'category.name',
Field::make('category.name') // hasOne
->relation()
->label('Category'),
])
```

{: .note }
If you have `preloadRelations`, you can skip `relation`, and vice versa.


#### With preload relations

When you use `preloadRelations`, you can directly define the relation field without the need for `relation`:

```php
$logger
->preloadRelations('categories')
->fields([
Field::make('categories.name')->label('Categories'),
])
```

#### With relation

If you prefer to use `relation`, it works similarly to `preloadRelations`:

```php
$logger->fields([
Field::make('categories.name')
->relation()
->label('Categories'),
])
```

{: .info }
`preloadRelations` and `relation` work similarly to Laravel's eager loading.


![Screenshot](./assets/images/relation-screenshot.png)

____
Expand Down
2 changes: 2 additions & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ callouts:
color: yellow
note:
color: purple
info:
color: blue


compress_html:
Expand Down
13 changes: 9 additions & 4 deletions docs/get-started/003.3-logger-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ class ProductLogger extends Logger
public static function resource(ResourceLogger $logger): ResourceLogger
{
return $logger
->preloadRelations(
// Preload your relationships,
// this is necessary to log the data correctly.
'categories',
)
// Resource Fields
->fields([
Field::make('title')
->label(__('Title')),

Field::make('categories')
->label(__('Categories'))
->relation('name'),
Field::make('categories.name')
->label(__('Categories')),

Field::make('media')
->label(__('Images'))
Expand All @@ -62,8 +66,9 @@ class ProductLogger extends Logger
])
// Relation Managers
->relationManagers([
'accessories' => fn (RelationManager $relationManager) => $relationManager
RelationManager::make('accessories')
->label('Accessory')
// ->preloadRelations()
->fields([
Field::make('name')
->label(__('Label')),
Expand Down
40 changes: 21 additions & 19 deletions resources/views/components/badge.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
@endphp

<div class="flex flex-wrap gap-2">
@if (is_string($value) || $field->is('enum'))
@if (is_array($value))
@foreach ($value as $label)
<x-filament::badge
:color="$field->badgeColor"
class="w-fit"
>
{{ $label }}
</x-filament::badge>
@endforeach
@elseif($field->is('enum'))
@php
if (is_string($value)) {
$label = $value;
$color = $field->badgeColor;
$icon = null;
} else {
$label = $value instanceof HasLabel ? $value->getLabel() : $value;
$color = $value instanceof HasColor ? $value->getColor() : $field->badgeColor;
$icon = $value instanceof HasIcon ? $value->getIcon() : null;
}
$label = $value instanceof HasLabel ? $value->getLabel() : $value;
$color = $value instanceof HasColor ? $value->getColor() : $field->badgeColor;
$icon = $value instanceof HasIcon ? $value->getIcon() : null;
@endphp

<x-filament::badge
Expand All @@ -25,14 +28,13 @@ class="w-fit"
>
{{ $label }}
</x-filament::badge>
@elseif(is_array($value))
@foreach ($value as $label)
<x-filament::badge
:color="$field->badgeColor"
class="w-fit"
>
{{ $label }}
</x-filament::badge>
@endforeach
@else
<x-filament::badge
:color="$field->badgeColor"
class="w-fit"
>
{{ $value }}
</x-filament::badge>
@endif

</div>
38 changes: 20 additions & 18 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<x-filament-tables::table class="w-full overflow-hidden text-sm">
<x-slot:header>
@foreach (array_keys($value[0]) as $key)
<x-filament-tables::header-cell class="!py-2">
{{ $key }}
</x-filament-tables::header-cell>
@endforeach
</x-slot:header>
@if (!empty($value))
<x-filament-tables::table class="w-full overflow-hidden text-sm">
<x-slot:header>
@foreach (array_keys($value[0]) as $key)
<x-filament-tables::header-cell class="!py-2">
{{ $key }}
</x-filament-tables::header-cell>
@endforeach
</x-slot:header>


@foreach ($value as $item)
<x-filament-tables::row @class(['bg-gray-100/30 dark:bg-gray-900' => $loop->even])>
@foreach ($item as $_value)
<x-filament-tables::cell class="px-4 py-2 align-top sm:first-of-type:ps-6 sm:last-of-type:pe-6">
{{ $_value }}
</x-filament-tables::cell>
@endforeach
</x-filament-tables::row>
@endforeach
</x-filament-tables::table>
@foreach ($value as $item)
<x-filament-tables::row @class(['bg-gray-100/30 dark:bg-gray-900' => $loop->even])>
@foreach ($item as $_value)
<x-filament-tables::cell class="px-4 py-2 align-top sm:first-of-type:ps-6 sm:last-of-type:pe-6">
{{ $_value }}
</x-filament-tables::cell>
@endforeach
</x-filament-tables::row>
@endforeach
</x-filament-tables::table>
@endif
7 changes: 7 additions & 0 deletions src/Extensions/LogActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@ public static function mount(): void
{
$self = new static;

// TODO:

// * Table Actions
// TableActions\AssociateAction::configureUsing(fn ($action) => $self->associate($action));
// TableActions\AttachAction::configureUsing(fn ($action) => $self->attach($action));
TableActions\CreateAction::configureUsing(fn ($action) => $self->create($action));
TableActions\DeleteAction::configureUsing(fn ($action) => $self->delete($action));
// TableActions\DeleteBulkAction::configureUsing(fn ($action) => $self->delete($action));
// TableActions\DetachAction::configureUsing(fn ($action) => $self->detach($action));
// TableActions\DetachBulkAction::configureUsing(fn ($action) => $self->detach($action));
// TableActions\DissociateAction::configureUsing(fn ($action) => $self->dissociate($action));
// TableActions\DissociateBulkAction::configureUsing(fn ($action) => $self->dissociate($action));
TableActions\EditAction::configureUsing(fn ($action) => $self->edit($action));
TableActions\ForceDeleteAction::configureUsing(fn ($action) => $self->delete($action));
// TableActions\ForceDeleteBulkAction::configureUsing(fn ($action) => $self->delete($action));
TableActions\ReplicateAction::configureUsing(fn ($action) => $self->create($action));
TableActions\RestoreAction::configureUsing(fn ($action) => $self->restore($action));
// TableActions\RestoreBulkAction::configureUsing(fn ($action) => $self->restore($action));

// * Page Actions
PageActions\CreateAction::configureUsing(fn ($action) => $self->create($action));
Expand Down
4 changes: 4 additions & 0 deletions src/Loggers/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function updated(): void
[$beforeValue, $afterValue] = $field->resolveTableDifference($beforeValue, $afterValue);
}

if ($field->is('key-value') && $field->keyValueDifferenceOnly) {
[$beforeValue, $afterValue] = $field->resolveKeyValueDifference($beforeValue, $afterValue);
}

if ($beforeValue !== $afterValue) {
$old[$field->name] = $beforeValue;
$new[$field->name] = $afterValue;
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceLogger/Concerns/CanStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public function getStorableValue(Model $record): mixed
return call_user_func($this->resolveStateCallback, $record);
}

return $record->{$this->name};
return data_get($record, $this->name);
}
}
2 changes: 1 addition & 1 deletion src/ResourceLogger/Concerns/HasFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getRelationNames(): array
}
}

return $relations;
return array_merge($relations, $this->preloadRelations);
}

public function getFieldByName(string $name): ?Field
Expand Down
18 changes: 18 additions & 0 deletions src/ResourceLogger/Concerns/HasRelationLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Noxo\FilamentActivityLog\ResourceLogger\Concerns;

trait HasRelationLoader
{
/**
* @var array<string>
*/
protected array $preloadRelations = [];

public function preloadRelations($relations): static
{
$this->preloadRelations = is_string($relations) ? func_get_args() : $relations;

return $this;
}
}
8 changes: 7 additions & 1 deletion src/ResourceLogger/Concerns/HasRelationManagers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ trait HasRelationManagers
public function relationManagers(array $relationManagers): static
{
$this->relationManagers = array_map(
static fn (Closure $closure, string $name) => $closure(new RelationManager($name)),
function (Closure|RelationManager $closure, ?string $name) {
if ($closure instanceof RelationManager) {
return $closure;
}

return $closure(new RelationManager($name));
},
$relationManagers,
array_keys($relationManagers),
);
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceLogger/Concerns/Types/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function boolean(): static
$this->enum(config('filament-activity-log.boolean'));
$this->view('badge');

$this->resolveStateUsing(fn (Model $model) => $model->{$this->name} ? 'true' : 'false');
$this->resolveStateUsing(fn (Model $record) => data_get($record, $this->name) ? 'true' : 'false');

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceLogger/Concerns/Types/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function datetime(string $format = null): static
$format ??= 'j F, Y H:i:s';
$this->type('datetime', $format);

$this->resolveStateUsing(fn (Model $model) => $model->{$this->name}?->toISOString());
$this->resolveStateUsing(fn (Model $record) => data_get($record, $this->name)?->toISOString());

$this->formatStateUsing(function ($state): ?string {
if (blank($state)) {
Expand Down
4 changes: 4 additions & 0 deletions src/ResourceLogger/Concerns/Types/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function difference(string $method = 'diffWords', array $options = []): s
$this->type('difference', compact('method', 'options'));

$this->formatStateUsing(function ($state) {
if (is_null($state)) {
$state = '';
}

// TODO: not working with array
// * resources/views/components/difference.blade.php
return json_encode($state);
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceLogger/Concerns/Types/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function enum(string $enum): static
$this->type('enum', $enum);
$this->view('badge');

$this->resolveStateUsing(fn (Model $model) => $model->{$this->name}?->name);
$this->resolveStateUsing(fn (Model $record) => data_get($record, $this->name)?->name);

$this->formatStateUsing(fn ($state): ?UnitEnum => Helper::resolveEnum($this->options, $state));

Expand Down
35 changes: 35 additions & 0 deletions src/ResourceLogger/Concerns/Types/KeyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Noxo\FilamentActivityLog\ResourceLogger\Concerns\Types;

trait KeyValue
{
public bool $keyValueDifferenceOnly = true;

public function keyValue(bool $differenceOnly = true): static
{
$this->type('key-value');
$this->view('key-value');
$this->keyValueDifferenceOnly = $differenceOnly;

return $this;
}

public function resolveKeyValueDifference(mixed $array1, mixed $array2): array
{
if (! is_array($array1) || ! is_array($array2)) {
return [$array1, $array2];
}

foreach ($array1 as $key1 => $row1) {
foreach ($array2 as $key2 => $row2) {
if ($row1 === $row2) {
unset($array1[$key1]);
unset($array2[$key2]);
}
}
}

return [$array1, $array2];
}
}
4 changes: 2 additions & 2 deletions src/ResourceLogger/Concerns/Types/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function media(?bool $gallery = false): static
$this->square();
}

$this->resolveStateUsing(function (Model $model): mixed {
$relation = $model->{$this->name};
$this->resolveStateUsing(function (Model $record): mixed {
$relation = data_get($record, $this->name);

if ($this->gallery) {
return $relation->pluck('original_url')->toArray();
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceLogger/Concerns/Types/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function money(string $currency = 'EUR'): static
$this->type('money', $currency);
$this->view('badge');

$this->formatStateUsing(fn ($state): ?string => format_money($state, $this->options));
$this->formatStateUsing(fn ($state): ?string => format_money((float) $state, $this->options));

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ResourceLogger/Concerns/Types/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public function relation(string $column = null): static
$this->isRelation = true;

if ($column) {
$this->resolveStateUsing(function (Model $model) use ($column): mixed {
$relation = $model->{$this->name};
$this->resolveStateUsing(function (Model $record) use ($column): mixed {
$relation = data_get($record, $this->name);

if ($relation instanceof Collection) {
return $relation->pluck($column)->toArray();
}

return $relation->{$column};
return data_get($relation, $column);
});
}

Expand Down
Loading

0 comments on commit 5f0d3b2

Please sign in to comment.