feat(tasks): complete task management system with Kanban, Calendar, notifications
- Add Task model with subtasks, priorities, status transitions, dates, hours - Add Comment polymorphic model for tasks/issues/projects - Livewire components: TaskManager (list+filters), TaskForm (modal), TaskDetail, TaskKanban (drag&drop), TaskCalendar (FullCalendar) - TaskPolicy with permissions (view/create/edit/delete/assign/manage all) - Notifications: assigned, status change, overdue, comment added - Daily overdue notification job scheduled - Dashboard widget unifies IssueTask + Task - i18n: en/es/fr/ru (393 keys each) - Routes, navigation, offline sync support - 101 tests passing, Pint compliant on new files
This commit is contained in:
@@ -11,14 +11,22 @@ class LanguageSwitcher extends Component
|
||||
{
|
||||
public string $currentLocale;
|
||||
|
||||
/** Idiomas disponibles con nombre y bandera. */
|
||||
public array $languages = [
|
||||
'en' => ['name' => 'English', 'flag' => 'gb.svg'],
|
||||
'es' => ['name' => 'Español', 'flag' => 'es.svg'],
|
||||
'fr' => ['name' => 'Français', 'flag' => 'fr.svg'],
|
||||
'ru' => ['name' => 'Русский', 'flag' => 'ru.svg'],
|
||||
];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->currentLocale = App::getLocale();
|
||||
}
|
||||
|
||||
public function switchLanguage(string $locale): void
|
||||
public function updatedCurrentLocale(string $locale): void
|
||||
{
|
||||
if (!in_array($locale, ['en', 'es'])) {
|
||||
if (!in_array($locale, ['en', 'es', 'fr', 'ru'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -31,11 +39,14 @@ class LanguageSwitcher extends Component
|
||||
}
|
||||
|
||||
// Dispatch a browser event — JavaScript reloads the page.
|
||||
// PHP-side redirects break because $this->redirect() runs inside
|
||||
// /livewire/update (the AJAX endpoint), not on the real page URL.
|
||||
$this->dispatch('locale-changed');
|
||||
}
|
||||
|
||||
public function switchLanguage(string $locale): void
|
||||
{
|
||||
$this->updatedCurrentLocale($locale);
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.common.language-switcher');
|
||||
|
||||
@@ -83,20 +83,27 @@ class InspectionTable extends DataTableComponent
|
||||
->html(),
|
||||
|
||||
Column::make('Acciones')
|
||||
->label(fn ($row) =>
|
||||
'<div class="flex justify-end gap-1">
|
||||
<button wire:click="$dispatch(\'map-view-inspection\', { id: ' . $row->id . ' })"
|
||||
class="btn btn-xs btn-ghost" title="Ver inspección">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
|
||||
</button>
|
||||
@can("edit inspections")
|
||||
<button wire:click="$dispatch(\'edit-inspection\', { id: ' . $row->id . ' })"
|
||||
class="btn btn-xs btn-ghost" title="Editar inspección">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
|
||||
</button>
|
||||
@endcan
|
||||
</div>')
|
||||
->html(),
|
||||
->label(fn ($row) =>
|
||||
'<div class="flex justify-end gap-1">'
|
||||
. '<button wire:click="$dispatch(\'map-view-inspection\', { id: ' . $row->id . ' })"'
|
||||
. 'class="btn btn-xs btn-ghost" title="Ver inspección">'
|
||||
. '<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>'
|
||||
. '</button>'
|
||||
. '@can("edit inspections")'
|
||||
. '<button wire:click="$dispatch(\'edit-inspection\', { id: ' . $row->id . ' })"'
|
||||
. 'class="btn btn-xs btn-ghost" title="Editar inspección">'
|
||||
. '<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>'
|
||||
. '</button>'
|
||||
. '@endcan'
|
||||
. '@can("delete inspections")'
|
||||
. '<button wire:click="$dispatch(\'delete-inspection\', { id: ' . $row->id . ' })"'
|
||||
. 'class="btn btn-xs btn-ghost btn-error" title="Eliminar inspección"'
|
||||
. 'onclick="return confirm(\'¿Eliminar esta inspección? No se puede deshacer.\');">'
|
||||
. '<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>'
|
||||
. '</button>'
|
||||
. '@endcan'
|
||||
. '</div>')
|
||||
->html(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use Livewire\Component;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\WithFileUploads;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Models\Project;
|
||||
use App\Models\Phase;
|
||||
use App\Models\Layer;
|
||||
@@ -13,6 +14,7 @@ use App\Models\Feature;
|
||||
use App\Models\Inspection;
|
||||
use App\Models\InspectionTemplate;
|
||||
use App\Models\Issue;
|
||||
use App\Models\Media;
|
||||
|
||||
class ProjectMap extends Component
|
||||
{
|
||||
@@ -353,6 +355,14 @@ class ProjectMap extends Component
|
||||
$this->dispatch('notify', 'Inspección guardada correctamente');
|
||||
}
|
||||
|
||||
// Notificar a usuarios del proyecto (excepto creador)
|
||||
$usersToNotify = $this->project->users()
|
||||
->where('user_id', '!=', auth()->id())
|
||||
->get();
|
||||
foreach ($usersToNotify as $user) {
|
||||
$user->notify(new \App\Notifications\InspectionCompletedNotification($inspection));
|
||||
}
|
||||
|
||||
// Reload global list
|
||||
$this->allInspections = Inspection::where('project_id', $this->project->id)
|
||||
->with(['feature.layer.phase', 'template', 'user'])
|
||||
@@ -547,6 +557,58 @@ class ProjectMap extends Component
|
||||
$this->dispatch('notify', 'Inspección actualizada correctamente');
|
||||
}
|
||||
|
||||
// ─── Delete Inspection ───────────────────────────────────────────────────────
|
||||
|
||||
#[On('delete-inspection')]
|
||||
public function deleteInspection($id)
|
||||
{
|
||||
\Log::info('deleteInspection: START', ['id' => $id]);
|
||||
|
||||
if (!auth()->user()->can('delete inspections')) {
|
||||
$this->dispatch('notify', 'Sin permisos para eliminar inspecciones.');
|
||||
\Log::info('deleteInspection: permission denied');
|
||||
return;
|
||||
}
|
||||
|
||||
$ins = Inspection::where('project_id', $this->project->id)
|
||||
->with(['feature', 'media'])
|
||||
->find($id);
|
||||
if (!$ins) {
|
||||
\Log::info('deleteInspection: inspection not found', ['id' => $id]);
|
||||
$this->dispatch('notify', 'Inspección no encontrada');
|
||||
return;
|
||||
}
|
||||
|
||||
\Log::info('deleteInspection: BEFORE delete', ['id' => $ins->id, 'deleted_at' => $ins->deleted_at, 'project_id' => $ins->project_id]);
|
||||
|
||||
// Delete associated media files (booted event deletes physical files)
|
||||
$ins->media()->get()->each->delete();
|
||||
|
||||
$result = $ins->delete();
|
||||
\Log::info('deleteInspection: delete() returned', ['result' => $result]);
|
||||
|
||||
$fresh = $ins->fresh();
|
||||
\Log::info('deleteInspection: AFTER delete', ['id' => $id, 'fresh_deleted_at' => $fresh ? $fresh->deleted_at : 'null', 'fresh_exists' => $fresh ? 'yes' : 'no']);
|
||||
|
||||
// Refresh lists
|
||||
$this->allInspections = Inspection::where('project_id', $this->project->id)
|
||||
->with(['feature.layer.phase', 'template', 'user'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
$this->loadInspectionHistory();
|
||||
|
||||
$this->dispatch('notify', 'Inspección eliminada correctamente');
|
||||
\Log::info('deleteInspection: dispatched notify SUCCESS');
|
||||
|
||||
// Notificar a usuarios del proyecto (excepto eliminador)
|
||||
$usersToNotify = $this->project->users()
|
||||
->where('user_id', '!=', auth()->id())
|
||||
->get();
|
||||
foreach ($usersToNotify as $user) {
|
||||
$user->notify(new \App\Notifications\InspectionDeletedNotification($ins));
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Feature images ──────────────────────────────────────────────────────────
|
||||
|
||||
public function toggleFeatureImages()
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Tasks;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class TaskCalendar extends Component
|
||||
{
|
||||
public ?Project $project = null;
|
||||
|
||||
public $viewMode = 'dayGridMonth'; // dayGridMonth, timeGridWeek, timeGridDay, listWeek
|
||||
|
||||
public $search = '';
|
||||
|
||||
public $assigneeFilter = '';
|
||||
|
||||
public $phaseFilter = '';
|
||||
|
||||
public $priorityFilter = '';
|
||||
|
||||
public $statusFilter = '';
|
||||
|
||||
protected $queryString = [
|
||||
'viewMode' => ['except' => 'dayGridMonth'],
|
||||
'search' => ['except' => ''],
|
||||
'assigneeFilter' => ['except' => ''],
|
||||
'phaseFilter' => ['except' => ''],
|
||||
'priorityFilter' => ['except' => ''],
|
||||
'statusFilter' => ['except' => ''],
|
||||
];
|
||||
|
||||
public function mount(?Project $project = null): void
|
||||
{
|
||||
$this->project = $project;
|
||||
|
||||
if ($project) {
|
||||
abort_unless($this->canAccessProject() && Auth::user()->can('view tasks'), 403);
|
||||
} else {
|
||||
abort_unless(Auth::user()->can('view tasks'), 403);
|
||||
}
|
||||
}
|
||||
|
||||
private function canAccessProject(): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
return $user->can('manage all')
|
||||
|| ($this->project && $this->project->users()->where('user_id', $user->id)->exists());
|
||||
}
|
||||
|
||||
#[On('tasksChanged')]
|
||||
public function refreshCalendar(): void
|
||||
{
|
||||
// Trigger re-render
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$query = Task::with(['project', 'phase', 'assignee'])
|
||||
->where(function ($q) use ($user) {
|
||||
if (! $user->can('manage all tasks')) {
|
||||
$q->whereHas('project', function ($sub) use ($user) {
|
||||
$sub->whereHas('users', function ($sub2) use ($user) {
|
||||
$sub2->where('user_id', $user->id);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if ($this->project) {
|
||||
$query->where('project_id', $this->project->id);
|
||||
}
|
||||
|
||||
if ($this->search) {
|
||||
$query->where(function ($q) {
|
||||
$q->where('title', 'like', '%'.$this->search.'%')
|
||||
->orWhere('description', 'like', '%'.$this->search.'%');
|
||||
});
|
||||
}
|
||||
|
||||
if ($this->assigneeFilter) {
|
||||
$query->where('assigned_to', $this->assigneeFilter);
|
||||
}
|
||||
|
||||
if ($this->phaseFilter) {
|
||||
$query->where('phase_id', $this->phaseFilter);
|
||||
}
|
||||
|
||||
if ($this->priorityFilter) {
|
||||
$query->where('priority', $this->priorityFilter);
|
||||
}
|
||||
|
||||
if ($this->statusFilter) {
|
||||
$query->where('status', $this->statusFilter);
|
||||
}
|
||||
|
||||
$tasks = $query->get();
|
||||
|
||||
// Transform tasks to FullCalendar events
|
||||
$events = $tasks->map(function ($task) {
|
||||
$start = $task->start_date ?? $task->due_date ?? $task->created_at->toDateString();
|
||||
$end = $task->due_date ?? $task->start_date;
|
||||
|
||||
return [
|
||||
'id' => $task->id,
|
||||
'title' => $task->title,
|
||||
'start' => $start,
|
||||
'end' => $end ? $end->addDay()->toDateString() : null, // FullCalendar end is exclusive
|
||||
'url' => route('projects.tasks.show', [$task->project_id, $task->id]),
|
||||
'backgroundColor' => $task->priority_color,
|
||||
'borderColor' => $task->priority_color,
|
||||
'textColor' => '#ffffff',
|
||||
'extendedProps' => [
|
||||
'status' => $task->status,
|
||||
'statusLabel' => $task->status_label,
|
||||
'priority' => $task->priority,
|
||||
'priorityLabel' => $task->priority_label,
|
||||
'project' => $task->project->name ?? '—',
|
||||
'phase' => $task->phase->name ?? '—',
|
||||
'assignee' => $task->assignee->name ?? 'Sin asignar',
|
||||
'dueDate' => $task->due_date?->format('d/m/Y'),
|
||||
'isOverdue' => $task->is_overdue,
|
||||
'isDueToday' => $task->is_due_today,
|
||||
'progress' => $task->progress,
|
||||
'subtasksCount' => $task->subtasks->count(),
|
||||
'completedSubtasksCount' => $task->subtasks->where('status', 'completed')->count(),
|
||||
],
|
||||
'classNames' => [
|
||||
'fc-task-'.$task->status,
|
||||
$task->is_overdue ? 'fc-task-overdue' : '',
|
||||
$task->is_due_today ? 'fc-task-due-today' : '',
|
||||
],
|
||||
];
|
||||
})->toArray();
|
||||
|
||||
// Filter options
|
||||
$assignees = User::whereHas('assignedTasks', function ($q) use ($user) {
|
||||
if (! $user->can('manage all tasks')) {
|
||||
$q->whereHas('project', function ($sub) use ($user) {
|
||||
$sub->whereHas('users', function ($sub2) use ($user) {
|
||||
$sub2->where('user_id', $user->id);
|
||||
});
|
||||
});
|
||||
}
|
||||
})->get(['id', 'name']);
|
||||
|
||||
$phases = $this->project
|
||||
? $this->project->phases()->get(['id', 'name'])
|
||||
: collect();
|
||||
|
||||
return view('livewire.tasks.task-calendar', [
|
||||
'events' => json_encode($events),
|
||||
'assignees' => $assignees,
|
||||
'phases' => $phases,
|
||||
'statusOptions' => Task::statusOptions(),
|
||||
'priorityOptions' => Task::priorityOptions(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Tasks;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Project;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class TaskDetail extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public Task $task;
|
||||
|
||||
public ?Project $project = null;
|
||||
|
||||
// Comment form
|
||||
public $commentBody = '';
|
||||
|
||||
public $editingCommentId = null;
|
||||
|
||||
public $editingCommentBody = '';
|
||||
|
||||
// Subtask form
|
||||
public $subtaskTitle = '';
|
||||
|
||||
protected $listeners = ['tasksChanged' => '$refresh', 'commentAdded' => '$refresh', 'subtaskAdded' => '$refresh'];
|
||||
|
||||
public function mount(Task $task): void
|
||||
{
|
||||
$this->task = $task->load(['project', 'phase', 'assignee', 'creator', 'completer', 'subtasks', 'media', 'comments.user', 'comments.replies.user']);
|
||||
$this->project = $this->task->project;
|
||||
|
||||
abort_unless(Auth::user()->can('view', $this->task), 403);
|
||||
}
|
||||
|
||||
public function addComment(): void
|
||||
{
|
||||
$this->validate(['commentBody' => 'required|string|max:5000']);
|
||||
|
||||
$this->task->comments()->create([
|
||||
'user_id' => Auth::id(),
|
||||
'body' => $this->commentBody,
|
||||
]);
|
||||
|
||||
$this->commentBody = '';
|
||||
$this->dispatch('commentAdded');
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function startEditComment($commentId): void
|
||||
{
|
||||
$comment = $this->task->comments()->find($commentId);
|
||||
if ($comment && $comment->user_id === Auth::id()) {
|
||||
$this->editingCommentId = $commentId;
|
||||
$this->editingCommentBody = $comment->body;
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEditComment(): void
|
||||
{
|
||||
$this->validate(['editingCommentBody' => 'required|string|max:5000']);
|
||||
|
||||
$comment = Comment::find($this->editingCommentId);
|
||||
if ($comment && $comment->user_id === Auth::id()) {
|
||||
$comment->update(['body' => $this->editingCommentBody]);
|
||||
}
|
||||
|
||||
$this->editingCommentId = null;
|
||||
$this->editingCommentBody = '';
|
||||
$this->dispatch('commentAdded');
|
||||
}
|
||||
|
||||
public function cancelEditComment(): void
|
||||
{
|
||||
$this->editingCommentId = null;
|
||||
$this->editingCommentBody = '';
|
||||
}
|
||||
|
||||
public function deleteComment($commentId): void
|
||||
{
|
||||
$comment = Comment::find($commentId);
|
||||
if ($comment && ($comment->user_id === Auth::id() || Auth::user()->can('manage all tasks'))) {
|
||||
$comment->delete();
|
||||
$this->dispatch('commentAdded');
|
||||
}
|
||||
}
|
||||
|
||||
public function addSubtask(): void
|
||||
{
|
||||
$this->validate(['subtaskTitle' => 'required|string|max:255']);
|
||||
|
||||
$maxOrder = $this->task->subtasks()->max('order') ?? 0;
|
||||
|
||||
$this->task->subtasks()->create([
|
||||
'title' => $this->subtaskTitle,
|
||||
'status' => 'pending',
|
||||
'priority' => 'medium',
|
||||
'project_id' => $this->task->project_id,
|
||||
'phase_id' => $this->task->phase_id,
|
||||
'parent_task_id' => $this->task->id,
|
||||
'created_by' => Auth::id(),
|
||||
'order' => $maxOrder + 1,
|
||||
'uuid' => Str::uuid(),
|
||||
]);
|
||||
|
||||
$this->subtaskTitle = '';
|
||||
$this->dispatch('subtaskAdded');
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function updateSubtaskStatus(Task $subtask, string $status): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('update', $subtask), 403);
|
||||
|
||||
$data = ['status' => $status];
|
||||
if ($status === 'completed') {
|
||||
$data['completed_at'] = now();
|
||||
$data['completed_by'] = Auth::id();
|
||||
} elseif ($subtask->status === 'completed') {
|
||||
$data['completed_at'] = null;
|
||||
$data['completed_by'] = null;
|
||||
}
|
||||
|
||||
$subtask->update($data);
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function deleteSubtask(Task $subtask): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('delete', $subtask), 403);
|
||||
$subtask->delete();
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function changeStatus(string $status): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('update', $this->task), 403);
|
||||
|
||||
$oldStatus = $this->task->status;
|
||||
$data = ['status' => $status];
|
||||
|
||||
if ($oldStatus !== 'completed' && $status === 'completed') {
|
||||
$data['completed_at'] = now();
|
||||
$data['completed_by'] = Auth::id();
|
||||
} elseif ($oldStatus === 'completed' && $status !== 'completed') {
|
||||
$data['completed_at'] = null;
|
||||
$data['completed_by'] = null;
|
||||
}
|
||||
|
||||
$this->task->update($data);
|
||||
$this->task->refresh();
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$assignees = User::whereHas('assignedTasks', function ($q) {
|
||||
if ($this->project) {
|
||||
$q->where('project_id', $this->project->id);
|
||||
}
|
||||
})->get(['id', 'name']);
|
||||
|
||||
return view('livewire.tasks.task-detail', [
|
||||
'assignees' => $assignees,
|
||||
'statusOptions' => Task::statusOptions(),
|
||||
'priorityOptions' => Task::priorityOptions(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Tasks;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\Validate;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class TaskForm extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public ?Task $task = null;
|
||||
|
||||
public ?Project $project = null;
|
||||
|
||||
// Form fields
|
||||
#[Validate('required|string|max:255')]
|
||||
public $title = '';
|
||||
|
||||
#[Validate('nullable|string')]
|
||||
public $description = '';
|
||||
|
||||
#[Validate('nullable|exists:phases,id')]
|
||||
public $phase_id = '';
|
||||
|
||||
#[Validate('required|in:pending,in_progress,completed,cancelled')]
|
||||
public $status = 'pending';
|
||||
|
||||
#[Validate('required|in:low,medium,high,critical')]
|
||||
public $priority = 'medium';
|
||||
|
||||
#[Validate('nullable|exists:users,id')]
|
||||
public $assigned_to = '';
|
||||
|
||||
#[Validate('nullable|date|after_or_equal:start_date')]
|
||||
public $due_date = '';
|
||||
|
||||
#[Validate('nullable|date|before_or_equal:due_date')]
|
||||
public $start_date = '';
|
||||
|
||||
#[Validate('nullable|integer|min:0|max:10000')]
|
||||
public $estimated_hours = '';
|
||||
|
||||
#[Validate('nullable|integer|min:0|max:10000')]
|
||||
public $actual_hours = '';
|
||||
|
||||
#[Validate('nullable|integer|min:0')]
|
||||
public $order = 0;
|
||||
|
||||
// For subtasks
|
||||
#[Validate('nullable|exists:tasks,id')]
|
||||
public $parent_task_id = '';
|
||||
|
||||
// UI state
|
||||
public $showModal = false;
|
||||
|
||||
public $mode = 'create'; // 'create' or 'edit'
|
||||
|
||||
protected $listeners = ['openTaskModal' => 'openModal'];
|
||||
|
||||
public function mount(?Project $project = null, ?Task $task = null): void
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->task = $task;
|
||||
|
||||
if ($task) {
|
||||
$this->mode = 'edit';
|
||||
$this->fillFromTask($task);
|
||||
abort_unless(Auth::user()->can('update', $task), 403);
|
||||
} else {
|
||||
$this->mode = 'create';
|
||||
abort_unless(Auth::user()->can('create tasks', $project), 403);
|
||||
}
|
||||
}
|
||||
|
||||
public function openModal(array $params = []): void
|
||||
{
|
||||
$this->resetForm();
|
||||
$this->mode = $params['mode'] ?? 'create';
|
||||
$this->project = $params['project'] ?? $this->project;
|
||||
$this->task = $params['task'] ?? null;
|
||||
|
||||
if ($this->task) {
|
||||
$this->fillFromTask($this->task);
|
||||
} else {
|
||||
$this->status = 'pending';
|
||||
$this->priority = 'medium';
|
||||
$this->order = 0;
|
||||
}
|
||||
|
||||
$this->showModal = true;
|
||||
}
|
||||
|
||||
private function fillFromTask(Task $task): void
|
||||
{
|
||||
$this->title = $task->title;
|
||||
$this->description = $task->description;
|
||||
$this->phase_id = $task->phase_id ?? '';
|
||||
$this->status = $task->status;
|
||||
$this->priority = $task->priority;
|
||||
$this->assigned_to = $task->assigned_to ?? '';
|
||||
$this->due_date = $task->due_date?->format('Y-m-d') ?? '';
|
||||
$this->start_date = $task->start_date?->format('Y-m-d') ?? '';
|
||||
$this->estimated_hours = $task->estimated_hours ?? '';
|
||||
$this->actual_hours = $task->actual_hours ?? '';
|
||||
$this->order = $task->order ?? 0;
|
||||
$this->parent_task_id = $task->parent_task_id ?? '';
|
||||
}
|
||||
|
||||
private function resetForm(): void
|
||||
{
|
||||
$this->reset(['title', 'description', 'phase_id', 'status', 'priority', 'assigned_to', 'due_date', 'start_date', 'estimated_hours', 'actual_hours', 'order', 'parent_task_id']);
|
||||
$this->resetValidation();
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$validated = $this->validate();
|
||||
|
||||
$data = [
|
||||
'title' => $validated['title'],
|
||||
'description' => $validated['description'] ?? null,
|
||||
'phase_id' => $validated['phase_id'] ?? null,
|
||||
'status' => $validated['status'],
|
||||
'priority' => $validated['priority'],
|
||||
'assigned_to' => $validated['assigned_to'] ?? null,
|
||||
'due_date' => $validated['due_date'] ?? null,
|
||||
'start_date' => $validated['start_date'] ?? null,
|
||||
'estimated_hours' => $validated['estimated_hours'] ?? null,
|
||||
'actual_hours' => $validated['actual_hours'] ?? null,
|
||||
'order' => $validated['order'],
|
||||
'parent_task_id' => $validated['parent_task_id'] ?? null,
|
||||
'project_id' => $this->project?->id,
|
||||
'created_by' => Auth::id(),
|
||||
];
|
||||
|
||||
// Handle status transitions
|
||||
if ($this->mode === 'edit' && $this->task) {
|
||||
$oldStatus = $this->task->status;
|
||||
$newStatus = $data['status'];
|
||||
|
||||
if ($oldStatus !== 'completed' && $newStatus === 'completed') {
|
||||
$data['completed_at'] = now();
|
||||
$data['completed_by'] = Auth::id();
|
||||
} elseif ($oldStatus === 'completed' && $newStatus !== 'completed') {
|
||||
$data['completed_at'] = null;
|
||||
$data['completed_by'] = null;
|
||||
}
|
||||
|
||||
$this->task->update($data);
|
||||
$task = $this->task;
|
||||
session()->flash('message', 'Tarea actualizada correctamente');
|
||||
} else {
|
||||
$data['uuid'] = Str::uuid();
|
||||
$task = Task::create($data);
|
||||
session()->flash('message', 'Tarea creada correctamente');
|
||||
}
|
||||
|
||||
$this->dispatch('tasksChanged');
|
||||
$this->dispatch('taskSaved', taskId: $task->id);
|
||||
$this->closeModal();
|
||||
}
|
||||
|
||||
public function closeModal(): void
|
||||
{
|
||||
$this->showModal = false;
|
||||
$this->resetForm();
|
||||
$this->mode = 'create';
|
||||
$this->task = null;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$assignees = User::whereHas('assignedTasks', function ($q) {
|
||||
if ($this->project) {
|
||||
$q->where('project_id', $this->project->id);
|
||||
}
|
||||
})->get(['id', 'name']);
|
||||
|
||||
$phases = $this->project ? $this->project->phases->pluck('name', 'id') : collect();
|
||||
|
||||
// For parent task selector (only sibling tasks in same project)
|
||||
$parentTasks = $this->project
|
||||
? Task::where('project_id', $this->project->id)
|
||||
->whereNull('parent_task_id')
|
||||
->when($this->task, fn ($q) => $q->where('id', '!=', $this->task->id))
|
||||
->get(['id', 'title'])
|
||||
: collect();
|
||||
|
||||
return view('livewire.tasks.task-form', [
|
||||
'assignees' => $assignees,
|
||||
'phases' => $phases,
|
||||
'parentTasks' => $parentTasks,
|
||||
'statusOptions' => Task::statusOptions(),
|
||||
'priorityOptions' => Task::priorityOptions(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Tasks;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class TaskKanban extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public ?Project $project = null;
|
||||
|
||||
public $search = '';
|
||||
|
||||
public $assigneeFilter = '';
|
||||
|
||||
public $phaseFilter = '';
|
||||
|
||||
public $priorityFilter = '';
|
||||
|
||||
public $showOverdueOnly = false;
|
||||
|
||||
public $perPage = 50;
|
||||
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'assigneeFilter' => ['except' => ''],
|
||||
'phaseFilter' => ['except' => ''],
|
||||
'priorityFilter' => ['except' => ''],
|
||||
'showOverdueOnly' => ['except' => false],
|
||||
];
|
||||
|
||||
public function mount(?Project $project = null): void
|
||||
{
|
||||
$this->project = $project;
|
||||
|
||||
if ($project) {
|
||||
abort_unless($this->canAccessProject() && Auth::user()->can('view tasks'), 403);
|
||||
} else {
|
||||
abort_unless(Auth::user()->can('view tasks'), 403);
|
||||
}
|
||||
}
|
||||
|
||||
private function canAccessProject(): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
return $user->can('manage all')
|
||||
|| ($this->project && $this->project->users()->where('user_id', $user->id)->exists());
|
||||
}
|
||||
|
||||
#[On('tasksChanged')]
|
||||
public function refreshTasks(): void
|
||||
{
|
||||
// Trigger re-render
|
||||
}
|
||||
|
||||
public function updateTaskStatus(Task $task, string $status): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('update', $task), 403);
|
||||
|
||||
$oldStatus = $task->status;
|
||||
$data = ['status' => $status];
|
||||
|
||||
if ($oldStatus !== 'completed' && $status === 'completed') {
|
||||
$data['completed_at'] = now();
|
||||
$data['completed_by'] = Auth::id();
|
||||
} elseif ($oldStatus === 'completed' && $status !== 'completed') {
|
||||
$data['completed_at'] = null;
|
||||
$data['completed_by'] = null;
|
||||
}
|
||||
|
||||
$task->update($data);
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function updateTaskOrder(int $taskId, int $order): void
|
||||
{
|
||||
$task = Task::find($taskId);
|
||||
if ($task && Auth::user()->can('update', $task)) {
|
||||
$task->update(['order' => $order]);
|
||||
}
|
||||
}
|
||||
|
||||
public function resetFilters(): void
|
||||
{
|
||||
$this->reset(['search', 'assigneeFilter', 'phaseFilter', 'priorityFilter', 'showOverdueOnly']);
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$baseQuery = Task::with(['project', 'phase', 'assignee', 'creator', 'subtasks'])
|
||||
->where(function ($q) use ($user) {
|
||||
if (! $user->can('manage all tasks')) {
|
||||
$q->whereHas('project', function ($sub) use ($user) {
|
||||
$sub->whereHas('users', function ($sub2) use ($user) {
|
||||
$sub2->where('user_id', $user->id);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if ($this->project) {
|
||||
$baseQuery->where('project_id', $this->project->id);
|
||||
}
|
||||
|
||||
if ($this->search) {
|
||||
$baseQuery->where(function ($q) {
|
||||
$q->where('title', 'like', '%'.$this->search.'%')
|
||||
->orWhere('description', 'like', '%'.$this->search.'%');
|
||||
});
|
||||
}
|
||||
|
||||
if ($this->assigneeFilter) {
|
||||
$baseQuery->where('assigned_to', $this->assigneeFilter);
|
||||
}
|
||||
|
||||
if ($this->phaseFilter) {
|
||||
$baseQuery->where('phase_id', $this->phaseFilter);
|
||||
}
|
||||
|
||||
if ($this->priorityFilter) {
|
||||
$baseQuery->where('priority', $this->priorityFilter);
|
||||
}
|
||||
|
||||
if ($this->showOverdueOnly) {
|
||||
$baseQuery->overdue();
|
||||
}
|
||||
|
||||
// Get tasks grouped by status
|
||||
$statuses = ['pending', 'in_progress', 'completed', 'cancelled'];
|
||||
$columns = [];
|
||||
|
||||
foreach ($statuses as $status) {
|
||||
$query = clone $baseQuery;
|
||||
$query->where('status', $status)
|
||||
->orderBy('order')
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
$tasks = $query->get();
|
||||
|
||||
$columns[$status] = [
|
||||
'label' => Task::statusOptions()[$status],
|
||||
'color' => $this->getStatusColor($status),
|
||||
'tasks' => $tasks,
|
||||
'count' => $tasks->count(),
|
||||
];
|
||||
}
|
||||
|
||||
// Filter options
|
||||
$assignees = User::whereHas('assignedTasks', function ($q) use ($user) {
|
||||
if (! $user->can('manage all tasks')) {
|
||||
$q->whereHas('project', function ($sub) use ($user) {
|
||||
$sub->whereHas('users', function ($sub2) use ($user) {
|
||||
$sub2->where('user_id', $user->id);
|
||||
});
|
||||
});
|
||||
}
|
||||
})->get(['id', 'name']);
|
||||
|
||||
$phases = $this->project
|
||||
? $this->project->phases()->get(['id', 'name'])
|
||||
: collect();
|
||||
|
||||
return view('livewire.tasks.task-kanban', [
|
||||
'columns' => $columns,
|
||||
'assignees' => $assignees,
|
||||
'phases' => $phases,
|
||||
'statusOptions' => Task::statusOptions(),
|
||||
'priorityOptions' => Task::priorityOptions(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function getStatusColor(string $status): string
|
||||
{
|
||||
return match ($status) {
|
||||
'pending' => '#6b7280', // gray
|
||||
'in_progress' => '#3b82f6', // blue
|
||||
'completed' => '#10b981', // green
|
||||
'cancelled' => '#ef4444', // red
|
||||
default => '#6b7280',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Tasks;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Task;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class TaskManager extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public ?Project $project = null;
|
||||
|
||||
public $search = '';
|
||||
|
||||
public $statusFilter = '';
|
||||
|
||||
public $priorityFilter = '';
|
||||
|
||||
public $assigneeFilter = '';
|
||||
|
||||
public $phaseFilter = '';
|
||||
|
||||
public $showOverdueOnly = false;
|
||||
|
||||
public $perPage = 25;
|
||||
|
||||
public $sortField = 'created_at';
|
||||
|
||||
public $sortDirection = 'desc';
|
||||
|
||||
public $viewMode = 'table'; // 'table' or 'kanban'
|
||||
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'statusFilter' => ['except' => ''],
|
||||
'priorityFilter' => ['except' => ''],
|
||||
'assigneeFilter' => ['except' => ''],
|
||||
'phaseFilter' => ['except' => ''],
|
||||
'showOverdueOnly' => ['except' => false],
|
||||
'perPage' => ['except' => 25],
|
||||
'sortField' => ['except' => 'created_at'],
|
||||
'sortDirection' => ['except' => 'desc'],
|
||||
'viewMode' => ['except' => 'table'],
|
||||
];
|
||||
|
||||
public function mount(?Project $project = null): void
|
||||
{
|
||||
$this->project = $project;
|
||||
|
||||
if ($project) {
|
||||
abort_unless($this->canAccessProject() && Auth::user()->can('view tasks'), 403);
|
||||
} else {
|
||||
abort_unless(Auth::user()->can('view tasks'), 403);
|
||||
}
|
||||
}
|
||||
|
||||
private function canAccessProject(): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
return $user->can('manage all')
|
||||
|| ($this->project && $this->project->users()->where('user_id', $user->id)->exists());
|
||||
}
|
||||
|
||||
#[On('tasksChanged')]
|
||||
public function refreshTasks(): void
|
||||
{
|
||||
// Trigger re-render
|
||||
}
|
||||
|
||||
public function sortBy(string $field): void
|
||||
{
|
||||
if ($this->sortField === $field) {
|
||||
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
||||
} else {
|
||||
$this->sortField = $field;
|
||||
$this->sortDirection = 'asc';
|
||||
}
|
||||
}
|
||||
|
||||
public function resetFilters(): void
|
||||
{
|
||||
$this->reset(['search', 'statusFilter', 'priorityFilter', 'assigneeFilter', 'phaseFilter', 'showOverdueOnly']);
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function deleteTask(Task $task): void
|
||||
{
|
||||
abort_unless(Auth::user()->can('delete', $task), 403);
|
||||
$task->delete();
|
||||
session()->flash('message', 'Tarea eliminada');
|
||||
$this->dispatch('tasksChanged');
|
||||
}
|
||||
|
||||
public function toggleViewMode(): void
|
||||
{
|
||||
$this->viewMode = $this->viewMode === 'table' ? 'kanban' : 'table';
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$query = Task::with(['project', 'phase', 'assignee', 'creator', 'subtasks'])
|
||||
->where(function ($q) use ($user) {
|
||||
if (! $user->can('manage all tasks')) {
|
||||
$q->whereHas('project', function ($sub) use ($user) {
|
||||
$sub->whereHas('users', function ($sub2) use ($user) {
|
||||
$sub2->where('user_id', $user->id);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if ($this->project) {
|
||||
$query->where('project_id', $this->project->id);
|
||||
}
|
||||
|
||||
if ($this->search) {
|
||||
$query->where(function ($q) {
|
||||
$q->where('title', 'like', '%'.$this->search.'%')
|
||||
->orWhere('description', 'like', '%'.$this->search.'%');
|
||||
});
|
||||
}
|
||||
|
||||
if ($this->statusFilter) {
|
||||
$query->where('status', $this->statusFilter);
|
||||
}
|
||||
|
||||
if ($this->priorityFilter) {
|
||||
$query->where('priority', $this->priorityFilter);
|
||||
}
|
||||
|
||||
if ($this->assigneeFilter) {
|
||||
$query->where('assigned_to', $this->assigneeFilter);
|
||||
}
|
||||
|
||||
if ($this->phaseFilter) {
|
||||
$query->where('phase_id', $this->phaseFilter);
|
||||
}
|
||||
|
||||
if ($this->showOverdueOnly) {
|
||||
$query->overdue();
|
||||
}
|
||||
|
||||
// Sorting
|
||||
$allowedSorts = ['title', 'status', 'priority', 'due_date', 'created_at', 'order'];
|
||||
if (in_array($this->sortField, $allowedSorts)) {
|
||||
$query->orderBy($this->sortField, $this->sortDirection);
|
||||
} else {
|
||||
$query->orderBy('order')->orderBy('created_at', 'desc');
|
||||
}
|
||||
|
||||
$tasks = $query->paginate($this->perPage);
|
||||
|
||||
// Filter options
|
||||
$assignees = User::whereHas('assignedTasks', function ($q) use ($user) {
|
||||
if (! $user->can('manage all tasks')) {
|
||||
$q->whereHas('project', function ($sub) use ($user) {
|
||||
$sub->whereHas('users', function ($sub2) use ($user) {
|
||||
$sub2->where('user_id', $user->id);
|
||||
});
|
||||
});
|
||||
}
|
||||
})->get(['id', 'name']);
|
||||
|
||||
$phases = $this->project
|
||||
? $this->project->phases()->get(['id', 'name'])
|
||||
: collect();
|
||||
|
||||
return view('livewire.tasks.task-manager', [
|
||||
'tasks' => $tasks,
|
||||
'assignees' => $assignees,
|
||||
'phases' => $phases,
|
||||
'statusOptions' => Task::statusOptions(),
|
||||
'priorityOptions' => Task::priorityOptions(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,8 @@ class UserForm extends Component
|
||||
public array $languages = [
|
||||
'es' => ['name' => 'Español', 'flag' => 'es.svg'],
|
||||
'en' => ['name' => 'English', 'flag' => 'gb.svg'],
|
||||
'fr' => ['name' => 'Français', 'flag' => 'fr.svg'],
|
||||
'ru' => ['name' => 'Русский', 'flag' => 'ru.svg'],
|
||||
];
|
||||
|
||||
public function mount(?User $user = null): void
|
||||
|
||||
Reference in New Issue
Block a user