File Coverage

blib/lib/Labyrinth/Plugin/Album/Photos.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::Album::Photos;
2              
3 1     1   5046 use strict;
  1         2  
  1         39  
4 1     1   4 use warnings;
  1         2  
  1         60  
5              
6             our $VERSION = '1.11';
7              
8             =head1 NAME
9              
10             Labyrinth::Plugin::Album::Photos - Photo album photos handler for Labyrinth
11              
12             =head1 DESCRIPTION
13              
14             Contains all the photo album handling functionality for the Labyrinth
15             framework.
16              
17             =cut
18              
19             #----------------------------------------------------------------------------
20             # Libraries
21              
22 1     1   4 use base qw(Labyrinth::Plugin::Base);
  1         1  
  1         478  
23              
24             use Image::Size;
25              
26             use Labyrinth::Audit;
27             use Labyrinth::DBUtils;
28             use Labyrinth::DTUtils;
29             use Labyrinth::Media;
30             use Labyrinth::MLUtils;
31             use Labyrinth::Support;
32             use Labyrinth::Variables;
33              
34             use Labyrinth::Plugin::Hits;
35              
36             #----------------------------------------------------------------------------
37             # Variables
38              
39             # type: 0 = optional, 1 = mandatory
40             # html: 0 = none, 1 = text, 2 = textarea
41              
42             my %fields = (
43             photoid => { type => 1, html => 0 },
44             pageid => { type => 0, html => 0 },
45             thumb => { type => 0, html => 0 },
46             image => { type => 0, html => 0 },
47             tagline => { type => 0, html => 1 },
48             summary => { type => 0, html => 2 },
49             hide => { type => 0, html => 0 },
50             );
51              
52             my (@mandatory,@allfields);
53             for(keys %fields) {
54             push @mandatory, $_ if($fields{$_}->{type});
55             push @allfields, $_;
56             }
57              
58             my $LEVEL = EDITOR;
59             my $INDEXKEY = 'photoid';
60              
61             my $hits = Labyrinth::Plugin::Hits->new();
62              
63             #----------------------------------------------------------------------------
64             # Public Interface Functions
65              
66             =head1 PUBLIC INTERFACE METHODS
67              
68             =head2 Public Methods
69              
70             =over 4
71              
72             =item List
73              
74             Provides a list of all the public photos within a given photo album.
75              
76             =item View
77              
78             Returns details of a specific photo.
79              
80             =item Random
81              
82             Stores random images in the $tvars{"irand$inx"} template variable array.
83             Number of images stored is determined for configuration settings variable,
84             'random'. If not set, will default to 1 image.
85              
86             =item Gallery
87              
88             Provides a set of images to be used within a gallery pop-up window. Assumes
89             a 3x3 grid of nine images. Includes links for prev & next to further gallery
90             images.
91              
92             Images returned are determined by the given metadata.
93              
94             =item Albums
95              
96             Retrieve a collection of albums and their photos. This is particularly useful
97             when multiple galleries are being displayed. The results are stored in the
98             template variable $tvars{albums}{}{records}, as an array of the
99             photos, as per the List method.
100              
101             =back
102              
103             =cut
104              
105             sub List {
106             my ($pageid,@rs);
107             if($cgiparams{'pid'} || $cgiparams{'pageid'}) {
108             $pageid = $cgiparams{'pid'} || $cgiparams{'pageid'};
109             @rs = $dbi->GetQuery('hash','GetPage',$pageid);
110             } elsif($cgiparams{'path'}) {
111             @rs = $dbi->GetQuery('hash','GetPageByPath',"photos/$cgiparams{'path'}");
112             $pageid = $rs[0]->{pageid} if(@rs);
113             }
114              
115             if(@rs) {
116             # get page details
117             $rs[0]->{month} = isMonth($rs[0]->{month});
118             for(keys %fields) {
119             if($fields{$_}->{html} == 1) { $rs[0]->{$_} = CleanHTML($rs[0]->{$_}); }
120             elsif($fields{$_}->{html} == 2) { $rs[0]->{$_} = CleanTags($rs[0]->{$_}); }
121             }
122              
123             $tvars{album}->{page} = $rs[0];
124              
125             # get photo listing
126             my @recs = $dbi->GetQuery('hash','ListPhotos',$pageid);
127             my $orderno = 1;
128             for my $rec (@recs) {
129             $rec->{tagline} =~ s/'/\'/g if $rec->{tagline};
130             $rec->{orderno} = $orderno++;
131             }
132              
133             $tvars{album}->{records} = \@recs if(@recs);
134             }
135              
136             $tvars{album}->{iid} = 0;
137             }
138              
139             # image page required
140              
141             sub View {
142             my $photoid = $cgiparams{'iid'} || $cgiparams{'photoid'};
143             unless($photoid) {
144             $tvars{errcode} = 'ERROR';
145             return;
146             }
147              
148             # get photo details
149             my @rs = $dbi->GetQuery('hash','GetPhotoDetail',$photoid);
150             unless(@rs) {
151             $tvars{errcode} = 'ERROR';
152             return;
153             }
154              
155             $rs[0]->{tagline} =~ s/'/\'/g if $rs[0]->{tagline};
156             $tvars{photo} = $rs[0];
157              
158             # get page details
159             my $pageid = $rs[0]->{pageid};
160             @rs = $dbi->GetQuery('hash','GetPage',$pageid);
161             unless(@rs) {
162             $tvars{errcode} = 'ERROR';
163             return;
164             }
165              
166             $rs[0]->{month} = isMonth($rs[0]->{month});
167             for(keys %fields) {
168             if($fields{$_}->{html} == 1) { $rs[0]->{$_} = CleanHTML($rs[0]->{$_}); }
169             elsif($fields{$_}->{html} == 2) { $rs[0]->{$_} = CleanTags($rs[0]->{$_}); }
170             }
171              
172             $tvars{page} = $rs[0];
173              
174             my ($orderno,$this,@order) = (0,0);
175             @rs = $dbi->GetQuery('hash','ListPhotos',$pageid);
176             for my $row (@rs) {
177             $this = $orderno if($row->{photoid} eq $photoid);
178             push @order, $row->{photoid};
179             $orderno++;
180             }
181              
182             $tvars{photo}->{prev} = $order[$this-1] unless($this == 0);
183             $tvars{photo}->{next} = $order[$this+1] unless($this == $#order);
184             $tvars{ddmonths} = MonthSelect($tvars{month});
185             $tvars{ddyears} = YearSelect($tvars{years});
186             $tvars{today} = formatDate(7);
187              
188             # Get the size of image
189             if($tvars{photo}->{dimensions}) {
190             my ($size_x) = split("x",$tvars{photo}->{dimensions});
191             $tvars{photo}->{toobig} = 1 if($size_x > $tvars{maxpicwidth});
192             } else {
193             my $file = "$settings{webdir}/photos/$tvars{photo}->{image}";
194             if(-f $file) {
195             my ($size_x) = imgsize($file);
196             $tvars{photo}->{toobig} = 1 if($size_x > $tvars{maxpicwidth});
197             }
198             }
199             }
200              
201             sub Random {
202             my $count = $settings{random} || 1;
203             my @max = $dbi->GetQuery('array','MaxPhotoID');
204             return unless(@max);
205              
206             my $max = $max[0]->[0];
207             if($max <= $count) {
208             foreach my $index (1..$max) {
209             my @rows = $dbi->GetQuery('hash','GetRandomPhoto',$index);
210             next unless(@rows);
211             $tvars{"irand$index"} = $rows[0];
212             }
213             return;
214             }
215              
216             my (%done,@random);
217             srand;
218             while(1) {
219             my $index = int((rand) * $max);
220             next if($done{$index});
221             my @rows = $dbi->GetQuery('hash','GetRandomPhoto',$index);
222             next unless(@rows);
223              
224             # Get the size of image
225             if($rows[0]->{dimensions}) {
226             my ($size_x) = split("x",$rows[0]->{dimensions});
227             $rows[0]->{toobig} = 1 if($size_x > $tvars{randpicwidth});
228             } elsif($rows[0]->{image}) {
229             my $file = "$settings{webdir}/photos/$rows[0]->{image}";
230             if(-f $file) {
231             my ($size_x) = imgsize($file);
232             $rows[0]->{toobig} = 1 if($size_x > $tvars{randpicwidth});
233             }
234             } else {
235             next;
236             }
237              
238             push @random, $rows[0];
239             $done{$index} = 1;
240             last if(@random >= $count);
241             }
242              
243             foreach my $inx (1..$count) {
244             $tvars{"irand$inx"} = $random[($inx-1)];
245             $tvars{"random"} = \@random;
246             }
247             }
248              
249             my @blanks = (
250             { thumb=>'images/blank.png',tagline=>'' },
251             { thumb=>'images/blank.png',tagline=>'' },
252             { thumb=>'images/blank.png',tagline=>'' },
253             { thumb=>'images/blank.png',tagline=>'' },
254             { thumb=>'images/blank.png',tagline=>'' },
255             { thumb=>'images/blank.png',tagline=>'' },
256             { thumb=>'images/blank.png',tagline=>'' },
257             { thumb=>'images/blank.png',tagline=>'' },
258             { thumb=>'images/blank.png',tagline=>'' },
259             );
260              
261             sub Gallery {
262             return unless AccessUser(EDITOR);
263             my $start = $cgiparams{'start'} || 1;
264             my $key = $cgiparams{'searchmeta'} ? 'Meta' : '';
265             my $where;
266              
267             if($cgiparams{'searchmeta'}) {
268             $cgiparams{'searchmeta'} =~ s/[,\s]+/,/g;
269             $cgiparams{'searchmeta'} = join(",", map {"'$_'"} split(",",$cgiparams{'searchmeta'}));
270             $where .= " AND m.tag IN ($cgiparams{'searchmeta'})" if($cgiparams{'searchmeta'});
271             }
272              
273             my @rows = $dbi->GetQuery('hash',$key.'Gallery',{where=>$where},$start);
274             $tvars{next} = $rows[9]->{photoid} unless(@rows < 10);
275             for my $row (@rows) { $row->{thumb} = 'photos/' . $row->{thumb} unless($row->{thumb} =~ m!^images/!) }
276              
277             push @rows, @blanks;
278             $tvars{data} = \@rows if(@rows);
279             my @prev = $dbi->GetQuery('hash',$key.'GalleryMin',{where=>$where},$start);
280             $tvars{prev} = $prev[8]->{photoid} unless(@prev < 9);
281             }
282              
283             sub Albums {
284             return unless($cgiparams{'pages'});
285             my @pages = split(',',$cgiparams{'pages'});
286             for my $page (@pages) {
287             $cgiparams{pageid} = $page;
288             List();
289             $tvars{albums}{$page}{records} = $tvars{album}->{records};
290             }
291             }
292              
293             #----------------------------------------------------------------------------
294             # Administration Interface Functions
295              
296             =head1 ADMIN INTERFACE METHODS
297              
298             =head2 Administration Methods
299              
300             =over 4
301              
302             =item Admin
303              
304             Provides a list of all photos.
305              
306             =item Add
307              
308             Prep for adding a photo.
309              
310             =item Edit
311              
312             Edit details of an existing photo.
313              
314             =item Move
315              
316             Move a photo between albums.
317              
318             =item Save
319              
320             Saves details of a given photo.
321              
322             =item Archive
323              
324             Delete a photo reference if held in the archive photo album, or move to the
325             archive photo album, if held in another album.
326              
327             =back
328              
329             =cut
330              
331             sub Admin {
332             return unless AccessUser($LEVEL);
333             my @rs = $dbi->GetQuery('hash','AdminPhotos');
334             # foreach my $rec (@rs) {
335             # $rec->{title} =~ s/'/\'/g if $rec->{title};
336             # $rec->{tagline} =~ s/'/\'/g if $rec->{tagline};
337             # }
338              
339             $tvars{records} = \@rs if(@rs);
340             }
341              
342             sub Add {
343             return unless AccessUser($LEVEL);
344             my @rs = $dbi->GetQuery('hash','ListPages');
345             $tvars{pages} = $rs[0] if(@rs);
346             }
347              
348             sub Edit {
349             return unless AccessUser($LEVEL);
350             my $photoid = $cgiparams{'iid'};
351              
352             # get page details
353             my @pg = $dbi->GetQuery('hash','ListPages');
354             $tvars{pages} = \@pg if(@pg);
355             # get photo details
356             my @rs = $dbi->GetQuery('hash','GetPhotoDetail',$photoid);
357             if(@rs) {
358             $rs[0]->{tagline} =~ s/'/\'/g if $rs[0]->{tagline};
359             $tvars{record} = $rs[0];
360             }
361              
362             }
363              
364             sub Move {
365             return unless AccessUser($LEVEL);
366             my $photoid = $cgiparams{'iid'};
367             my $pageid = $cgiparams{'pid'};
368             my $oldid = $cgiparams{'oid'};
369              
370             return unless($photoid && $pageid && $oldid);
371              
372             # get page details
373             $dbi->DoQuery('MovePhoto',$pageid,$oldid,$photoid);
374             }
375              
376             sub Save {
377             return unless AccessUser($LEVEL);
378             my @fields;
379              
380             for(keys %fields) {
381             if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
382             elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
383             elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
384             }
385              
386             return if FieldCheck(\@allfields,\@mandatory);
387              
388             $tvars{data}->{hide} = $tvars{data}->{hide} ? 1 : 0;
389             push @fields, $tvars{data}->{$_} for(qw(pageid thumb image tagline hide photoid));
390             $dbi->DoQuery('UpdatePhoto2',@fields);
391              
392             $hits->SetUpdates('album',0,$tvars{data}->{pageid});
393             $tvars{thanks_message} = 'Photo saved successfully.';
394             }
395              
396             sub Archive {
397             return unless AccessUser($LEVEL);
398             return unless $cgiparams{$INDEXKEY};
399              
400             my @rows = $dbi->GetQuery('hash','GetPhotoDetail',$cgiparams{$INDEXKEY});
401             if($rows[0]->{pageid} == 1) {
402             return unless AccessUser(ADMIN);
403             my @photo = $dbi->GetQuery('hash','CheckPhoto',1,$cgiparams{$INDEXKEY});
404             if(@photo && $photo[0]->{count} == 1) { # only delete if no others match
405             DeleteFile( file => "$settings{webdir}/photos/$rows[0]->{image}" );
406             DeleteFile( file => "$settings{webdir}/photos/$rows[0]->{thumb}" );
407             }
408             $dbi->DoQuery('DeletePhoto',$cgiparams{$INDEXKEY});
409             $tvars{thanks_message} = 'Photo deleted successfully.';
410             } else {
411             $dbi->DoQuery('MovePhoto',1,$cgiparams{$INDEXKEY});
412             $tvars{thanks_message} = 'Photo archived successfully.';
413             }
414             }
415              
416             1;
417              
418             __END__