+sub get_errors { (@fatal_errors, @errors) }
+sub get_debug {
+ return unless $debug && @debug;
+ return @debug;
+}
+sub get_msgs { @msgs };
+sub get_hints { @hints };
+sub get_notes { @notes };
+sub get_all_messages {
+ return {
+ errors => [ @fatal_errors, @errors ],
+ debugs => $debug ? \@debug : [],
+ msgs => \@msgs,
+ hints => \@hints,
+ notes => \@notes,
+ };
+}
+
+our $USE_PAGED_MODE = 1;
+use constant DEFAULT_PAGE => 1;
+use constant DEFAULT_RES_PER_PAGE => 50;
+our %page_params = ( page => { default => DEFAULT_PAGE,
+ match => '(\d+)' },
+ number => { default => DEFAULT_RES_PER_PAGE,
+ match => '(\d+)' } );
+
+sub parse_params {
+ my ( $cgi, $params_def, $opts ) = @_;
+
+ my %params_ret = ( values => {}, errors => {} );
+ my %params;
+ if ($USE_PAGED_MODE) {
+ debug( "Use PAGED_MODE", 2 ) if DEBUG;
+ %params = %$params_def;
+ foreach (keys %page_params) {
+ delete $params{$_};
+ }
+ %params = ( %params, %page_params );
+ } else {
+ %params = %$params_def;
+ }
+
+ foreach my $param ( keys %params ) {
+
+ debug( "Param <strong>$param</strong>", 2 ) if DEBUG;
+
+ my $p_value_orig = $cgi->param($param);
+
+ if (!defined($p_value_orig)
+ && defined $params_def->{$param}{alias}
+ && defined $cgi->param($params_def->{$param}{alias})) {
+ $p_value_orig = $cgi->param($params_def->{$param}{alias});
+ debug( "Used alias <strong>$params_def->{$param}{alias}</strong>",
+ 2 );
+ }
+
+ my @p_value = ($p_value_orig);
+
+ debug( "Value (Orig) ".($p_value_orig||""), 2 ) if DEBUG;
+
+ if ($params_def->{$param}{array} && defined $p_value_orig) {
+ @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
+ debug( "Value (Array Split) ". join('##',@p_value), 2 ) if DEBUG;
+ }
+
+ if ($params_def->{$param}{match} && defined $p_value_orig) {
+ @p_value = map
+ { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
+ @p_value;
+ }
+ @p_value = grep { defined $_ } @p_value;
+
+ debug( "Value (Match) ". join('##',@p_value), 2 ) if DEBUG;
+
+ unless (@p_value) {
+ if (defined $params{$param}{default}) {
+ @p_value = ($params{$param}{default});
+ } else {
+ @p_value = undef;
+ $params_ret{errors}{$param} = "undef";
+ next;
+ }
+ }
+
+ debug( "Value (Default) ". join('##',@p_value), 2 ) if DEBUG;
+ my @p_value_no_replace = @p_value;
+
+ if ($params{$param}{replace} && @p_value) {
+ foreach my $pattern (keys %{$params{$param}{replace}}) {
+ my @p_value_tmp = @p_value;
+ @p_value = ();
+ foreach (@p_value_tmp) {
+ if ($_ eq $pattern) {
+ my $replacement = $params{$param}{replace}{$_};
+ if (ref $replacement) {
+ push @p_value, @$replacement;
+ } else {
+ push @p_value, $replacement;
+ }
+ } else {
+ push @p_value, $_;
+ }
+ }
+ }
+ }
+
+ debug( "Value (Final) ". join('##',@p_value), 2 ) if DEBUG;
+
+ if ($params_def->{$param}{array}) {
+ $params_ret{values}{$param} = {
+ orig => $p_value_orig,
+ no_replace => \@p_value_no_replace,
+ final => \@p_value,
+ };
+ @{$params_def->{$param}{var}} = @p_value
+ if $params_def->{$param}{var};
+ } else {
+ $params_ret{values}{$param} = {
+ orig => $p_value_orig,
+ no_replace => $p_value_no_replace[0],
+ final => $p_value[0],
+ };
+ ${$params_def->{$param}{var}} = $p_value[0]
+ if $params_def->{$param}{var};
+ }
+ $opts->{$param} = $params_ret{values}{$param}{final} if $opts;