{type => 'csv', desc => 'CSV File'},
];
+sub _build_query {
+ my ( $params, $table ) = @_;
+ my @fields = exists $params->{fields} ? @{ $params->{fields} } : ();
+ my $query = "SELECT " . ( @fields ? join(', ', @fields ) : '*' ) . " FROM $table";
+ my @where_args;
+ if ( exists $params->{filters} ) {
+ $query .= ' WHERE 1 ';
+ while ( my ( $field, $values ) = each %{ $params->{filters} } ) {
+ if ( ref( $values ) ) {
+ $query .= " AND $field IN ( " . ( ('?') x scalar( @$values ) ) . " ) ";
+ push @where_args, @$values;
+ } else {
+ $query .= " AND $field = ? ";
+ push @where_args, $values;
+ }
+ }
+ }
+ $query .= (exists $params->{orderby} ? " ORDER BY $params->{orderby} " : '');
+ return ( $query, @where_args );
+}
+
=head2 C4::Creators::Lib::get_all_templates()
my $templates = get_all_templates();
=cut
sub get_all_templates {
- my %params = @_;
+ my ( $params ) = @_;
my @templates = ();
- my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_templates";
- $query .= ($params{'filter'} ? " WHERE $params{'filter'} " : '');
- $query .= ($params{'orderby'} ? " ORDER BY $params{'orderby'} " : '');
+ my ( $query, @where_args ) = _build_query( $params, 'creator_templates' );
my $sth = C4::Context->dbh->prepare($query);
- $sth->execute();
+ $sth->execute( @where_args );
if ($sth->err) {
warn sprintf('Database returned the following error: %s', $sth->errstr);
return -1;
=cut
sub get_all_layouts {
- my %params = @_;
+ my ( $params ) = @_;
my @layouts = ();
- my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_layouts";
- $query .= ($params{'filter'} ? " WHERE $params{'filter'} " : '');
- $query .= ($params{'orderby'} ? " ORDER BY $params{'orderby'} " : '');
+ my ( $query, @where_args ) = _build_query( $params, 'creator_layouts' );
my $sth = C4::Context->dbh->prepare($query);
- $sth->execute();
+ $sth->execute( @where_args );
if ($sth->err) {
warn sprintf('Database returned the following error: %s', $sth->errstr);
return -1;
my $profiles = get_all_profiles();
- my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
+ my $profiles = get_all_profiles({ fields => [@fields], filters => { filters => [$value1, $value2] } });
This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
to the Apache log. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
=cut
sub get_all_profiles {
- my %params = @_;
+ my ( $params ) = @_;
my @profiles = ();
- my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM printers_profile";
- $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
+ my ( $query, @where_args ) = _build_query( $params, 'printers_profile' );
my $sth = C4::Context->dbh->prepare($query);
-# $sth->{'TraceLevel'} = 3 if $debug;
- $sth->execute();
+ $sth->execute( @where_args );
if ($sth->err) {
warn sprintf('Database returned the following error: %s', $sth->errstr);
return -1;
=cut
sub get_batch_summary {
- my %params = @_;
+ my ( $params ) = @_;
my @batches = ();
- my $query = "SELECT batch_id,count(batch_id) as _item_count FROM creator_batches WHERE creator=?";
- $query .= ($params{'filter'} ? " AND $params{'filter'}" : '');
+ $params->{fields} = ['batch_id', 'count(batch_id) as _item_count'];
+ my ( $query, @where_args ) = _build_query( $params, 'creator_batches' );
$query .= " GROUP BY batch_id";
my $sth = C4::Context->dbh->prepare($query);
-# $sth->{'TraceLevel'} = 3;
- $sth->execute($params{'creator'});
+ $sth->execute( @where_args );
if ($sth->err) {
warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
return -1;
if ($op eq 'edit') {
$profile = C4::Labels::Profile->retrieve(profile_id => $profile_id);
- $template_list = get_all_templates(table_name => 'creator_templates', field_list => 'template_id,template_code, profile_id');
+ $template_list = get_all_templates( fields => [ qw( template_id template_code profile_id) ] );
}
elsif ($op eq 'save') {
my @params = (
if ($op eq 'edit') {
$label_template = C4::Labels::Template->retrieve(template_id => $template_id);
- $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin',filter => "template_id=$template_id OR template_id=''");
+ $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [ $template_id, '' ] } } );
push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
foreach my $profile (@$profile_list) {
if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
}
else { # if we get here, this is a new layout
$label_template = C4::Labels::Template->new();
- $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin',filter => "template_id=''");
+ $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [''] } });
push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
foreach my $profile (@$profile_list) {
if ($profile->{'profile_id'} == 0) {
else {} # FIXME: Some error trapping code
}
-if ($label_element eq 'layout') {$db_rows = get_all_layouts(table_name => 'creator_layouts', filter => 'creator=\'Labels\'');}
-elsif ($label_element eq 'template') {$db_rows = get_all_templates(table_name => 'creator_templates', filter => 'creator=\'Labels\'');}
-elsif ($label_element eq 'profile') {$db_rows = get_all_profiles(table_name => 'printers_profile', filter => 'creator=\'Labels\'');}
-elsif ($label_element eq 'batch') {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\' OR branch_code=\'NB\'", creator => 'Labels');}
+if ($label_element eq 'layout') {$db_rows = get_all_layouts( { filters => { creator => 'Labels' } });}
+elsif ($label_element eq 'template') {$db_rows = get_all_templates( { filters => { creator => 'Labels' } });}
+elsif ($label_element eq 'profile') {$db_rows = get_all_profiles( { filters => { creator => 'Labels' } });}
+elsif ($label_element eq 'batch') {$db_rows = get_batch_summary( { filters => { branch_code => [$branch_code, 'NB'], creator => 'Labels' } });}
else {} # FIXME: Some error trapping code
my $table = html_table($display_columns->{$label_element}, $db_rows);
@batch_ids = map{{batch_id => $_}} @batch_ids;
@label_ids = map{{label_id => $_}} @label_ids;
@item_numbers = map{{item_number => $_}} @item_numbers;
- $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Labels"', orderby => 'template_code' );
- $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Labels"', orderby => 'layout_name' );
+ $templates = get_all_templates( { fields => [ qw( template_id template_code ) ], filters => { creator => "Labels" }, orderby => 'template_code' } );
+ $layouts = get_all_layouts( { fields => [ qw( layout_id layout_name ) ], filters => { creator => "Labels" }, orderby => 'layout_name' } );
$output_formats = get_output_formats();
$template->param(
batch_ids => \@batch_ids,
if ($op eq 'edit') {
$profile = C4::Patroncards::Profile->retrieve(profile_id => $profile_id);
- $template_list = get_all_templates(table_name => 'creator_templates', field_list => 'template_id,template_code, profile_id');
+ $template_list = get_all_templates({ fields => [ qw( template_id template_code profile_id ) ] });
}
elsif ($op eq 'save') {
my @params = (
if ($op eq 'edit') {
$card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
- $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin', filter => "template_id=$template_id OR template_id=''");
+ $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => {template_id => [ $template_id, '' ]} } );
}
elsif ($op eq 'save') {
my @params = ( profile_id => scalar $cgi->param('profile_id') || '',
exit;
}
elsif ($op eq 'none') {
- if ($card_element eq 'layout') {$db_rows = get_all_layouts(table_name => 'creator_layouts', filter => 'creator=\'Patroncards\'');}
- elsif ($card_element eq 'template') {$db_rows = get_all_templates(table_name => 'creator_templates', filter => 'creator=\'Patroncards\'');}
- elsif ($card_element eq 'profile') {$db_rows = get_all_profiles(table_name => 'printers_profile', filter => 'creator=\'Patroncards\'');}
- elsif ($card_element eq 'batch') {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\' OR branch_code=\'NB\'", creator => 'Patroncards');}
+ if ($card_element eq 'layout') {$db_rows = get_all_layouts( { filters => { creator => 'Patroncards' } });}
+ elsif ($card_element eq 'template') {$db_rows = get_all_templates( { filters => { creator => 'Patroncards' } });}
+ elsif ($card_element eq 'profile') {$db_rows = get_all_profiles( { filters => { creator => 'Patroncards' } });}
+ elsif ($card_element eq 'batch') {$db_rows = get_batch_summary( { filters => { branch_code => [ $branch_code, 'NB' ], creator => 'Patroncards' } });}
else {warn sprintf("Unknown card element passed in: %s.",$card_element); $errstr = 202;}
}
else { # trap unsupported operations here
@batch_ids = grep{$_ = {batch_id => $_}} @batch_ids;
@label_ids = grep{$_ = {label_id => $_}} @label_ids;
@borrower_numbers = grep{$_ = {borrower_number => $_}} @borrower_numbers;
- $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Patroncards"');
- $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Patroncards"');
+ $templates = get_all_templates( { fields => [qw( template_id template_code ) ], filters => { creator => "Patroncards" } });
+ $layouts = get_all_layouts({ fields => [ qw( layout_id layout_name ) ], filters => { creator => "Patroncards" } });
$output_formats = get_output_formats();
$template->param(
batch_ids => \@batch_ids,
is( $templates->[1]->{creator}, 'Labels', 'creator is good' );
# With field_list params --------------
-$templates = get_all_templates( field_list => 'units, cols, rows' );
+$templates = get_all_templates( {fields=> [qw(units cols rows)] } );
$query = '
SELECT count(*)
is( $layouts->[1]->{creator}, 'Labels', 'creator is good' );
# With field_list params --------------
-$layouts = get_all_layouts( field_list => 'barcode_type, layout_name, font' );
+$layouts = get_all_layouts( { fields => [qw(barcode_type layout_name font)] });
$query = '
SELECT count(*)
is( $profiles->[1]->{creator}, 'Labels', 'creator is good' );
# With field_list params --------------
-$profiles = get_all_profiles( field_list => 'printer_name, template_id' );
+$profiles = get_all_profiles( { fields => [qw(printer_name template_id)] });
$query = '
SELECT count(*)
isnt( exists $profiles->[1]->{creator}, 'Labels', 'creator is good' );
# With filter params ------------------
-$profiles = get_all_profiles( filter => 'template_id = 1235' );
+$profiles = get_all_profiles( filters => { template_id => 1235 } );
$query = '
SELECT count(*)