Skip to content

Commit

Permalink
adding empty entires (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic authored Feb 27, 2024
1 parent 666b8e4 commit 6be6e8d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a

### Changed
- The security validation feature will no longer trigger count files and step requests.
- Entries now have ability to save empty fields based on the settings.

### Fixed
- Typo for fallback function.
- Custom mailer now supports saving entires.

## [3.0.2]

Expand Down
9 changes: 8 additions & 1 deletion src/Entries/EntriesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use EightshiftFormsVendor\EightshiftFormsUtils\Config\UtilsConfig;
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsGeneralHelper;
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsHooksHelper;
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsSettingsHelper;

/**
* EntriesHelper class.
Expand Down Expand Up @@ -48,11 +49,17 @@ public static function setEntryByFormDataRef(array $formDetails)

$params = UtilsGeneralHelper::removeUneceseryParamFields($params);

$saveEmptyFields = UtilsSettingsHelper::isSettingCheckboxChecked(SettingsEntries::SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS, SettingsEntries::SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS, $formId);

foreach ($params as $param) {
$name = $param['name'] ?? '';
$value = $param['value'] ?? '';

if (!$name || !$value) {
if (!$name) {
continue;
}

if (!$value && !$saveEmptyFields) {
continue;
}

Expand Down
41 changes: 28 additions & 13 deletions src/Entries/SettingsEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class SettingsEntries implements UtilsSettingGlobalInterface, UtilsSettingInterf
*/
public const SETTINGS_ENTRIES_SETTINGS_USE_KEY = 'entries-settings-use';

/**
* Entries settings save empty fields key.
*/
public const SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS = 'entries-save-empty-fields';

/**
* Data data key.
Expand Down Expand Up @@ -156,26 +160,37 @@ public function getSettingsData(string $formId): array
[
'component' => 'checkbox',
'checkboxLabel' => \__('Store entries in database', 'eightshift-forms'),
// translators: %s is replaced with the form entries page URL.
'checkboxHelp' => $isUsed ? \sprintf(\__("You can find all form entries <a href='%s' rel='noopener noreferrer' target='_blank'>here</a>.", 'eightshift-forms'), UtilsGeneralHelper::getFormsEntriesPageUrl($formId)) : '',
'checkboxIsChecked' => $isUsed,
'checkboxValue' => self::SETTINGS_ENTRIES_SETTINGS_USE_KEY,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
]
]
],
[
'component' => 'divider',
'dividerExtraVSpacing' => true,
],
[
'component' => 'intro',
// translators: %s is the link to the entries page.
'introSubtitle' => \sprintf(\__("
<p>
You can find all form entries <a href='%s' rel='noopener noreferrer' target='_blank'>here</a>.
</p>
", 'eightshift-forms'), UtilsGeneralHelper::getFormsEntriesPageUrl($formId)),
],
...($isUsed ? [
[
'component' => 'divider',
'dividerExtraVSpacing' => true,
],
[
'component' => 'checkboxes',
'checkboxesFieldLabel' => '',
'checkboxesName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS),
'checkboxesContent' => [
[
'component' => 'checkbox',
'checkboxLabel' => \__('Save empty fields to database', 'eightshift-forms'),
'checkboxHelp' => \__('All empty field values will not be saved to database by default.', 'eightshift-forms'),
'checkboxIsChecked' => UtilsSettingsHelper::isSettingCheckboxChecked(self::SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS, self::SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS, $formId),
'checkboxValue' => self::SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS,
'checkboxSingleSubmit' => true,
'checkboxAsToggle' => true,
]
]
],
] : []),
],
],
],
Expand Down
10 changes: 5 additions & 5 deletions src/Rest/Routes/AbstractFormSubmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ abstract class AbstractFormSubmit extends AbstractUtilsBaseRoute
*
* @var string
*/
protected const VALIDATION_ERROR_SEND_FAlLBACK = 'validationErrorSendFallback';
protected const VALIDATION_ERROR_SEND_FALLBACK = 'validationErrorSendFallback';
protected const VALIDATION_ERROR_CODE = 'validationErrorCode';
protected const VALIDATION_ERROR_DATA = 'validationErrorData';
protected const VALIDATION_ERROR_MSG = 'validationErrorMsg';
Expand Down Expand Up @@ -120,7 +120,7 @@ public function routeCallback(WP_REST_Request $request)
throw new UnverifiedRequestException(
$this->getValidatorLabels()->getLabel('validationMissingMandatoryParams'),
[
self::VALIDATION_ERROR_SEND_FAlLBACK => true,
self::VALIDATION_ERROR_SEND_FALLBACK => true,
self::VALIDATION_ERROR_CODE => 'validationMissingMandatoryParams',
]
);
Expand Down Expand Up @@ -159,7 +159,7 @@ public function routeCallback(WP_REST_Request $request)
self::VALIDATION_ERROR_OUTPUT => [
$uploadFileId => $this->getValidatorLabels()->getLabel('validationFileUpload'),
],
self::VALIDATION_ERROR_SEND_FAlLBACK => true,
self::VALIDATION_ERROR_SEND_FALLBACK => true,
self::VALIDATION_ERROR_CODE => 'validationFileUploadProcessError',
]
);
Expand Down Expand Up @@ -207,7 +207,7 @@ public function routeCallback(WP_REST_Request $request)
throw new UnverifiedRequestException(
$this->getValidatorLabels()->getLabel('validationSecurity'),
[
self::VALIDATION_ERROR_SEND_FAlLBACK => true,
self::VALIDATION_ERROR_SEND_FALLBACK => true,
self::VALIDATION_ERROR_CODE => 'validationSecurity',
]
);
Expand Down Expand Up @@ -258,7 +258,7 @@ public function routeCallback(WP_REST_Request $request)
// Do Action.
return $this->submitAction($formDetails);
} catch (UnverifiedRequestException $e) {
if (isset($e->getData()[self::VALIDATION_ERROR_SEND_FAlLBACK]) && $e->getData()[self::VALIDATION_ERROR_SEND_FAlLBACK]) {
if (isset($e->getData()[self::VALIDATION_ERROR_SEND_FALLBACK]) && $e->getData()[self::VALIDATION_ERROR_SEND_FALLBACK]) {
// Send fallback email.
$this->getFormSubmitMailer()->sendFallbackProcessingEmail(
$formDetails,
Expand Down
8 changes: 8 additions & 0 deletions src/Rest/Routes/Integrations/Mailer/FormSubmitCustomRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace EightshiftForms\Rest\Routes\Integrations\Mailer;

use EightshiftForms\Captcha\CaptchaInterface;
use EightshiftForms\Entries\EntriesHelper;
use EightshiftForms\Entries\SettingsEntries;
use EightshiftForms\Integrations\Mailer\SettingsMailer;
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsGeneralHelper;
use EightshiftForms\Validation\ValidatorInterface;
Expand Down Expand Up @@ -79,6 +81,12 @@ protected function submitAction(array $formDetails)
$action = $formDetails[UtilsConfig::FD_ACTION];
$actionExternal = $formDetails[UtilsConfig::FD_ACTION_EXTERNAL];

// Save entries.
if (\apply_filters(SettingsEntries::FILTER_SETTINGS_IS_VALID_NAME, $formId)) {
$entryId = EntriesHelper::setEntryByFormDataRef($formDetails);
$formDetails[UtilsConfig::FD_ENTRY_ID] = $entryId ? (string) $entryId : '';
}

$debug = [
'formDetails' => $formDetails,
];
Expand Down

0 comments on commit 6be6e8d

Please sign in to comment.