File Coverage

blib/lib/Labyrinth/Plugin/Images.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Labyrinth::Plugin::Images;
2              
3 2     2   4548 use warnings;
  2         4  
  2         58  
4 2     2   7 use strict;
  2         2  
  2         81  
5              
6             our $VERSION = '5.19';
7              
8             =head1 NAME
9              
10             Labyrinth::Plugin::Images - Plugin Images handler for Labyrinth
11              
12             =head1 DESCRIPTION
13              
14             Contains all the image handling functionality for the Labyrinth
15             framework.
16              
17             =cut
18              
19             # -------------------------------------
20             # Library Modules
21              
22 2     2   7 use base qw(Labyrinth::Plugin::Base);
  2         3  
  2         604  
23              
24             use Image::Size;
25             use File::Copy;
26             use File::Basename;
27              
28             use Labyrinth::Audit;
29             use Labyrinth::Globals;
30             use Labyrinth::DBUtils;
31             use Labyrinth::DIUtils;
32             use Labyrinth::Media;
33             use Labyrinth::Metadata;
34             use Labyrinth::MLUtils;
35             use Labyrinth::Support;
36             use Labyrinth::Variables;
37              
38             # -------------------------------------
39             # Constants
40              
41             use constant MaxDefaultWidth => 120;
42             use constant MaxDefaultHeight => 120;
43             use constant MaxDefaultThumb => 120;
44              
45             # -------------------------------------
46             # Variables
47              
48             # type: 0 = optional, 1 = mandatory
49             # html: 0 = none, 1 = text, 2 = textarea
50              
51             my %fields = (
52             imageid => { type => 1, html => 0 },
53             image => { type => 0, html => 0 },
54             tag => { type => 0, html => 1 },
55             link => { type => 0, html => 1 },
56             type => { type => 1, html => 0 },
57             href => { type => 0, html => 0 },
58             metadata => { type => 0, html => 1 },
59             );
60              
61             my (@mandatory,@allfields);
62             for(keys %fields) {
63             push @mandatory, $_ if($fields{$_}->{type});
64             push @allfields, $_;
65             }
66              
67             my $LEVEL = ADMIN;
68              
69             # -------------------------------------
70             # The Subs
71              
72             =head1 PUBLIC INTERFACE METHODS
73              
74             The following are convience methods to provide the appropriate functionality
75             to list the require number of random images for the page.
76              
77             These are to be deprecated in the future, in favour of using configuration
78             settings and/or parameter values.
79              
80             =over 4
81              
82             =item Random()
83              
84             Stores a random image in template variable 'irandX', where X is 1.
85              
86             =item Random4()
87              
88             Stores 4 random images in template variable 'irandX', where X is 1 to 4.
89              
90             =item Random6()
91              
92             Stores 6 random images in template variable 'irandX', where X is 1 to 6.
93              
94             =item Random8()
95              
96             Stores 8 random images in template variable 'irandX', where X is 1 to 8.
97              
98             =back
99              
100             =cut
101              
102             sub Random { RandomN(1); }
103             sub Random4 { RandomN(4); }
104             sub Random6 { RandomN(6); }
105             sub Random8 { RandomN(8); }
106              
107             =head1 LOCAL INTERFACE METHODS
108              
109             =over 4
110              
111             =item RandomN()
112              
113             Provide 'n' random images, prefixed in a template variable with 'irand'.
114              
115             =back
116              
117             =cut
118              
119             sub RandomN {
120             my $count = shift;
121             my @rows = $dbi->GetQuery('hash','GetImagesByType',2);
122             my $max = @rows;
123              
124             my %blank = (link=>$settings{blank},tag=>'');
125              
126             if($max <= $count) {
127             foreach my $inx (1..$max) { $tvars{"irand$inx"} = $rows[($inx-1)]; }
128             return;
129             }
130              
131             my (%done,@random);
132             srand;
133             while(1) {
134             my $index = int((rand) * $max);
135             next if($done{$index});
136             push @random, $rows[$index];
137             $done{$index} = 1;
138             last if(@random >= $count);
139             }
140              
141             foreach my $inx (1..$count) { $tvars{"irand$inx"} = $random[($inx-1)]; }
142             }
143              
144             =head1 ADMIN INTERFACE METHODS
145              
146             Note that in all cases the images referred to do not include photos uploaded
147             via the Album plugin, for photo albums.
148              
149             =over 4
150              
151             =item List
152              
153             List all uploaded images.
154              
155             =item Add
156              
157             Create a template variable hash for a new image.
158              
159             =item Edit
160              
161             Edit the attributes of a given image.
162              
163             =item EditAmendments
164              
165             Additional drop downs and fields for editing.
166              
167             =item Save
168              
169             Save a given image.
170              
171             =item Delete
172              
173             Delete a given image.
174              
175             =item Gallery
176              
177             Provide a set of images as a gallery for selection.
178              
179             =back
180              
181             =cut
182              
183             sub List {
184             return unless AccessUser($LEVEL);
185              
186             my @delete = CGIArray('DELETE');
187             if(@delete) {
188             $cgiparams{'imageid'} = $_;
189             Delete();
190             }
191              
192             my $key = $cgiparams{'searchmeta'} ? 'MetaImages' : 'AllImages';
193             $cgiparams{'searchmeta'} =~ s/[,\s]+/,/g;
194              
195             my @where = ();
196             push @where, "i.type=$cgiparams{'stockid'}" if($cgiparams{'stockid'});
197             push @where, "m.tag IN ($cgiparams{'searchmeta'})" if($cgiparams{'searchmeta'});
198             my $where = @where ? 'WHERE '.join(' AND ',@where) : '';
199              
200             my @rows = $dbi->GetQuery('hash',$key,{where=>$where});
201             foreach (@rows) { $_->{typename} = StockName($_->{type}); }
202             $tvars{data} = \@rows if(@rows);
203              
204             $tvars{ddstock} = StockSelect();
205             }
206              
207             sub Add {
208             return unless AccessUser($LEVEL);
209              
210             my %data = (
211             imageid => 0,
212             tag => '',
213             metadata => '',
214             link => $settings{blank},
215             ddstock => StockSelect(),
216             );
217              
218             $tvars{data} = \%data;
219             }
220              
221             sub Edit {
222             return unless AccessUser($LEVEL);
223             return unless $cgiparams{'imageid'};
224              
225             my @rows = $dbi->GetQuery('hash','GetImageByID',$cgiparams{'imageid'});
226             return unless(@rows);
227              
228             $tvars{data} = $rows[0];
229             EditAmendments();
230             }
231              
232             sub EditAmendments {
233             $tvars{data}->{metadata} = MetaGet($cgiparams{'imageid'},['Image']) if($cgiparams{'imageid'});
234             $tvars{data}->{typename} = StockName($tvars{data}->{type});
235             $tvars{data}->{ddstock} = StockSelect($tvars{data}->{type});
236              
237             for(keys %fields) {
238             if($fields{$_}->{html} == 1) { $tvars{data}->{$_} = CleanHTML($tvars{data}->{$_}) }
239             elsif($fields{$_}->{html} == 2) { $tvars{data}->{$_} = SafeHTML($tvars{data}->{$_}) }
240             }
241              
242             return unless($tvars{data}->{link});
243              
244             # Get the size of image
245             ($tvars{data}->{x_resize},$tvars{data}->{y_resize})
246             = ResizeDimensions($tvars{data}->{dimensions},"$settings{webdir}/$tvars{data}->{link}",$tvars{maxpicwidth},$tvars{maxpicheight});
247             }
248              
249             sub Save {
250             return unless AccessUser($LEVEL);
251             return unless AuthorCheck('GetImageByID','imageid',$LEVEL);
252             EditAmendments();
253              
254             for(keys %fields) {
255             if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
256             elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
257             elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
258             }
259              
260             my $link = $tvars{data}->{link};
261             return if FieldCheck(\@allfields,\@mandatory);
262              
263             if($cgiparams{image}) {
264             my ($name,$filename) = CGIFile('image',$tvars{data}->{type});
265             unless($name) { # blank if anything goes wrong
266             $tvars{errcode} = 'ERROR';
267             return;
268             }
269              
270             my $i = Labyrinth::DIUtils->new("$settings{webdir}/$filename");
271             $i->reduce(MaxDefaultWidth,MaxDefaultHeight);
272              
273             $tvars{data}->{link} = $filename;
274             } else {
275             $tvars{data}->{link} = $link;
276             }
277              
278             $cgiparams{imageid} = SaveImage( $cgiparams{imageid},
279             $tvars{data}->{tag},
280             $tvars{data}->{link},
281             $tvars{data}->{type},
282             $tvars{data}->{href});
283              
284             my @metadata = $tvars{data}->{metadata} ? split(qr/[, ]+/,$tvars{data}->{metadata}) : ();
285             MetaSave($cgiparams{imageid},['Image'],@metadata);
286             }
287              
288             sub Delete {
289             return unless AccessUser($LEVEL);
290             return unless($cgiparams{'imageid'});
291              
292             # check whether image still referenced
293             if(ImageCheck($cgiparams{'imageid'})) {
294             $tvars{errcode} = 'MESSAGE';
295             $tvars{errmess} = 'Sorry cannot delete that image, it is used within other areas of the site.';
296             return;
297             }
298              
299             my @rows = $dbi->GetQuery('hash','GetImageByID',$cgiparams{'imageid'});
300              
301             # do the delete
302             if($dbi->DoQuery('DeleteImage',$cgiparams{'imageid'})) {
303             unlink "$settings{webdir}/" . $rows[0]->{link};
304             }
305             }
306              
307             my @blanks = (
308             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
309             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
310             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
311             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
312             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
313             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
314             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
315             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
316             { imageid=>1,link=>$settings{blank},tag=>'',height=>100,width=>100 },
317             );
318              
319             sub Gallery {
320             return unless AccessUser(EDITOR);
321             my $start = $cgiparams{'start'} || 2;
322             my $key = '';
323              
324             if($cgiparams{'searchmeta'}) {
325             $key = 'Meta';
326             $cgiparams{'searchmeta'} =~ s/[,\s]+/,/g;
327             $cgiparams{'searchmeta'} = join(",", map {"'$_'"} split(",",$cgiparams{'searchmeta'}));
328             }
329              
330             my $where;
331             $where .= " AND i.type IN ($cgiparams{'imagetype'})" if($cgiparams{'imagetype'});
332             $where .= " AND m.tag IN ($cgiparams{'searchmeta'})" if($cgiparams{'searchmeta'});
333              
334             my @rows = $dbi->GetQuery('hash',$key.'Gallery',{where=>$where},$start);
335             for(@rows) {
336             ($_->{width},$_->{heigth})
337             = ResizeDimensions($_->{dimensions},"$settings{webdir}/$_->{link}",100,100);
338             }
339              
340             $tvars{next} = $rows[9]->{imageid} unless(@rows < 10);
341             push @rows, @blanks;
342             $tvars{data} = \@rows if(@rows);
343             my @prev = $dbi->GetQuery('hash',$key.'GalleryMin',{where=>$where},$start);
344              
345             $tvars{prev} = $prev[8]->{imageid} unless(@prev < 9);
346             $tvars{imagetype} = $cgiparams{'imagetype'};
347             $tvars{ddstock} = StockSelect();
348             }
349              
350             1;
351              
352             __END__