App-ZofCMS

 view release on metacpan or  search on metacpan

lib/App/ZofCMS/Plugin/Barcode.pm  view on Meta::CPAN

package App::ZofCMS::Plugin::Barcode;

use warnings;
use strict;

our $VERSION = '1.001008'; # VERSION

use GD::Barcode;
use base 'App::ZofCMS::Plugin::Base';

sub _key { 'plug_barcode' }
sub _defaults {
    code    => undef,
    format  => 'png', # gif or png
    type    => 'UPCA',
    no_text => 0,
    height  => 50,
    file    => undef,
}
sub _do {
    my ( $self, $conf, $t, $q, $config ) = @_;

    $conf->{code} = $conf->{code}->( $t, $q, $config )
        if ref $conf->{code} eq 'CODE';

    return
        unless defined $conf->{code}
            and length $conf->{code};

    $conf->{type} = $conf->{type}->( $t, $q, $config )
        if ref $conf->{type} eq 'CODE';

    return
        unless defined $conf->{type}
            and length $conf->{type};

    my $bar = GD::Barcode->new( @$conf{ qw/type code/ } );

    unless ( $bar ) {
        $t->{t}{plug_barcode_error} = $GD::Barcode::errStr;
        return;
    }

    if ( defined $conf->{file} and length $conf->{file} ) {
        if ( open my $fh, '>', $conf->{file} ) {
            binmode $fh;
            print $fh $conf->{format} eq 'png'
                ? $bar->plot( Height => $conf->{height}, NoText => $conf->{no_text} )->png
                : $bar->plot( Height => $conf->{height}, NoText => $conf->{no_text} )->gif;
        }
        else {
            $t->{t}{plug_barcode_error} = $!;
            return;
        }
    }
    else {
        binmode STDOUT;
        if ( $conf->{format} eq 'png' ) {
            print "Content-Type: image/png\n\n";
            print $bar->plot( Height => $conf->{height}, NoText => $conf->{no_text} )->png;
        }
        else {
            print "Content-Type: image/gif\n\n";
            print $bar->plot( Height => $conf->{height}, NoText => $conf->{no_text} )->gif;
        }
        exit;
    }
}

1;
__END__

=encoding utf8

=head1 NAME

App::ZofCMS::Plugin::Barcode - plugin to generate various bar codes

=head1 SYNOPSIS

In your Main Config File or ZofCMS Template:

    plugins => [
        qw/Barcode/
    ],

    # direct output to browser with default values for optional arguments
    plug_barcode => {
        code => '12345678901',
    },

    # or

    # save to file with all options set
    plug_barcode => {
        code    => '12345678901',
        file    => 'bar.png',
        type    => 'UPCA', # default
        format  => 'png',  # default
        no_text => 0,      # default
        height  => 50,     # default
    },

In your HTML::Template template (in case errors occur):

    <tmpl_if name='plug_barcode_error'>
        <p>Error: <tmpl_var escape='html' name='plug_barcode_error'></p>
    </tmpl_if>

=head1 DESCRIPTION

The module is a plugin for L<App::ZofCMS> that provides means to generate various
types of barcodes and either output them directly to the browser or save them as
an image.

This documentation assumes you've read L<App::ZofCMS>, L<App::ZofCMS::Config> and
L<App::ZofCMS::Template>



( run in 0.751 second using v1.01-cache-2.11-cpan-39bf76dae61 )