Initial commit
This commit is contained in:
110
app/Http/Resources/FormResource.php
Normal file
110
app/Http/Resources/FormResource.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Http\Middleware\Form\PasswordProtectedForm;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class FormResource extends JsonResource
|
||||
{
|
||||
private Array $cleanings = [];
|
||||
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$userIsFormOwner = Auth::check() && Auth::user()->workspaces()->find($this->workspace_id) !== null;
|
||||
if(!$userIsFormOwner && $this->doesMissPassword($request)){
|
||||
return $this->getPasswordProtectedForm();
|
||||
}
|
||||
|
||||
$ownerData = $userIsFormOwner ? [
|
||||
'creator' => $this->creator,
|
||||
'views_count' => $this->when($this->workspace->is_pro, $this->views_count),
|
||||
'submissions_count' => $this->when($this->workspace->is_pro, $this->submissions_count),
|
||||
'notifies' => $this->notifies,
|
||||
'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->cleanings,
|
||||
'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,
|
||||
'notification_emails' => $this->notification_emails,
|
||||
] : [];
|
||||
|
||||
$baseData = $this->getFilteredFormData(parent::toArray($request), $userIsFormOwner);
|
||||
|
||||
return array_merge($baseData, $ownerData, [
|
||||
'workspace_id' => $this->workspace_id,
|
||||
'workspace' => new WorkspaceResource($this->workspace),
|
||||
'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
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if (!$this->is_pro || !$this->has_password) return false;
|
||||
|
||||
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' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
58
app/Http/Resources/FormSubmissionResource.php
Normal file
58
app/Http/Resources/FormSubmissionResource.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class FormSubmissionResource extends JsonResource
|
||||
{
|
||||
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$this->generateFileLinks();
|
||||
$this->addTimestamp();
|
||||
|
||||
return [
|
||||
'data' => $this->data,
|
||||
'form_id' => $this->form_id,
|
||||
'id' => $this->id,
|
||||
];
|
||||
}
|
||||
|
||||
private function addTimestamp()
|
||||
{
|
||||
$this->data = array_merge($this->data, [
|
||||
"created_at" => $this->created_at->toDateTimeString()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Link to the file (generating signed s3 URL)
|
||||
* @return void
|
||||
*/
|
||||
private function generateFileLinks()
|
||||
{
|
||||
$data = $this->data;
|
||||
$formFields = collect($this->form->properties)->concat(collect($this->form->removed_properties));
|
||||
$fileFields = $formFields->filter(function ($field) {
|
||||
return $field['type'] == 'files';
|
||||
});
|
||||
foreach ($fileFields as $field) {
|
||||
if (isset($data[$field['id']]) && !empty($data[$field['id']])) {
|
||||
$data[$field['id']] = collect($data[$field['id']])->map(function ($file) {
|
||||
return [
|
||||
'file_url' => route('open.forms.submissions.file', [$this->form_id, $file]),
|
||||
'file_name' => $file,
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
||||
24
app/Http/Resources/WorkspaceResource.php
Normal file
24
app/Http/Resources/WorkspaceResource.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class WorkspaceResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'is_enterprise' => $this->is_enterprise,
|
||||
'is_pro' => $this->is_pro,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user