2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
|
|
|
|
use App\Http\Middleware\Form\PasswordProtectedForm;
|
2022-09-21 12:59:32 +00:00
|
|
|
use App\Http\Resources\UserResource;
|
2022-09-20 19:59:52 +00:00
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class FormResource extends JsonResource
|
|
|
|
{
|
2022-09-21 12:59:32 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-09-21 12:59:32 +00:00
|
|
|
if(!$this->userIsFormOwner() && $this->doesMissPassword($request)){
|
2022-09-20 19:59:52 +00:00
|
|
|
return $this->getPasswordProtectedForm();
|
|
|
|
}
|
|
|
|
|
2022-09-21 12:59:32 +00:00
|
|
|
$ownerData = $this->userIsFormOwner() ? [
|
|
|
|
'creator' => new UserResource($this->creator),
|
2023-08-30 07:58:29 +00:00
|
|
|
'views_count' => $this->views_count,
|
|
|
|
'submissions_count' => $this->submissions_count,
|
2022-09-20 19:59:52 +00:00
|
|
|
'notifies' => $this->notifies,
|
2022-09-21 15:23:37 +00:00
|
|
|
'notifies_slack' => $this->notifies_slack,
|
2023-02-19 12:19:16 +00:00
|
|
|
'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,
|
2023-08-30 07:58:29 +00:00
|
|
|
'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,
|
2022-10-16 17:27:14 +00:00
|
|
|
'visibility' => $this->visibility,
|
2022-09-20 19:59:52 +00:00
|
|
|
'notification_emails' => $this->notification_emails,
|
2022-09-21 15:23:37 +00:00
|
|
|
'slack_webhook_url' => $this->slack_webhook_url,
|
2023-02-19 12:19:16 +00:00
|
|
|
'discord_webhook_url' => $this->discord_webhook_url,
|
2022-11-01 13:46:31 +00:00
|
|
|
'removed_properties' => $this->removed_properties,
|
|
|
|
'last_edited_human' => $this->updated_at?->diffForHumans()
|
2022-09-20 19:59:52 +00:00
|
|
|
] : [];
|
|
|
|
|
2022-09-21 12:59:32 +00:00
|
|
|
$baseData = $this->getFilteredFormData(parent::toArray($request), $this->userIsFormOwner());
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
return array_merge($baseData, $ownerData, [
|
2022-09-21 12:59:32 +00:00
|
|
|
'is_pro' => $this->workspaceIsPro(),
|
2022-09-20 19:59:52 +00:00
|
|
|
'workspace_id' => $this->workspace_id,
|
2022-09-21 12:59:32 +00:00
|
|
|
'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,
|
2022-11-16 10:20:28 +00:00
|
|
|
'max_number_of_submissions_reached' => $this->max_number_of_submissions_reached,
|
|
|
|
'form_pending_submission_key' => $this->form_pending_submission_key
|
2022-09-20 19:59:52 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter form data to hide properties from users.
|
|
|
|
* - For relation fields, hides the relation information
|
|
|
|
*/
|
|
|
|
private function getFilteredFormData(array $data, bool $userIsFormOwner)
|
|
|
|
{
|
|
|
|
if ($userIsFormOwner) return $data;
|
|
|
|
|
|
|
|
$properties = collect($data['properties'])->map(function($property){
|
|
|
|
// Remove database details from relation
|
|
|
|
if ($property['type'] === 'relation') {
|
|
|
|
if (isset($property['relation'])) {
|
|
|
|
unset($property['relation']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $property;
|
|
|
|
});
|
|
|
|
|
|
|
|
$data['properties'] = $properties->toArray();
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCleanings(array $cleanings)
|
|
|
|
{
|
|
|
|
$this->cleanings = $cleanings;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doesMissPassword(Request $request)
|
|
|
|
{
|
2023-08-30 07:58:29 +00:00
|
|
|
if (!$this->has_password) return false;
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
return !PasswordProtectedForm::hasCorrectPassword($request, $this->resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPasswordProtectedForm()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'title' => $this->title,
|
|
|
|
'slug' => $this->slug,
|
|
|
|
'custom_code' => $this->custom_code,
|
|
|
|
'dark_mode' => $this->dark_mode,
|
|
|
|
'transparent_background' => $this->transparent_background,
|
|
|
|
'color' => $this->color,
|
|
|
|
'is_password_protected' => true,
|
|
|
|
'has_password' => $this->has_password,
|
|
|
|
'width' => 'centered',
|
|
|
|
'properties' => []
|
|
|
|
];
|
|
|
|
}
|
2022-09-21 12:59:32 +00:00
|
|
|
|
|
|
|
private function getWorkspace() {
|
|
|
|
return $this->extra?->loadedWorkspace ?? $this->workspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function workspaceIsPro() {
|
|
|
|
return $this->extra?->workspaceIsPro ?? $this->getWorkspace()->is_pro ?? $this->is_pro;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function userIsFormOwner() {
|
|
|
|
return $this->extra?->userIsOwner ??
|
|
|
|
(
|
|
|
|
Auth::check()
|
|
|
|
&& Auth::user()->workspaces()->find($this->workspace_id) !== null
|
|
|
|
);
|
|
|
|
}
|
2023-08-30 07:58:29 +00:00
|
|
|
|
|
|
|
private function getCleanigns()
|
|
|
|
{
|
|
|
|
return $this->extra?->cleanings ?? $this->cleanings;
|
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|