From 5088d6106904a2626c000747b1e28fc6692bd76b Mon Sep 17 00:00:00 2001 From: Martin Kamerbeek Date: Thu, 9 Dec 2004 17:51:35 +0000 Subject: [PATCH] Added user selectable subscription code length --- lib/WebGUI/Operation/Subscription.pm | 27 ++++++++++++++++++++----- lib/WebGUI/i18n/English/Subscription.pm | 10 +++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/WebGUI/Operation/Subscription.pm b/lib/WebGUI/Operation/Subscription.pm index 75b025edf..3c41967e5 100644 --- a/lib/WebGUI/Operation/Subscription.pm +++ b/lib/WebGUI/Operation/Subscription.pm @@ -16,6 +16,19 @@ use WebGUI::Template; use WebGUI::Form; use WebGUI::International; +#------------------------------------------------------------------- +sub _generateCode { + my ($codeLength, @codeElements, $code, $i); + $codeLength = shift || 64; + @codeElements = ('A'..'Z', 'a'..'z', 0..9, '-'); + + for ($i=0; $i < $codeLength; $i++) { + $code .= $codeElements[rand(63)]; + } + + return $code; +} + #------------------------------------------------------------------- sub _submenu { my $i18n = WebGUI::International->new("Subscription"); @@ -56,6 +69,11 @@ sub www_createSubscriptionCodeBatch { -label => $i18n->get('noc'), -value => $session{form}{noc} || 1 ); + $f->integer( + -name => 'codeLength', + -label => $i18n->get('code length'), + -value => $session{form}{codeLength} || 64 + ); $f->interval( -name => 'expires', -label => $i18n->get('codes expire'), @@ -94,19 +112,18 @@ sub www_createSubscriptionCodeBatchSave { push(@error, $i18n->get('no description error')) unless ($description); push(@error, $i18n->get('no association error')) unless ($session{form}{subscriptionId}); + push(@error, $i18n->get('code length error')) unless ($session{form}{codeLength} >= 10 && $session{form}{codeLength} <= 64 && $session{form}{codeLength} =~ m/^\d\d$/); + return www_createSubscriptionCodeBatch(\@error) if (@error); $creationEpoch = time(); WebGUI::SQL->write("insert into subscriptionCodeBatch (batchId, description) values (". quote($batchId).", ".quote($description).")"); - @codeElements = ('A'..'Z', 'a'..'z', 0..9, '-'); for ($currentCode=0; $currentCode < $numberOfCodes; $currentCode++) { - $code = ""; - for ($i=0; $i < 64; $i++) { - $code .= $codeElements[rand(63)]; - } + $code = _generateCode($session{form}{codeLength}); + $code = _generateCode($session{form}{codeLength}) while (WebGUI::SQL->quickArray("select code from subscriptionCode where code=".quote($code))); WebGUI::SQL->write("insert into subscriptionCode (batchId, code, status, dateCreated, dateUsed, expires, usedBy)". " values (".quote($batchId).",".quote($code).", 'Unused', ".quote($creationEpoch).", 0, ".quote($expires).", 0)"); diff --git a/lib/WebGUI/i18n/English/Subscription.pm b/lib/WebGUI/i18n/English/Subscription.pm index 6c8bd052e..03920f1f7 100755 --- a/lib/WebGUI/i18n/English/Subscription.pm +++ b/lib/WebGUI/i18n/English/Subscription.pm @@ -369,6 +369,16 @@ The form in which the user can enter his subscription code.
|, lastUpdated => 1101754848, context => q|The title of the help page of the code redemption template.| }, + 'code length' => { + message => q|Subscription code length|, + lastUpdated => 0, + context => q|The label of the formfield in which the length of a subscription code is entered.| + }, + 'code length error' => { + message => q|You must enter a subscription code length between 10 and 64 (border values included).|, + lastUpdated => 0, + context => q|The error message that shows up when a wrong code length is specified.| + }, };