Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/Forms/AugmentedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,51 @@
namespace Statamic\Forms;

use Statamic\Data\AbstractAugmented;
use Statamic\Fields\Section;
use Statamic\Fields\Tab;
use Statamic\Statamic;

class AugmentedForm extends AbstractAugmented
{
public function keys()
{
$keys = ['handle', 'title', 'fields', 'status', 'api_url'];
$keys = ['handle', 'title', 'fields', 'sections', 'pages', 'status', 'require_login', 'closed_message', 'require_login_message', 'api_url'];

if (! Statamic::isApiRoute()) {
$keys[] = 'honeypot';
}

return $keys;
}

public function sections(): array
{
return $this->data->sections()
->map(fn (Section $section) => $this->sectionToArray($section))
->all();
}

public function pages(): array
{
return $this->data->pages()->map(fn (Tab $page) => [
'id' => $page->handle(),
'display' => $page->display(),
'instructions' => $page->instructions(),
'sections' => $page->expandedSections()->map(fn (Section $section) => $this->sectionToArray($section))->all(),
])->all();
}

private function sectionToArray(Section $section): array
{
return [
'display' => $section->display(),
'instructions' => $section->instructions(),
'fields' => $section->fields()->all()->map->toArray()->all(),
];
}

public function requireLogin(): bool
{
return (bool) $this->data->get('require_login');
}
}
31 changes: 29 additions & 2 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Forms\Form as FormContract;
Expand All @@ -27,6 +28,7 @@
use Statamic\Facades\User;
use Statamic\Facades\YAML;
use Statamic\Fields\Blueprint;
use Statamic\Fields\Tab;
use Statamic\Forms\Connectors\Connector;
use Statamic\Forms\Exporters\Exporter;
use Statamic\Forms\Fields\FormFields;
Expand Down Expand Up @@ -361,6 +363,21 @@ public function fields()
return $this->blueprint()->fields()->all();
}

public function sections(): Collection
{
return $this->pages()->flatMap(fn (Tab $page) => $page->expandedSections());
}

public function pages(): Collection
{
$pages = $this->formFields()->pages()->keyBy('id');

return $this->blueprint()->tabs()->values()->map(fn (Tab $tab) => (new Tab($tab->handle()))->setContents([
...$tab->contents(),
'display' => $pages->has($tab->handle()) ? ($pages->get($tab->handle())['display'] ?? null) : $tab->display(),
]));
}

/**
* Get path.
*
Expand Down Expand Up @@ -555,16 +572,26 @@ public function restricted(): bool
public function restrictionMessage(): ?string
{
if ($this->closingDateHasPassed() || $this->submissionLimitReached()) {
return ($msg = $this->get('closed_message')) ? __($msg) : __('statamic::messages.form_closed_message');
return $this->closedMessage();
}

if ($this->get('require_login') && ! User::current()) {
return ($msg = $this->get('require_login_message')) ? __($msg) : __('statamic::messages.form_require_login_message');
return $this->requireLoginMessage();
}

return null;
}

public function closedMessage(): string
{
return ($msg = $this->get('closed_message')) ? __($msg) : __('statamic::messages.form_closed_message');
}

public function requireLoginMessage(): string
{
return ($msg = $this->get('require_login_message')) ? __($msg) : __('statamic::messages.form_require_login_message');
}

private function closingDateHasPassed(): bool
{
if (! $date = $this->get('close_date')) {
Expand Down
2 changes: 2 additions & 0 deletions src/GraphQL/TypeRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Statamic\GraphQL\Types\DateRangeType;
use Statamic\GraphQL\Types\EntryInterface;
use Statamic\GraphQL\Types\FieldType;
use Statamic\GraphQL\Types\FormPageType;
use Statamic\GraphQL\Types\FormType;
use Statamic\GraphQL\Types\GlobalSetInterface;
use Statamic\GraphQL\Types\JsonArgument;
Expand Down Expand Up @@ -66,6 +67,7 @@ public function register()
GraphQL::addType(GlobalSetInterface::class);
GraphQL::addType(FieldType::class);
GraphQL::addType(SectionType::class);
GraphQL::addType(FormPageType::class);
GraphQL::addType(LinkValueType::class);

PageInterface::addTypes();
Expand Down
44 changes: 44 additions & 0 deletions src/GraphQL/Types/FormPageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Statamic\GraphQL\Types;

use Statamic\Facades\GraphQL;

class FormPageType extends \Rebing\GraphQL\Support\Type
{
const NAME = 'FormPage';

protected $attributes = [
'name' => self::NAME,
];

public function fields(): array
{
return [
'id' => [
'type' => GraphQL::nonNull(GraphQL::string()),
'resolve' => function ($page) {
return $page->handle();
},
],
'display' => [
'type' => GraphQL::string(),
'resolve' => function ($page) {
return $page->display();
},
],
'instructions' => [
'type' => GraphQL::string(),
'resolve' => function ($page) {
return $page->instructions();
},
],
'sections' => [
'type' => GraphQL::listOf(GraphQL::type(SectionType::NAME)),
'resolve' => function ($page) {
return $page->expandedSections()->all();
},
],
];
}
}
17 changes: 16 additions & 1 deletion src/GraphQL/Types/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public function fields(): array
'status' => [
'type' => GraphQL::nonNull(GraphQL::string()),
],
'require_login' => [
'type' => GraphQL::nonNull(GraphQL::boolean()),
],
'closed_message' => [
'type' => GraphQL::nonNull(GraphQL::string()),
],
'require_login_message' => [
'type' => GraphQL::nonNull(GraphQL::string()),
],
'fields' => [
'type' => GraphQL::listOf(GraphQL::type(FieldType::NAME)),
'resolve' => function ($form, $args, $context, $info) {
Expand Down Expand Up @@ -59,7 +68,13 @@ public function fields(): array
'sections' => [
'type' => GraphQL::listOf(GraphQL::type(SectionType::NAME)),
'resolve' => function ($form, $args, $context, $info) {
return $form->blueprint()->tabs()->first()->sections()->all();
return $form->sections()->all();
},
],
'pages' => [
'type' => GraphQL::listOf(GraphQL::type(FormPageType::NAME)),
'resolve' => function ($form, $args, $context, $info) {
return $form->pages()->all();
},
],
])->map(function (array $arr) {
Expand Down
Loading
Loading