View | Details | Raw Unified | Return to bug 17900
Collapse All | Expand All

(-)a/C4/Creators/Lib.pm (-21 / +35 lines)
Lines 142-147 my $output_formats = [ Link Here
142
    {type       => 'csv',       desc    => 'CSV File'},
142
    {type       => 'csv',       desc    => 'CSV File'},
143
];
143
];
144
144
145
sub _build_query {
146
    my ( $params, $table ) = @_;
147
    my @fields = exists $params->{fields} ? @{ $params->{fields} } : ();
148
    my $query = "SELECT " . ( @fields ? join(', ', @fields ) : '*' ) . " FROM $table";
149
    my @where_args;
150
    if ( exists $params->{filters} ) {
151
        $query .= ' WHERE 1 ';
152
        while ( my ( $field, $values ) = each %{ $params->{filters} } ) {
153
            if ( ref( $values ) ) {
154
                $query .= " AND $field IN ( " . ( ('?') x scalar( @$values ) ) . " ) ";
155
                push @where_args, @$values;
156
            } else {
157
                $query .= " AND $field = ? ";
158
                push @where_args, $values;
159
            }
160
        }
161
    }
162
    $query .= (exists $params->{orderby} ? " ORDER BY $params->{orderby} " : '');
163
    return ( $query, @where_args );
164
}
165
145
=head2 C4::Creators::Lib::get_all_templates()
166
=head2 C4::Creators::Lib::get_all_templates()
146
167
147
  my $templates = get_all_templates();
168
  my $templates = get_all_templates();
Lines 151-163 This function returns a reference to a hash containing all templates upon succes Link Here
151
=cut
172
=cut
152
173
153
sub get_all_templates {
174
sub get_all_templates {
154
    my %params = @_;
175
    my ( $params ) = @_;
155
    my @templates = ();
176
    my @templates = ();
156
    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_templates";
177
    my ( $query, @where_args ) = _build_query( $params, 'creator_templates' );
157
    $query .= ($params{'filter'} ? " WHERE $params{'filter'} " : '');
158
    $query .= ($params{'orderby'} ? " ORDER BY $params{'orderby'} " : '');
159
    my $sth = C4::Context->dbh->prepare($query);
178
    my $sth = C4::Context->dbh->prepare($query);
160
    $sth->execute();
179
    $sth->execute( @where_args );
161
    if ($sth->err) {
180
    if ($sth->err) {
162
        warn sprintf('Database returned the following error: %s', $sth->errstr);
181
        warn sprintf('Database returned the following error: %s', $sth->errstr);
163
        return -1;
182
        return -1;
Lines 178-190 This function returns a reference to a hash containing all layouts upon success Link Here
178
=cut
197
=cut
179
198
180
sub get_all_layouts {
199
sub get_all_layouts {
181
    my %params = @_;
200
    my ( $params ) = @_;
182
    my @layouts = ();
201
    my @layouts = ();
183
    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM creator_layouts";
202
    my ( $query, @where_args ) = _build_query( $params, 'creator_layouts' );
184
    $query .= ($params{'filter'} ? " WHERE $params{'filter'} " : '');
185
    $query .= ($params{'orderby'} ? " ORDER BY $params{'orderby'} " : '');
186
    my $sth = C4::Context->dbh->prepare($query);
203
    my $sth = C4::Context->dbh->prepare($query);
187
    $sth->execute();
204
    $sth->execute( @where_args );
188
    if ($sth->err) {
205
    if ($sth->err) {
189
        warn sprintf('Database returned the following error: %s', $sth->errstr);
206
        warn sprintf('Database returned the following error: %s', $sth->errstr);
190
        return -1;
207
        return -1;
Lines 200-206 sub get_all_layouts { Link Here
200
217
201
  my $profiles = get_all_profiles();
218
  my $profiles = get_all_profiles();
202
219
203
  my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
220
  my $profiles = get_all_profiles({ fields => [@fields], filters => { filters => [$value1, $value2] } });
204
221
205
This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
222
This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
206
to the Apache log. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
223
to the Apache log. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
Lines 211-223 NOTE: Do not pass in the keyword 'WHERE.' Link Here
211
=cut
228
=cut
212
229
213
sub get_all_profiles {
230
sub get_all_profiles {
214
    my %params = @_;
231
    my ( $params ) = @_;
215
    my @profiles = ();
232
    my @profiles = ();
216
    my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM printers_profile";
233
    my ( $query, @where_args ) = _build_query( $params, 'printers_profile' );
217
    $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
218
    my $sth = C4::Context->dbh->prepare($query);
234
    my $sth = C4::Context->dbh->prepare($query);
219
#    $sth->{'TraceLevel'} = 3 if $debug;
235
    $sth->execute( @where_args );
220
    $sth->execute();
221
    if ($sth->err) {
236
    if ($sth->err) {
222
        warn sprintf('Database returned the following error: %s', $sth->errstr);
237
        warn sprintf('Database returned the following error: %s', $sth->errstr);
223
        return -1;
238
        return -1;
Lines 262-275 NOTE: Do not pass in the keyword 'WHERE.' Link Here
262
=cut
277
=cut
263
278
264
sub get_batch_summary {
279
sub get_batch_summary {
265
    my %params = @_;
280
    my ( $params ) = @_;
266
    my @batches = ();
281
    my @batches = ();
267
    my $query = "SELECT batch_id,count(batch_id) as _item_count FROM creator_batches WHERE creator=?";
282
    $params->{fields} = ['batch_id', 'count(batch_id) as _item_count'];
268
    $query .= ($params{'filter'} ? " AND $params{'filter'}" : '');
283
    my ( $query, @where_args ) = _build_query( $params, 'creator_batches' );
269
    $query .= " GROUP BY batch_id";
284
    $query .= " GROUP BY batch_id";
270
    my $sth = C4::Context->dbh->prepare($query);
285
    my $sth = C4::Context->dbh->prepare($query);
271
#    $sth->{'TraceLevel'} = 3;
286
    $sth->execute( @where_args );
272
    $sth->execute($params{'creator'});
273
    if ($sth->err) {
287
    if ($sth->err) {
274
        warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
288
        warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
275
        return -1;
289
        return -1;
(-)a/labels/label-edit-profile.pl (-1 / +1 lines)
Lines 50-56 my $units = get_unit_values(); Link Here
50
50
51
if ($op eq 'edit') {
51
if ($op eq 'edit') {
52
    $profile = C4::Labels::Profile->retrieve(profile_id => $profile_id);
52
    $profile = C4::Labels::Profile->retrieve(profile_id => $profile_id);
53
    $template_list = get_all_templates(table_name => 'creator_templates', field_list => 'template_id,template_code, profile_id');
53
    $template_list = get_all_templates( fields => [ qw( template_id template_code profile_id) ] );
54
}
54
}
55
elsif ($op eq 'save') {
55
elsif ($op eq 'save') {
56
    my @params = (
56
    my @params = (
(-)a/labels/label-edit-template.pl (-2 / +2 lines)
Lines 49-55 my $units = get_unit_values(); Link Here
49
49
50
if ($op eq 'edit') {
50
if ($op eq 'edit') {
51
    $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
51
    $label_template = C4::Labels::Template->retrieve(template_id => $template_id);
52
    $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin',filter => "template_id=$template_id OR template_id=''");
52
    $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [ $template_id, '' ] } } );
53
    push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
53
    push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
54
    foreach my $profile (@$profile_list) {
54
    foreach my $profile (@$profile_list) {
55
        if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
55
        if ($profile->{'profile_id'} == $label_template->get_attr('profile_id')) {
Lines 116-122 elsif ($op eq 'save') { Link Here
116
}
116
}
117
else {  # if we get here, this is a new layout
117
else {  # if we get here, this is a new layout
118
    $label_template = C4::Labels::Template->new();
118
    $label_template = C4::Labels::Template->new();
119
    $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin',filter => "template_id=''");
119
    $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => { template_id => [''] } });
120
    push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
120
    push @$profile_list, {paper_bin => 'N/A', profile_id => 0, printer_name => 'No Profile'};
121
    foreach my $profile (@$profile_list) {
121
    foreach my $profile (@$profile_list) {
122
        if ($profile->{'profile_id'} == 0) {
122
        if ($profile->{'profile_id'} == 0) {
(-)a/labels/label-manage.pl (-4 / +4 lines)
Lines 87-96 if ($op eq 'delete') { Link Here
87
    else                                        {}      # FIXME: Some error trapping code
87
    else                                        {}      # FIXME: Some error trapping code
88
}
88
}
89
89
90
if      ($label_element eq 'layout')    {$db_rows = get_all_layouts(table_name => 'creator_layouts', filter => 'creator=\'Labels\'');}
90
if      ($label_element eq 'layout')    {$db_rows = get_all_layouts( { filters => { creator => 'Labels' } });}
91
elsif   ($label_element eq 'template')  {$db_rows = get_all_templates(table_name => 'creator_templates', filter => 'creator=\'Labels\'');}
91
elsif   ($label_element eq 'template')  {$db_rows = get_all_templates( { filters => { creator => 'Labels' } });}
92
elsif   ($label_element eq 'profile')   {$db_rows = get_all_profiles(table_name => 'printers_profile', filter => 'creator=\'Labels\'');}
92
elsif   ($label_element eq 'profile')   {$db_rows = get_all_profiles( { filters => { creator => 'Labels' } });}
93
elsif   ($label_element eq 'batch')     {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\' OR branch_code=\'NB\'", creator => 'Labels');}
93
elsif   ($label_element eq 'batch')     {$db_rows = get_batch_summary( { filters => { branch_code => [$branch_code, 'NB'], creator => 'Labels' } });}
94
else                                    {}      # FIXME: Some error trapping code
94
else                                    {}      # FIXME: Some error trapping code
95
95
96
my $table = html_table($display_columns->{$label_element}, $db_rows);
96
my $table = html_table($display_columns->{$label_element}, $db_rows);
(-)a/labels/label-print.pl (-2 / +2 lines)
Lines 115-122 elsif ($op eq 'none') { Link Here
115
    @batch_ids = map{{batch_id => $_}} @batch_ids;
115
    @batch_ids = map{{batch_id => $_}} @batch_ids;
116
    @label_ids = map{{label_id => $_}} @label_ids;
116
    @label_ids = map{{label_id => $_}} @label_ids;
117
    @item_numbers = map{{item_number => $_}} @item_numbers;
117
    @item_numbers = map{{item_number => $_}} @item_numbers;
118
    $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Labels"', orderby => 'template_code' );
118
    $templates = get_all_templates( { fields => [ qw( template_id template_code ) ], filters => { creator => "Labels" }, orderby => 'template_code' } );
119
    $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Labels"', orderby => 'layout_name' );
119
    $layouts = get_all_layouts( { fields => [ qw( layout_id layout_name ) ], filters => { creator => "Labels" }, orderby => 'layout_name' } );
120
    $output_formats = get_output_formats();
120
    $output_formats = get_output_formats();
121
    $template->param(
121
    $template->param(
122
                    batch_ids                   => \@batch_ids,
122
                    batch_ids                   => \@batch_ids,
(-)a/patroncards/edit-profile.pl (-1 / +1 lines)
Lines 50-56 my $units = get_unit_values(); Link Here
50
50
51
if ($op eq 'edit') {
51
if ($op eq 'edit') {
52
    $profile = C4::Patroncards::Profile->retrieve(profile_id => $profile_id);
52
    $profile = C4::Patroncards::Profile->retrieve(profile_id => $profile_id);
53
    $template_list = get_all_templates(table_name => 'creator_templates', field_list => 'template_id,template_code, profile_id');
53
    $template_list = get_all_templates({ fields => [ qw( template_id template_code profile_id ) ] });
54
}
54
}
55
elsif ($op eq 'save') {
55
elsif ($op eq 'save') {
56
    my @params = (
56
    my @params = (
(-)a/patroncards/edit-template.pl (-1 / +1 lines)
Lines 50-56 my $units = get_unit_values(); Link Here
50
50
51
if ($op eq 'edit') {
51
if ($op eq 'edit') {
52
    $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
52
    $card_template = C4::Patroncards::Template->retrieve(template_id => $template_id);
53
    $profile_list = get_all_profiles(field_list => 'profile_id,printer_name,paper_bin', filter => "template_id=$template_id OR template_id=''");
53
    $profile_list = get_all_profiles({ fields => [ qw( profile_id printer_name paper_bin ) ], filters => {template_id => [ $template_id, '' ]} } );
54
}
54
}
55
elsif ($op eq 'save') {
55
elsif ($op eq 'save') {
56
    my @params = (      profile_id      => scalar $cgi->param('profile_id') || '',
56
    my @params = (      profile_id      => scalar $cgi->param('profile_id') || '',
(-)a/patroncards/manage.pl (-4 / +4 lines)
Lines 93-102 if ($op eq 'delete') { Link Here
93
    exit;
93
    exit;
94
}
94
}
95
elsif ($op eq 'none') {
95
elsif ($op eq 'none') {
96
    if      ($card_element eq 'layout')    {$db_rows = get_all_layouts(table_name => 'creator_layouts', filter => 'creator=\'Patroncards\'');}
96
    if      ($card_element eq 'layout')    {$db_rows = get_all_layouts( { filters => { creator => 'Patroncards' } });}
97
    elsif   ($card_element eq 'template')  {$db_rows = get_all_templates(table_name => 'creator_templates', filter => 'creator=\'Patroncards\'');}
97
    elsif   ($card_element eq 'template')  {$db_rows = get_all_templates( { filters => { creator => 'Patroncards' } });}
98
    elsif   ($card_element eq 'profile')   {$db_rows = get_all_profiles(table_name => 'printers_profile', filter => 'creator=\'Patroncards\'');}
98
    elsif   ($card_element eq 'profile')   {$db_rows = get_all_profiles( { filters => { creator => 'Patroncards' } });}
99
    elsif   ($card_element eq 'batch')     {$db_rows = get_batch_summary(filter => "branch_code=\'$branch_code\' OR branch_code=\'NB\'", creator => 'Patroncards');}
99
    elsif   ($card_element eq 'batch')     {$db_rows = get_batch_summary( { filters => { branch_code => [ $branch_code, 'NB' ], creator => 'Patroncards' } });}
100
    else                                   {warn sprintf("Unknown card element passed in: %s.",$card_element); $errstr = 202;}
100
    else                                   {warn sprintf("Unknown card element passed in: %s.",$card_element); $errstr = 202;}
101
}
101
}
102
else { # trap unsupported operations here
102
else { # trap unsupported operations here
(-)a/patroncards/print.pl (-2 / +2 lines)
Lines 122-129 elsif ($op eq 'none') { Link Here
122
    @batch_ids = grep{$_ = {batch_id => $_}} @batch_ids;
122
    @batch_ids = grep{$_ = {batch_id => $_}} @batch_ids;
123
    @label_ids = grep{$_ = {label_id => $_}} @label_ids;
123
    @label_ids = grep{$_ = {label_id => $_}} @label_ids;
124
    @borrower_numbers = grep{$_ = {borrower_number => $_}} @borrower_numbers;
124
    @borrower_numbers = grep{$_ = {borrower_number => $_}} @borrower_numbers;
125
    $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Patroncards"');
125
    $templates = get_all_templates( { fields => [qw( template_id template_code ) ], filters => { creator => "Patroncards" } });
126
    $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Patroncards"');
126
    $layouts = get_all_layouts({ fields => [ qw( layout_id layout_name ) ], filters => { creator => "Patroncards" } });
127
    $output_formats = get_output_formats();
127
    $output_formats = get_output_formats();
128
    $template->param(
128
    $template->param(
129
                    batch_ids                   => \@batch_ids,
129
                    batch_ids                   => \@batch_ids,
(-)a/t/db_dependent/Creators/Lib.t (-5 / +4 lines)
Lines 314-320 is( $templates->[1]->{units}, 'POINT', 'units is good Link Here
314
is( $templates->[1]->{creator},          'Labels',     'creator          is good' );
314
is( $templates->[1]->{creator},          'Labels',     'creator          is good' );
315
315
316
# With field_list params --------------
316
# With field_list params --------------
317
$templates = get_all_templates( field_list => 'units, cols, rows' );
317
$templates = get_all_templates( {fields=> [qw(units cols rows)] } );
318
318
319
$query = '
319
$query = '
320
  SELECT count(*)
320
  SELECT count(*)
Lines 502-508 is( $layouts->[1]->{layout_xml}, 'layout_xml2', 'layout_xml is good' ); Link Here
502
is( $layouts->[1]->{creator},       'Labels',      'creator        is good' );
502
is( $layouts->[1]->{creator},       'Labels',      'creator        is good' );
503
503
504
# With field_list params --------------
504
# With field_list params --------------
505
$layouts = get_all_layouts( field_list => 'barcode_type, layout_name, font' );
505
$layouts = get_all_layouts( { fields => [qw(barcode_type layout_name font)] });
506
506
507
$query = '
507
$query = '
508
  SELECT count(*)
508
  SELECT count(*)
Lines 662-668 is( $profiles->[1]->{units}, 'POINT', 'units is good' ); Link Here
662
is( $profiles->[1]->{creator},      'Labels',       'creator        is good' );
662
is( $profiles->[1]->{creator},      'Labels',       'creator        is good' );
663
663
664
# With field_list params --------------
664
# With field_list params --------------
665
$profiles = get_all_profiles( field_list => 'printer_name, template_id' );
665
$profiles = get_all_profiles( { fields => [qw(printer_name template_id)] });
666
666
667
$query = '
667
$query = '
668
  SELECT count(*)
668
  SELECT count(*)
Lines 696-702 isnt( exists $profiles->[1]->{units}, 'POINT', 'units is Link Here
696
isnt( exists $profiles->[1]->{creator},       'Labels',       'creator        is good' );
696
isnt( exists $profiles->[1]->{creator},       'Labels',       'creator        is good' );
697
697
698
# With filter params ------------------
698
# With filter params ------------------
699
$profiles = get_all_profiles( filter => 'template_id = 1235' );
699
$profiles = get_all_profiles( filters => { template_id => 1235 } );
700
700
701
$query = '
701
$query = '
702
  SELECT count(*)
702
  SELECT count(*)
703
- 

Return to bug 17900