This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Concerns;
|
||||
|
||||
use App\Models\Folder;
|
||||
use App\Models\Todo;
|
||||
use App\TodoStatus;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* Adds sortable column state and query ordering for todo tables.
|
||||
*/
|
||||
trait SortsTodos
|
||||
{
|
||||
public ?string $sortBy = null;
|
||||
|
||||
public string $sortDirection = 'asc';
|
||||
|
||||
/**
|
||||
* Toggle the sort column and direction. Clicking the active column flips
|
||||
* its direction, and clicking it once more restores the manual order.
|
||||
*/
|
||||
public function sort(string $column): void
|
||||
{
|
||||
if (! in_array($column, $this->sortableTodoColumns(), true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->sortBy !== $column) {
|
||||
$this->sortBy = $column;
|
||||
$this->sortDirection = 'asc';
|
||||
} elseif ($this->sortDirection === 'asc') {
|
||||
$this->sortDirection = 'desc';
|
||||
} else {
|
||||
$this->sortBy = null;
|
||||
$this->sortDirection = 'asc';
|
||||
}
|
||||
|
||||
$this->sortChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a column sort is currently applied.
|
||||
*/
|
||||
protected function hasActiveSort(): bool
|
||||
{
|
||||
return $this->sortBy !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
protected function sortableTodoColumns(): array
|
||||
{
|
||||
return ['description', 'status', 'folder', 'due_date', 'created_at', 'completed_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
protected function applyTodoSort(Builder $query): Builder
|
||||
{
|
||||
if ($this->sortBy === null) {
|
||||
return $query->priority();
|
||||
}
|
||||
|
||||
$direction = $this->sortDirection === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
return (match ($this->sortBy) {
|
||||
'description' => $query->orderBy('description', $direction),
|
||||
'status' => $this->orderByStatus($query, $direction),
|
||||
'folder' => $query->orderBy(
|
||||
Folder::query()->select('name')->whereColumn('folders.id', 'todos.folder_id'),
|
||||
$direction
|
||||
),
|
||||
'due_date' => $query
|
||||
->orderByRaw('due_date IS NULL')
|
||||
->orderBy('due_date', $direction),
|
||||
'created_at' => $query->orderBy('created_at', $direction),
|
||||
'completed_at' => $query
|
||||
->orderByRaw('completed_at IS NULL')
|
||||
->orderBy('completed_at', $direction),
|
||||
default => $query->priority(),
|
||||
})->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
private function orderByStatus(Builder $query, string $direction): Builder
|
||||
{
|
||||
$statuses = TodoStatus::cases();
|
||||
|
||||
$cases = collect($statuses)
|
||||
->map(fn (TodoStatus $status, int $index) => 'WHEN ? THEN '.$index)
|
||||
->implode(' ');
|
||||
|
||||
return $query->orderByRaw(
|
||||
"CASE todos.status {$cases} ELSE 999 END {$direction}",
|
||||
collect($statuses)->map(fn (TodoStatus $status) => $status->value)->all()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for components to reset their pagination when the sort changes.
|
||||
*/
|
||||
protected function sortChanged(): void {}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
@@ -30,6 +31,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
* @property WorkspaceType $type
|
||||
* @property bool $personal_workspace
|
||||
* @property bool $lifetime_access
|
||||
* @property int|null $plan_id
|
||||
* @property string|null $stripe_id
|
||||
* @property string|null $pm_type
|
||||
* @property string|null $pm_last_four
|
||||
@@ -37,7 +39,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['name', 'type', 'personal_workspace', 'lifetime_access'])]
|
||||
#[Fillable(['name', 'type', 'personal_workspace', 'lifetime_access', 'plan_id'])]
|
||||
#[Hidden(['stripe_id'])]
|
||||
class Organization extends Model
|
||||
{
|
||||
@@ -69,6 +71,14 @@ class Organization extends Model
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Plan, $this>
|
||||
*/
|
||||
public function plan(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Plan::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Folder, $this>
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\WorkspaceType;
|
||||
use Database\Factories\PlanFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* A plan is a tier of access: it decides what kind of workspace a subscriber
|
||||
* gets, how many members it may hold, and how many folders and notes its
|
||||
* members may create. Every plan is editable by a site administrator, so the
|
||||
* limits live in the database rather than in configuration.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $key
|
||||
* @property string $name
|
||||
* @property WorkspaceType $workspace_type
|
||||
* @property string|null $stripe_price
|
||||
* @property string $interval
|
||||
* @property int $amount
|
||||
* @property int|null $seat_limit
|
||||
* @property int|null $max_folders
|
||||
* @property int|null $max_folder_depth
|
||||
* @property int|null $max_notes_per_folder
|
||||
* @property int|null $note_chars
|
||||
* @property int $sort_order
|
||||
* @property bool $is_active
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable([
|
||||
'key',
|
||||
'name',
|
||||
'workspace_type',
|
||||
'stripe_price',
|
||||
'interval',
|
||||
'amount',
|
||||
'seat_limit',
|
||||
'max_folders',
|
||||
'max_folder_depth',
|
||||
'max_notes_per_folder',
|
||||
'note_chars',
|
||||
'sort_order',
|
||||
'is_active',
|
||||
])]
|
||||
class Plan extends Model
|
||||
{
|
||||
/** @use HasFactory<PlanFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'workspace_type' => WorkspaceType::class,
|
||||
'amount' => 'integer',
|
||||
'seat_limit' => 'integer',
|
||||
'max_folders' => 'integer',
|
||||
'max_folder_depth' => 'integer',
|
||||
'max_notes_per_folder' => 'integer',
|
||||
'note_chars' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Organization, $this>
|
||||
*/
|
||||
public function organizations(): HasMany
|
||||
{
|
||||
return $this->hasMany(Organization::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the plan can be sold, that is, it has a Stripe price.
|
||||
*/
|
||||
public function isPurchasable(): bool
|
||||
{
|
||||
return is_string($this->stripe_price) && $this->stripe_price !== '';
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -31,7 +31,7 @@ use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
* @property Carbon|null $two_factor_confirmed_at
|
||||
* @property string|null $remember_token
|
||||
* @property bool $is_admin
|
||||
* @property int $notes_limit
|
||||
* @property int|null $notes_limit
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user