-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Fields; | ||
|
||
use Closure; | ||
use Cone\Root\Http\Controllers\RelationController; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Router; | ||
|
||
class Events extends MorphMany | ||
{ | ||
/** | ||
* Indicates whether the relation is a sub resource. | ||
*/ | ||
protected bool $asSubResource = true; | ||
|
||
/** | ||
* The relations to eager load on every query. | ||
*/ | ||
protected array $with = [ | ||
'user', | ||
'target', | ||
]; | ||
|
||
/** | ||
* Create a new relation field instance. | ||
*/ | ||
public function __construct(?string $label = null, Closure|string|null $modelAttribute = 'rootEvents', Closure|string|null $relation = null) | ||
{ | ||
parent::__construct($label ?: __('Events'), $modelAttribute, $relation); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function persist(Request $request, Model $model, mixed $value): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveHydrate(Request $request, Model $model, mixed $value): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function routes(Router $router): void | ||
{ | ||
$router->get('/', [RelationController::class, 'index']); | ||
$router->get("/{{$this->getRouteKeyName()}}", [RelationController::class, 'show']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapRelationAbilities(Request $request, Model $model): array | ||
{ | ||
return [ | ||
'viewAny' => $this->resolveAbility('viewAny', $request, $model), | ||
'create' => false, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function mapRelatedAbilities(Request $request, Model $model, Model $related): array | ||
{ | ||
return [ | ||
'view' => $this->resolveAbility('view', $request, $model, $related), | ||
'update' => false, | ||
'restore' => false, | ||
'delete' => false, | ||
'forceDelete' => false, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fields(Request $request): array | ||
{ | ||
return [ | ||
ID::make()->sortable(), | ||
|
||
Text::make(__('Action'), 'action')->sortable(), | ||
|
||
BelongsTo::make(__('User'), 'user') | ||
->display('name'), | ||
|
||
Date::make(__('Date'), 'created_at') | ||
->withTime(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Traits; | ||
|
||
use Cone\Root\Models\Event; | ||
use Cone\Root\Models\User; | ||
use Illuminate\Database\Eloquent\Relations\MorphMany; | ||
|
||
trait HasRootEvents | ||
{ | ||
/** | ||
* Get the events for the model. | ||
*/ | ||
public function rootEvents(): MorphMany | ||
{ | ||
return $this->morphMany(Event::getProxiedClass(), 'target'); | ||
} | ||
|
||
/** | ||
* Record a new root event for the model. | ||
*/ | ||
public function recordRootEvent(string $action, ?User $user = null, ?array $payload = null): Event | ||
{ | ||
$event = $this->rootEvents()->make([ | ||
'action' => $action, | ||
'payload' => $payload, | ||
]); | ||
|
||
$event->user()->associate($user)->save(); | ||
|
||
return $event; | ||
} | ||
} |