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

(-)a/misc/cronjobs/gather_print_notices.pl (-108 / +85 lines)
Lines 101-134 foreach my $message (@all_messages) { Link Here
101
    $message->{'content'} = $_;
101
    $message->{'content'} = $_;
102
}
102
}
103
103
104
print_notices_html({
104
print_notices({
105
    messages => \@all_messages,
105
    messages => \@all_messages,
106
    split => $split,
106
    split => $split,
107
    output_directory => $output_directory,
107
    output_directory => $output_directory,
108
    format => 'html',
108
}) if $html;
109
}) if $html;
109
110
110
print_notices_csv({
111
print_notices({
111
    messages => \@all_messages,
112
    messages => \@all_messages,
112
    split => $split,
113
    split => $split,
113
    output_directory => $output_directory,
114
    output_directory => $output_directory,
115
    format => 'csv',
114
}) if $csv;
116
}) if $csv;
115
117
116
print_notices_ods({
118
print_notices({
117
    messages => \@all_messages,
119
    messages => \@all_messages,
118
    split => $split,
120
    split => $split,
119
    output_directory => $output_directory,
121
    output_directory => $output_directory,
122
    format => 'ods',
120
}) if $ods;
123
}) if $ods;
121
124
122
sub print_notices_html {
125
sub print_notices {
123
    my ( $params ) = @_;
126
    my ( $params ) = @_;
124
127
125
    my $messages = $params->{messages};
128
    my $messages = $params->{messages};
126
    my $split = $params->{split};
129
    my $split = $params->{split};
127
    my $output_directory = $params->{output_directory};
130
    my $output_directory = $params->{output_directory};
128
    my $set_status = $params->{set_status} // 1;
131
    my $set_status = $params->{set_status} // 1;
129
    my @filenames;
132
    my $format = $params->{format} // 'html';
133
134
    die "Format $format is not known"
135
        unless $format =~ m[^html$|^csv$|^ods$];
136
137
    my ( @filenames, $messages_by_branch );
130
138
131
    my $messages_by_branch;
132
    if ( $split ) {
139
    if ( $split ) {
133
        foreach my $message (@$messages) {
140
        foreach my $message (@$messages) {
134
            push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
141
            push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
Lines 139-162 sub print_notices_html { Link Here
139
146
140
    while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
147
    while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
141
        my $filename = $split
148
        my $filename = $split
142
            ? 'holdnotices-' . $today->output('iso') . "-$branchcode.html"
149
            ? 'holdnotices-' . $today->output('iso') . "-$branchcode.$format"
143
            : 'holdnotices-' . $today->output('iso') . ".html";
150
            : 'holdnotices-' . $today->output('iso') . ".$format";
144
151
145
        my $template =
152
        my $filepath = File::Spec->catdir( $output_directory, $filename );
146
          C4::Templates::gettemplate( 'batch/print-notices.tmpl', 'intranet',
153
        if ( $format eq 'html' ) {
147
            new CGI );
154
            generate_html({
148
155
                messages => $branch_messages,
149
        $template->param(
156
                filepath => $filepath,
150
            stylesheet => C4::Context->preference("NoticeCSS"),
157
            });
151
            today      => $today->output(),
158
        } elsif ( $format eq 'csv' ) {
152
            messages   => $branch_messages,
159
            generate_csv ({
153
        );
160
                messages => $branch_messages,
154
161
                filepath => $filepath,
155
        my $output_file = File::Spec->catdir( $output_directory, $filename )
162
            });
156
        open my $OUTPUT, '>', $output_file
163
        } elsif ( $format eq 'ods' ) {
157
            or die "Could not open $output_file: $!";
164
            generate_ods ({
158
        print $OUTPUT $template->output;
165
                messages => $branch_messages,
159
        close $OUTPUT;
166
                filepath => $filepath,
167
            });
168
        }
160
169
161
        if ( $set_status ) {
170
        if ( $set_status ) {
162
            foreach my $message ( @$branch_messages ) {
171
            foreach my $message ( @$branch_messages ) {
Lines 173-276 sub print_notices_html { Link Here
173
    return \@filenames;
182
    return \@filenames;
174
}
183
}
175
184
176
sub print_notices_csv {
185
sub generate_html {
177
    my ( $params ) = @_;
186
    my ( $params ) = @_;
178
179
    my $messages = $params->{messages};
187
    my $messages = $params->{messages};
180
    my $split = $params->{split};
188
    my $filepath = $params->{filepath};
181
    my $output_directory = $params->{output_directory};
189
182
    my $set_status = $params->{set_status} // 1;
190
    my $template =
183
    my @filenames;
191
      C4::Templates::gettemplate( 'batch/print-notices.tmpl', 'intranet',
192
        new CGI );
193
194
    $template->param(
195
        stylesheet => C4::Context->preference("NoticeCSS"),
196
        today      => $today->output(),
197
        messages   => $messages,
198
    );
199
200
    open my $OUTPUT, '>', $filepath
201
        or die "Could not open $filepath: $!";
202
    print $OUTPUT $template->output;
203
    close $OUTPUT;
204
}
184
205
185
    my $messages_by_branch;
206
sub generate_csv {
186
    if ( $split ) {
207
    my ( $params ) = @_;
187
        foreach my $message (@$messages) {
208
    my $messages = $params->{messages};
188
            push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
209
    my $filepath = $params->{filepath};
210
211
    open my $OUTPUT, '>', $filepath
212
        or die "Could not open $filepath: $!";
213
    my ( @csv_lines, $headers );
214
    foreach my $message ( @$messages ) {
215
        my @lines = split /\n/, $message->{content};
216
217
        # We don't have headers, get them
218
        unless ( $headers ) {
219
            $headers = $lines[0];
220
            chomp $headers;
221
            say $OUTPUT $headers;
189
        }
222
        }
190
    } else {
191
        $messages_by_branch->{all_branches} = $messages;
192
    }
193
223
194
    while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
224
        shift @lines;
195
        my $filename = $split
225
        for my $line ( @lines ) {
196
            ? 'holdnotices-' . $today->output('iso') . "-$branchcode.csv"
226
            chomp $line;
197
            : 'holdnotices-' . $today->output('iso') . ".csv";
227
            next if $line =~ /^\s$/;
198
228
            say $OUTPUT $line;
199
        open my $OUTPUT, '>', File::Spec->catdir( $output_directory, $filename );
200
        my ( @csv_lines, $headers );
201
        foreach my $message ( @$branch_messages ) {
202
            my @lines = split /\n/, $message->{content};
203
204
            # We don't have headers, get them
205
            unless ( $headers ) {
206
                $headers = $lines[0];
207
                chomp $headers;
208
                say $OUTPUT $headers;
209
            }
210
211
            shift @lines;
212
            for my $line ( @lines ) {
213
                chomp $line;
214
                next if $line =~ /^\s$/;
215
                say $OUTPUT $line;
216
            }
217
218
            if ( $set_status ) {
219
                C4::Letters::_set_message_status(
220
                    {
221
                        message_id => $message->{'message_id'},
222
                        status => 'sent'
223
                    }
224
                );
225
            }
226
        }
229
        }
227
        close $OUTPUT;
228
        push @filenames, $filename;
229
    }
230
    }
230
    return \@filenames;
231
}
231
}
232
232
233
sub print_notices_ods {
233
sub generate_ods {
234
    my ( $params ) = @_;
234
    my ( $params ) = @_;
235
236
    my $messages = $params->{messages};
235
    my $messages = $params->{messages};
237
    my $split = $params->{split};
236
    my $filepath = $params->{filepath};
238
    my $output_directory = $params->{output_directory};
239
    my $set_status = $params->{set_status} // 1;
240
    my @filenames;
241
242
    my $messages_by_branch;
243
    if ( $split ) {
244
        foreach my $message (@$messages) {
245
            push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
246
        }
247
    } else {
248
        $messages_by_branch->{all_branches} = $messages;
249
    }
250
237
251
    while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
238
    my $tmpdir = File::Temp->newdir();
252
        my $filename = $split
253
            ? 'holdnotices-' . $today->output('iso') . "-$branchcode.ods"
254
            : 'holdnotices-' . $today->output('iso') . ".ods";
255
256
        my $tmpdir = File::Temp->newdir();
257
258
        my ( $csv_filename ) = @{ print_notices_csv({
259
            messages => $branch_messages,
260
            output_directory => $tmpdir,
261
            set_status => 0,
262
        }) };
263
239
264
        my $ods_output_filepath = File::Spec->catdir( $output_directory, $filename );
240
    my ( $csv_filename ) = @{ print_notices({
265
        my $csv_filepath = File::Spec->catdir( $tmpdir, $csv_filename );
241
        messages => $messages,
242
        output_directory => $tmpdir,
243
        set_status => 0,
244
        format => 'csv',
245
    }) };
266
246
267
        qx|
247
    my $csv_filepath = File::Spec->catdir( $tmpdir, $csv_filename );
268
            $csv2ods_cmd -i $csv_filepath -o $ods_output_filepath -d $delimiter
269
        |;
270
248
271
        push @filenames, $filename;
249
    qx|
272
    }
250
        $csv2ods_cmd -i $csv_filepath -o $filepath -d $delimiter
273
    return \@filenames;
251
    |;
274
}
252
}
275
253
276
=head1 NAME
254
=head1 NAME
277
- 

Return to bug 11603