Skip to content

Commit

Permalink
adding increment ID
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Jan 14, 2025
1 parent a7966b3 commit 8abf769
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 32 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
50 changes: 50 additions & 0 deletions src/Entries/EntriesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsHelper;
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsHooksHelper;
use EightshiftFormsVendor\EightshiftFormsUtils\Helpers\UtilsSettingsHelper;
use Hamcrest\Util;

/**
* EntriesHelper class.
Expand Down Expand Up @@ -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.
*
Expand Down
135 changes: 114 additions & 21 deletions src/Entries/SettingsEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
Expand Down Expand Up @@ -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 <a href="%s">this</a> 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' => '',
Expand Down Expand Up @@ -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),
]
],
],
],
] : []),
],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/General/SettingsGeneral.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
],
],
],
[
Expand Down
2 changes: 2 additions & 0 deletions src/Helpers/FormsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,6 @@ public static function getFormUniqueHash(): string
{
return \str_pad((string) \wp_rand(1, 9999999999), 10, '0', \STR_PAD_LEFT);
}


}
3 changes: 3 additions & 0 deletions src/Hooks/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
];
}
}
1 change: 1 addition & 0 deletions src/Hooks/FiltersSettingsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function getSettingsFiltersData(): array
'mailerFormTitle' => '',
'mailerTimestamp' => '',
'mailerTimestampHuman' => '',
'mailerIncrementId' => '',
],
'labels' => [
'title' => \__('Mailer', 'eightshift-forms'),
Expand Down
Loading

0 comments on commit 8abf769

Please sign in to comment.