Separated laravel app to its own folder (#540)
This commit is contained in:
34
api/app/Http/Controllers/Forms/AiFormController.php
Normal file
34
api/app/Http/Controllers/Forms/AiFormController.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AiGenerateFormRequest;
|
||||
use App\Models\Forms\AI\AiFormCompletion;
|
||||
|
||||
class AiFormController extends Controller
|
||||
{
|
||||
public function generateForm(AiGenerateFormRequest $request)
|
||||
{
|
||||
$this->middleware('throttle:4,1');
|
||||
|
||||
return $this->success([
|
||||
'message' => 'We\'re working on your form, please wait ~1 min.',
|
||||
'ai_form_completion_id' => AiFormCompletion::create([
|
||||
'form_prompt' => $request->input('form_prompt'),
|
||||
'ip' => $request->ip(),
|
||||
])->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(AiFormCompletion $aiFormCompletion)
|
||||
{
|
||||
if ($aiFormCompletion->ip != request()->ip()) {
|
||||
return $this->error('You are not authorized to view this AI completion.', 403);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'ai_form_completion' => $aiFormCompletion,
|
||||
]);
|
||||
}
|
||||
}
|
||||
274
api/app/Http/Controllers/Forms/FormController.php
Normal file
274
api/app/Http/Controllers/Forms/FormController.php
Normal file
@@ -0,0 +1,274 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StoreFormRequest;
|
||||
use App\Http\Requests\UpdateFormRequest;
|
||||
use App\Http\Requests\UploadAssetRequest;
|
||||
use App\Http\Resources\FormResource;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Workspace;
|
||||
use App\Service\Forms\FormCleaner;
|
||||
use App\Service\Storage\StorageFileNameParser;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class FormController extends Controller
|
||||
{
|
||||
public const ASSETS_UPLOAD_PATH = 'assets/forms';
|
||||
|
||||
private FormCleaner $formCleaner;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth', ['except' => ['uploadAsset']]);
|
||||
$this->formCleaner = new FormCleaner();
|
||||
}
|
||||
|
||||
public function index($workspaceId)
|
||||
{
|
||||
$workspace = Workspace::findOrFail($workspaceId);
|
||||
$this->authorize('view', $workspace);
|
||||
$this->authorize('viewAny', Form::class);
|
||||
|
||||
$workspaceIsPro = $workspace->is_pro;
|
||||
$forms = $workspace->forms()
|
||||
->orderByDesc('updated_at')
|
||||
->paginate(10)->through(function (Form $form) use ($workspace, $workspaceIsPro) {
|
||||
|
||||
// Add attributes for faster loading
|
||||
$form->extra = (object) [
|
||||
'loadedWorkspace' => $workspace,
|
||||
'workspaceIsPro' => $workspaceIsPro,
|
||||
'userIsOwner' => true,
|
||||
'cleanings' => $this->formCleaner
|
||||
->processForm(request(), $form)
|
||||
->simulateCleaning($workspace)
|
||||
->getPerformedCleanings(),
|
||||
];
|
||||
|
||||
return $form;
|
||||
});
|
||||
|
||||
return FormResource::collection($forms);
|
||||
}
|
||||
|
||||
public function show($slug)
|
||||
{
|
||||
$form = Form::whereSlug($slug)->firstOrFail();
|
||||
$this->authorize('view', $form);
|
||||
|
||||
// Add attributes for faster loading
|
||||
$workspace = $form->workspace;
|
||||
$form->extra = (object)[
|
||||
'loadedWorkspace' => $workspace,
|
||||
'workspaceIsPro' => $workspace->is_pro,
|
||||
'userIsOwner' => true,
|
||||
'cleanings' => $this->formCleaner
|
||||
->processForm(request(), $form)
|
||||
->simulateCleaning($workspace)
|
||||
->getPerformedCleanings(),
|
||||
];
|
||||
|
||||
return new FormResource($form);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all user forms, used for zapier
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function indexAll()
|
||||
{
|
||||
$forms = collect();
|
||||
foreach (Auth::user()->workspaces as $workspace) {
|
||||
$this->authorize('view', $workspace);
|
||||
$this->authorize('viewAny', Form::class);
|
||||
|
||||
$workspaceIsPro = $workspace->is_pro;
|
||||
$newForms = $workspace->forms()->get()->map(function (Form $form) use ($workspace, $workspaceIsPro) {
|
||||
// Add attributes for faster loading
|
||||
$form->extra = (object) [
|
||||
'loadedWorkspace' => $workspace,
|
||||
'workspaceIsPro' => $workspaceIsPro,
|
||||
'userIsOwner' => true,
|
||||
];
|
||||
|
||||
return $form;
|
||||
});
|
||||
|
||||
$forms = $forms->merge($newForms);
|
||||
}
|
||||
|
||||
return FormResource::collection($forms);
|
||||
}
|
||||
|
||||
public function store(StoreFormRequest $request)
|
||||
{
|
||||
$this->authorize('create', Form::class);
|
||||
|
||||
$workspace = Workspace::findOrFail($request->get('workspace_id'));
|
||||
$this->authorize('view', $workspace);
|
||||
|
||||
$formData = $this->formCleaner
|
||||
->processRequest($request)
|
||||
->simulateCleaning($workspace)
|
||||
->getData();
|
||||
|
||||
$form = Form::create(array_merge($formData, [
|
||||
'creator_id' => $request->user()->id,
|
||||
]));
|
||||
|
||||
if ($this->formCleaner->hasCleaned()) {
|
||||
$formStatus = $form->workspace->is_trialing ? 'Non-trial' : 'Pro';
|
||||
$message = 'Form successfully created, but the ' . $formStatus . ' features you used will be disabled when sharing your form:';
|
||||
} else {
|
||||
$message = 'Form created.';
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'message' => $message . ($form->visibility == 'draft' ? ' But other people won\'t be able to see the form since it\'s currently in draft mode' : ''),
|
||||
'form' => (new FormResource($form))->setCleanings($this->formCleaner->getPerformedCleanings()),
|
||||
'users_first_form' => $request->user()->forms()->count() == 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(UpdateFormRequest $request, string $id)
|
||||
{
|
||||
$form = Form::findOrFail($id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
$formData = $this->formCleaner
|
||||
->processRequest($request)
|
||||
->simulateCleaning($form->workspace)
|
||||
->getData();
|
||||
|
||||
// Set Removed Properties
|
||||
$formData['removed_properties'] = array_merge($form->removed_properties, collect($form->properties)->filter(function ($field) use ($formData) {
|
||||
return !Str::of($field['type'])->startsWith('nf-') && !in_array($field['id'], collect($formData['properties'])->pluck('id')->toArray());
|
||||
})->toArray());
|
||||
|
||||
$form->update($formData);
|
||||
|
||||
if ($this->formCleaner->hasCleaned()) {
|
||||
$formSubscription = $form->is_pro ? 'Enterprise' : 'Pro';
|
||||
$formStatus = $form->workspace->is_trialing ? 'Non-trial' : $formSubscription;
|
||||
$message = 'Form successfully updated, but the ' . $formStatus . ' features you used will be disabled when sharing your form.';
|
||||
} else {
|
||||
$message = 'Form updated.';
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'message' => $message . ($form->visibility == 'draft' ? ' But other people won\'t be able to see the form since it\'s currently in draft mode' : ''),
|
||||
'form' => (new FormResource($form))->setCleanings($this->formCleaner->getPerformedCleanings()),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$form = Form::findOrFail($id);
|
||||
$this->authorize('delete', $form);
|
||||
|
||||
$form->delete();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form was deleted.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function duplicate($id)
|
||||
{
|
||||
$form = Form::findOrFail($id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
// Create copy
|
||||
$formCopy = $form->replicate();
|
||||
$formCopy->title = 'Copy of ' . $formCopy->title;
|
||||
$formCopy->save();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form successfully duplicated. You are now editing the duplicated version of the form.',
|
||||
'new_form' => new FormResource($formCopy),
|
||||
]);
|
||||
}
|
||||
|
||||
public function regenerateLink($id, $option)
|
||||
{
|
||||
$form = Form::findOrFail($id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
if ($option == 'slug') {
|
||||
$form->generateSlug();
|
||||
} elseif ($option == 'uuid') {
|
||||
$form->slug = Str::uuid();
|
||||
}
|
||||
$form->save();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form url successfully updated. Your new form url now is: ' . $form->share_url . '.',
|
||||
'form' => new FormResource($form),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a form asset
|
||||
*/
|
||||
public function uploadAsset(UploadAssetRequest $request)
|
||||
{
|
||||
$fileNameParser = StorageFileNameParser::parse($request->url);
|
||||
|
||||
// Make sure we retrieve the file in tmp storage, move it to persistent
|
||||
$fileName = PublicFormController::TMP_FILE_UPLOAD_PATH . '/' . $fileNameParser->uuid;
|
||||
if (!Storage::exists($fileName)) {
|
||||
// File not found, we skip
|
||||
return null;
|
||||
}
|
||||
$newPath = self::ASSETS_UPLOAD_PATH . '/' . $fileNameParser->getMovedFileName();
|
||||
Storage::move($fileName, $newPath);
|
||||
|
||||
return $this->success([
|
||||
'message' => 'File uploaded.',
|
||||
'url' => route('forms.assets.show', [$fileNameParser->getMovedFileName()]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* File uploads retrieval
|
||||
*/
|
||||
public function viewFile($id, $fileName)
|
||||
{
|
||||
$form = Form::findOrFail($id);
|
||||
$this->authorize('view', $form);
|
||||
|
||||
$path = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $form->id) . '/' . $fileName;
|
||||
if (!Storage::exists($path)) {
|
||||
return $this->error([
|
||||
'message' => 'File not found.',
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->to(Storage::temporaryUrl($path, now()->addMinutes(5)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a form's workspace
|
||||
*/
|
||||
public function updateWorkspace($id, $workspace_id)
|
||||
{
|
||||
$form = Form::findOrFail($id);
|
||||
$workspace = Workspace::findOrFail($workspace_id);
|
||||
|
||||
$this->authorize('update', $form);
|
||||
$this->authorize('view', $workspace);
|
||||
|
||||
$form->workspace_id = $workspace_id;
|
||||
$form->creator_id = auth()->user()->id;
|
||||
$form->save();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form workspace updated successfully.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
38
api/app/Http/Controllers/Forms/FormStatsController.php
Normal file
38
api/app/Http/Controllers/Forms/FormStatsController.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Forms\Form;
|
||||
use Carbon\CarbonPeriod;
|
||||
|
||||
class FormStatsController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function getFormStats(string $workspaceId, string $formId)
|
||||
{
|
||||
$form = Form::findOrFail($formId);
|
||||
|
||||
$this->authorize('view', $form);
|
||||
|
||||
$formStats = $form->statistics()->where('date', '>', now()->subDays(29)->startOfDay())->get();
|
||||
$periodStats = ['views' => [], 'submissions' => []];
|
||||
foreach (CarbonPeriod::create(now()->subDays(29), now()) as $dateObj) {
|
||||
$date = $dateObj->format('d-m-Y');
|
||||
|
||||
$statisticData = $formStats->where('date', $dateObj->format('Y-m-d'))->first();
|
||||
$periodStats['views'][$date] = $statisticData->data['views'] ?? 0;
|
||||
$periodStats['submissions'][$date] = $form->submissions()->whereDate('created_at', $dateObj)->count();
|
||||
|
||||
if ($dateObj->toDateString() === now()->toDateString()) {
|
||||
$periodStats['views'][$date] += $form->views()->count();
|
||||
}
|
||||
}
|
||||
|
||||
return $periodStats;
|
||||
}
|
||||
}
|
||||
96
api/app/Http/Controllers/Forms/FormSubmissionController.php
Normal file
96
api/app/Http/Controllers/Forms/FormSubmissionController.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Exports\FormSubmissionExport;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AnswerFormRequest;
|
||||
use App\Http\Resources\FormSubmissionResource;
|
||||
use App\Jobs\Form\StoreFormSubmissionJob;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Forms\FormSubmission;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class FormSubmissionController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth', ['except' => ['submissionFile']]);
|
||||
$this->middleware('signed', ['only' => ['submissionFile']]);
|
||||
}
|
||||
|
||||
public function submissions(string $id)
|
||||
{
|
||||
$form = Form::findOrFail((int) $id);
|
||||
$this->authorize('view', $form);
|
||||
|
||||
return FormSubmissionResource::collection($form->submissions()->paginate(100));
|
||||
}
|
||||
|
||||
public function update(AnswerFormRequest $request, $id, $submissionId)
|
||||
{
|
||||
$form = $request->form;
|
||||
$this->authorize('update', $form);
|
||||
$job = new StoreFormSubmissionJob($request->form, $request->validated());
|
||||
$job->setSubmissionId($submissionId)->handle();
|
||||
|
||||
$data = new FormSubmissionResource(FormSubmission::findOrFail($submissionId));
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Record successfully updated.',
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export(string $id)
|
||||
{
|
||||
$form = Form::findOrFail((int) $id);
|
||||
$this->authorize('view', $form);
|
||||
|
||||
$allRows = [];
|
||||
foreach ($form->submissions->toArray() as $row) {
|
||||
$formatter = (new FormSubmissionFormatter($form, $row['data']))
|
||||
->outputStringsOnly()
|
||||
->setEmptyForNoValue()
|
||||
->showRemovedFields()
|
||||
->showHiddenFields()
|
||||
->useSignedUrlForFiles();
|
||||
$allRows[] = [
|
||||
'id' => Hashids::encode($row['id']),
|
||||
'created_at' => date('Y-m-d H:i', strtotime($row['created_at'])),
|
||||
...$formatter->getCleanKeyValue(),
|
||||
];
|
||||
}
|
||||
$csvExport = (new FormSubmissionExport($allRows));
|
||||
|
||||
return Excel::download(
|
||||
$csvExport,
|
||||
$form->slug.'-submission-data.csv',
|
||||
\Maatwebsite\Excel\Excel::CSV
|
||||
);
|
||||
}
|
||||
|
||||
public function submissionFile($id, $fileName)
|
||||
{
|
||||
$fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id).'/'
|
||||
.urldecode($fileName);
|
||||
|
||||
if (! Storage::exists($fileName)) {
|
||||
return $this->error([
|
||||
'message' => 'File not found.',
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (config('filesystems.default') !== 's3') {
|
||||
return response()->file(Storage::path($fileName));
|
||||
}
|
||||
|
||||
return redirect(
|
||||
Storage::temporaryUrl($fileName, now()->addMinute())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms\Integration;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Integration\FormIntegrationsRequest;
|
||||
use App\Http\Resources\FormIntegrationResource;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
|
||||
class FormIntegrationsController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function index(string $id)
|
||||
{
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('view', $form);
|
||||
|
||||
$integrations = FormIntegration::query()
|
||||
->where('form_id', $form->id)
|
||||
->with('provider.user')
|
||||
->get();
|
||||
|
||||
return FormIntegrationResource::collection($integrations);
|
||||
}
|
||||
|
||||
public function create(FormIntegrationsRequest $request, string $id)
|
||||
{
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
/** @var FormIntegration $formIntegration */
|
||||
$formIntegration = FormIntegration::create(
|
||||
array_merge([
|
||||
'form_id' => $form->id,
|
||||
], $request->toIntegrationData())
|
||||
);
|
||||
|
||||
$formIntegration->refresh();
|
||||
$formIntegration->load('provider.user');
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form Integration was created.',
|
||||
'form_integration' => FormIntegrationResource::make($formIntegration)
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(FormIntegrationsRequest $request, string $id, string $integrationid)
|
||||
{
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
$formIntegration = FormIntegration::findOrFail((int)$integrationid);
|
||||
$formIntegration->update($request->toIntegrationData());
|
||||
$formIntegration->load('provider.user');
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form Integration was updated.',
|
||||
'form_integration' => FormIntegrationResource::make($formIntegration)
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(string $id, string $integrationid)
|
||||
{
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
$formIntegration = FormIntegration::findOrFail((int)$integrationid);
|
||||
$formIntegration->delete();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form Integration was deleted.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms\Integration;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\FormIntegrationsEventResource;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegrationsEvent;
|
||||
|
||||
class FormIntegrationsEventController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function index(string $id, string $integrationid)
|
||||
{
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('view', $form);
|
||||
|
||||
return FormIntegrationsEventResource::collection(
|
||||
FormIntegrationsEvent::where('integration_id', (int)$integrationid)->orderByDesc('created_at')->get()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms\Integration;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Integration\StoreFormZapierWebhookRequest;
|
||||
use App\Models\Integration\FormZapierWebhook;
|
||||
|
||||
class FormZapierWebhookController extends Controller
|
||||
{
|
||||
/**
|
||||
* Controller for Zappier webhook subscriptions.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function store(StoreFormZapierWebhookRequest $request)
|
||||
{
|
||||
$hook = $request->instanciateHook();
|
||||
$this->authorize('store', $hook);
|
||||
|
||||
$hook->save();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Webhook created.',
|
||||
'hook' => $hook,
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$hook = FormZapierWebhook::findOrFail($id);
|
||||
$this->authorize('store', $hook);
|
||||
|
||||
$hook->delete();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Webhook deleted.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
133
api/app/Http/Controllers/Forms/PublicFormController.php
Normal file
133
api/app/Http/Controllers/Forms/PublicFormController.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AnswerFormRequest;
|
||||
use App\Http\Resources\FormResource;
|
||||
use App\Http\Resources\FormSubmissionResource;
|
||||
use App\Jobs\Form\StoreFormSubmissionJob;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Forms\FormSubmission;
|
||||
use App\Service\Forms\FormCleaner;
|
||||
use App\Service\WorkspaceHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class PublicFormController extends Controller
|
||||
{
|
||||
public const FILE_UPLOAD_PATH = 'forms/?/submissions';
|
||||
|
||||
public const TMP_FILE_UPLOAD_PATH = 'tmp/';
|
||||
|
||||
public function show(Request $request, string $slug)
|
||||
{
|
||||
$form = Form::whereSlug($slug)->whereIn('visibility', ['public', 'closed'])->firstOrFail();
|
||||
if ($form->workspace == null) {
|
||||
// Workspace deleted
|
||||
return $this->error([
|
||||
'message' => 'Form not found.',
|
||||
], 404);
|
||||
}
|
||||
|
||||
$formCleaner = new FormCleaner();
|
||||
|
||||
// Disable pro features if needed
|
||||
$form->fill(
|
||||
$formCleaner
|
||||
->processForm($request, $form)
|
||||
->performCleaning($form->workspace)
|
||||
->getData()
|
||||
);
|
||||
|
||||
// Increase form view counter if not login
|
||||
if (!Auth::check()) {
|
||||
$form->views()->create();
|
||||
}
|
||||
|
||||
return (new FormResource($form))
|
||||
->setCleanings($formCleaner->getPerformedCleanings());
|
||||
}
|
||||
|
||||
public function listUsers(Request $request)
|
||||
{
|
||||
// Check that form has user field
|
||||
$form = $request->form;
|
||||
if (!$form->has_user_field) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Use serializer
|
||||
$workspace = $form->workspace;
|
||||
|
||||
return (new WorkspaceHelper($workspace))->getAllUsers();
|
||||
}
|
||||
|
||||
public function showAsset($assetFileName)
|
||||
{
|
||||
$path = FormController::ASSETS_UPLOAD_PATH . '/' . $assetFileName;
|
||||
if (!Storage::exists($path)) {
|
||||
return $this->error([
|
||||
'message' => 'File not found.',
|
||||
'file_name' => $assetFileName,
|
||||
]);
|
||||
}
|
||||
|
||||
$internal_url = Storage::temporaryUrl($path, now()->addMinutes(5));
|
||||
|
||||
foreach(config('filesystems.disks.s3.temporary_url_rewrites') as $from => $to) {
|
||||
$internal_url = str_replace($from, $to, $internal_url);
|
||||
}
|
||||
|
||||
return redirect()->to($internal_url);
|
||||
}
|
||||
|
||||
public function answer(AnswerFormRequest $request)
|
||||
{
|
||||
$form = $request->form;
|
||||
$submissionId = false;
|
||||
|
||||
if ($form->editable_submissions) {
|
||||
$job = new StoreFormSubmissionJob($form, $request->validated());
|
||||
$job->handle();
|
||||
$submissionId = Hashids::encode($job->getSubmissionId());
|
||||
} else {
|
||||
StoreFormSubmissionJob::dispatch($form, $request->validated());
|
||||
}
|
||||
|
||||
return $this->success(array_merge([
|
||||
'message' => 'Form submission saved.',
|
||||
'submission_id' => $submissionId,
|
||||
], $request->form->is_pro && $request->form->redirect_url ? [
|
||||
'redirect' => true,
|
||||
'redirect_url' => $request->form->redirect_url,
|
||||
] : [
|
||||
'redirect' => false,
|
||||
]));
|
||||
}
|
||||
|
||||
public function fetchSubmission(Request $request, string $slug, string $submissionId)
|
||||
{
|
||||
$submissionId = ($submissionId) ? Hashids::decode($submissionId) : false;
|
||||
$submissionId = isset($submissionId[0]) ? $submissionId[0] : false;
|
||||
$form = Form::whereSlug($slug)->whereVisibility('public')->firstOrFail();
|
||||
if ($form->workspace == null || !$form->editable_submissions || !$submissionId) {
|
||||
return $this->error([
|
||||
'message' => 'Not allowed.',
|
||||
]);
|
||||
}
|
||||
|
||||
$submission = new FormSubmissionResource(FormSubmission::findOrFail($submissionId));
|
||||
$submission->publiclyAccessed();
|
||||
|
||||
if ($submission->form_id != $form->id) {
|
||||
return $this->error([
|
||||
'message' => 'Not allowed.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
return $this->success($submission->toArray($request));
|
||||
}
|
||||
}
|
||||
23
api/app/Http/Controllers/Forms/RecordController.php
Normal file
23
api/app/Http/Controllers/Forms/RecordController.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Forms;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Forms\Form;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class RecordController extends Controller
|
||||
{
|
||||
public function delete(Request $request, $id, $recordId)
|
||||
{
|
||||
$form = Form::findOrFail((int) $id);
|
||||
$this->authorize('delete', $form);
|
||||
|
||||
$record = $form->submissions()->where('id', $recordId)->firstOrFail();
|
||||
$record->delete();
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Record successfully removed.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user