941dbd5997
Restores all files to thef8a1310security-review snapshot as requested, plus the 2 boot-critical fixes froma24c8a2(config/session.php env() instead of app()->environment(), and removal of the duplicate $activeTab in ProjectMap.php) so the application actually boots. Forward commit, no history rewrite. The7d854ffstate remains in history. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
161 lines
4.4 KiB
PHP
161 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use App\Models\InspectionTemplate;
|
|
use App\Models\Project;
|
|
use App\Models\Phase;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class TemplateManager extends Component
|
|
{
|
|
public $project;
|
|
public $templates;
|
|
public $phases;
|
|
public $editingTemplate = null;
|
|
public $showForm = false; // Controla si mostrar el formulario
|
|
public $form = [
|
|
'name' => '',
|
|
'description' => '',
|
|
'phase_id' => null,
|
|
'fields' => [],
|
|
];
|
|
public $fieldTypes = [
|
|
'text' => 'Texto corto',
|
|
'textarea' => 'Texto largo',
|
|
'integer' => 'Número entero',
|
|
'decimal' => 'Número decimal',
|
|
'percentage' => 'Porcentaje (0-100)',
|
|
'boolean' => 'Sí/No (checkbox)',
|
|
'date' => 'Fecha',
|
|
'select' => 'Lista desplegable',
|
|
];
|
|
|
|
protected $listeners = ['showTemplateForm' => 'newTemplate'];
|
|
|
|
public function mount(Project $project)
|
|
{
|
|
$user = Auth::user();
|
|
if (!$user->hasRole('Admin') && !$project->users()->where('user_id', $user->id)->exists()) {
|
|
abort(403);
|
|
}
|
|
$this->project = $project;
|
|
$this->loadPhases();
|
|
$this->loadTemplates();
|
|
}
|
|
|
|
public function loadPhases()
|
|
{
|
|
$this->phases = $this->project->phases()->orderBy('name')->get();
|
|
}
|
|
|
|
public function loadTemplates()
|
|
{
|
|
$this->templates = InspectionTemplate::where('project_id', $this->project->id)->get();
|
|
}
|
|
|
|
public function newTemplate()
|
|
{
|
|
$this->resetForm();
|
|
$this->editingTemplate = null;
|
|
$this->showForm = true;
|
|
}
|
|
|
|
public function editTemplate($id)
|
|
{
|
|
$template = InspectionTemplate::where('id', $id)
|
|
->where('project_id', $this->project->id)
|
|
->firstOrFail();
|
|
$this->form = $template->only(['name', 'description', 'phase_id', 'fields']);
|
|
$this->editingTemplate = $id;
|
|
$this->showForm = true;
|
|
}
|
|
|
|
public function cancelForm()
|
|
{
|
|
$this->showForm = false;
|
|
$this->resetForm();
|
|
}
|
|
|
|
public function resetForm()
|
|
{
|
|
$this->form = [
|
|
'name' => '',
|
|
'description' => '',
|
|
'phase_id' => null,
|
|
'fields' => [],
|
|
];
|
|
$this->editingTemplate = null;
|
|
}
|
|
|
|
public function addField()
|
|
{
|
|
$this->form['fields'][] = [
|
|
'name' => '',
|
|
'label' => '',
|
|
'type' => 'text',
|
|
'options' => [],
|
|
'required' => false,
|
|
'min' => null,
|
|
'max' => null,
|
|
'step' => null,
|
|
];
|
|
}
|
|
|
|
public function removeField($index)
|
|
{
|
|
unset($this->form['fields'][$index]);
|
|
$this->form['fields'] = array_values($this->form['fields']);
|
|
}
|
|
|
|
public function saveTemplate()
|
|
{
|
|
$this->validate([
|
|
'form.name' => 'required|string|max:255',
|
|
'form.phase_id' => 'nullable|exists:phases,id',
|
|
'form.fields' => 'array',
|
|
]);
|
|
|
|
if ($this->editingTemplate) {
|
|
$template = InspectionTemplate::where('id', $this->editingTemplate)
|
|
->where('project_id', $this->project->id)
|
|
->firstOrFail();
|
|
$template->update([
|
|
'name' => $this->form['name'],
|
|
'description' => $this->form['description'],
|
|
'phase_id' => $this->form['phase_id'],
|
|
'fields' => $this->form['fields'],
|
|
]);
|
|
session()->flash('message', 'Template actualizado');
|
|
} else {
|
|
InspectionTemplate::create([
|
|
'name' => $this->form['name'],
|
|
'description' => $this->form['description'],
|
|
'project_id' => $this->project->id,
|
|
'phase_id' => $this->form['phase_id'],
|
|
'fields' => $this->form['fields'],
|
|
]);
|
|
session()->flash('message', 'Template creado');
|
|
}
|
|
|
|
$this->cancelForm();
|
|
$this->loadTemplates();
|
|
}
|
|
|
|
public function deleteTemplate($id)
|
|
{
|
|
InspectionTemplate::where('id', $id)
|
|
->where('project_id', $this->project->id)
|
|
->firstOrFail()
|
|
->delete();
|
|
$this->loadTemplates();
|
|
session()->flash('message', 'Template eliminado');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.template-manager');
|
|
}
|
|
}
|