App-ZofCMS-PluginBundle-Naughty
view release on metacpan or search on metacpan
lib/App/ZofCMS/Plugin/ImageGallery.pm view on Meta::CPAN
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'>&dir=<tmpl_var escape='html' name='dir'>">Return to image list.</a>
lib/App/ZofCMS/Plugin/ImageGallery.pm view on Meta::CPAN
my $self = shift;
@_ and $self->{TEMPLATE} = shift;
return $self->{TEMPLATE};
}
sub query {
my $self = shift;
@_ and $self->{QUERY} = shift;
return $self->{QUERY};
}
sub config {
my $self = shift;
@_ and $self->{CONFIG} = shift;
return $self->{CONFIG};
}
sub dbh {
my $self = shift;
@_ and $self->{dbh} = shift;
return $self->{dbh};
}
1;
__END__
=encoding utf8
=for stopwords lightbox crapolio subref
=head1 NAME
App::ZofCMS::Plugin::ImageGallery - CRUD-like plugin for managing images.
=head1 SYNOPSIS
In your Main Config File or ZofCMS Template file:
plugins => [ qw/ImageGallery/ ],
plug_image_gallery => {
dsn => "DBI:mysql:database=test;host=localhost",
user => 'test',
pass => 'test',
no_form => 0,
allow_edit => 1,
},
In your L<HTML::Template> template:
<tmpl_var name='plug_image_gallery_form'>
<tmpl_var name='plug_image_gallery_list'>
Viola, now you can upload photos with descriptions, delete them and edit descriptions. \o/
=head1 DESCRIPTION
The module is a plugin for L<App::ZofCMS> that allows one to add a CRUD-like functionality
for managing photos. The plugin automatically makes thumbnails and can also resize the
actual photos if you tell it to. So far, only
C<.jpg>, C<.png> and C<.gif> images are supported; however, plugin does not check
C<Content-Type> of the uploaded image.
The image file name and description are stored in a SQL database.
This documentation assumes you've read L<App::ZofCMS>, L<App::ZofCMS::Config> and L<App::ZofCMS::Template>
=head1 USED SQL TABLE FORMAT
When C<create_table> option is turned on (see below) the plugin will create the following
table where C<table_name> is derived from C<table> argument in C<plug_image_gallery>
(see below).
CREATE TABLE table_name (
photo TEXT,
width SMALLINT,
height SMALLINT,
thumb_width SMALLINT,
thumb_height SMALLINT,
description TEXT,
time VARCHAR(10),
id TEXT
);
=head1 MAIN CONFIG FILE AND ZofCMS TEMPLATE FIRST-LEVEL KEYS
=head2 C<plugins>
plugins => [ qw/ImageGallery/, ],
You obviously need to include the plugin in the list of plugins to execute.
=head2 C<plug_image_gallery>
plug_image_gallery => {
dsn => "DBI:mysql:database=test;host=localhost",
# everything below is optional
user => '',
pass => '',
opt => { RaiseError => 1, AutoCommit => 1 },
table => 'photos',
photo_dir => 'photos/',
filename => '[:rand:]',
thumb_dir => 'photos/thumbs/',
create_table => 0,
t_name => 'plug_image_gallery',
no_form => 1,
no_list => 0,
no_thumb_desc => 0,
allow_edit => 0,
thumb_size => { 200, 200 },
# photo_size => [ 600, 600 ],
has_view => 1,
want_lightbox => 0,
lightbox_rel => 'lightbox',
lightbox_desc => 1,
}
plug_image_gallery => sub {
my ( $t, $q, $config ) = @_;
return {
lib/App/ZofCMS/Plugin/ImageGallery.pm view on Meta::CPAN
=head1 GENERATED HTML CODE
=head2 form
<form action="" method="POST" id="plug_image_gallery_form" enctype="multipart/form-data">
<div>
<input type="hidden" name="page" value="photos">
<input type="hidden" name="dir" value="/admin/">
<ul>
<li>
<label for="plug_image_gallery_file">Image: </label
><input type="file" name="plug_image_gallery_file" id="plug_image_gallery_file">
</li>
<li>
<label for="plug_image_gallery_description">Description: </label
><textarea name="plug_image_gallery_description" id="plug_image_gallery_description" cols="60" rows="5"></textarea>
</li>
</ul>
<input type="submit" name="plug_image_gallery_submit" value="Upload">
</div>
</form>
=head2 form when "Edit" was clicked
<form action="" method="POST" id="plug_image_gallery_form" enctype="multipart/form-data">
<div>
<input type="hidden" name="page" value="photos">
<input type="hidden" name="dir" value="/admin/">
<input type="hidden" name="plug_image_gallery_id" value="07537915760568812292592510718228816144752">
<ul>
<li>
<label for="plug_image_gallery_description">Description: </label
><textarea name="plug_image_gallery_description" id="plug_image_gallery_description" cols="60" rows="5">Teh Descripshun!</textarea>
</li>
</ul>
<input type="submit" name="plug_image_gallery_submit" value="Update">
</div>
</form>
=head2 form when upload or update was successful
<p>Your image has been successfully uploaded.</p>
<p><a href="/index.pl?page=photos&amp;dir=/admin/">Upload another image</a></p>
=head2 list (when both C<allow_edit> and C<has_view> is set to true values)
<ul class="plug_image_gallery_list">
<li>
<a href="/index.pl?page=photos&dir=/admin/&plug_image_gallery_photo_id=037142535745273312292651650508033404216754"><img src="/photos/thumbs/0029243203419358812292651650444418525180907.jpg" width="191" height="200" alt=""></a>
<form action="" method="POST">
<div>
<input type="hidden" name="plug_image_gallery_id" value="037142535745273312292651650508033404216754">
<input type="hidden" name="page" value="photos">
<input type="hidden" name="dir" value="/admin/">
<input type="submit" name="plug_image_gallery_action" value="Edit">
<input type="submit" name="plug_image_gallery_action" value="Delete">
</div>
</form>
</li>
<li class="alt">
<a href="/index.pl?page=photos&dir=/admin/&plug_image_gallery_photo_id=07537915760568812292592510718228816144752"><img src="/photos/thumbs/058156553244134912292592510947564500241668.png" width="200" height="125" alt=""></a>
<p>Teh Descripshun!</p>
<form action="" method="POST">
<div>
<input type="hidden" name="plug_image_gallery_id" value="07537915760568812292592510718228816144752">
<input type="hidden" name="page" value="photos">
<input type="hidden" name="dir" value="/admin/">
<input type="submit" name="plug_image_gallery_action" value="Edit">
<input type="submit" name="plug_image_gallery_action" value="Delete">
</div>
</form>
</li>
</ul>
=head2 original image view
<a class="plug_image_gallery_return_to_image_list" href="/index.pl?page=photos&dir=/admin/">Return to image list.</a>
<div id="plug_image_gallery_photo"><img src="/photos/0029243203419358812292651650444418525180907.jpg" width="575" height="600" alt="">
<p class="plug_image_gallery_description">Uber hawt chick</p>
</div>
=head1 REPOSITORY
Fork this module on GitHub:
L<https://github.com/zoffixznet/App-ZofCMS-PluginBundle-Naughty>
=head1 BUGS
To report bugs or request features, please use
L<https://github.com/zoffixznet/App-ZofCMS-PluginBundle-Naughty/issues>
If you can't access GitHub, you can email your request
to C<bug-App-ZofCMS-PluginBundle-Naughty at rt.cpan.org>
=head1 AUTHOR
Zoffix Znet <zoffix at cpan.org>
(L<http://zoffix.com/>, L<http://haslayout.net/>)
=head1 LICENSE
You can use and distribute this module under the same terms as Perl itself.
See the C<LICENSE> file included in this distribution for complete
details.
=cut
( run in 0.968 second using v1.01-cache-2.11-cpan-df04353d9ac )