From 8abf7699fd42719605b5977e3502028fa70e71f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ruz=CC=8Cevic=CC=81?= Date: Tue, 14 Jan 2025 15:34:29 +0100 Subject: [PATCH] adding increment ID --- composer.json | 2 +- composer.lock | 12 +- .../admin-settings/admin-settings-admin.scss | 1 - src/Entries/EntriesHelper.php | 50 +++++++ src/Entries/SettingsEntries.php | 135 +++++++++++++++--- src/General/SettingsGeneral.php | 2 +- src/Helpers/FormsHelper.php | 2 + src/Hooks/Filters.php | 3 + src/Hooks/FiltersSettingsBuilder.php | 1 + src/Rest/Routes/AbstractFormSubmit.php | 13 ++ .../Calculator/FormSubmitCalculatorRoute.php | 2 - 11 files changed, 191 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index a63ed5509..fcb90e612 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "require": { "php": ">=8.3", "erusev/parsedown": "^1.7.4", - "infinum/eightshift-forms-utils": "^3.0.16" + "infinum/eightshift-forms-utils": "^3.0.18" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index a8f265e2a..84c8429ff 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a94413b69de972942cd908264c7eed55", + "content-hash": "04dc9d89e994d026dbd74a899d6497b1", "packages": [ { "name": "erusev/parsedown", @@ -58,16 +58,16 @@ }, { "name": "infinum/eightshift-forms-utils", - "version": "3.0.16", + "version": "3.0.18", "source": { "type": "git", "url": "https://github.com/infinum/eightshift-forms-utils.git", - "reference": "de03021b7b7817297be241c4957411ec81f234b4" + "reference": "28a6ab29433a14b51d3752bcfd52179f41051d36" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/infinum/eightshift-forms-utils/zipball/de03021b7b7817297be241c4957411ec81f234b4", - "reference": "de03021b7b7817297be241c4957411ec81f234b4", + "url": "https://api.github.com/repos/infinum/eightshift-forms-utils/zipball/28a6ab29433a14b51d3752bcfd52179f41051d36", + "reference": "28a6ab29433a14b51d3752bcfd52179f41051d36", "shasum": "" }, "require": { @@ -120,7 +120,7 @@ "issues": "https://github.com/infinum/eightshift-forms/issues", "source": "https://github.com/infinum/eightshift-forms" }, - "time": "2025-01-13T11:18:49+00:00" + "time": "2025-01-14T14:33:01+00:00" }, { "name": "infinum/eightshift-libs", diff --git a/src/Blocks/components/admin-settings/admin-settings-admin.scss b/src/Blocks/components/admin-settings/admin-settings-admin.scss index 102a3bc98..732dc0774 100644 --- a/src/Blocks/components/admin-settings/admin-settings-admin.scss +++ b/src/Blocks/components/admin-settings/admin-settings-admin.scss @@ -8,7 +8,6 @@ .es-form[data-settings-type='documentation'], .es-form[data-settings-type='migration'], .es-form[data-settings-type='transfer'], - .es-form[data-settings-type='entries'], .es-form[data-form-type='settingsGlobal'][data-settings-type='geolocation'], .es-form[data-form-type='settingsGlobal'][data-settings-type='cloudflare'], .es-form[data-form-type='settingsGlobal'][data-settings-type='wp'], diff --git a/src/Entries/EntriesHelper.php b/src/Entries/EntriesHelper.php index fe24a921b..41d60f175 100644 --- a/src/Entries/EntriesHelper.php +++ b/src/Entries/EntriesHelper.php @@ -15,6 +15,7 @@ use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsHelper; use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsHooksHelper; use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsSettingsHelper; +use Hamcrest\Util; /** * EntriesHelper class. @@ -314,6 +315,55 @@ public static function deleteEntry(string $id): bool return true; } + /** + * Get increment. + * + * @param string $formId Form Id. + * + * @return string + */ + public static function getIncrement(string $formId): string + { + $value = UtilsSettingsHelper::getSettingValue(SettingsEntries::INCREMENT_META_KEY, $formId); + if (!$value) { + $value = 0; + } + + $length = UtilsSettingsHelper::getSettingValue(SettingsEntries::SETTINGS_ENTRIES_INCREMENT_LENGTH_KEY, $formId); + if ($length) { + $value = \str_pad($value, (int) $length, '0', STR_PAD_LEFT); + } + + return $value; + } + + /** + * Set increment. + * + * @param string $formId Form Id. + * + * @return string + */ + public static function setIncrement(string $formId): string + { + $start = UtilsSettingsHelper::getSettingValue(SettingsEntries::SETTINGS_ENTRIES_INCREMENT_START_KEY, $formId); + $value = UtilsSettingsHelper::getSettingValue(SettingsEntries::INCREMENT_META_KEY, $formId); + + if (!$value) { + $value = $start; + } + + if ((int) $start > (int) $value) { + $value = $start; + } + + $value = (int) $value + 1; + + update_post_meta($formId, UtilsSettingsHelper::getSettingName(SettingsEntries::INCREMENT_META_KEY), $value); + + return (string) $value; + } + /** * Prepare entry output from DB. * diff --git a/src/Entries/SettingsEntries.php b/src/Entries/SettingsEntries.php index 6d19b0c32..9d7027a26 100644 --- a/src/Entries/SettingsEntries.php +++ b/src/Entries/SettingsEntries.php @@ -12,6 +12,7 @@ use EightshiftFormsVendor\EightshiftFormsUtils\Config\UtilsConfig; use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsGeneralHelper; +use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsHelper; use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsSettingsOutputHelper; use EightshiftFormsVendor\EightshiftFormsUtils\Settings\UtilsSettingInterface; use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsSettingsHelper; @@ -43,6 +44,13 @@ class SettingsEntries implements UtilsSettingGlobalInterface, UtilsSettingInterf */ public const FILTER_SETTINGS_GLOBAL_IS_VALID_NAME = 'es_forms_settings_global_is_valid_entries'; + /** + * Increment meta key. + * + * @var string + */ + public const INCREMENT_META_KEY = 'es_forms_increment'; + /** * Settings key. */ @@ -59,16 +67,27 @@ class SettingsEntries implements UtilsSettingGlobalInterface, UtilsSettingInterf public const SETTINGS_ENTRIES_SETTINGS_USE_KEY = 'entries-settings-use'; /** - * Entries settings save empty fields key. + * Save empty fields key. */ public const SETTINGS_ENTRIES_SAVE_EMPTY_FIELDS = 'entries-save-empty-fields'; + /** + * Increment start key. + */ + public const SETTINGS_ENTRIES_INCREMENT_START_KEY = 'entries-increment-start'; + + /** + * Increment length key. + */ + public const SETTINGS_ENTRIES_INCREMENT_LENGTH_KEY = 'entries-increment-length'; + /** * Entries settings send entry in form submit key. */ public const SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_KEY = 'entries-save-additional-values'; public const SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_REDIRECT_URL_KEY = 'redirect-url'; public const SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_VARIATIONS_KEY = 'variations'; + public const SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_INCREMENT_ID_KEY = 'increment-id'; /** * Data data key. @@ -152,47 +171,52 @@ public function getSettingsData(string $formId): array return [ UtilsSettingsOutputHelper::getIntro(self::SETTINGS_TYPE_KEY), - [ + ($isUsed ? [ 'component' => 'layout', 'layoutType' => 'layout-v-stack-clean', 'layoutContent' => [ [ 'component' => 'card-inline', - 'cardInlineTitle' => \__('Store entries in database', 'eightshift-forms'), + 'cardInlineTitle' => \__('View all entries in database', 'eightshift-forms'), 'cardInlineRightContent' => [ - $isUsed ? [ + [ 'component' => 'submit', 'submitVariant' => 'ghost', 'submitButtonAsLink' => true, 'submitButtonAsLinkUrl' => UtilsGeneralHelper::getListingPageUrl(UtilsConfig::SLUG_ADMIN_LISTING_ENTRIES, $formId), 'submitValue' => \__('View', 'eightshift-forms'), - ] : [], + ], + ], + ], + ], + ] : []), + [ + 'component' => 'tabs', + 'tabsContent' => [ + [ + 'component' => 'tab', + 'tabLabel' => \__('Entries', 'eightshift-forms'), + 'tabContent' => [ [ 'component' => 'checkboxes', 'checkboxesName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_ENTRIES_SETTINGS_USE_KEY), 'checkboxesContent' => [ [ 'component' => 'checkbox', + 'checkboxLabel' => \__('Store entries in database', 'eightshift-forms'), 'checkboxIsChecked' => $isUsed, 'checkboxValue' => self::SETTINGS_ENTRIES_SETTINGS_USE_KEY, 'checkboxSingleSubmit' => true, 'checkboxAsToggle' => true, ], ], + 'checkboxesFieldHelp' => $isUsed ? sprintf(\__('View all stored entries on this link.', 'eightshift-forms'), UtilsGeneralHelper::getListingPageUrl(UtilsConfig::SLUG_ADMIN_LISTING_ENTRIES, $formId)) : '', ], - ], - ], - ], - ], - ...($isUsed ? [ - [ - 'component' => 'tabs', - 'tabsFull' => true, - 'tabsContent' => [ - [ - 'component' => 'tab', - 'tabLabel' => \__('Options', 'eightshift-forms'), - 'tabContent' => [ + ...($isUsed ? [ + [ + 'component' => 'divider', + 'dividerExtraVSpacing' => true, + ], [ 'component' => 'checkboxes', 'checkboxesFieldLabel' => '', @@ -235,14 +259,83 @@ public function getSettingsData(string $formId): array 'checkboxValue' => self::SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_VARIATIONS_KEY, 'checkboxSingleSubmit' => true, 'checkboxAsToggle' => true, - ] - ] + ], + [ + 'component' => 'checkbox', + 'checkboxLabel' => \__('Increment ID', 'eightshift-forms'), + 'checkboxHelp' => \__('Increment ID set by the form successful submission.', 'eightshift-forms'), + 'checkboxIsChecked' => UtilsSettingsHelper::isSettingCheckboxChecked(self::SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_INCREMENT_ID_KEY, self::SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_KEY, $formId), + 'checkboxValue' => self::SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_INCREMENT_ID_KEY, + 'checkboxSingleSubmit' => true, + 'checkboxAsToggle' => true, + ], + ], + ], + ] : []), + ], + ], + [ + 'component' => 'tab', + 'tabLabel' => \__('Increment', 'eightshift-forms'), + 'tabContent' => [ + [ + 'component' => 'layout', + 'layoutType' => 'layout-v-stack', + 'layoutContent' => [ + [ + 'component' => 'intro', + 'introTitle' => \__('Export', 'eightshift-forms'), + ], + [ + 'component' => 'card-inline', + 'cardInlineTitle' => \__('Increment Id settings'), + 'cardInlineSubTitle' => sprintf(\__('Current increment Id is: %s', 'eightshift-forms'), EntriesHelper::getIncrement($formId)), + 'cardInlineRightContent' => [ + [ + 'component' => 'submit', + 'submitValue' => \__('Reset', 'eightshift-forms'), + 'submitVariant' => 'ghost', + 'submitAttrs' => [ + // UtilsHelper::getStateAttribute('migrationType') => self::TYPE_EXPORT_GLOBAL_SETTINGS, + ], + 'additionalClass' => UtilsHelper::getStateSelectorAdmin('incrementReset'), + ], + ], + ], ], ], + [ + 'component' => 'divider', + 'dividerExtraVSpacing' => true, + ], + [ + 'component' => 'input', + 'inputName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_ENTRIES_INCREMENT_START_KEY), + 'inputId' => UtilsSettingsHelper::getSettingName(self::SETTINGS_ENTRIES_INCREMENT_START_KEY), + 'inputFieldLabel' => \__('Increment start number', 'eightshift-forms'), + 'inputFieldHelp' => \__('Set the starting increment number of each successful form submission.', 'eightshift-forms'), + 'inputType' => 'number', + 'inputMin' => 1, + 'inputStep' => 1, + 'inputIsNumber' => true, + 'inputValue' => UtilsSettingsHelper::getSettingValue(self::SETTINGS_ENTRIES_INCREMENT_START_KEY, $formId), + ], + [ + 'component' => 'input', + 'inputName' => UtilsSettingsHelper::getSettingName(self::SETTINGS_ENTRIES_INCREMENT_LENGTH_KEY), + 'inputId' => UtilsSettingsHelper::getSettingName(self::SETTINGS_ENTRIES_INCREMENT_LENGTH_KEY), + 'inputFieldLabel' => \__('Increment length number', 'eightshift-forms'), + 'inputFieldHelp' => \__('Define minimal increment length you want to use. If the number is less than starting number, increment will have leading zeros.', 'eightshift-forms'), + 'inputType' => 'number', + 'inputMin' => 1, + 'inputStep' => 1, + 'inputIsNumber' => true, + 'inputValue' => UtilsSettingsHelper::getSettingValue(self::SETTINGS_ENTRIES_INCREMENT_LENGTH_KEY, $formId), + ] ], ], ], - ] : []), + ], ]; } diff --git a/src/General/SettingsGeneral.php b/src/General/SettingsGeneral.php index a74a47550..82194740e 100644 --- a/src/General/SettingsGeneral.php +++ b/src/General/SettingsGeneral.php @@ -355,7 +355,7 @@ function ($selectOption) use ($successRedirectVariation) { 'inputFieldHelp' => \__('Target a form (or a set of forms) and apply changes through filters, in code.', 'eightshift-forms'), 'inputType' => 'text', 'inputValue' => UtilsSettingsHelper::getSettingValue(self::SETTINGS_FORM_CUSTOM_NAME_KEY, $formId), - ] + ], ], ], [ diff --git a/src/Helpers/FormsHelper.php b/src/Helpers/FormsHelper.php index e15a9e921..766587db6 100644 --- a/src/Helpers/FormsHelper.php +++ b/src/Helpers/FormsHelper.php @@ -258,4 +258,6 @@ public static function getFormUniqueHash(): string { return \str_pad((string) \wp_rand(1, 9999999999), 10, '0', \STR_PAD_LEFT); } + + } diff --git a/src/Hooks/Filters.php b/src/Hooks/Filters.php index 89695f921..eeff91c71 100644 --- a/src/Hooks/Filters.php +++ b/src/Hooks/Filters.php @@ -31,6 +31,7 @@ use EightshiftForms\Troubleshooting\SettingsDebug; use EightshiftForms\Troubleshooting\SettingsFallback; use EightshiftForms\Captcha\SettingsCaptcha; +use EightshiftForms\Entries\SettingsEntries; use EightshiftForms\Integrations\Calculator\SettingsCalculator; use EightshiftForms\Integrations\Corvus\SettingsCorvus; use EightshiftForms\Integrations\Paycek\SettingsPaycek; @@ -437,6 +438,8 @@ private static function getSettingsNoneTranslatableNames(): array SettingsDebug::SETTINGS_DEBUG_DEBUGGING_KEY, SettingsSettings::SETTINGS_GENERAL_DISABLE_DEFAULT_ENQUEUE_KEY, + + SettingsEntries::INCREMENT_META_KEY, ]; } } diff --git a/src/Hooks/FiltersSettingsBuilder.php b/src/Hooks/FiltersSettingsBuilder.php index fd9690998..3d85dcf09 100644 --- a/src/Hooks/FiltersSettingsBuilder.php +++ b/src/Hooks/FiltersSettingsBuilder.php @@ -238,6 +238,7 @@ public function getSettingsFiltersData(): array 'mailerFormTitle' => '', 'mailerTimestamp' => '', 'mailerTimestampHuman' => '', + 'mailerIncrementId' => '', ], 'labels' => [ 'title' => \__('Mailer', 'eightshift-forms'), diff --git a/src/Rest/Routes/AbstractFormSubmit.php b/src/Rest/Routes/AbstractFormSubmit.php index 72c2a2599..3fa45e3a1 100644 --- a/src/Rest/Routes/AbstractFormSubmit.php +++ b/src/Rest/Routes/AbstractFormSubmit.php @@ -487,6 +487,9 @@ protected function getIntegrationResponseSuccessOutputAdditionalData(array $form 'public' => [], ]; + // Set increment and add it to the output. + $output['private'][UtilsHelper::getStateResponseOutputKey('incrementId')] = EntriesHelper::setIncrement($formId); + // Set entries. $useEntries = \apply_filters(SettingsEntries::FILTER_SETTINGS_IS_VALID_NAME, $formId); if ($useEntries) { @@ -597,6 +600,13 @@ protected function getIntegrationResponseSuccessOutputAdditionalData(array $form $entryNewData[UtilsHelper::getStateResponseOutputKey('variation')] = $output['public'][UtilsHelper::getStateResponseOutputKey('variation')]; } + if ( + UtilsSettingsHelper::isSettingCheckboxChecked(SettingsEntries::SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_INCREMENT_ID_KEY, SettingsEntries::SETTINGS_ENTRIES_SAVE_ADDITONAL_VALUES_KEY, $formId) && + isset($output['private'][UtilsHelper::getStateResponseOutputKey('incrementId')]) + ) { + $entryNewData[UtilsHelper::getStateResponseOutputKey('incrementId')] = $output['private'][UtilsHelper::getStateResponseOutputKey('incrementId')]; + } + EntriesHelper::updateEntry($entryNewData, $output['private'][UtilsHelper::getStateResponseOutputKey('entry')]); } } @@ -715,6 +725,9 @@ protected function getCommonEmailResponseTags(array $data, array $formDetails): case 'mailerTimestampHuman': $output[$key] = \current_datetime()->format('Y-m-d H:i:s'); break; + case 'mailerIncrementId': + $output[$key] = $data[UtilsHelper::getStateResponseOutputKey('incrementId')] ?? ''; + break; case 'mailerEntryUrl': $entryId = $data[UtilsHelper::getStateResponseOutputKey('entry')] ?? ''; $formId = $data[UtilsHelper::getStateResponseOutputKey('formId')] ?? ''; diff --git a/src/Rest/Routes/Integrations/Calculator/FormSubmitCalculatorRoute.php b/src/Rest/Routes/Integrations/Calculator/FormSubmitCalculatorRoute.php index 0ac88ef02..69cfcfaa4 100644 --- a/src/Rest/Routes/Integrations/Calculator/FormSubmitCalculatorRoute.php +++ b/src/Rest/Routes/Integrations/Calculator/FormSubmitCalculatorRoute.php @@ -89,8 +89,6 @@ protected function submitAction(array $formDetails) $formDetails[UtilsConfig::FD_PARAMS] = \apply_filters($filterName, $formDetails[UtilsConfig::FD_PARAMS], $formId) ?? []; } - $successAdditionalData = $this->getIntegrationResponseSuccessOutputAdditionalData($formDetails); - // Set validation submit once. $this->validator->setValidationSubmitOnce($formId);