Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Last active September 4, 2025 13:48
Show Gist options
  • Save IgorDePaula/9365a0a39c1f814eea7328668011993f to your computer and use it in GitHub Desktop.
Save IgorDePaula/9365a0a39c1f814eea7328668011993f to your computer and use it in GitHub Desktop.
<?php
namespace App\Filament\Resources\ProjectResource\Pages;
use App\Enums\ReportTypeButtonsEnum;
use App\Filament\Resources\ProjectResource;
use App\Models\CustomReport;
use App\Models\Report;
use App\Models\ReportTemplate;
use App\Models\ReportTemplateResponse;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ToggleButtons;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Filament\Resources\Pages\Page;
use Illuminate\Contracts\Support\Htmlable;
class TemplateReport extends Page implements HasForms
{
use InteractsWithRecord, InteractsWithForms;
protected static string $resource = ProjectResource::class;
protected static string $view = 'filament.resources.project-resource.pages.template-report';
public $data;
public $id;
public $project;
public $artifact;
public function mount()
{
$this->id = request()->get('id');
$custom = request()->get('custom_report');
$this->artifact = request()->get('artifact');
$this->record = ReportTemplate::findOrFail($this->id);
$array = [];
$this->form->fill();
$this->project = request('record');
if ($custom) {
$questions = [];
$customReport = CustomReport::findOrFail($custom)->toArray();
$array['description'] = $customReport['description'];
$array['customReport'] = $customReport['id'];
$customResponses = ReportTemplateResponse::where('custom_report_id', $customReport['id'])->get();
foreach ($customResponses as $customResponse) {
$questions["question_{$customResponse->report_template_question_id}"] = $customResponse->response;
}
$array['responses'] = $customResponses;
$array = array_merge($array, $questions);
$this->data = $array;
$this->form->fill($array);
}
}
public function getTitle(): string|Htmlable
{
return 'Relatório ' . $this->record->name;
}
public function form(Form $form): Form
{
$fields = [];
$questions = $this->record->questions;
$fields[] = TextInput::make('description')->label('Descrição')->required();
foreach ($questions as $question) {
$fields[] = $this->getField($question->type, $question->id, $question->name);
$form->{'question_' . $question->id} = '';
}
if($this->record->has_photography){
$fields[] = Repeater::make('photography')->schema([
FileUpload::make('photo')->label('Foto')->required(),
TextInput::make('legend')->label('Legenda')->required(),
])->label('Registro fotográfico');
}
return $form->schema([
Section::make()->schema($fields)
])->statePath('data')->fill($this->data);
}
public function getField($inputType, $id, $label)
{
return $inputType == ReportTypeButtonsEnum::Text ? $this->getInputFree("question_{$id}", $label) : $this->getButtons("question_{$id}", $label);
}
public function getInputFree($name, $label)
{
return TextInput::make($name)->label($label);
}
public function getButtons($name, $label)
{
return ToggleButtons::make($name)->label($label)->options([
'OK' => 'OK',
'NC' => 'NC',
'NCI' => 'NCI',
'NA' => 'NA',
])->inline();
}
public function submit()
{
$questions = [];
dd($this->data);
if (isset($this->data['customReport'])) {
$custom = CustomReport::find($this->data['customReport']);
$custom->update(['description' => $this->data['description']]);
foreach ($this->data as $key => $value) {
if (strpos($key, 'question_') === 0) {
[$_, $id] = explode('_', $key);
$response = ReportTemplateResponse::where('report_template_question_id', $id);
$response->update(['response' => $value]);
}
}
Notification::make('response-report-custom')->success()->title('Registrado com sucesso!')->send();
} else {
$custom = CustomReport::create([
'description' => $this->data['description'],
'project_id' => $this->project,
'project_artifact_id' => $this->artifact,
]);
unset($this->data['description']);
foreach ($this->data as $key => $response) {
[$_, $id] = explode('_', $key);
$questions[$id] = $response;
ReportTemplateResponse::create([
'custom_report_id' => $custom->id,
'project_id' => $this->project,
'report_template_id' => $this->record->id,
'report_template_question_id' => $id,
'response' => $response,
'user_id' => auth()->id(),
]);
}
Report::create([
'project_id' => $this->project,
'reportable_id' => $custom->id,
'reportable_type' => 'App\Models\CustomReport',
]);
Notification::make('report-template-success')->title('Registro salvo com sucesso!')->success()->send();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment