first commit
This commit is contained in:
158
app/Http/Controllers/ProjectController.php
Normal file
158
app/Http/Controllers/ProjectController.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
use AuthorizesRequests; // ← Añadir este trait
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$projects = Project::withCount('documents')
|
||||
->whereHas('users', function($query) {
|
||||
$query->where('user_id', auth()->id());
|
||||
})
|
||||
->filter(['search' => request('search')])
|
||||
->paginate(9);
|
||||
|
||||
return view('projects.index', compact('projects'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$this->authorize('create projects');
|
||||
|
||||
return view('projects.create', [
|
||||
'categories' => Category::orderBy('name')->get(),
|
||||
'users' => User::where('id', '!=', auth()->id())->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'status' => 'required|in:active,inactive',
|
||||
'team' => 'sometimes|array',
|
||||
'team.*' => 'exists:users,id',
|
||||
'project_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'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',
|
||||
'icon' => 'nullable|in:'.implode(',', config('project.icons')),
|
||||
'start_date' => 'nullable|date',
|
||||
'deadline' => 'nullable|date|after:start_date',
|
||||
'categories' => 'array|exists:categories,id',
|
||||
//'categories' => 'required|array',
|
||||
'categories.*' => 'exists:categories,id',
|
||||
'documents.*' => 'file|max:5120|mimes:pdf,docx,xlsx,jpg,png'
|
||||
]);
|
||||
|
||||
|
||||
try {
|
||||
// Combinar datos del usuario autenticado
|
||||
$validated = array_merge($validated, [
|
||||
'creator_id' => auth()->id()
|
||||
]);
|
||||
|
||||
// Manejar la imagen
|
||||
if ($request->hasFile('project_image')) {
|
||||
$path = $request->file('project_image')->store('project-images', 'public');
|
||||
$validated['project_image_path'] = $path; // Usar el nombre correcto de columna
|
||||
}
|
||||
|
||||
// Crear el proyecto con todos los datos validados
|
||||
$project = Project::create($validated);
|
||||
|
||||
// Adjuntar categorías
|
||||
if($request->has('categories')) {
|
||||
$project->categories()->sync($request->categories);
|
||||
}
|
||||
|
||||
// Manejar documentos adjuntos
|
||||
if($request->hasFile('documents')) {
|
||||
foreach ($request->file('documents') as $file) {
|
||||
$project->documents()->create([
|
||||
'file_path' => $file->store('project-documents', 'public'),
|
||||
'original_name' => $file->getClientOriginalName()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('projects.show', $project)
|
||||
->with('success', 'Proyecto creado exitosamente');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return back()->withInput()
|
||||
->with('error', 'Error al crear el proyecto: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Project $project)
|
||||
{
|
||||
$this->authorize('view', $project); // Si usas políticas
|
||||
$project->load(['categories', 'documents']);
|
||||
|
||||
return view('projects.show', [
|
||||
'project' => $project->load(['rootFolders', 'documents', 'categories']),
|
||||
'documents' => $project->documents()->paginate(10),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Project $project)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Project $project)
|
||||
{
|
||||
$this->authorize('update', $project);
|
||||
// Lógica de actualización
|
||||
$project->update($request->all());
|
||||
return redirect()->route('projects.show', $project)->with('success', 'Project updated successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Project $project)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function __invoke(Project $project)
|
||||
{
|
||||
return view('projects.show', [
|
||||
'project' => $project
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user