Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions src/Database/Eloquent/Concerns/HasTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ trait HasTimestamps
*/
public function setCreatedAt($value): static
{
$this->setPerformedBy();
$this->{$this->getCreatedAtColumn()} = $value;
$this->{$this->getCreatedByColumn()} = $this->performBy;
if (config('koffinate.core.model.use_perform_by')) {
$this->setPerformedBy();
$this->{$this->getCreatedByColumn()} = $this->performBy;
}

return $this;
}
Expand All @@ -35,9 +37,11 @@ public function setCreatedAt($value): static
*/
public function setUpdatedAt($value): static
{
$this->setPerformedBy();
$this->{$this->getUpdatedAtColumn()} = $value;
$this->{$this->getUpdatedByColumn()} = $this->performBy;
if (config('koffinate.core.model.use_perform_by')) {
$this->setPerformedBy();
$this->{$this->getUpdatedByColumn()} = $this->performBy;
}

return $this;
}
Expand Down
15 changes: 4 additions & 11 deletions src/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ class Model extends BaseModel
*/
protected bool $useTryOnEnumCast = true;

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'created_at', 'updated_at', 'deleted_at', 'restore_at',
'created_by', 'updated_by', 'deleted_by', 'restore_by',
];

/**
* Create a new Eloquent model instance.
*
Expand Down Expand Up @@ -182,7 +172,10 @@ protected function performerAsPlain(?string $performer = null): Fluent
*/
protected function setPerformedBy(): void
{
if (auth()->user() && empty($this->performBy) && config('koffinate.core.model.use_perform_by')) {
// reset performer
$this->performBy = null;

if (auth()->check() && config('koffinate.core.model.use_perform_by') && empty($this->performBy)) {
$user = auth()->user();
if ($user instanceof User) {
if ($this->performerMode == 'users') {
Expand Down
35 changes: 22 additions & 13 deletions src/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,34 @@ protected function runSoftDelete(): void
{
$query = $this->setKeysForSaveQuery($this->newModelQuery());

$this->setPerformedBy();

$time = $this->freshTimestamp();

$columns = [
$this->getDeletedAtColumn() => $this->fromDateTime($time),
$this->getDeletedByColumn() => $this->performBy,
];
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];

$this->{$this->getDeletedAtColumn()} = $time;
$this->{$this->getDeletedByColumn()} = $this->performBy;

if (config('koffinate.core.model.use_perform_by')) {
$this->setPerformedBy();
$columns[$this->getDeletedByColumn()] = $this->performBy;
$this->{$this->getDeletedByColumn()} = $this->performBy;
}

if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$this->{$this->getUpdatedByColumn()} = $this->performBy;

$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
$columns[$this->getUpdatedByColumn()] = $this->performBy;

if (config('koffinate.core.model.use_perform_by')) {
$this->{$this->getUpdatedByColumn()} = $this->performBy;
$columns[$this->getUpdatedByColumn()] = $this->performBy;
}
}

$query->update($columns);

$this->syncOriginalAttributes(array_keys($columns));

$this->fireModelEvent('trashed', false);
}

/**
Expand All @@ -54,12 +61,14 @@ public function restore(): ?bool
return false;
}

$this->setPerformedBy();

$this->{$this->getDeletedAtColumn()} = null;
$this->{$this->getDeletedByColumn()} = null;
$this->{$this->getRestoreAtColumn()} = $this->freshTimestamp();
$this->{$this->getRestoreByColumn()} = $this->performBy;

if (config('koffinate.core.model.use_perform_by')) {
$this->setPerformedBy();
$this->{$this->getDeletedByColumn()} = null;
$this->{$this->getRestoreByColumn()} = $this->performBy;
}

// Once we have saved the model, we will fire the "restored" event so this
// developer will do anything they need to after a restore operation is
Expand Down
82 changes: 54 additions & 28 deletions src/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@ public function __construct($table, Closure $callback = null, $prefix = '')
*/
public function timestamps($precision = 0): void
{
$foreignType = in_array($this->userKeyType, ['int', 'integer']) ? 'foreignId' : 'foreignUuid';

$this->timestamp('created_at', $precision)->nullable();
if ($this->performerMode == 'users') {
$this->{$foreignType}('created_by')->nullable()
->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict');
} else {
$this->string('created_by', 100)->nullable();
}
$this->makePerformerColumn('created_by');

$this->timestamp('updated_at', $precision)->nullable();
if ($this->performerMode == 'users') {
$this->{$foreignType}('updated_by')->nullable()
->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict');
} else {
$this->string('updated_by', 100)->nullable();
}
$this->makePerformerColumn('updated_by');
}

/**
* Add creation and update timestampTz columns to the table.
*
* @param int|null $precision
*
* @return void
*/
public function timestampsTz($precision = 0): void
{
$this->timestampTz('created_at', $precision)->nullable();
$this->makePerformerColumn('created_by');

$this->timestampTz('updated_at', $precision)->nullable();
$this->makePerformerColumn('updated_by');
}

/**
Expand All @@ -77,27 +81,49 @@ public function timestamps($precision = 0): void
*/
public function softDeletes($column = 'deleted_at', $precision = 0): ColumnDefinition
{
$foreignType = in_array($this->userKeyType, ['int', 'integer']) ? 'foreignId' : 'foreignUuid';

$deletedByColumn = str($column)->replaceMatches('/_at$/i', '')->append('_by')->toString();
$deletedColum = $this->timestamp($column, $precision)->nullable();
if ($this->performerMode == 'users') {
$this->{$foreignType}('deleted_by')->nullable()
->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict');
} else {
$this->string('deleted_by', 100)->nullable();
}
$this->makePerformerColumn($deletedByColumn);

$this->timestamp('restore_at', $precision)->nullable();
if ($this->performerMode == 'users') {
$this->{$foreignType}('restore_by')->nullable()
->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict');
} else {
$this->string('restore_by', 100)->nullable();
}
$this->makePerformerColumn('restore_by');

return $deletedColum;
}

/**
* Add a "deleted at" timestampTz for the table.
*
* @param string $column
* @param int|null $precision
*
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletesTz($column = 'deleted_at', $precision = 0): ColumnDefinition
{
$deletedByColumn = str($column)->replaceMatches('/_at$/i', '')->append('_by')->toString();
$deletedColum = $this->timestampTz($column, $precision)->nullable();
$this->makePerformerColumn($deletedByColumn);

$this->timestampTz('restore_at', $precision)->nullable();
$this->makePerformerColumn('restore_by');

return $deletedColum;
}

private function makePerformerColumn(string $column): void
{
if (config('koffinate.core.model.use_perform_by')) {
if ($this->performerMode == 'users') {
$foreignType = in_array($this->userKeyType, ['int', 'integer']) ? 'foreignId' : 'foreignUuid';
$this->{$foreignType}($column)->nullable()
->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict');
} else {
$this->string($column, 100)->nullable();
}
}
}

/**
* Add the proper columns for a polymorphic table.
*
Expand Down