opnform/app/Http/Requests/Templates/FormTemplateRequest.php

96 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Requests\Templates;
use App\Models\Template;
use Illuminate\Foundation\Http\FormRequest;
class FormTemplateRequest extends FormRequest
{
2024-02-23 10:54:12 +00:00
public const IGNORED_KEYS = [
2022-12-01 12:13:32 +00:00
'id',
'creator',
'cleanings',
'closes_at',
'deleted_at',
'updated_at',
'form_pending_submission_key',
'is_closed',
'is_pro',
'is_password_protected',
'last_edited_human',
'max_number_of_submissions_reached',
'notifies',
'notification_body',
'notification_emails',
'notification_sender',
'notification_subject',
'notifications_include_submission',
'notifies_slack',
'slack_webhook_url',
'removed_properties',
'creator_id',
'extra',
'workspace',
'workspace_id',
'submissions',
'submissions_count',
'views',
'views_count',
'visibility',
'webhook_url',
];
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
$slugRule = '';
2024-02-23 10:54:12 +00:00
if ($this->id) {
$slugRule = ','.$this->id;
}
2024-02-23 10:54:12 +00:00
return [
'form' => 'required|array',
'publicly_listed' => 'boolean',
'name' => 'required|string|max:60',
'slug' => 'required|string|alpha_dash|unique:templates,slug'.$slugRule,
'short_description' => 'required|string|max:1000',
2023-09-08 13:30:57 +00:00
'description' => 'required|string',
'image_url' => 'required|string',
'types' => 'nullable|array',
'industries' => 'nullable|array',
'related_templates' => 'nullable|array',
'questions' => 'array',
];
}
2022-12-01 12:13:32 +00:00
public function getTemplate(): Template
{
$structure = $this->form;
2022-12-01 12:13:32 +00:00
foreach ($structure as $key => $val) {
if (in_array($key, self::IGNORED_KEYS)) {
unset($structure[$key]);
}
}
2023-09-08 13:30:57 +00:00
return new Template([
'creator_id' => $this->user()?->id ?? null,
'publicly_listed' => $this->publicly_listed,
'name' => $this->name,
'slug' => $this->slug,
'short_description' => $this->short_description,
'description' => $this->description,
'image_url' => $this->image_url,
'structure' => $structure,
'types' => $this->types ?? [],
'industries' => $this->industries ?? [],
'related_templates' => $this->related_templates ?? [],
2024-02-23 10:54:12 +00:00
'questions' => $this->questions ?? [],
]);
}
}