set numbered parameters using entry::set_numbers

This commit is contained in:
Milan
2020-03-08 14:04:35 +01:00
parent b2dc101354
commit 94d4ca59d8
27 changed files with 136 additions and 207 deletions

38
lib/calcms/entry.pm Normal file
View File

@@ -0,0 +1,38 @@
package entry;
use warnings;
use strict;
sub from_valid($$) {
my ( $params, $attrs ) = @_;
return { map { defined $params->{$_} ? { $_ => $params->{$_} } : () } @$attrs };
}
sub set_numbers($$$) {
my ( $entry, $params, $fields ) = @_;
for my $field (@$fields) {
next unless defined $params->{$field};
if ( $params->{$field} =~ /([\-\d]+)/ ){
$entry->{$field} = $1;
}
}
}
sub set_strings($$$) {
my ( $entry, $params, $attrs ) = @_;
for my $field (@$attrs) {
next unless defined $params->{$field};
$entry->{$field} = $params->{$field};
$entry->{$field} =~ s/^\s+//g;
$entry->{$field} =~ s/\s+$//g;
}
}
sub element_of($$) {
my ( $value, $attrs ) = @_;
return { map { $_ => $_ } @$attrs }->{$value};
}
# do not delete last line
1;

View File

@@ -222,11 +222,9 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
for my $param ( 'id', 'project_id', 'studio_id', 'series_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'series_id'
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -368,11 +368,9 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
for my $param ( 'id', 'project_id', 'studio_id', 'series_id', 'event_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'series_id', 'event_id'
]);
for my $param ('event_ids') {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^[\d,]+$/ ) ) {

View File

@@ -545,13 +545,8 @@ sub check_params {
$checked->{template} =
template::check( $config, $params->{template}, 'upload-audio-recordings' );
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id', 'series_id', 'event_id', 'id' )
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id', 'series_id', 'event_id', 'id']);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -12,6 +12,7 @@ use utf8();
use params();
use config();
use log();
use entry();
use template();
use calendar();
use auth();
@@ -1871,25 +1872,15 @@ sub check_params {
my $template = '';
$checked->{template} = template::check( $config, $params->{template}, 'series' );
my $debug = $params->{debug} || '';
if ( $debug =~ /([a-z\_\,]+)/ ) {
$debug = $1;
}
$checked->{debug} = $debug;
#numeric values
$checked->{part} = 0;
$checked->{list} = 0;
for my $param (
$checked->{open_end} = 1;
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'default_studio_id',
'user_id', 'series_id', 'event_id', 'part',
'list', 'day_start'
)
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
'list', 'day_start', 'open_end'
]);
$checked->{day_start} = $config->{date}->{day_starting_hour}
unless defined $checked->{day_start};
@@ -1909,56 +1900,27 @@ sub check_params {
$checked->{studio_id} = $checked->{default_studio_id};
}
$checked->{open_end} = 1;
for my $param ('open_end') {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
for my $param ('expires') {
$checked->{$param} = time::check_datetime( $params->{$param} );
}
#scalars
$checked->{search} = '';
$checked->{filter} = '';
#$checked->{range}='month';
for my $param ( 'search', 'filter', 'range' ) {
if ( defined $params->{$param} ) {
$checked->{$param} = $params->{$param};
$checked->{$param} =~ s/^\s+//g;
$checked->{$param} =~ s/\s+$//g;
}
}
for my $param ('expires') {
$checked->{$param} = time::check_datetime( $params->{$param} );
}
for my $param ( 'date', 'from_date', 'till_date' ) {
$checked->{$param} = time::check_date( $params->{$param} );
}
for my $param (
entry::set_strings( $checked, $params, [
'search', 'filter', 'range',
'series_name', 'title', 'excerpt', 'content',
'program', 'category', 'image', 'user_content'
)
{
if ( defined $params->{$param} ) {
]);
#$checked->{$param}=uri_unescape();
$checked->{$param} = $params->{$param};
$checked->{$param} =~ s/^\s+//g;
$checked->{$param} =~ s/\s+$//g;
}
}
#actions and roles
if ( defined $params->{action} ) {
if ( $params->{action} =~
/^(add_user|remove_user|delete|save|details|show|edit_event|save_event)$/ )
{
$checked->{action} = $params->{action};
}
}
$checked->{action} = entry::element_of( $params->{action},
[ 'add_user', 'remove_user', 'delete', 'save', 'details', 'show', 'edit_event', 'save_event' ]
);
return $checked;
}

View File

@@ -260,12 +260,9 @@ sub check_params {
}
$checked->{template} = $template;
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id']);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -219,11 +219,8 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
$checked->{duration} = 28;
for my $param ( 'id', 'project_id', 'studio_id', 'duration' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'duration']);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -254,13 +254,9 @@ sub check_params {
$checked->{debug} = $debug;
#numeric values
for
my $param ( 'id', 'project_id', 'studio_id', 'default_studio_id', 'user_id', 'series_id', 'event_id', 'v1', 'v2' )
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'default_studio_id', 'user_id', 'series_id', 'event_id', 'v1', 'v2' )
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -12,6 +12,7 @@ use Encode::Locale();
use params();
use config();
use log();
use entry();
use template();
use db();
use auth();
@@ -807,17 +808,11 @@ sub check_params {
}
$checked->{debug} = $debug;
#numeric values
for my $param (
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'default_studio_id',
'user_id', 'series_id', 'event_id', 'source_event_id',
'episode'
)
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -628,11 +628,9 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
for my $param ( 'id', 'project_id', 'studio_id', 'default_studio_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'default_studio_id' ]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -371,9 +371,9 @@ sub check_params {
$checked->{template} = template::check( $config, $params->{template}, 'image-upload' );
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id' ) {
$checked->{$param} = $params->{$param} if ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ );
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id'
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -418,11 +418,10 @@ sub check_params {
#numeric values
$checked->{limit} = 100;
for my $param ( 'project_id', 'studio_id', 'series_id', 'event_id', 'pid', 'default_studio_id', 'limit' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'series_id', 'event_id', 'pid', 'default_studio_id', 'limit'
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -248,12 +248,9 @@ sub check_params {
}
$checked->{debug} = $debug;
#numeric values
for my $param ( 'event_id', 'project_id', 'studio_id', 'default_studio_id', 'user_id', 'series_id', 'duration' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'event_id', 'project_id', 'studio_id', 'default_studio_id', 'user_id', 'series_id', 'duration'
]);
for my $param ( 'subject', 'to', 'cc', 'content' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /\S/ ) ) {

View File

@@ -131,11 +131,8 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
for my $param ( 'project_id', 'studio_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id']);
#dates
for my $param ('start_date') {

View File

@@ -291,12 +291,9 @@ sub check_params {
}
}
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id', 'pid', 'sid' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id', 'pid', 'sid']);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -441,12 +441,10 @@ sub check_params {
}
}
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id'
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -8,6 +8,7 @@ use Data::Dumper;
use params();
use config();
use entry();
use log();
use template();
use auth();
@@ -130,12 +131,9 @@ sub check_params {
}
$checked->{debug} = $debug;
#numeric values
for my $param ( 'id', 'project_id', 'studio_id', 'series_id', 'event_id', 'p_id', 's_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^[\-\d]+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'series_id', 'event_id', 'p_id', 's_id'
]);
for my $param ( 'selectProjectStudio', 'selectSeries', 'selectRange' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} eq '1' ) ) {

View File

@@ -8,6 +8,7 @@ use Data::Dumper;
use params();
use config();
use entry();
use log();
use template();
use auth();
@@ -120,12 +121,9 @@ sub check_params {
}
$checked->{debug} = $debug;
#numeric values
for my $param ( 'id', 'project_id', 'studio_id', 'series_id', 'p_id', 's_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^[\-\d]+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'series_id', 'p_id', 's_id'
]);
for my $param ( 'selectProjectStudio', 'selectSeries', 'selectRange' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} eq '1' ) ) {

View File

@@ -11,6 +11,7 @@ use Encode();
use utf8();
use params();
use config();
use entry();
use log();
use template();
use auth();
@@ -1432,7 +1433,7 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
$checked->{action} = $params->{action};
for my $param (
entry::set_numbers( $checked, $params, [
'id', 'project_id',
'studio_id', 'default_studio_id',
'user_id', 'new_series_id',
@@ -1441,12 +1442,8 @@ sub check_params {
'event_id', 'weekday',
'week_of_month', 'month',
'nextDay', 'predecessor_id'
)
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {
@@ -1465,15 +1462,11 @@ sub check_params {
$checked->{create_events} = 0;
$checked->{publish_events} = 0;
}
for my $param (
entry::set_numbers( $checked, $params, [
'frequency', 'duration', 'default_duration', 'create_events',
'publish_events', 'live', 'count_episodes'
)
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /(\d+)/ ) ) {
$checked->{$param} = $1;
}
}
]);
#scalars
for my $param ( 'search', 'from', 'till', 'period_type' ) {

View File

@@ -8,6 +8,7 @@ use Data::Dumper;
use params();
use config();
use entry();
use log();
use template();
use auth();
@@ -121,11 +122,9 @@ sub check_params {
}
#numeric values
for my $param ( 'project_id', 'studio_id', 'series_id', 'event_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^[\-\d]+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'series_id', 'event_id'
])
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -139,13 +139,9 @@ sub check_params {
$checked->{error} = '';
$checked->{template} = template::check( $config, $params->{template}, 'show-playout' );
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id', 'series_id', 'event_id', 'id' )
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id', 'series_id', 'event_id', 'id'
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};

View File

@@ -373,11 +373,9 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
for my $param ( 'id', 'project_id', 'studio_id', 'default_studio_id', 'schedule_id', 'schedule_studio_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'id', 'project_id', 'studio_id', 'default_studio_id', 'schedule_id', 'schedule_studio_id'
]);
if ( ( defined $params->{show_date} ) && ( $params->{show_date} =~ /^(\d\d\d\d)/ ) ) {
$checked->{show_date} = $1;

View File

@@ -235,12 +235,10 @@ sub check_params {
}
}
#numeric values
for my $param ( 'project_id', 'studio_id', 'default_studio_id', 'id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id', 'id'
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -221,13 +221,10 @@ sub check_params {
$checked->{template} = $template;
#numeric values
for my $param ( 'project_id', 'default_studio_id', 'studio_id', 'default_studio',
'default_project' )
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'default_studio_id', 'studio_id', 'default_studio',
'default_project' ]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -124,12 +124,9 @@ sub check_params {
}
}
#numeric values
for my $param ( 'project_id', 'default_studio_id', 'studio_id', 'series_id' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'default_studio_id', 'studio_id', 'series_id']);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -399,12 +399,9 @@ sub check_params {
$template = template::check( $config, $params->{template}, 'users' );
$checked->{template} = $template;
#numeric values
for my $param ( 'project_id', 'user_id', 'default_studio_id', 'studio_id', 'disabled' ) {
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
entry::set_numbers( $checked, $params, [
'project_id', 'user_id', 'default_studio_id', 'studio_id', 'disabled']);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {

View File

@@ -280,15 +280,11 @@ sub check_params {
#numeric values
$checked->{exclude} = 0;
for my $param (
entry::set_numbers( $checked, $params, [
'project_id', 'studio_id', 'default_studio_id', 'schedule_id',
'exclude', 'show_hint_to_add_schedule', 'weekday week_of_month', 'month'
)
{
if ( ( defined $params->{$param} ) && ( $params->{$param} =~ /^\d+$/ ) ) {
$checked->{$param} = $params->{$param};
}
}
]);
if ( defined $checked->{studio_id} ) {
$checked->{default_studio_id} = $checked->{studio_id};
} else {