Skip to content

Commit

Permalink
[Forms] add custom status field to form and allow for filtering in ba…
Browse files Browse the repository at this point in the history
…ckend
  • Loading branch information
Numkil committed Jul 17, 2023
1 parent b4c4f67 commit af432aa
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/controllers/FormsController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace verbb\formie\controllers;

use verbb\formie\Formie;
Expand Down
60 changes: 50 additions & 10 deletions src/elements/Form.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace verbb\formie\elements;

use verbb\formie\Formie;
Expand Down Expand Up @@ -62,6 +63,12 @@ class Form extends Element
// =========================================================================

public const EVENT_MODIFY_HTML_TAG = 'modifyHtmlTag';
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
public const STATUSES = [
self::STATUS_ACTIVE,
self::STATUS_INACTIVE,
];


// Static Methods
Expand Down Expand Up @@ -99,6 +106,19 @@ public static function hasContent(): bool
return true;
}

public static function hasStatuses(): bool
{
return true;
}

public static function statuses(): array
{
return [
self::STATUS_ACTIVE => Craft::t('app', 'Active'),
self::STATUS_INACTIVE => Craft::t('app', 'Inactive'),
];
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -155,7 +175,7 @@ public static function defineSources(string $context = null): array

return $sources;
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -228,6 +248,7 @@ protected static function defineTableAttributes(): array
'title' => ['label' => Craft::t('app', 'Title')],
'id' => ['label' => Craft::t('app', 'ID')],
'handle' => ['label' => Craft::t('app', 'Handle')],
'formStatus' => ['label' => Craft::t('formie', 'Status')],
'template' => ['label' => Craft::t('app', 'Template')],
'usageCount' => ['label' => Craft::t('formie', 'Usage Count')],
'dateCreated' => ['label' => Craft::t('app', 'Date Created')],
Expand Down Expand Up @@ -288,6 +309,7 @@ protected static function defineSortOptions(): array
// Properties
// =========================================================================

private ?string $formStatus = self::STATUS_ACTIVE;
public ?string $handle = null;
public ?string $oldHandle = null;
public ?string $fieldContentTable = null;
Expand Down Expand Up @@ -385,7 +407,7 @@ public function behaviors(): array

return $behaviors;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -555,6 +577,23 @@ public function getDefaultStatus(): ?Status
return $this->_defaultStatus;
}

public function setFormStatus(string $status = null): void
{
if ($status !== null) {
$this->formStatus = $status;
}
}

public function getFormStatus(): string
{
return $this->formStatus ?? self::STATUS_ACTIVE;
}

public function getStatus(): string
{
return $this->getFormStatus();
}

/**
* Sets the default status.
*
Expand Down Expand Up @@ -1296,7 +1335,7 @@ public function getPageFieldErrors($submission): array
public function renderTemplate(array|string $components, array $variables = []): string
{
$view = Craft::$app->getView();

// Normalise the components to allow for a single component
if (!is_array($components)) {
$components = [$components];
Expand Down Expand Up @@ -1579,7 +1618,7 @@ public function defineHtmlTag(string $key, array $context = []): ?HtmlTag
$page = $context['page'] ?? null;
$inputAttributes = $page->settings->getInputAttributes() ?? [];
$saveButtonStyle = $page->settings->saveButtonStyle ?? 'link';

return new HtmlTag('button', [
'class' => [
'fui-btn fui-save',
Expand Down Expand Up @@ -1899,7 +1938,7 @@ public function setSettings($settings, $updateSnapshot = true): void
public function setFieldSettings($handle, $settings, $updateSnapshot = true): void
{
$field = null;

// Check for nested fields so we can use `group.dropdown` or `dropdown`.
$handles = explode('.', $handle);

Expand Down Expand Up @@ -1930,7 +1969,7 @@ public function setIntegrationSettings(string $handle, array $settings, $updateS
{
// Get the integration settings so we only override what we want
$integrationSettings = $this->settings->integrations[$handle] ?? [];

// Update the integration settings
$this->settings->integrations[$handle] = array_merge($integrationSettings, $settings);

Expand Down Expand Up @@ -2018,7 +2057,7 @@ public function isBeforeSchedule(): bool
if ($this->settings->scheduleForm && $this->settings->scheduleFormStart) {
return !DateTimeHelper::isInThePast($this->settings->scheduleFormStart);
}

return false;
}

Expand All @@ -2027,7 +2066,7 @@ public function isAfterSchedule(): bool
if ($this->settings->scheduleForm && $this->settings->scheduleFormEnd) {
return DateTimeHelper::isInThePast($this->settings->scheduleFormEnd);
}

return false;
}

Expand Down Expand Up @@ -2065,7 +2104,7 @@ public function isWithinSubmissionsLimit(): bool
return false;
}
}

return true;
}

Expand Down Expand Up @@ -2149,6 +2188,7 @@ public function afterSave(bool $isNew): void

$record->handle = $this->handle;
$record->fieldContentTable = $this->fieldContentTable;
$record->formStatus = $this->formStatus;
$record->settings = $this->settings;
$record->templateId = $this->templateId;
$record->submitActionEntryId = $this->submitActionEntryId;
Expand Down Expand Up @@ -2286,7 +2326,7 @@ protected function defineRules(): array
];

$rules[] = [
'handle', function($attribute, $params, Validator $validator): void {
'handle', function ($attribute, $params, Validator $validator): void {
$query = static::find()->handle($this->$attribute);
if ($this->id) {
$query = $query->id("not {$this->id}");
Expand Down
26 changes: 25 additions & 1 deletion src/elements/db/FormQuery.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace verbb\formie\elements\db;

use verbb\formie\models\FormTemplate;

use verbb\formie\elements\Form;
use craft\db\Query;
use craft\elements\db\ElementQuery;
use craft\helpers\Db;
Expand All @@ -14,6 +15,7 @@ class FormQuery extends ElementQuery

public mixed $handle = null;
public mixed $templateId = null;
public mixed $formStatus = null;

protected array $defaultOrderBy = ['elements.dateCreated' => SORT_DESC];

Expand Down Expand Up @@ -50,6 +52,23 @@ public function templateId($value): static
return $this;
}

public function status(array|string|null $value): static
{
$this->formStatus = $value;

return $this;
}


protected function statusCondition(string $status): mixed
{
if (in_array($status, FORM::STATUSES, true)) {
return ['formie_forms.formStatus' => $status];
}

return [];
}


// Protected Methods
// =========================================================================
Expand All @@ -60,6 +79,7 @@ protected function beforePrepare(): bool

$this->query->select([
'formie_forms.id',
'formie_forms.formStatus',
'formie_forms.handle',
'formie_forms.fieldContentTable',
'formie_forms.settings',
Expand All @@ -83,6 +103,10 @@ protected function beforePrepare(): bool
$this->subQuery->andWhere(Db::parseParam('formie_forms.templateId', $this->templateId));
}

if ($this->formStatus) {
$this->subQuery->andWhere(Db::parseParam('formie_forms.formStatus', $this->formStatus));
}

return parent::beforePrepare();
}
}
34 changes: 34 additions & 0 deletions src/migrations/m230716_000000_add_status_to_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace verbb\formie\migrations;

use craft\db\Migration;
use verbb\formie\elements\Form;

class m230716_000000_add_status_to_form extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
if (!$this->db->columnExists('{{%formie_forms}}', 'formStatus')) {
$this->addColumn('{{%formie_forms}}', 'formStatus', $this->string());
$this->update('{{%formie_forms}}', ['formStatus' => Form::STATUS_ACTIVE]);
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
if ($this->db->columnExists('{{%formie_forms}}', 'formStatus')) {
$this->dropColumn('{{%formie_forms}}', 'formStatus');
}

return true;
}
}
7 changes: 5 additions & 2 deletions src/services/Forms.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace verbb\formie\services;

use verbb\formie\Formie;
Expand Down Expand Up @@ -214,7 +215,7 @@ public function saveForm(Form $form, bool $runValidation = true): bool
foreach ($captchas as $captcha) {
// Check to see if we have any data already applied from a stencil
$integrationEnabled = $form->settings->integrations[$captcha->handle]['enabled'] ?? null;

if ($captcha->getEnabled() && $integrationEnabled === null) {
$form->settings->integrations[$captcha->handle]['enabled'] = true;
}
Expand Down Expand Up @@ -261,7 +262,7 @@ public function saveForm(Form $form, bool $runValidation = true): bool
$transaction->rollBack();

$form->addErrors(['general' => $e->getMessage()]);

Formie::error('Unable to save form “' . $form->handle . '”: ' . $e->getMessage());

return false;
Expand Down Expand Up @@ -380,6 +381,7 @@ public function buildFormFromPost(): Form
$request = Craft::$app->getRequest();
$formId = $request->getParam('formId');
$siteId = $request->getParam('siteId');
$status = $request->getParam('status');
$duplicate = (bool)$request->getParam('duplicate');

if ($formId) {
Expand All @@ -406,6 +408,7 @@ public function buildFormFromPost(): Form
}

$form->siteId = $siteId ?? $form->siteId;
$form->setFormStatus($status ?? $form->getFormStatus());
$form->handle = $request->getParam('handle', $form->handle);
$form->templateId = \verbb\formie\helpers\StringHelper::toId($request->getParam('templateId', $form->templateId));
$form->defaultStatusId = \verbb\formie\helpers\StringHelper::toId($request->getParam('defaultStatusId', $form->defaultStatusId));
Expand Down
21 changes: 21 additions & 0 deletions src/templates/forms/_panes/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ <h2>{{ 'Form Settings' | t('formie') }}</h2>

<hr>

{{ forms.selectField({
label: 'Status' | t('formie'),
instructions: 'What is the current status of this form' | t('formie'),
id: 'status',
name: 'status',
required: true,
options: [
{
label: 'Active' | t('formie'),
value: 'active',
},
{
label: 'Inactive' | t('formie'),
value: 'inactive',
},
],
value: form.getFormStatus(),
}) }}

<hr>

<h2>{{ 'Submissions' | t('formie') }}</h2>

{{ forms.hidden({
Expand Down

0 comments on commit af432aa

Please sign in to comment.