File Coverage

blib/lib/Labyrinth/Plugin/News.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::News;
2              
3 2     2   6373 use warnings;
  2         12  
  2         59  
4 2     2   8 use strict;
  2         2  
  2         75  
5              
6             my $VERSION = '5.17';
7              
8             =head1 NAME
9              
10             Labyrinth::Plugin::News - Plugin News handler for Labyrinth
11              
12             =head1 DESCRIPTION
13              
14             Contains all the news handling functionality for the Labyrinth
15             framework.
16              
17             =cut
18              
19             # -------------------------------------
20             # Library Modules
21              
22 2     2   6 use base qw(Labyrinth::Plugin::Base);
  2         1  
  2         680  
23              
24             use Clone qw(clone);
25              
26             use Labyrinth::Globals;
27             use Labyrinth::DBUtils;
28             use Labyrinth::DTUtils;
29             use Labyrinth::Media;
30             use Labyrinth::MLUtils;
31             use Labyrinth::Session;
32             use Labyrinth::Support;
33             use Labyrinth::Users;
34             use Labyrinth::Variables;
35              
36             # -------------------------------------
37             # Constants
38              
39             use constant FRONTPAGE => 5;
40             use constant MAINNEWS => 5;
41             use constant INBRIEF => 10;
42              
43             # -------------------------------------
44             # Variables
45              
46             # type: 0 = optional, 1 = mandatory
47             # html: 0 = none, 1 = text, 2 = textarea
48              
49             my %fields = (
50             newsid => { type => 0, html => 0 },
51             folderid => { type => 0, html => 0 },
52             title => { type => 1, html => 1 },
53             userid => { type => 0, html => 0 },
54             body => { type => 1, html => 2 },
55             postdate => { type => 1, html => 0 },
56             imageid => { type => 0, html => 0 },
57             ALIGN0 => { type => 0, html => 0 },
58             front => { type => 0, html => 0 },
59             alttag => { type => 0, html => 1 },
60             publish => { type => 1, html => 0 },
61             href => { type => 0, html => 1 },
62             );
63              
64             my (@mandatory,@allfields);
65             for(keys %fields) {
66             push @mandatory, $_ if($fields{$_}->{type});
67             push @allfields, $_;
68             }
69              
70             my @savefields = qw(folderid userid title body imageid ALIGN0 front publish createdate);
71             my @addfields = qw(folderid userid title body imageid ALIGN0 front publish createdate);
72             my $INDEXKEY = 'newsid';
73             my $ALLSQL = 'AllNews';
74             my $SAVESQL = 'SaveNews';
75             my $ADDSQL = 'AddNews';
76             my $GETSQL = 'GetNewsByID';
77             my $DELETESQL = 'DeleteNews';
78             my $PROMOTESQL = 'PromoteNews';
79             my $LEVEL = EDITOR;
80             my $LEVEL2 = ADMIN;
81             my $NEXTCOMMAND = 'news-edit';
82              
83             # -------------------------------------
84             # The Subs
85              
86             =head1 PUBLIC INTERFACE METHODS
87              
88             =over 4
89              
90             =item Front
91              
92             Front page news items only.
93              
94             =item Main
95              
96             Main news items in full, section in brief, the remainder just titles.
97              
98             =item Archive
99              
100             Archived news items only.
101              
102             =item Item
103              
104             Single specified news items in full.
105              
106             =back
107              
108             =cut
109              
110             sub Front {
111             my $front = $settings{'frontpage'} || FRONTPAGE;
112             my (@mainnews);
113             my @rows = $dbi->GetQuery('hash','FrontNews',{limit => "LIMIT $front"});
114             for my $row (@rows) {
115             $row->{name} = UserName($row->{userid});
116             $row->{postdate} = formatDate(3,$row->{createdate});
117             $row->{alignment} = AlignClass($row->{align});
118             }
119             $tvars{frontnews} = \@rows if(@rows);
120             }
121              
122             sub Main {
123             my $main = $settings{'mainnews'} || MAINNEWS;
124             my $brief = $settings{'inbrief'} || INBRIEF;
125             my (@mainnews,@inbrief,@archive);
126             my @rows = $dbi->GetQuery('hash','PubNews');
127             foreach my $row (@rows) {
128             last unless($brief);
129             # first n stories in full
130             if($main) {
131             $row->{name} = UserName($row->{userid});
132             $row->{postdate} = formatDate(3,$row->{createdate});
133             $row->{alignment} = AlignClass($row->{align});
134             push @mainnews, $row;
135             $main--;
136             next;
137             }
138             # next n stories in brief
139             if($brief) {
140             push @inbrief, {newsid => $row->{newsid}, title => $row->{title}, snippet => substr(CleanHTML($row->{body}),0,(82-length($row->{title})))};
141             $brief--;
142             next;
143             }
144             # remaining stories, just titles
145             push @archive, {newsid => $row->{newsid}, title => $row->{title}};
146             }
147             $tvars{news}{main} = \@mainnews if(@mainnews);
148             $tvars{news}{brief} = \@inbrief if(@inbrief);
149             $tvars{news}{other} = \@archive if(@archive);
150             }
151              
152             sub Archive {
153             my (@archive);
154             my @rows = $dbi->GetQuery('hash','OldNews');
155             foreach my $row (@rows) {
156             push @archive, {newsid => $row->{newsid}, title => $row->{title}, snippet => substr(CleanHTML($row->{body}),0,(82-length($row->{title})))};
157             }
158             $tvars{archive} = \@archive if(@archive);
159             }
160              
161             sub Item {
162             return unless($cgiparams{'newsid'});
163             my @rows = $dbi->GetQuery('hash','GetNewsByID',$cgiparams{'newsid'});
164             if(@rows) {
165             $rows[0]->{name} = UserName($rows[0]->{userid});
166             $rows[0]->{postdate} = formatDate(3,$rows[0]->{createdate});
167             $rows[0]->{alignment} = AlignClass($rows[0]->{align});
168             $tvars{news}{item} = $rows[0];
169             }
170             }
171              
172             =head1 ADMIN INTERFACE METHODS
173              
174             =over 4
175              
176             =item Access
177              
178             Check whether user has access to admin functions. Must be Editor or greater.
179              
180             =item ImageCheck
181              
182             Checks whether images are being referenced in a news item. Used to allow the
183             images plugin to delete unused images.
184              
185             =item Admin
186              
187             List new items.
188              
189             =item Add
190              
191             Create a template variable hash to create a news item.
192              
193             =item Edit
194              
195             Edit the given news item.
196              
197             =item Copy
198              
199             Copy a specified news item to create a new one. Called via Admin().
200              
201             =item EditAmendments
202              
203             Provide additional drop downs and fields for editing.
204              
205             =item Promote
206              
207             Promote the given news item.
208              
209             =item Save
210              
211             Save the given news item.
212              
213             =item Delete
214              
215             Delete the listed news items. Called via Admin().
216              
217             =back
218              
219             =cut
220              
221             sub Access { Authorised(EDITOR) }
222              
223             sub ImageCheck {
224             my @rows = $dbi->GetQuery('array','NewsImageCheck',$_[0]);
225             @rows ? 1 : 0;
226             }
227              
228             sub Admin {
229             return unless AccessUser(EDITOR);
230             if($cgiparams{doaction}) {
231             if($cgiparams{doaction} eq 'Delete') { Delete(); }
232             elsif($cgiparams{doaction} eq 'Copy') { Copy(); }
233             }
234             # (un)check front paged items
235             my @front = CGIArray('FRONT');
236             if(@front) {
237             my @check = $dbi->GetQuery('hash','CheckFrontPageNews');
238             my %check = map {$_->{newsid} => 1} @check;
239             for my $id (@front) {
240             if($check{$id}) {
241             $check{$id} = 0;
242             } else {
243             $dbi->DoQuery('SetFrontPageNews',$id);
244             }
245             }
246             for my $id (keys %check) {
247             next unless($check{$id});
248             $dbi->DoQuery('ClearFrontPageNews',$id);
249             }
250             }
251             # selected or default (Draft/Submitted/Published)
252             my $publish = $cgiparams{'publish'} || '1,2,3';
253             my @where;
254             push @where, "userid=$tvars{'loginid'}" unless(Authorised(PUBLISHER));
255             push @where, "publish IN ($publish)" if($publish);
256             my $where = @where ? 'WHERE '.join(' AND ',@where) : '';
257             my @rows = $dbi->GetQuery('hash','AllNews',{where=>$where});
258             foreach my $row (@rows) {
259             $row->{publishstate} = PublishState($row->{publish});
260             $row->{name} = UserName($row->{userid});
261             $row->{postdate} = formatDate(3,$row->{createdate});
262             }
263             $tvars{data} = \@rows;
264             $publish = $cgiparams{'publish'};
265             $tvars{ddpublish} = PublishSelect($publish,1);
266             }
267              
268             sub Add {
269             return unless AccessUser($LEVEL);
270             my $ddpublish;
271             if(Authorised($LEVEL2)) {
272             $ddpublish = PublishSelect($tvars{data}->{publish});
273             } else {
274             $ddpublish = PublishAction(1,1);
275             }
276             my %data = (
277             folderid => 1,
278             title => '',
279             userid => $tvars{loginid},
280             name => $tvars{user}->{name},
281             postdate => formatDate(3),
282             body => '',
283             link => 'images/blank.png',
284             alttag => '',
285             ddpublish => $ddpublish,
286             ddalign => AlignSelect(1),
287             imageid => 1,
288             align => 1,
289             href => '',
290             front => 0,
291             );
292             $tvars{data} = \%data;
293             }
294              
295             sub Edit {
296             return unless $cgiparams{$INDEXKEY};
297             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
298             EditAmendments();
299             }
300              
301             sub Copy {
302             $cgiparams{'newsid'} = $cgiparams{'LISTED'};
303             return unless AuthorCheck('GetNewsByID','newsid',EDITOR);
304              
305             my @fields = ( $tvars{data}->{folderid},
306             $tvars{data}->{title},
307             $tvars{data}->{body},
308             $tvars{data}->{imageid},
309             $tvars{data}->{align},
310             $tvars{data}->{href},
311             $tvars{data}->{alttag},
312             1,
313             formatDate(0),
314             $tvars{loginid});
315              
316             $cgiparams{newsid} = $dbi->IDQuery('AddNews',@fields);
317             #LogDebug("Copy: newsid=[$cgiparams{newsid}]");
318             SetCommand('news-edit');
319             }
320              
321             sub EditAmendments {
322             $tvars{data}->{align} = $cgiparams{ALIGN0} if $cgiparams{ALIGN0};
323             $tvars{data}->{alignment} = AlignClass($tvars{data}->{align});
324             $tvars{data}->{ddalign} = AlignSelect($tvars{data}->{align});
325             $tvars{data}->{ddpublish} = PublishSelect($tvars{data}->{publish});
326             $tvars{data}->{name} = UserName($tvars{data}->{userid});
327             $tvars{data}->{postdate} = formatDate(3,$tvars{data}->{createdate});
328             $tvars{data}->{body} =~ s/^\s+//;
329              
330             if(Authorised($LEVEL2)) {
331             $tvars{data}->{ddpublish} = PublishSelect($tvars{data}->{publish});
332             } else {
333             my $promote = 0;
334             $promote = 1 if($tvars{data}->{publish} == 1);
335             $promote = 1 if($tvars{data}->{publish} == 2 && AccessUser(PUBLISHER));
336             $promote = 1 if($tvars{data}->{publish} == 3 && AccessUser(PUBLISHER));
337             $tvars{data}->{ddpublish} = PublishAction($tvars{data}->{publish},$promote);
338             }
339              
340             $tvars{preview} = clone($tvars{data}); # data fields need to be editable
341              
342             for(keys %fields) {
343             if($fields{$_}->{html} == 1) { $tvars{preview}->{$_} = CleanHTML($tvars{preview}->{$_});
344             $tvars{data}->{$_} = CleanHTML($tvars{data}->{$_}); }
345             elsif($fields{$_}->{html} == 2) { $tvars{data}->{$_} = SafeHTML($tvars{data}->{$_}); }
346             }
347             }
348              
349             sub Promote {
350             return unless AccessUser($LEVEL);
351             my @ids = CGIArray('LISTED');
352             return unless @ids;
353              
354             for my $id (@ids) {
355             $cgiparams{$INDEXKEY} = $id;
356             next unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
357             my $publish = $tvars{data}->{publish} + 1;
358             next unless($publish < 5);
359             $dbi->DoQuery($PROMOTESQL,$publish,$cgiparams{$INDEXKEY});
360             }
361             }
362              
363             sub Save {
364             return unless AuthorCheck('GetNewsByID','newsid',EDITOR);
365             my $publish = $tvars{data}->{publish} || 0;
366              
367             EditAmendments();
368             for(keys %fields) {
369             if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
370             elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
371             elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
372             }
373             return if FieldCheck(\@allfields,\@mandatory);
374              
375             if($cgiparams{image}) {
376             ($tvars{data}->{'imageid'}) =
377             SaveImageFile(
378             param => 'image',
379             stock => 'Special',
380             href => $tvars{data}->{href},
381             alttag => $tvars{data}->{alttag}
382             );
383             }
384              
385             $tvars{data}->{createdate} = formatDate(0) if($tvars{data}->{publish} == 3 && $publish < 3);
386             $tvars{data}->{createdate} = unformatDate(3,$tvars{data}->{postdate});
387             $tvars{data}->{front} = $tvars{data}->{front} ? 1 : 0;
388             $tvars{data}->{latest} = $tvars{data}->{latest} ? 1 : 0;
389             $tvars{data}->{folderid} ||= 1;
390             $tvars{data}->{imageid} ||= 1;
391              
392             my @fields = ( $tvars{data}->{folderid},
393             $tvars{data}->{title},
394             $tvars{data}->{snippet},
395             $tvars{data}->{body},
396             $tvars{data}->{imageid},
397             $tvars{data}->{align},
398             $tvars{data}->{front},
399             $tvars{data}->{latest},
400             $tvars{data}->{publish},
401             $tvars{data}->{createdate},
402             );
403              
404             if($tvars{data}->{newsid})
405             { $dbi->DoQuery('SaveNews',@fields,$tvars{data}->{newsid}); }
406             else { $cgiparams{newsid} = $dbi->IDQuery('AddNews',@fields,$tvars{loginid}); }
407             $tvars{thanks} = 1;
408             }
409              
410             sub Delete {
411             return unless AccessUser($LEVEL2);
412             my @ids = CGIArray('LISTED');
413             return unless @ids;
414             for my $id (@ids) {
415             $cgiparams{'newsid'} = $id;
416             next unless AuthorCheck('GetNewsByID','newsid',EDITOR);
417             $dbi->DoQuery('DeleteNews',$cgiparams{'newsid'});
418             }
419             }
420              
421             1;
422              
423             __END__