Files
Nexora/app/Http/Controllers/ProjectController.php

192 lines
6.3 KiB
PHP
Raw Normal View History

2025-04-23 00:14:33 +06:00
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Folder;
2025-04-23 00:14:33 +06:00
use App\Models\Project;
use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
2025-05-23 00:26:53 +02:00
use Illuminate\Support\Facades\Storage;
2025-04-23 00:14:33 +06:00
class ProjectController extends Controller
{
use AuthorizesRequests;
2025-04-23 00:14:33 +06:00
/**
* Display a listing of the resource.
*/
public function index()
{
2025-04-30 20:56:28 +02:00
$projects = auth()->user()->hasRole('admin')
? Project::get() // Todos los proyectos para admin
: auth()->user()->projects()->latest()->get(); // Solo proyectos asignados
2025-04-30 20:56:28 +02:00
/*
$projects = Project::whereHas('users', function($query) {
2025-04-23 00:14:33 +06:00
$query->where('user_id', auth()->id());
})
->filter(['search' => request('search')])
2025-04-30 20:56:28 +02:00
->paginate(9);*/
2025-04-23 00:14:33 +06:00
return view('projects.index', compact('projects'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$this->authorize('create projects');
2025-05-23 00:26:53 +02:00
$project = new Project();
2025-04-23 00:14:33 +06:00
return view('projects.create', [
2025-05-23 00:26:53 +02:00
'project' => $project,
2025-04-23 00:14:33 +06:00
'categories' => Category::orderBy('name')->get(),
'users' => User::where('id', '!=', auth()->id())->get(),
'companies' => \App\Models\Company::all(), // Pass companies to the view,
2025-04-23 00:14:33 +06:00
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'reference' => 'required|string|max:12',
2025-04-23 00:14:33 +06:00
'name' => 'required|string|max:255',
2025-04-30 20:56:28 +02:00
'description' => 'nullable|string',
'status' => 'required|in:Activo,Inactivo',
2025-04-23 00:14:33 +06:00
'address' => 'nullable|string|max:255',
'province' => 'nullable|string|max:100',
2025-05-23 00:26:53 +02:00
'country' => 'nullable|string|size:2',
2025-04-23 00:14:33 +06:00
'postal_code' => 'nullable|string|max:10',
'latitude' => 'required|numeric|between:-90,90',
'longitude' => 'required|numeric|between:-180,180',
'start_date' => 'nullable|date',
'deadline' => 'nullable|date|after:start_date',
2025-04-30 20:56:28 +02:00
'categories' => 'nullable|array|exists:categories,id',
2025-05-23 00:26:53 +02:00
'project_image_path' => 'nullable|string',
'company_id' => 'required|exists:companies,id', // Validate company_id
2025-04-23 00:14:33 +06:00
]);
try {
// Combinar datos del usuario autenticado
$validated = array_merge($validated, [
'creator_id' => auth()->id()
]);
// Manejar la imagen
2025-05-23 00:26:53 +02:00
if ($request->has('project_image_path') && $request->project_image_path) {
$tempPath = $request->project_image_path;
$newPath = 'images/projects/' . basename($tempPath);
Storage::move($tempPath, $newPath); // Mover el archivo
$validated['project_image_path'] = $newPath; // Actualizar path
2025-04-23 00:14:33 +06:00
}
2025-05-23 00:26:53 +02:00
2025-04-23 00:14:33 +06:00
// Crear el proyecto con todos los datos validados
$project = Project::create($validated);
// Adjuntar categorías
if($request->has('categories')) {
$project->categories()->sync($request->categories);
}
Folder::create([
'name' => 'Project',
'project_id' => $project->id,
'parent_id' => null,
]);
2025-04-23 00:14:33 +06:00
2025-04-30 20:56:28 +02:00
return redirect()->route('projects.show', $project)->with('success', 'Proyecto creado exitosamente');
2025-04-23 00:14:33 +06:00
} catch (\Exception $e) {
2025-04-30 20:56:28 +02:00
return back()->withInput()->with('error', 'Error al crear el proyecto: ' . $e->getMessage());
2025-04-23 00:14:33 +06:00
}
}
2025-05-23 00:26:53 +02:00
/**
* Show the form for editing the specified resource.
*/
public function edit(Project $project)
{
$this->authorize('update', $project);
return view('projects.create', [
'project' => $project,
'categories' => Category::orderBy('name')->get(),
'users' => User::where('id', '!=', auth()->id())->get(),
'companies' => \App\Models\Company::all(), // Pass companies to the view
2025-05-23 00:26:53 +02:00
]);
}
2025-04-23 00:14:33 +06:00
/**
* Display the specified resource.
*/
public function show(Project $project)
{
2025-04-30 20:56:28 +02:00
//$this->authorize('view', $project); // Si usas políticas
2025-04-23 00:14:33 +06:00
$project->load(['categories', 'documents']);
return view('projects.show', [
'project' => $project->load(['rootFolders', 'documents', 'categories']),
'documents' => $project->documents()->paginate(10),
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Project $project)
{
$this->authorize('update', $project);
$validated = $request->validate([
'reference' => 'required|string|max:255',
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'status' => 'required|in:Activo,Inactivo',
'address' => 'nullable|string|max:255',
'province' => 'nullable|string|max:100',
'country' => 'nullable|string|size:2',
'postal_code' => 'nullable|string|max:10',
'latitude' => 'required|numeric|between:-90,90',
'longitude' => 'required|numeric|between:-180,180',
'start_date' => 'nullable|date',
'deadline' => 'nullable|date|after:start_date',
'categories' => 'nullable|array|exists:categories,id',
'project_image_path' => 'nullable|string',
'company_id' => 'required|exists:companies,id', // Validate company_id
]);
$project->update($validated);
if ($request->has('categories')) {
$project->categories()->sync($request->categories);
}
2025-04-23 00:14:33 +06:00
return redirect()->route('projects.show', $project)->with('success', 'Project updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Project $project)
{
//
}
/**
* Display the specified resource.
*/
2025-04-23 00:14:33 +06:00
public function __invoke(Project $project)
{
return view('projects.show', [
'project' => $project
]);
}
}