| 1 | package App::ZofCMS::Plugin::DataToExcel; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.001010'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base 'App::ZofCMS::Plugin::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Spreadsheet::DataToExcel; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub _key { 'plug_data_to_excel' } | ||||||||||||||||||||||||||
|
Subroutine
_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub _defaults { | ||||||||||||||||||||||||||
|
Subroutine
_defaults: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 13 | return ( | ||||||||||||||||||||||||||||
| 14 | 0 | trigger => 1, | |||||||||||||||||||||||||||
| 15 | filename => 'ExcelData.xls', | ||||||||||||||||||||||||||||
| 16 | no_exit => 0, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub _do { | ||||||||||||||||||||||||||
|
Subroutine
_do: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 20 | 0 | my ( $self, $conf, $t, $q, $config ) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | for ( qw/trigger data/ ) { | |||||||||||||||||||||||||||
| 23 | $conf->{$_} = $conf->{$_}->( $t, $q, $config ) | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ref $conf->{$_} eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | return | ||||||||||||||||||||||||||||
| 28 | unless $conf->{trigger} | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | and $conf->{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$conf->{"trigger"} and $conf->{"data"}
MC/DC0%0/2
$conf->{"trigger"} and $conf->{"data"}
$conf->{"trigger"}
$conf->{"data"}
Truth table0%0/3
A and B
A= $conf->{"trigger"}B= $conf->{"data"}
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | my $excel = Spreadsheet::DataToExcel->new; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my $output; | |||||||||||||||||||||||||||
| 34 | open my $fh, '>', \$output | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $t->{t}{plug_data_to_excel_error} = $!; | |||||||||||||||||||||||||||
| 37 | 0 | return; | |||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | unless ( $excel->dump( $fh, @$conf{qw/data options/} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $t->{t}{plug_data_to_excel_error} = $excel->error; | |||||||||||||||||||||||||||
| 42 | 0 | return; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | print "Content-Disposition: inline; filename=$conf->{filename}\n"; | |||||||||||||||||||||||||||
| 46 | 0 | print "Content-type: application/vnd.ms-excel\n\n"; | |||||||||||||||||||||||||||
| 47 | 0 | print $output; | |||||||||||||||||||||||||||
| 48 | exit | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | unless $conf->{no_exit}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | 1; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 NAME | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | App::ZofCMS::Plugin::DataToExcel - ZofCMS plugin to output data as an Excel file | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | plugins => [ | ||||||||||||||||||||||||||||
| 61 | qw/DataToExcel/, | ||||||||||||||||||||||||||||
| 62 | ], | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 65 | data => [ | ||||||||||||||||||||||||||||
| 66 | [ qw/Foo Bar Baz/ ], | ||||||||||||||||||||||||||||
| 67 | [ qw/Foo1 Bar1 Baz1/ ], | ||||||||||||||||||||||||||||
| 68 | [ qw/Foo2 Bar2 Baz2/ ], | ||||||||||||||||||||||||||||
| 69 | ], | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # this argument is optional; by default not specified | ||||||||||||||||||||||||||||
| 72 | options => { | ||||||||||||||||||||||||||||
| 73 | text_wrap => 1, | ||||||||||||||||||||||||||||
| 74 | calc_column_widths => 1, | ||||||||||||||||||||||||||||
| 75 | width_multiplier => 1, | ||||||||||||||||||||||||||||
| 76 | center_first_row => 1, | ||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # arguments below are optional; defaults are shown | ||||||||||||||||||||||||||||
| 80 | trigger => 1, | ||||||||||||||||||||||||||||
| 81 | filename => 'ExcelData.xls', | ||||||||||||||||||||||||||||
| 82 | no_exit => 0, | ||||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | The module is a plugin for L<App::ZofCMS> that provides means to easily | ||||||||||||||||||||||||||||
| 88 | export an arrayref of arrayrefs (your data) as an Excel file presented | ||||||||||||||||||||||||||||
| 89 | to the user. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | This documentation assumes you've read L<App::ZofCMS>, | ||||||||||||||||||||||||||||
| 92 | L<App::ZofCMS::Config> and L<App::ZofCMS::Template>. Familarity with | ||||||||||||||||||||||||||||
| 93 | L<Spreadsheet::DataToExcel> would be helpful. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 FIRST-LEVEL ZofCMS TEMPLATE AND MAIN CONFIG FILE KEYS | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 C<plugins> | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | plugins => [ qw/DataToExcel/ ], | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | B<Mandatory>. You need to include the plugin in the list of | ||||||||||||||||||||||||||||
| 102 | plugins to execute. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 C<plug_data_to_excel> | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 107 | data => [ | ||||||||||||||||||||||||||||
| 108 | [ qw/Foo Bar Baz/ ], | ||||||||||||||||||||||||||||
| 109 | [ qw/Foo1 Bar1 Baz1/ ], | ||||||||||||||||||||||||||||
| 110 | [ qw/Foo2 Bar2 Baz2/ ], | ||||||||||||||||||||||||||||
| 111 | ], | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # this argument is optional; by default not specified | ||||||||||||||||||||||||||||
| 114 | options => { | ||||||||||||||||||||||||||||
| 115 | text_wrap => 1, | ||||||||||||||||||||||||||||
| 116 | calc_column_widths => 1, | ||||||||||||||||||||||||||||
| 117 | width_multiplier => 1, | ||||||||||||||||||||||||||||
| 118 | center_first_row => 1, | ||||||||||||||||||||||||||||
| 119 | }, | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # arguments below are optional; defaults are shown | ||||||||||||||||||||||||||||
| 122 | trigger => 1, | ||||||||||||||||||||||||||||
| 123 | filename => 'ExcelData.xls', | ||||||||||||||||||||||||||||
| 124 | no_exit => 0, | ||||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # or | ||||||||||||||||||||||||||||
| 128 | plug_data_to_excel => sub { | ||||||||||||||||||||||||||||
| 129 | my ( $t, $q, $config ) = @_; | ||||||||||||||||||||||||||||
| 130 | return $hashref_to_assign_to_this_key_instead_of_subref; | ||||||||||||||||||||||||||||
| 131 | }, | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | B<Mandatory>. Takes either a hashref or a subref as a value. | ||||||||||||||||||||||||||||
| 134 | If subref is specified, its return value will be assigned to | ||||||||||||||||||||||||||||
| 135 | C<plug_data_to_excel> as if it were already there. If sub returns an | ||||||||||||||||||||||||||||
| 136 | C<undef> or an empty list, then plugin will stop further processing. The | ||||||||||||||||||||||||||||
| 137 | C<@_> of the subref will contain C<$t>, C<$q>, and C<$config> | ||||||||||||||||||||||||||||
| 138 | (in that order), where C<$t> is ZofCMS Template hashref, C<$q> is | ||||||||||||||||||||||||||||
| 139 | query parameter hashref, and C<$config> is L<App::ZofCMS::Config> | ||||||||||||||||||||||||||||
| 140 | object. Possible keys/values for the hashref are as follows: | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head3 C<data> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 145 | data => [ | ||||||||||||||||||||||||||||
| 146 | [ qw/Foo Bar Baz/ ], | ||||||||||||||||||||||||||||
| 147 | [ qw/Foo1 Bar1 Baz1/ ], | ||||||||||||||||||||||||||||
| 148 | [ qw/Foo2 Bar2 Baz2/ ], | ||||||||||||||||||||||||||||
| 149 | ], | ||||||||||||||||||||||||||||
| 150 | ... | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 153 | data => sub { | ||||||||||||||||||||||||||||
| 154 | my ( $t, $q, $config ) = @_; | ||||||||||||||||||||||||||||
| 155 | return $arrayref_to_assign_to_the_key_instead_of_this_subref; | ||||||||||||||||||||||||||||
| 156 | }, | ||||||||||||||||||||||||||||
| 157 | ... | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | B<Mandatory>. If not specified, plugin will not execute. Takes an | ||||||||||||||||||||||||||||
| 160 | arrayref or a subref as a value. If a subref is specified, it must | ||||||||||||||||||||||||||||
| 161 | return either an C<undef> or an empty list, in which case the plugin | ||||||||||||||||||||||||||||
| 162 | will not run, or an arrayref that will be assigned to C<data> as | ||||||||||||||||||||||||||||
| 163 | if it were there instead of the subref. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | If subref is specified, the C<@_> of the subref will contain | ||||||||||||||||||||||||||||
| 166 | C<$t>, C<$q>, and C<$config> (in that order), where C<$t> is ZofCMS | ||||||||||||||||||||||||||||
| 167 | Template hashref, C<$q> is query parameter hashref, and C<$config> is | ||||||||||||||||||||||||||||
| 168 | L<App::ZofCMS::Config> object. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | The arrayref must be in the same format as the I<second> argument | ||||||||||||||||||||||||||||
| 171 | of L<Spreadsheet::DataToExcel>'s C<dump()> method. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head3 C<options> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 176 | options => { | ||||||||||||||||||||||||||||
| 177 | text_wrap => 1, | ||||||||||||||||||||||||||||
| 178 | calc_column_widths => 1, | ||||||||||||||||||||||||||||
| 179 | width_multiplier => 1, | ||||||||||||||||||||||||||||
| 180 | center_first_row => 1, | ||||||||||||||||||||||||||||
| 181 | }, | ||||||||||||||||||||||||||||
| 182 | ... | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | B<Optional>. Takes a hashref of arguments that specify how to format | ||||||||||||||||||||||||||||
| 185 | the Excel file. See the I<third> argument for | ||||||||||||||||||||||||||||
| 186 | L<Spreadsheet::DataToExcel>'s C<dump()>'s method for details. | ||||||||||||||||||||||||||||
| 187 | B<By default is not specified> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head3 C<trigger> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 192 | trigger => 1, | ||||||||||||||||||||||||||||
| 193 | ... | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 196 | trigger => sub { | ||||||||||||||||||||||||||||
| 197 | my ( $t, $q, $config ) = @_; | ||||||||||||||||||||||||||||
| 198 | return $actual_value_for_trigger; | ||||||||||||||||||||||||||||
| 199 | }, | ||||||||||||||||||||||||||||
| 200 | ... | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | B<Optional>. Takes either true or false values, or a subref that | ||||||||||||||||||||||||||||
| 203 | returns either true or false values. Plugin will run only if | ||||||||||||||||||||||||||||
| 204 | C<trigger> is set to a true value. If set to a subref, the C<@_> of the | ||||||||||||||||||||||||||||
| 205 | subref will contain C<$t>, C<$q>, and C<$config> (in that order), where | ||||||||||||||||||||||||||||
| 206 | C<$t> is ZofCMS Template hashref, C<$q> is query parameter hashref, and | ||||||||||||||||||||||||||||
| 207 | C<$config> is L<App::ZofCMS::Config> object. B<Defaults to:> C<1> | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head3 C<filename> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 212 | filename => 'ExcelData.xls', | ||||||||||||||||||||||||||||
| 213 | ... | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | B<Optional>. Takes a string as a value that specifies the filename | ||||||||||||||||||||||||||||
| 216 | that the browser will propose to the user (when saving your Excel file). | ||||||||||||||||||||||||||||
| 217 | B<Defaults to:> C<ExcelData.xls> | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head3 C<no_exit> | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | plug_data_to_excel => { | ||||||||||||||||||||||||||||
| 222 | no_exit => 0, | ||||||||||||||||||||||||||||
| 223 | ... | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | B<Optional>. Takes either true or false values. When plugin | ||||||||||||||||||||||||||||
| 226 | finishes outputting the Excel file to the user, it will call | ||||||||||||||||||||||||||||
| 227 | C<exit()>, unless C<no_exit> is set to a true value. Should that be | ||||||||||||||||||||||||||||
| 228 | the case, it is your responsibility to call C<exit()> at a later time; | ||||||||||||||||||||||||||||
| 229 | although, OpenOffice didn't seem to mind extraneous HTML code added | ||||||||||||||||||||||||||||
| 230 | to the Excel file. B<Defaults to:> C<0> | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 HTML::Template TEMPLATE VARIABLES | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head2 C<plug_data_to_excel_error> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | <tmpl_if name='plug_data_to_excel_error'> | ||||||||||||||||||||||||||||
| 237 | <tmpl_var escape='html' name='plug_data_to_excel_error'> | ||||||||||||||||||||||||||||
| 238 | </tmpl_if> | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Should an error occur (e.g. when you give the plugin invalid | ||||||||||||||||||||||||||||
| 241 | C<data> argument), the plugin will place the description of | ||||||||||||||||||||||||||||
| 242 | the error into C<plug_data_to_excel_error> key of C<{t}> ZofCMS | ||||||||||||||||||||||||||||
| 243 | Template special key. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head1 REQUIRED MODULES | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | The plugin requires the following modules/versions | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | App::ZofCMS::Plugin::Base => 0.0111, | ||||||||||||||||||||||||||||
| 250 | Spreadsheet::DataToExcel => 0.0103, | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Fork this module on GitHub: | ||||||||||||||||||||||||||||
| 255 | L<https://github.com/zoffixznet/App-ZofCMS> | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 BUGS | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | To report bugs or request features, please use | ||||||||||||||||||||||||||||
| 260 | L<https://github.com/zoffixznet/App-ZofCMS/issues> | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | If you can't access GitHub, you can email your request | ||||||||||||||||||||||||||||
| 263 | to C<bug-App-ZofCMS at rt.cpan.org> | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | Zoffix Znet <zoffix at cpan.org> | ||||||||||||||||||||||||||||
| 268 | (L<http://zoffix.com/>, L<http://haslayout.net/>) | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | You can use and distribute this module under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 273 | See the C<LICENSE> file included in this distribution for complete | ||||||||||||||||||||||||||||
| 274 | details. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =cut | ||||||||||||||||||||||||||||