*/ public array $collaboratorRoles = []; public bool $showTodoForm = false; #[Locked] public ?int $todoFolderId = null; public string $todoDescription = ''; public string $todoStatus = 'open'; public ?string $todoDueDate = null; public string $todoNotes = ''; public function createTopLevelFolder(): void { $organization = auth()->user()->currentOrganization()->first(); abort_unless( $organization !== null && auth()->user()->can('create', [Folder::class, $organization]), 403 ); $validated = $this->validate([ 'newFolderName' => $this->folderNameRules($organization->id, parentId: null), 'newFolderColor' => $this->folderColorRules(), ]); Folder::create([ 'user_id' => auth()->id(), 'name' => $validated['newFolderName'], 'color' => filled($validated['newFolderColor']) ? $validated['newFolderColor'] : null, 'parent_id' => null, ]); $this->reset('newFolderName', 'newFolderColor'); } public function startChildCreate(int $folderId): void { $this->folderForAction($folderId, 'createChild'); $this->creatingChildOf = $folderId; $this->reset('newChildName', 'newChildColor'); $this->resetErrorBag('newChildName', 'newChildColor'); } public function cancelChildCreate(): void { $this->reset('creatingChildOf', 'newChildName', 'newChildColor'); } public function createChildFolder(): void { $parent = $this->folderForAction($this->creatingChildOf ?? 0, 'createChild'); $validated = $this->validate([ 'newChildName' => $this->folderNameRules($parent->organization_id, parentId: $parent->id), 'newChildColor' => $this->folderColorRules(), ]); Folder::create([ 'user_id' => auth()->id(), 'organization_id' => $parent->organization_id, 'name' => $validated['newChildName'], 'color' => filled($validated['newChildColor']) ? $validated['newChildColor'] : null, 'parent_id' => $parent->id, ]); $this->reset('creatingChildOf', 'newChildName', 'newChildColor'); } public function startRename(int $folderId): void { $folder = $this->folderForAction($folderId, 'update'); $this->renamingFolderId = $folder->id; $this->renameName = $folder->name; $this->resetErrorBag('renameName'); } public function cancelRename(): void { $this->reset('renamingFolderId', 'renameName'); } public function saveRename(): void { $folder = $this->folderForAction($this->renamingFolderId ?? 0, 'update'); $validated = $this->validate([ 'renameName' => $this->folderNameRules($folder->organization_id, parentId: $folder->parent_id, ignoreId: $folder->id), ]); $folder->update(['name' => $validated['renameName']]); $this->reset('renamingFolderId', 'renameName'); } public function confirmFolderDelete(int $folderId): void { $this->folderForAction($folderId, 'delete'); $this->confirmingFolderDelete = $folderId; $this->modal('confirm-folder-delete')->show(); } public function deleteFolder(): void { $folder = $this->folderForAction($this->confirmingFolderDelete ?? 0, 'delete'); if ($this->expandedFolderId === $folder->id) { $this->expandedFolderId = null; } $folder->delete(); $this->confirmingFolderDelete = null; $this->modal('confirm-folder-delete')->close(); } public function toggleExpandFolder(int $folderId): void { $this->visibleFolder($folderId); $this->expandedFolderId = $this->expandedFolderId === $folderId ? null : $folderId; } public function startColorEdit(int $folderId): void { $folder = $this->folderForAction($folderId, 'update'); $this->colorEditingFolderId = $folder->id; $this->colorDraft = $folder->color ?? ''; $this->resetErrorBag('colorDraft'); $this->modal('folder-color')->show(); } public function saveFolderColor(): void { $folder = $this->folderForAction($this->colorEditingFolderId ?? 0, 'update'); $validated = $this->validate([ 'colorDraft' => $this->folderColorRules(), ]); $folder->update([ 'color' => filled($validated['colorDraft']) ? $validated['colorDraft'] : null, ]); $this->reset('colorEditingFolderId', 'colorDraft'); $this->modal('folder-color')->close(); } public function startShare(int $folderId): void { $folder = $this->folderForAction($folderId, 'manageShares'); $this->sharingFolderId = $folder->id; $this->shareVisibility = $folder->visibility->value; $this->collaboratorRoles = $folder->shares() ->get() ->mapWithKeys(fn (User $user): array => [$user->id => (string) $user->pivot->role]) ->all(); $this->reset('inviteEmail', 'inviteRole'); $this->resetErrorBag('inviteEmail', 'inviteRole'); $this->modal('folder-share')->show(); } public function closeShare(): void { $this->reset('sharingFolderId', 'inviteEmail', 'inviteRole', 'collaboratorRoles'); $this->modal('folder-share')->close(); } public function updatedCollaboratorRoles(string $value, string $key): void { $this->updateCollaboratorRole((int) $key, $value); } public function updatedShareVisibility(string $value): void { $this->updateVisibility($value); } public function updateVisibility(string $visibility): void { $folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares'); $validated = validator( ['shareVisibility' => $visibility], ['shareVisibility' => ['required', Rule::enum(FolderVisibility::class)]], )->validate(); // Personal workspaces hold a single member, so there is nothing to // share a folder with. Only ever move such a folder back to private. if ($validated['shareVisibility'] === FolderVisibility::Workspace->value && $folder->organization?->isPersonal()) { return; } $folder->update(['visibility' => $validated['shareVisibility']]); unset($this->sharingFolder); Flux::toast(variant: 'success', text: __('Folder sharing updated.')); } public function inviteCollaborator(): void { $folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares'); $validated = $this->validate([ 'inviteEmail' => ['required', 'string', 'email', 'max:255'], 'inviteRole' => ['required', Rule::enum(FolderRole::class)], ]); $email = Str::lower($validated['inviteEmail']); if ($folder->shares()->where('users.email', $email)->exists()) { $this->addError('inviteEmail', __('That person already has access to this folder.')); return; } if (Str::lower((string) $folder->user?->email) === $email) { $this->addError('inviteEmail', __('That person already owns this folder.')); return; } $invitation = Invitation::updateOrCreate( [ 'inviteable_type' => Folder::class, 'inviteable_id' => $folder->id, 'email' => $email, ], [ 'inviter_id' => auth()->id(), 'role' => $validated['inviteRole'], 'token' => Str::random(64), 'expires_at' => now()->addDays(Invitation::EXPIRY_DAYS), 'accepted_at' => null, 'accepted_by_id' => null, ], ); Mail::to($email)->send(new InvitationMail($invitation)); $this->reset('inviteEmail', 'inviteRole'); unset($this->sharingInvitations); Flux::toast(variant: 'success', text: __('Invitation sent to :email.', ['email' => $email])); } public function updateCollaboratorRole(int $userId, string $role): void { $folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares'); if ($userId === $folder->user_id) { return; } $validated = validator( ['role' => $role], ['role' => ['required', Rule::enum(FolderRole::class)]], )->validate(); $folder->shares()->updateExistingPivot($userId, ['role' => $validated['role']]); unset($this->sharingCollaborators); Flux::toast(variant: 'success', text: __('Collaborator role updated.')); } public function removeCollaborator(int $userId): void { $folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares'); if ($userId === $folder->user_id) { return; } $folder->shares()->detach($userId); unset($this->sharingCollaborators); Flux::toast(variant: 'success', text: __('Collaborator removed.')); } public function revokeFolderInvitation(int $invitationId): void { $folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares'); $invitation = $folder->invitations()->whereKey($invitationId)->firstOrFail(); abort_unless(auth()->user()->can('delete', $invitation), 403); $invitation->delete(); unset($this->sharingInvitations); Flux::toast(variant: 'success', text: __('Invitation revoked.')); } public function moveTodoToFolder(int $todoId, int $folderId): void { $todo = Todo::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->with('folder') ->findOrFail($todoId); abort_unless(auth()->user()->can('update', $todo), 403); $folder = $this->visibleFolder($folderId); abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403); $todo->update(['folder_id' => $folder->id]); } public function moveFolderToTopLevel(int $folderId): void { $folder = $this->folderForAction($folderId, 'update'); $hasSiblingConflict = Folder::query() ->where('organization_id', $folder->organization_id) ->whereNull('parent_id') ->whereKeyNot($folder->id) ->where('name', $folder->name) ->exists(); if ($hasSiblingConflict) { return; } $folder->update(['parent_id' => null]); } public function moveFolderToFolder(int $folderId, int $targetFolderId): void { $folder = $this->folderForAction($folderId, 'update'); $target = $this->folderForAction($targetFolderId, 'createChild'); if ($folder->id === $target->id || $this->isDescendantOf($target, $folder)) { return; } $hasSiblingConflict = $target->children() ->whereKeyNot($folder->id) ->where('name', $folder->name) ->exists(); if ($hasSiblingConflict) { return; } $folder->update(['parent_id' => $target->id]); } private function isDescendantOf(Folder $folder, Folder $ancestor): bool { $cursor = $folder->parent; while ($cursor !== null) { if ($cursor->id === $ancestor->id) { return true; } $cursor = $cursor->parent; } return false; } public function todosForFolder(int $folderId): Collection { return Todo::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->where('folder_id', $folderId) ->active() ->priority() ->get(); } public function startTodoCreate(int $folderId): void { $this->folderForTodoCreate($folderId); $this->todoFolderId = $folderId; $this->reset('todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes'); $this->resetErrorBag('todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes'); $this->showTodoForm = true; } public function saveFolderTodo(): void { $folder = $this->folderForTodoCreate($this->todoFolderId ?? 0); $validated = $this->validate([ 'todoDescription' => ['required', 'string', 'max:255'], 'todoStatus' => ['required', Rule::enum(TodoStatus::class)], 'todoDueDate' => ['nullable', 'date'], 'todoNotes' => ['nullable', 'string', 'max:'.auth()->user()->notesLimit()], ]); Todo::create([ 'user_id' => auth()->id(), 'folder_id' => $folder->id, 'description' => $validated['todoDescription'], 'status' => TodoStatus::from($validated['todoStatus']), 'due_date' => filled($validated['todoDueDate']) ? Carbon::parse($validated['todoDueDate']) : null, 'notes' => filled($validated['todoNotes']) ? $validated['todoNotes'] : null, 'position' => Todo::nextPositionFor($folder), ]); $this->reset('showTodoForm', 'todoFolderId', 'todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes'); } #[Computed] public function folderTree(): Collection { return Folder::treeForVisibleTo(auth()->id(), $this->activeWorkspaceId()); } #[Computed] public function todoFolder(): ?Folder { if ($this->todoFolderId === null) { return null; } return Folder::query() ->with('parent') ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->find($this->todoFolderId); } #[Computed] public function todoFolderPath(): string { if ($this->todoFolder === null) { return ''; } return collect([$this->todoFolder->parent?->name, $this->todoFolder->name]) ->filter() ->implode(' / '); } #[Computed] public function openCounts(): Collection { return Todo::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->active() ->pluck('folder_id') ->countBy(); } #[Computed] public function sharingFolder(): ?Folder { if ($this->sharingFolderId === null) { return null; } return Folder::query() ->with('organization') ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->find($this->sharingFolderId); } /** * @return Collection */ #[Computed] public function sharingCollaborators(): Collection { return $this->sharingFolder?->shares()->orderBy('name')->get() ?? collect(); } /** * @return Collection */ #[Computed] public function sharingInvitations(): Collection { return $this->sharingFolder?->invitations()->pending()->orderBy('email')->get() ?? collect(); } /** * @return array */ private function folderColorRules(): array { return ['nullable', 'string', 'max:20', Rule::in([...Folder::COLORS, ''])]; } /** * @return array */ private function folderNameRules(int $organizationId, ?int $parentId = null, ?int $ignoreId = null): array { return [ 'required', 'string', 'max:255', Rule::unique('folders', 'name') ->ignore($ignoreId) ->where(fn ($query) => $query ->where('organization_id', $organizationId) ->where('parent_id', $parentId)), ]; } private function activeWorkspaceId(): int { return (int) auth()->user()->current_organization_id; } private function visibleFolder(int $folderId): Folder { return Folder::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->findOrFail($folderId); } private function folderForAction(int $folderId, string $ability): Folder { $folder = $this->visibleFolder($folderId); abort_unless(auth()->user()->can($ability, $folder), 403); return $folder; } private function folderForTodoCreate(int $folderId): Folder { $folder = $this->visibleFolder($folderId); abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403); return $folder; } }; ?>
{{ __('Folders') }} {{ __('Organize your todos into a top-level and nested folder hierarchy.') }}
@if ($this->folderTree->isEmpty()) {{ __('No folders yet. Create your first top-level folder below.') }} @endif
{{ __('New top-level folder') }}
{{ __('Color') }}
{{ __('Add folder') }}
@forelse ($this->folderTree as $node)
{{ __('Drop here to make it a top-level folder') }}
@if ($renamingFolderId === $node['folder']->id)
{{ __('Save') }} {{ __('Cancel') }}
@else
@can('update', $node['folder'])
@endcan {{ $node['folder']->name }} {{ $node['folder']->organization?->name }} @if ($node['folder']->visibility === \App\FolderVisibility::Workspace || $node['folder']->shares_count > 0) {{ __('Shared') }} @endif
{{ $this->openCounts->get($node['folder']->id, 0) }} {{ __('open') }}
@can('manageShares', $node['folder']) @endcan @can('update', $node['folder']) @endcan @can('create', [App\Models\Todo::class, $node['folder']]) @endcan @can('createChild', $node['folder']) @endcan @can('update', $node['folder']) @endcan
@endif
@if ($expandedFolderId === $node['folder']->id)
{{ __('Drag a todo onto any folder row to move it.') }} @forelse ($this->todosForFolder($node['folder']->id) as $todo)
{{ $todo->description }} {{ $todo->status->label() }}
@empty {{ __('No open todos in this folder.') }} @endforelse
@endif @if ($creatingChildOf === $node['folder']->id)
{{ __('Sub-folder of') }} {{ $node['folder']->name }}
{{ __('Color') }}
{{ __('Add') }} {{ __('Cancel') }}
@endif
@empty
{{ __('No folders to display.') }}
@endforelse
@if ($this->todoFolder !== null) {{ __('Add todo') }} - {{ $this->todoFolderPath }} @else {{ __('Add todo') }} @endif {{ __('Add a task with a description. A due date is optional.') }} @if ($this->todoFolder !== null)
{{ __('Adding to') }} {{ $this->todoFolderPath }}
@endif
{{ __('Description') }} {{ __('Status') }} @foreach (TodoStatus::cases() as $status) {{ $status->label() }} @endforeach {{ __('Due date') }} {{ __('Notes') }}
{{ __('Cancel') }} {{ __('Add todo') }}
{{ __('Folder color') }} {{ __('Choose a color for this folder, or clear it to remove any color.') }}
{{ __('Color') }}
{{ __('Cancel') }} {{ __('Save color') }}
{{ __('Delete folder?') }} {{ __('Deleting a folder also deletes its sub-folders and every todo inside them. This action cannot be undone.') }}
{{ __('Cancel') }} {{ __('Delete folder') }}
@if ($this->sharingFolder !== null)
{{ __('Share folder') }} {{ __('Manage who can access ":name" and what they can do.', ['name' => $this->sharingFolder->name]) }}
@if ($this->sharingFolder->organization !== null && ! $this->sharingFolder->organization->isPersonal()) {{ __('Visibility') }} @foreach (FolderVisibility::cases() as $visibility) {{ $visibility->label() }} @endforeach {{ __('Workspace folders are visible to every member of :name.', ['name' => $this->sharingFolder->organization->name]) }} @else {{ __('This folder is only visible to you. Personal workspaces cannot hold other members.') }} @endif
{{ __('Collaborators') }}
{{ $this->sharingFolder->user?->name }} {{ __('Owner') }}
@forelse ($this->sharingCollaborators as $collaborator)
{{ $collaborator->name }} {{ $collaborator->email }}
@foreach (FolderRole::assignable() as $role) {{ $role->label() }} @endforeach
@empty {{ __('No one else has access yet.') }} @endforelse
{{ __('Invite by email') }}
@foreach (FolderRole::assignable() as $role) {{ $role->label() }} @endforeach
{{ __('Send invite') }}
@if ($this->sharingInvitations->isNotEmpty())
{{ __('Pending invitations') }} @foreach ($this->sharingInvitations as $invitation)
{{ $invitation->email }} {{ $invitation->roleLabel() }}
{{ __('Revoke') }}
@endforeach
@endif
{{ __('Done') }}
@endif