App-ZofCMS-PluginBundle-Naughty

 view release on metacpan or  search on metacpan

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

                ],
            );
        }
        $template->{t}{ $t_name_for{list} } = $t->output;
    }
}

sub _process_description {
    my $d = shift;
    encode_entities $d;
    $d =~ s/\r?\n\r?/<br>/g;
    return $d;
}

sub _process_form {
    my ( $self, $t ) = @_;
    my ( $conf, $query, $dbh ) = map $self->$_, qw/conf query dbh/;

    if ( defined $query->{plug_image_gallery_id}
         and length $query->{plug_image_gallery_id}
    ) {
        $dbh->do(
            "UPDATE $conf->{table} SET description = ? WHERE id = ?",
            undef,
            $query->{plug_image_gallery_description},
            $query->{plug_image_gallery_id},
        );
        $t->param( success => 1 );
        return;
    }

    my $cgi = $self->config->cgi;

    my $filename = $cgi->param('plug_image_gallery_file');

    $filename = 'foo.jpg'
        unless defined $filename;

    my ( $ext ) = $filename =~ /([.][^.]+)$/;

    $ext = '.jpg'
        unless defined $ext;

    my $photo_file = File::Spec->catfile( $conf->{photo_dir}, $conf->{filename} . $ext );
    my $thumb_file = File::Spec->catfile( $conf->{thumb_dir}, $conf->{filename} . $ext );

    my $fh = $cgi->upload('plug_image_gallery_file')
        or do {
            $t->param(
                error => 'Failed to upload file' . (
                    $cgi->cgi_error ? ': ' . $cgi->cgi_error : ''
                )
            );
            $t->param( success => 1 );
            return;
        };

    open my $fh_out, '>', $photo_file
        or do { $t->param( error => "Failed to create local file [$!]" ); return; };

    binmode $fh_out;

    {
        local $/ = \102400;
        while (<$fh>) {
            print $fh_out $_;
        }
    }
    close $fh_out;
    close $fh;

    my ( $x, $y ) = imgsize $photo_file;

    if ( ref $conf->{thumb_size} eq 'ARRAY'
        or ( ref $conf->{thumb_size} eq 'HASH'
            and ( (%{ $conf->{thumb_size} })[0] < $x
                or (%{ $conf->{thumb_size} })[1] < $y
            )
        )
    ) {
        $conf->{thumb_size} = [ %{ $conf->{thumb_size} } ]
            if ref $conf->{thumb_size} eq 'HASH';

        my $thumb = Image::Resize->new( $photo_file );
        my $gd = $thumb->resize( @{ $conf->{thumb_size} } );

        open my $fh_thumb, '>', $thumb_file
            or $t->param( error => "Failed to create thumbnail [$!]" )
                and return;

        my $img_type = substr $ext, 1;
        $img_type = 'jpeg'
            unless $img_type eq 'png' or $img_type eq 'gif';

        binmode $fh_thumb;
        print $fh_thumb $gd->$img_type();
        close $fh_thumb;
    }
    else {
        copy $photo_file, $thumb_file;
    }

    if ( ref $conf->{photo_size} eq 'ARRAY'
        or ( ref $conf->{photo_size} eq 'HASH'
            and ( (%{ $conf->{photo_size} })[0] < $x
                or (%{ $conf->{photo_size} })[1] < $y
            )
        )
    ) {
        $conf->{photo_size} = [ %{ $conf->{photo_size} } ]
            if ref $conf->{photo_size} eq 'HASH';

        my $photo = Image::Resize->new( $photo_file );
        my $gd = $photo->resize( @{ $conf->{photo_size} } );

        open my $fh_photo, '>', $photo_file
            or $t->param( error => "Failed to resize photo [$!]" )
                and return;

        my $img_type = substr $ext, 1;
        $img_type = 'jpeg'
            unless $img_type eq 'png' or $img_type eq 'gif';

        binmode $fh_photo;
        print $fh_photo $gd->$img_type();
        close $fh_photo;

        ( $x, $y ) = ( $gd->width, $gd->height );
    }
    my ( $thumb_x, $thumb_y ) = imgsize $thumb_file;

    $t->param( success => 1 );

    $dbh->do(
        "INSERT INTO $conf->{table} VALUES(?, ?, ?, ?, ?, ?, ?, ?);",
        undef,
        $conf->{filename} . $ext,
        $x,
        $y,
        $thumb_x,
        $thumb_y,
        $query->{plug_image_gallery_description},
        time(),
        do { my $id = rand() . time() . rand(); $id =~ tr/.//d; $id },
    );

    return 1;
}

sub _form_template {
    return <<'END_TEMPLATE';
<tmpl_if name='success'>
    <p>Your image has been successfully uploaded.</p>
    <p><a href="<tmpl_var escape='html' name='success_href'>">Upload another image</a></p>
<tmpl_else>
    <form action="" method="POST" id="plug_image_gallery_form" enctype="multipart/form-data">
    <div>
        <tmpl_if name='error'><p class="error"><tmpl_var escape='html' name='error'></p></tmpl_if>
        <input type="hidden" name="page" value="<tmpl_var escape='html' name='page'>">
        <input type="hidden" name="dir" value="<tmpl_var escape='html' name='dir'>">
        <tmpl_if name='is_edit'><input type="hidden" name="plug_image_gallery_id" value="<tmpl_var escape='html' name='id'>"></tmpl_if>
        <ul>
            <tmpl_unless name='is_edit'><li>
                <label for="plug_image_gallery_file">Image: </label
                ><input type="file" name="plug_image_gallery_file" id="plug_image_gallery_file">
            </li></tmpl_unless>
            <li>
                <label for="plug_image_gallery_description">Description: </label
                ><textarea name="plug_image_gallery_description" id="plug_image_gallery_description" cols="60" rows="5"><tmpl_var escape='html' name='plug_image_gallery_description'></textarea>
            </li>
        </ul>
        <input type="submit" name="plug_image_gallery_submit" value="<tmpl_if name='is_edit'>Update<tmpl_else>Upload</tmpl_if>">
    </div>
    </form>
</tmpl_if>
END_TEMPLATE
}

sub _list_template {
    return <<'END_TEMPLATE';
<tmpl_if name='is_view'>
    <a class="plug_image_gallery_return_to_image_list" href="/index.pl?page=<tmpl_var escape='html' name='page'>&amp;dir=<tmpl_var escape='html' name='dir'>">Return to image list.</a>
    <div id="plug_image_gallery_photo"><img src="/<tmpl_var escape='html' name='photo'>" width="<tmpl_var escape='html' name='width'>" height="<tmpl_var escape='html' name='height'>" alt=""><tmpl_if name='has_description'><p class="plug_image_gallery...
<tmpl_else>



( run in 0.549 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )