opnform/app/Http/Resources/FormResource.php

121 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Http\Resources;
use App\Http\Middleware\Form\ProtectedForm;
2022-09-20 19:59:52 +00:00
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;
class FormResource extends JsonResource
{
private array $cleanings = [];
2022-09-20 19:59:52 +00:00
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
2024-02-23 10:54:12 +00:00
if (! $this->userIsFormOwner() && ProtectedForm::isProtected($request, $this->resource)) {
return $this->getProtectedForm();
2022-09-20 19:59:52 +00:00
}
$ownerData = $this->userIsFormOwner() ? [
'views_count' => $this->views_count,
'submissions_count' => $this->submissions_count,
2022-09-20 19:59:52 +00:00
'notifies' => $this->notifies,
'notifies_webhook' => $this->notifies_webhook,
'notifies_slack' => $this->notifies_slack,
'notifies_discord' => $this->notifies_discord,
2022-09-20 19:59:52 +00:00
'send_submission_confirmation' => $this->send_submission_confirmation,
'webhook_url' => $this->webhook_url,
'redirect_url' => $this->redirect_url,
'database_fields_update' => $this->database_fields_update,
'cleanings' => $this->getCleanigns(),
2022-09-20 19:59:52 +00:00
'notification_sender' => $this->notification_sender,
'notification_subject' => $this->notification_subject,
'notification_body' => $this->notification_body,
'notifications_include_submission' => $this->notifications_include_submission,
'can_be_indexed' => $this->can_be_indexed,
'password' => $this->password,
'tags' => $this->tags,
'visibility' => $this->visibility,
2022-09-20 19:59:52 +00:00
'notification_emails' => $this->notification_emails,
'slack_webhook_url' => $this->slack_webhook_url,
'discord_webhook_url' => $this->discord_webhook_url,
'notification_settings' => $this->notification_settings,
2022-11-01 13:46:31 +00:00
'removed_properties' => $this->removed_properties,
'last_edited_human' => $this->updated_at?->diffForHumans(),
2023-12-15 11:43:05 +00:00
'seo_meta' => $this->seo_meta,
2022-09-20 19:59:52 +00:00
] : [];
return array_merge(parent::toArray($request), $ownerData, [
'is_pro' => $this->workspaceIsPro(),
2022-09-20 19:59:52 +00:00
'workspace_id' => $this->workspace_id,
'workspace' => new WorkspaceResource($this->getWorkspace()),
2022-09-20 19:59:52 +00:00
'is_closed' => $this->is_closed,
'is_password_protected' => false,
'has_password' => $this->has_password,
'max_number_of_submissions_reached' => $this->max_number_of_submissions_reached,
'form_pending_submission_key' => $this->form_pending_submission_key,
'max_file_size' => $this->max_file_size / 1000000,
2022-09-20 19:59:52 +00:00
]);
}
public function setCleanings(array $cleanings)
{
$this->cleanings = $cleanings;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this;
}
private function getProtectedForm()
2022-09-20 19:59:52 +00:00
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
2022-09-20 19:59:52 +00:00
'slug' => $this->slug,
'custom_code' => $this->custom_code,
'dark_mode' => $this->dark_mode,
'transparent_background' => $this->transparent_background,
'color' => $this->color,
'theme' => $this->theme,
2022-09-20 19:59:52 +00:00
'is_password_protected' => true,
'has_password' => $this->has_password,
'width' => 'centered',
2024-01-16 17:27:58 +00:00
'no_branding' => $this->no_branding,
'properties' => [],
'logo_picture' => $this->logo_picture,
'seo_meta' => $this->seo_meta,
'cover_picture' => $this->cover_picture,
2022-09-20 19:59:52 +00:00
];
}
2024-02-23 10:54:12 +00:00
private function getWorkspace()
{
return $this->extra?->loadedWorkspace ?? $this->workspace;
}
2024-02-23 10:54:12 +00:00
private function workspaceIsPro()
{
return $this->extra?->workspaceIsPro ?? $this->getWorkspace()->is_pro ?? $this->is_pro;
}
2024-02-23 10:54:12 +00:00
private function userIsFormOwner()
{
return $this->extra?->userIsOwner ??
(
Auth::check() && Auth::user()->ownsForm($this->resource)
);
}
private function getCleanigns()
{
return $this->extra?->cleanings ?? $this->cleanings;
}
2022-09-20 19:59:52 +00:00
}