File Coverage

blib/lib/Labyrinth/Plugin/Review/Book.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Labyrinth::Plugin::Review::Book;
2              
3 5     5   98875 use warnings;
  5         12  
  5         242  
4 5     5   27 use strict;
  5         7  
  5         203  
5              
6 5     5   24 use vars qw($VERSION);
  5         10  
  5         410  
7             $VERSION = '0.02';
8              
9             =head1 NAME
10              
11             Labyrinth::Plugin::Review::Book - Book Reviews plugin for the Labyrinth framework
12              
13             =head1 DESCRIPTION
14              
15             Contains all the functionality for book reviews.
16              
17             =cut
18              
19             # -------------------------------------
20             # Library Modules
21              
22 5     5   33 use base qw(Labyrinth::Plugin::Review);
  5         9  
  5         2224  
23              
24 5     5   2571 use Clone qw(clone);
  5         12903  
  5         389  
25 5     5   2584 use WWW::Scraper::ISBN;
  5         60550  
  5         162  
26 5     5   3522 use Labyrinth::Audit;
  0            
  0            
27             use Labyrinth::DBUtils;
28             use Labyrinth::DTUtils;
29             use Labyrinth::MLUtils;
30             use Labyrinth::Media;
31             use Labyrinth::Session;
32             use Labyrinth::Support;
33             use Labyrinth::Users;
34             use Labyrinth::Variables;
35              
36             use Data::Dumper;
37              
38             # -------------------------------------
39             # Constants
40              
41             use constant FRONTPAGE => 10;
42             use constant COVER_WIDTH => 150;
43             use constant COVER_HEIGHT => 200;
44              
45             # -------------------------------------
46             # Variables
47              
48             # type: 0 = optional, 1 = mandatory
49             # html: 0 = none, 1 = text, 2 = textarea
50              
51             my %fields = (
52             # review references
53             reviewid => { type => 0, html => 0 },
54             title => { type => 1, html => 1 },
55             userid => { type => 1, html => 0 },
56             reviewtypeid => { type => 1, html => 0 },
57             postdate => { type => 1, html => 0 },
58             imageid => { type => 0, html => 0 },
59             snippet => { type => 0, html => 2 },
60             body => { type => 1, html => 2 },
61             publish => { type => 1, html => 0 },
62             additional => { type => 0, html => 1 },
63              
64             # item references
65             author => { type => 1, html => 1 }, # aka brand
66             isbn => { type => 1, html => 0 }, # aka itemcode
67             publisherid => { type => 1, html => 0 }, # aka retailerid
68             book_link => { type => 1, html => 0 }, # aka itemlink
69             );
70              
71             my (@mandatory,@allfields);
72             for(keys %fields) {
73             push @mandatory, $_ if($fields{$_}->{type});
74             push @allfields, $_;
75             }
76              
77             # -------------------------------------
78             # The Subs
79              
80             =head1 PUBLIC INTERFACE METHODS
81              
82             =over 4
83              
84             =item * FrontPage
85              
86             Provides the an abbreviated list of the latest reviews to use on the front
87             page or a side panel of the website.
88              
89             =item * List
90              
91             Provides a full list, filtered based on any search criteria, of reviews for
92             the main reviews page on the website.
93              
94             =item * Item
95              
96             Provides all the details for a specific review.
97              
98             =back
99              
100             =cut
101              
102             sub List {
103             my $publish = 3 unless($tvars{command} eq 'admin');
104              
105             my @where;
106             push @where, "r.publish=$publish" if($publish);
107             push @where, "r.title LIKE '%$cgiparams{'searchtitle'}%'" if($cgiparams{'searchtitle'});
108             push @where, "r.reviewtypeid=$cgiparams{'reviewtypeid'}" if($cgiparams{'reviewtypeid'});
109             push @where, "r.userid=$cgiparams{'userid'}" if($cgiparams{'userid'});
110             push @where, "r.brand=$cgiparams{'author'}" if($cgiparams{'author'});
111             push @where, "r.retailerid=$cgiparams{'publisherid'}" if($cgiparams{'publisherid'});
112             my $where = @where ? 'WHERE '.join(' AND ',@where) : '';
113              
114             my @rows = $dbi->GetQuery('hash','AllReviews',{where=>$where});
115             foreach (@rows) {
116             $_->{publishstate} = PublishState($_->{publish});
117             $_->{postdate} = formatDate(3,$_->{createdate});
118             }
119             $tvars{data} = \@rows if(@rows);
120              
121             $tvars{searchtitle} = $cgiparams{searchtitle};
122             $tvars{ddreviewers} = UserSelect($cgiparams{userid},1,1,'Reviewer');
123             $tvars{ddrevtypes} = _dropdownReviewTypes($cgiparams{reviewtypeid},1);
124             $tvars{ddpublishers}= _dropdownRetailers($cgiparams{publisherid},1);
125             }
126              
127             =head1 ADMIN INTERFACE METHODS
128              
129             =over 4
130              
131             =item * Access
132              
133             Check default access to the Admin methods
134              
135             =item * ImageCheck
136              
137             Returns true or false as to whether the given image is referenced wihin a
138             review.
139              
140             Currently this method always returns false.
141              
142             =item * Admin
143              
144             List reviews for administration purposes.
145              
146             =item * Add
147              
148             Add a review.
149              
150             =item * PreEdit
151              
152             Following an add review page, there may be additional lookuos required to
153             check the validity of the product code given. This may be an additonal API
154             call to a 3rd party website, and is most likely to be used by sub-classed
155             modules.
156              
157             Requires a call to the Edit method to display current values.
158              
159             =item * Copy
160              
161             Copies an existing review to a new review. Typical when you want to use the
162             same product to review, and just want to use the same product references.
163              
164             Requires a call to the Edit method to display current values.
165              
166             =item * Edit
167              
168             Edit a review.
169              
170             =item * Save
171              
172             Save a review.
173              
174             =item * Delete
175              
176             Delete one or more reviews.
177              
178             =back
179              
180             =cut
181              
182             sub Access { Authorised(EDITOR) }
183             sub ImageCheck { return 0 }
184              
185             sub Add {
186             AccessUser(EDITOR);
187              
188             my $select;
189             if(Authorised(PUBLISHER)) {
190             $select = UserSelect(0,1);
191             } else {
192             $select = DropDownRows($tvars{user}->{userid},'userid','userid','name',{userid => $tvars{user}->{userid}, name => $tvars{user}->{name}});
193             }
194              
195             my %data = (
196             ddreviewers => $select,
197             ddpublishers => _dropdownRetailers(),
198             ddrevtypes => _dropdownReviewTypes(),
199             postdate => formatDate(3),
200             );
201              
202             $tvars{data} = \%data;
203             $tvars{preedit} = 1;
204             }
205              
206             sub PreEdit {
207             my @man = qw(userid reviewtypeid postdate isbn);
208             return if FieldCheck(\@mandatory,\@mandatory);
209              
210             $tvars{data}->{realname} = UserName($tvars{data}->{userid});
211             $tvars{data}->{createdate} = unformatDate(3,$tvars{data}->{postdate});
212              
213             my $scraper = WWW::Scraper::ISBN->new();
214             my @drivers = $scraper->available_drivers;
215             $scraper->drivers(@drivers);
216              
217             $tvars{data}->{isbn} =~ tr/0-9X//cd;
218             LogDebug("isbn=[$tvars{data}->{isbn}]");
219             my $result = $scraper->search($tvars{data}->{isbn});
220              
221             if($result->found) {
222             my @fields = qw(isbn title author book_link image_link publisher);
223             my $book = $result->book;
224             $tvars{data}->{$_} = $book->{$_} for(@fields);
225              
226             # do we have an image?
227             if($book->{image_link}) {
228             #LogDebug("thumb=[$book->{image_link}]");
229             ($tvars{data}->{imageid},$tvars{data}->{image_link}) = MirrorImageFile($book->{image_link},'Covers',COVER_WIDTH,COVER_HEIGHT);
230             #LogDebug("image=[$tvars{data}->{image_link}]");
231             }
232              
233             # do we have the publisher in the DB
234             my @rows = $dbi->GetQuery('hash','FindPublisher',$book->{publisher});
235             $tvars{data}->{publisherid} = $rows[0]->{publisherid} if(@rows);
236              
237             # O'Reilly have defined Errata links.
238             if(defined $tvars{data}->{publisher} && $tvars{data}->{publisher} =~ /Reilly/) {
239             $tvars{data}->{additional} = sprintf "Errata=%s/errata/",dirname($tvars{data}->{book_link});
240             }
241             }
242              
243             # use Data::Dumper;
244             # LogDebug(Dumper($tvars{data}));
245             }
246              
247             sub Copy {
248             $cgiparams{'reviewid'} = $cgiparams{'LISTED'};
249             return unless AuthorCheck('GetReviewByID','reviewid',PUBLISHER);
250              
251             my @fields = (
252             $tvars{data}->{reviewtypeid},
253             0,
254             $tvars{data}->{title},
255             $tvars{data}->{author},
256             $tvars{data}->{isbn},
257             $tvars{data}->{userid},
258             $tvars{data}->{createdate},
259             $tvars{data}->{snippet},
260             $tvars{data}->{body},
261             $tvars{data}->{imageid},
262             $tvars{data}->{book_link},
263             $tvars{data}->{publisherid},
264             1
265             );
266              
267             $cgiparams{reviewid} = $dbi->IDQuery('AddReview',@fields);
268              
269             $tvars{errcode} = 'NEXT';
270             $tvars{command} = 'revs-edit';
271             }
272              
273             sub Edit {
274             if($cgiparams{reviewid}) {
275             return unless AuthorCheck('GetReviewByID','reviewid',PUBLISHER);
276              
277             if($tvars{data}->{'imageid'}) {
278             my @img = $dbi->GetQuery('hash','GetImageByID',$tvars{data}->{'imageid'});
279             $tvars{data}->{'image_link'} = $img[0]->{'link'} if(@img);
280             }
281              
282             if($tvars{data}->{publish} == 4 && $tvars{command} ne 'view') {
283             $tvars{errcode} = 'FAILURE';
284             return;
285             }
286             }
287              
288             my $select;
289             if(Authorised(PUBLISHER)) {
290             $select = UserSelect($tvars{data}->{userid},1,1);
291             } else {
292             $select = DropDownRows($tvars{user}->{userid},'userid','userid','name',{userid => $tvars{user}->{userid}, name => $tvars{user}->{name}});
293             }
294              
295             my $promote = 0;
296             $promote = 1 if($tvars{data}->{publish} == 1 && Authorised(EDITOR));
297             $promote = 1 if($tvars{data}->{publish} == 2 && Authorised(PUBLISHER));
298             $promote = 1 if($tvars{data}->{publish} == 3 && Authorised(PUBLISHER));
299             $tvars{data}->{ddpublish} = PublishAction($tvars{data}->{publish},$promote);
300             $tvars{data}->{ddpublish} = PublishSelect($tvars{data}->{publish}) if(Authorised(ADMIN));
301              
302             $tvars{data}->{ddreviewers} = $select;
303             $tvars{data}->{ddpublishers} = _dropdownRetailers($tvars{data}->{publisherid});
304             $tvars{data}->{ddrevtypes} = _dropdownReviewTypes($tvars{data}->{reviewtypeid});
305             $tvars{data}->{postdate} = formatDate(3,$tvars{data}->{createdate});
306              
307             $tvars{preview} = clone($tvars{data}); # data fields need to be editable
308              
309             for(keys %fields) {
310             if($fields{$_}->{html} == 1) { $tvars{data}->{$_} = CleanHTML($tvars{data}->{$_});
311             $tvars{preview}->{$_} = CleanHTML($tvars{preview}->{$_}); }
312             elsif($fields{$_}->{html} == 2) { $tvars{data}->{$_} = SafeHTML($tvars{data}->{$_}); }
313             }
314              
315             if($tvars{data}->{additional}) {
316             my $html = '';
317             my @links = split ",", $tvars{data}->{additional};
318             foreach my $link (@links) {
319             my ($name,$url) = split "=", $link;
320             $html .= qq!$name
!;
321             }
322             $tvars{preview}->{additional} = $html;
323             }
324             }
325              
326             sub Save {
327             return unless AuthorCheck('GetReviewByID','reviewid',PUBLISHER);
328             return if FieldCheck(\@allfields,\@mandatory);
329              
330             for(keys %fields) {
331             if($fields{$_}->{html} == 1) { $tvars{data}->{$_} = CleanHTML($tvars{data}->{$_}) }
332             elsif($fields{$_}->{html} == 2) { $tvars{data}->{$_} = CleanTags($tvars{data}->{$_}) }
333             }
334              
335             $tvars{data}->{createdate} = unformatDate(3,$tvars{data}->{postdate}) if($tvars{data}->{postdate});
336             ($tvars{data}->{imageid}) = SaveImageFile(
337             param => 'image_link',
338             width => $settings{review_cover_width} || COVER_WIDTH,
339             height => $settings{review_cover_height} || COVER_HEIGHT,
340             stock => 'Covers'
341             ) if($cgiparams{image_link});
342              
343             my @fields = (
344             $tvars{data}->{reviewtypeid},
345             0,
346             $tvars{data}->{title},
347             $tvars{data}->{author},
348             $tvars{data}->{isbn},
349             $tvars{data}->{userid},
350             $tvars{data}->{createdate},
351             $tvars{data}->{snippet},
352             $tvars{data}->{body},
353             $tvars{data}->{imageid},
354             $tvars{data}->{book_link},
355             $tvars{data}->{publisherid},
356             $tvars{data}->{publish}
357             );
358              
359             # store review details
360             if($tvars{data}->{reviewid})
361             { $dbi->DoQuery('SaveReview',@fields,$tvars{data}->{reviewid}); }
362             else { $cgiparams{reviewid} = $dbi->IDQuery('AddReview',@fields); }
363              
364             $tvars{thanks} = 1;
365             }
366              
367             sub _dropdownRetailers {
368             my ($select,$blank) = @_;
369             my @rows = $dbi->GetQuery('hash','AllRetailers');
370             unshift @rows, {retailerid=>0,retailer=>'Select Publisher'} if(defined $blank && $blank == 1);
371             return DropDownRows($select,'publisherid','retailerid','retailer',@rows);
372             }
373              
374             1;
375              
376             __END__