opnform-host-nginx/api/app/Http/Resources/FormResource.php

114 lines
3.6 KiB
PHP
Raw Normal View History

2022-09-20 21:59:52 +02:00
<?php
namespace App\Http\Resources;
use App\Http\Middleware\Form\ProtectedForm;
2022-09-20 21:59:52 +02:00
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;
class FormResource extends JsonResource
{
private array $cleanings = [];
2022-09-20 21:59:52 +02:00
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
Remove old code related to notifications (#363) * Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * Remove old code related to notifications * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Run linters --------- Co-authored-by: Forms Dev <chirag+new@notionforms.io> Co-authored-by: Julien Nahum <julien@nahum.net>
2024-03-28 18:46:29 +01:00
if (!$this->userIsFormOwner() && ProtectedForm::isProtected($request, $this->resource)) {
return $this->getProtectedForm();
2022-09-20 21:59:52 +02:00
}
$ownerData = $this->userIsFormOwner() ? [
'views_count' => $this->views_count,
'submissions_count' => $this->submissions_count,
2022-09-20 21:59:52 +02:00
'redirect_url' => $this->redirect_url,
'submissions_url' => $this->submissions_url,
2022-09-20 21:59:52 +02:00
'database_fields_update' => $this->database_fields_update,
'cleanings' => $this->getCleanigns(),
2022-09-20 21:59:52 +02:00
'can_be_indexed' => $this->can_be_indexed,
'password' => $this->password,
'tags' => $this->tags,
'visibility' => $this->visibility,
2022-11-01 14:46:31 +01:00
'removed_properties' => $this->removed_properties,
'last_edited_human' => $this->updated_at?->diffForHumans(),
2023-12-15 12:43:05 +01:00
'seo_meta' => $this->seo_meta,
2022-09-20 21:59:52 +02:00
] : [];
return array_merge(parent::toArray($request), $ownerData, [
'is_pro' => $this->workspaceIsPro(),
'is_trialing' => $this->workspaceIsTrialing(),
2022-09-20 21:59:52 +02:00
'workspace_id' => $this->workspace_id,
'workspace' => new WorkspaceResource($this->getWorkspace()),
2022-09-20 21:59:52 +02: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 21:59:52 +02:00
]);
}
public function setCleanings(array $cleanings)
{
$this->cleanings = $cleanings;
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
return $this;
}
private function getProtectedForm()
2022-09-20 21:59:52 +02:00
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
2022-09-20 21:59:52 +02: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 21:59:52 +02:00
'is_password_protected' => true,
'has_password' => $this->has_password,
'width' => 'centered',
2024-01-16 18:27:58 +01: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 21:59:52 +02:00
];
}
2024-02-23 11:54:12 +01:00
private function getWorkspace()
{
return $this->extra?->loadedWorkspace ?? $this->workspace;
}
2024-02-23 11:54:12 +01:00
private function workspaceIsPro()
{
return $this->extra?->workspaceIsPro ?? $this->getWorkspace()->is_pro ?? $this->is_pro;
}
private function workspaceIsTrialing()
{
return $this->getWorkspace()->is_trialing;
}
2024-02-23 11:54:12 +01: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 21:59:52 +02:00
}