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