-
Notifications
You must be signed in to change notification settings - Fork 21
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
5 changed files
with
131 additions
and
7 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
101 changes: 101 additions & 0 deletions
101
src/Resources/Pages/Concerns/UsesRelationManagerResourceLock.php
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 Kenepa\ResourceLock\Resources\Pages\Concerns; | ||
|
||
use Filament\Facades\Filament; | ||
use Kenepa\ResourceLock\Models\ResourceLock; | ||
use Livewire\Attributes\On; | ||
|
||
trait UsesRelationManagerResourceLock | ||
{ | ||
use UsesLocks; | ||
|
||
public $relatedRecord; | ||
|
||
public string $parentClass; | ||
public string $relatedClass; | ||
|
||
public function bootUsesRelationManagerResourceLock(): void | ||
{ | ||
$this->parentClass = | ||
"App\\Models\\" . | ||
class_basename($this->getRelationship()->getParent()); | ||
|
||
$this->relatedClass = | ||
"App\\Models\\" . | ||
class_basename($this->getRelationship()->getRelated()); | ||
} | ||
|
||
#[On('resourceLockObserver::unlock')] | ||
public function resourceLockObserverUnlock(){ | ||
if ($this->relatedRecord) { | ||
if ($this->relatedRecord->unlock(force: true)) { | ||
$this->closeLockedResourceModal(); | ||
$this->relatedLock(); | ||
} | ||
} else { | ||
if ($this->getOwnerRecord()->unlock(force: true)) { | ||
$this->closeLockedResourceModal(); | ||
$this->getOwnerRecord()->lock(); | ||
} | ||
} | ||
} | ||
|
||
public function relatedLock() | ||
{ | ||
$resourceLockModel = config('resource-lock.models.ResourceLock', ResourceLock::class); | ||
$guard = Filament::auth()->name; | ||
$resourceLock = new $resourceLockModel; | ||
$resourceLock->user_id = auth()->guard($guard)->user()->id; | ||
$resourceLock->lockable_id = $this->relatedRecord->id; | ||
$resourceLock->lockable_type = $this->relatedRecord->getMorphClass(); | ||
$resourceLock->save(); | ||
} | ||
|
||
public function mountTableAction( | ||
string $name, | ||
?string $record = null, | ||
array $arguments = [] | ||
): mixed { | ||
parent::mountTableAction($name, $record); | ||
|
||
if ($name == "edit") { | ||
$this->relatedRecord = $this->relatedClass::find($record); | ||
$this->checkIfResourceLockHasExpired($this->relatedRecord); | ||
$this->lockResource($this->relatedRecord); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function unmountTableAction( | ||
bool $shouldCancelParentActions = true | ||
): void { | ||
if ($this->mountedTableActionRecord) { | ||
$this->relatedRecord = $this->relatedClass::find( | ||
$this->mountedTableActionRecord | ||
); | ||
|
||
$this->relatedRecord->unlock(); | ||
} | ||
|
||
parent::unmountTableAction($shouldCancelParentActions); | ||
} | ||
|
||
public function resourceLockReturnUrl() | ||
{ | ||
$parentClassResource = "App\\Filament\\Resources\\" . | ||
class_basename($this->getRelationship()->getParent()) . "Resource"; | ||
return $parentClassResource::getUrl("edit", ['record' => $this->getOwnerRecord()->id]); | ||
} | ||
|
||
public function getResourceLockOwner(): void | ||
{ | ||
if (config('resource-lock.lock_notice.display_resource_lock_owner', false)) { | ||
$getResourceLockOwnerActionClass = config('resource-lock.actions.get_resource_lock_owner_action'); | ||
$getResourceLockOwnerAction = app($getResourceLockOwnerActionClass); | ||
|
||
$this->resourceLockOwner = $getResourceLockOwnerAction->execute($this->relatedRecord->resourceLock->user); | ||
} | ||
} | ||
} |
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