SmartCane/laravel_app/app/Livewire/Projects/ProjectManager.php
Martin Folkerts 62b9942a53 wip
2024-03-12 12:09:29 +01:00

260 lines
8.1 KiB
PHP

<?php
namespace App\Livewire\Projects;
use App\Models\Project;
use App\Models\ProjectBoundingBox;
use App\Models\ProjectEmailRecipient;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;
class ProjectManager extends Component
{
use WithFileUploads;
public $formData = [];
public $processOutput = '';
public $confirmingReportDeletion = false;
public $showProjectModal = false;
public $showMailSettingsModal = false;
public $projectIdBeingDeleted;
public $span_json_path;
public $pivot_json_path;
/**
* Mount the component.
*
* @return void
*/
public function mount()
{
$this->resetFormData();
}
public function editProject(Project $project)
{
$this->showProjectModal = true;
$this->loadFormData($project);
}
public function editMailSettings(Project $project)
{
$this->showMailSettingsModal = true;
$this->loadFormData($project);
}
private function loadFormData(Project $project)
{
$this->formData = $project->toArray();
$this->pivot_json_path = $this->formData['pivot_json_path'];
$this->formData['pivot_json_path'] = null;
$this->span_json_path = $this->formData['span_json_path'];
$this->formData['span_json_path'] = null;
$this->formData['boundingBoxes'] = $project->boundingBoxes->toArray();
$this->formData['mail_recipients'] = $project->emailRecipients->toArray() ?: [
[
'name' => '',
'email' => '',
]
];
}
public function openCreateProjectModal()
{
$this->resetFormData();
$this->showProjectModal = true;
}
public function saveProject()
{
$this->resetErrorBag();
$this->validateForm();
Project::saveWithFormData($this->formData);
$this->resetFormData();
$this->showProjectModal = false;
$this->dispatch('saved');
}
public function saveMailSettings()
{
$this->resetErrorBag();
$this->validateEmailSettingsForm();
Project::saveWithFormData($this->formData);
$this->resetFormData();
$this->showMailSettingsModal = false;
$this->dispatch('saved');
}
public function addBoundingBox()
{
$this->formData['boundingBoxes'][] =
[
'name' => '',
'top_left_latitude' => '',
'top_left_longitude' => '',
'bottom_right_latitude' => '',
'bottom_right_longitude' => '',
];
}
public function addEmailRecipient()
{
$this->formData['mail_recipients'][] =
[
'name' => '',
'email' => '',
];
}
public function deleteBoundingBox($index)
{
if (array_key_exists('id', $this->formData['boundingBoxes'][$index])) {
ProjectBoundingBox::whereId($this->formData['boundingBoxes'][$index]['id'])->delete();
}
unset($this->formData['boundingBoxes'][$index]);
$this->formData['boundingBoxes'] = array_values($this->formData['boundingBoxes']);
$this->dispatch('saved');
}
public function deleteEmailRecipient($index)
{
if (array_key_exists('id', $this->formData['mail_recipients'][$index])) {
ProjectEmailRecipient::whereId($this->formData['mail_recipients'][$index]['id'])->delete();
}
unset($this->formData['mail_recipients'][$index]);
$this->formData['mail_recipients'] = array_values($this->formData['mail_recipients']);
$this->dispatch('saved');
}
/**
* Confirm that the given Project should be deleted.
*
* @param int $reportId
* @return void
*/
public function confirmReportDeletion($reportId)
{
$this->confirmingReportDeletion = true;
$this->reportIdBeingDeleted = $reportId;
}
/**
* Delete the project
*
* @return void
*/
public function deleteProject()
{
Project::whereId($this->reportIdBeingDeleted)->first()->delete();
$this->resetFormData();
$this->confirmingReportDeletion = false;
}
/**
* Get the current user of the application.
*
* @return mixed
*/
public function getProjectsProperty()
{
return Project::all();
}
public function render()
{
return view('livewire.project-manager');
}
private function resetFormData()
{
$this->formData = [
'name' => '',
'pivot_json_path' => '',
'span_json_path' => '',
'boundingBoxes' => [],
'mail_subject' => '',
'mail_template' => '',
'mail_frequency' => '',
'mail_day' => '',
'mail_recipients' => [],
];
$this->addBoundingBox();
$this->addEmailRecipient();
}
private function validateForm()
{
$projectIdentifier = $this->formData['id'] ?? null;
Validator::make([
'name' => $this->formData['name'],
'pivot_json_path' => $this->formData['pivot_json_path'], // TODO: 'required|file|mimes:csv,txt|max:2048
'span_json_path' => $this->formData['span_json_path'],
'boundingBoxes' => $this->formData['boundingBoxes'],
], [
'name' => [
'required',
Rule::unique('projects')->ignore($projectIdentifier),
'string',
'max:255'
],
'pivot_json_path' => [
function ($attribute, $value, $fail) {
if ($value && !is_file($value->getRealPath())) {
$fail($attribute.' is geen geldig bestand.');
} elseif ($value && !in_array(mime_content_type($value->getRealPath()), ['application/json'])) {
// $fail($attribute.' moet een JSON-bestand zijn.');
}
},
],
'span_json_path' => [
function ($attribute, $value, $fail) {
if ($value && !is_file($value->getRealPath())) {
$fail($attribute.' is geen geldig bestand.');
} elseif ($value && !in_array(mime_content_type($value->getRealPath()), ['application/json'])) {
// $fail($attribute.' moet een JSON-bestand zijn.');
}
},
],
'boundingBoxes' => ['required', 'array', 'min:1'],
'boundingBoxes.*.name' => ['required', 'string', 'max:255'],
'boundingBoxes.*.top_left_latitude' => ['required', 'string'],
'boundingBoxes.*.top_left_longitude' => ['required', 'string'],
'boundingBoxes.*.bottom_right_latitude' => ['required', 'string'],
'boundingBoxes.*.bottom_right_longitude' => ['required', 'string'],
])->validateWithBag('saveProject');
}
private function validateEmailSettingsForm()
{
Validator::make([
'mail_template' => $this->formData['mail_template'],
'mail_subject' => $this->formData['mail_subject'],
'mail_frequency' => $this->formData['mail_frequency'],
'mail_day' => $this->formData['mail_day'],
'mail_recipients' => $this->formData['mail_recipients'],
], [
'mail_template' => ['required', 'string',],
'mail_subject' => ['required', 'string',],
'mail_frequency' => ['required', 'string',],
'mail_day' => ['required', 'string',],
'mail_recipients' => ['required', 'array', 'min:1'],
'mail_recipients.*.name' => ['required', 'string', 'max:255'],
'mail_recipients.*.email' => ['required', 'email'],
])->validateWithBag('saveEmailSettingsForm');
}
}