/
Menus.pm
499 lines (400 loc) · 16 KB
/
Menus.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package Labyrinth::Plugin::Menus;
use warnings;
use strict;
my $VERSION = '5.11';
=head1 NAME
Labyrinth::Plugin::Menus - Plugin Menus handler for Labyrinth
=head1 DESCRIPTION
Contains all the menu handling functionality for the Labyrinth
framework.
=cut
# -------------------------------------
# Library Modules
use base qw(Labyrinth::Plugin::Base);
use Labyrinth::Audit;
use Labyrinth::Globals qw(:default);
use Labyrinth::DBUtils;
use Labyrinth::Media;
use Labyrinth::MLUtils;
use Labyrinth::Session;
use Labyrinth::Support;
use Labyrinth::Variables;
# -------------------------------------
# Constants
use constant MaxMenuWidth => 400;
use constant MaxMenuHeight => 400;
# -------------------------------------
# Variables
# type: 0 = optional, 1 = mandatory
# html: 0 = none, 1 = text, 2 = textarea
my %fields = (
menuid => { type => 0, html => 0 },
name => { type => 1, html => 1 },
realmid => { type => 1, html => 0 },
typeid => { type => 1, html => 0 },
title => { type => 0, html => 1 },
parentid => { type => 0, html => 0 },
);
my (@mandatory,@allfields);
for(keys %fields) {
push @mandatory, $_ if($fields{$_}->{type});
push @allfields, $_;
}
my @savefields = qw(name title typeid realmid parentid);
my $INDEXKEY = 'menuid';
my $ALLSQL = 'AllMenus';
my $SAVESQL = 'SaveMenu';
my $ADDSQL = 'AddMenu';
my $GETSQL = 'GetMenuByID';
my $DELETESQL = 'DeleteMenu';
my $LEVEL = ADMIN;
my %adddata = (
menuid => 0,
realmid => 0,
type => 0,
title => '',
);
# -------------------------------------
# The Subs
=head1 PUBLIC INTERFACE METHODS
=head2 Default Methods
=over 4
=item LoadMenus
=back
=cut
sub LoadMenus {
# get menu list for current realm
my @rows = $dbi->GetQuery('hash','GetMenus',RealmID($tvars{realm}));
$tvars{menus} = undef; # in case we're reloading
my $request = $ENV{REQUEST_URI};
my $script = $settings{script};
my (%match,%tree);
# LogDebug("script=[$script]");
# for each menu get option list
for my $row (@rows) {
$tvars{menus}->{$row->{menuid}}->{title} = $row->{title};
$tvars{menus}->{$row->{menuid}}->{typeid} = $row->{typeid};
$tvars{menus}->{$row->{menuid}}->{parentid} = $row->{parentid};
my @opts = $dbi->GetQuery('hash','GetOptions',$row->{menuid});
for my $opt (@opts) {
my @images;
if($row->{typeid} > 1) {
my @rs = $dbi->GetQuery('hash','GetOptImages',$opt->{optionid});
@images = map {$_->{'link'}} @rs;
}
#LogDebug("request=$request, opt=$opt->{href}");
#LogDebug("section=$tvars{section}, opt=$opt->{section}");
# try and find the selected option
my $inx = $tvars{menus}->{$row->{menuid}}->{data} ? scalar(@{$tvars{menus}->{$row->{menuid}}->{data}}) : 0;
$match{request} = [$row->{menuid},$inx,$opt->{optionid}] if($request && $request eq $opt->{href});
$match{section} = [$row->{menuid},$inx,$opt->{optionid}] if($opt->{section} && $tvars{section} eq $opt->{section});
$match{options} = [$row->{menuid},$inx,$opt->{optionid}] if($request && $request =~ /$opt->{href}/);
$match{hideout} = [$row->{menuid},$inx,$opt->{optionid}] if($opt->{section} && ($opt->{section} eq 'home' || $opt->{href} eq '/'));
$match{default} = [$row->{menuid},$inx,$opt->{optionid}] if(!$match{default});
$opt->{href} =~ s!^\?!$script\?!; # all query only links are local
# $opt->{href} =~ s!^/$script!$tvars{cgipath}/$script!; # all script links are local
push @{$tvars{menus}->{$row->{menuid}}->{data}},
[ 0,0,
$opt->{text},
$opt->{href},
($opt->{accessid}||0),
$opt->{name},
@images
];
$tree{$opt->{optionid}} = { menuid => $row->{menuid}, index => scalar(@{$tvars{menus}->{$row->{menuid}}->{data}}) - 1, parent => $row->{parentid} };
}
}
#LogDebug("match{$_}=$match{$_}[0]/$match{$_}[2]") for(qw(request section options hideout default));
my $match = 'default';
if($match{request}[0]) { $match = 'request'; }
elsif($match{section}[0]) { $match = 'section'; }
elsif($match{options}[0]) { $match = 'options'; }
elsif($match{hideout}[0]) { $match = 'hideout'; }
UpdateSession(optionid => $match{$match}->[2]);
# previous/next trail
$tvars{trail2}{prev} = _trail2($match{$match},-1);
$tvars{trail2}{this} = _trail2($match{$match}, 0);
$tvars{trail2}{next} = _trail2($match{$match}, 1);
$tvars{trail1} = undef;
# breadcrumbs trail
my $menu = 0;
my $opt = $match{$match}->[2];
$tvars{menus}->{$tree{$opt}{menuid}}->{data}->[$tree{$opt}{index}][1] = 1; # option has been selected
# use Data::Dumper;
# LogDebug("opt=$opt");
# LogDebug("tree=".Dumper(\%tree));
for(keys %tree) {
next unless(defined $opt && defined $tree{$_} && defined $tree{$_}->{parent});
$menu = $tree{$_}->{menuid} if($opt == $tree{$_}->{parent});
}
while($opt) {
$tvars{menus}->{$tree{$opt}{menuid}}->{data}->[$tree{$opt}{index}][0] = $menu; # submenu has been selected
push @{$tvars{trail1}},
{
text => $tvars{menus}->{$tree{$opt}{menuid}}->{data}->[$tree{$opt}{index}][2],
href => $tvars{menus}->{$tree{$opt}{menuid}}->{data}->[$tree{$opt}{index}][3]
};
$menu = $tree{$opt}{menuid};
$opt = $tree{$opt}{parent};
}
}
sub _trail2 {
my ($hash,$diff) = @_;
return unless(defined $hash && @$hash);
my %hash;
my $opt = $hash->[1] + $diff;
if($opt >= 0 && $opt < scalar(@{$tvars{menus}->{$hash->[0]}->{data}})) {
$hash{text} = $tvars{menus}->{$hash->[0]}->{data}->[$opt][2];
$hash{href} = $tvars{menus}->{$hash->[0]}->{data}->[$opt][3];
}
return \%hash;
}
sub _LoadMenus {
# get menu list for current realm
my @rows = $dbi->GetQuery('hash','GetMenus',RealmID($tvars{realm}));
$tvars{menus} = undef; # in case we're reloading
my $request = $ENV{REQUEST_URI};
my $script = $settings{script};
my (%tree,$last,$href);
# LogDebug("script=[$script]");
# for each menu get option list
for my $row (@rows) {
$tvars{menus}->{$row->{menuid}}->{title} = $row->{title};
$tvars{menus}->{$row->{menuid}}->{typeid} = $row->{typeid};
$tvars{menus}->{$row->{menuid}}->{parentid} = $row->{parentid};
$last = '';
$href = '';
my @opts = $dbi->GetQuery('hash','GetOptions',$row->{menuid});
for my $opt (@opts) {
my @images;
if($row->{typeid} > 1) {
my @rs = $dbi->GetQuery('hash','GetOptImages',$opt->{optionid});
@images = map {$_->{'link'}} @rs;
}
UpdateSession(optionid => $opt->{optionid}) if($request && $request =~ /$opt->{href}/);
$opt->{href} =~ s!^\?!$script\?!; # all query only links are local
# $opt->{href} =~ s!^/$script!$tvars{cgipath}/$script!; # all script links are local
# establish the current level trail
if($tvars{trail2} && !$tvars{trail2}{'next'}) {
$tvars{trail2}{'next'} = {text => $opt->{text}, href => $opt->{href}};
}
if($opt->{optionid} == $tvars{user}{option}) {
$tvars{trail2} = { 'prev' => {text => $last, href => $href},
'this' => {text => $opt->{text}}};
} else {
$last = $opt->{text};
$href = $opt->{href};
}
push @{$tvars{menus}->{$row->{menuid}}->{data}}, [0,0,$opt->{text},$opt->{href},($opt->{accessid}||0),@images];
$tree{$opt->{optionid}} = {
minx => $row->{menuid},
oinx => (scalar(@{$tvars{menus}->{$row->{menuid}}->{data}}) - 1),
opar => $row->{parentid}
};
}
}
# use Data::Dumper;
# LogDebug("LoadMenus: tree=".Dumper(\%tree));
# now establish the main trail
if($tvars{user}{option}) {
my $option = $tvars{user}{option};
for my $opt (keys %tree) {
if($tree{$opt}->{opar} == $option) {
$tvars{menus}{$tree{$option}->{minx}}{data}[$tree{$option}->{oinx}][0] = $tree{$opt}->{minx};
LogDebug("LoadMenus: option=$option, opt=$opt, minx=$tree{$option}->{minx}, oinx=$tree{$option}->{oinx}, minx=$tree{$opt}->{minx}");
# menu has a sub menu
last;
}
}
while($option) {
my $minx = $tree{$option}->{minx};
my $oinx = $tree{$option}->{oinx};
if($minx && $oinx) {
$tvars{menus}{$minx}{data}[$oinx][1] = 1; # menu has been selected
unshift @{$tvars{trail1}}, # breadcrumbs trail
{text => $tvars{menus}{$minx}{data}[$oinx][2],
href => $tvars{menus}{$minx}{data}[$oinx][3]};
}
$option = $tree{$option}->{opar};
next unless($option);
my $pinx = $tree{$option}->{minx};
$oinx = $tree{$option}->{oinx};
$tvars{menus}{$pinx}{data}[$oinx][0] = $minx; # menu has this sub menu
}
}
}
=head1 ADMIN INTERFACE METHODS
=head2 Menu Methods
=over
=item Admin
=item Add
=item Edit
=item Save
=item Delete
=item DeleteOptions
=item TypeSelect
=item TypeName
=item ParentSelect
=item CheckImages
=back
=cut
sub Admin {
return unless(AccessUser($LEVEL));
if($cgiparams{doaction}) {
if($cgiparams{doaction} eq 'Delete' ) { Delete(); }
}
my @rows = $dbi->GetQuery('hash',$ALLSQL);
for (@rows) {
$_->{type} = TypeName($_->{typeid});
$_->{realm} = RealmName($_->{realmid});
}
$tvars{data} = \@rows if(@rows);
}
sub Add {
return unless AccessUser($LEVEL);
$tvars{data} = \%adddata;
$tvars{data}->{ddtypes} = TypeSelect();
$tvars{data}->{ddrealms} = RealmSelect();
$tvars{data}->{ddparent} = ParentSelect();
}
sub Edit {
return unless AccessUser($LEVEL);
return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
my $script = $settings{script};
my @opts = $dbi->GetQuery('hash','GetOptions',$tvars{data}->{menuid});
for my $opt (@opts) {
my @images;
if($tvars{data}->{typeid} > 1) {
my @rs = $dbi->GetQuery('hash','GetOptImages',$opt->{optionid});
for(@rs) {
$opt->{'image' . $_->{typeid}} = $_->{'link'};
$opt->{'imageid' . $_->{typeid}} = $_->{'imageid'};
}
@images = map {$_->{'link'}} @rs;
}
$opt->{ddaccess} = AccessSelect($opt->{accessid},'ACCESS'.$opt->{optionid});
my $href = $opt->{href};
$href =~ s!^\?!$script\?!; # all query only links are local
push @{$tvars{preview}->{data}}, [0,0,$opt->{text},$href,($opt->{accessid}||0),$opt->{name},@images];
}
$tvars{data}->{options} = \@opts if(@opts);
$tvars{data}->{ddtypes} = TypeSelect($tvars{data}->{typeid});
$tvars{data}->{ddrealms} = RealmSelect($tvars{data}->{realmid});
$tvars{data}->{ddparent} = ParentSelect($tvars{data}->{parentid},$tvars{data}->{menuid});
$tvars{preview}->{$_} = $tvars{data}->{$_} for(qw(title typeid parentid));
}
sub Save {
return unless AccessUser($LEVEL);
return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
for(keys %fields) {
if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
}
return if FieldCheck(\@allfields,\@mandatory);
my @fields = map {$tvars{data}->{$_}} @savefields;
if($cgiparams{$INDEXKEY}) {
$dbi->DoQuery($SAVESQL,@fields,$cgiparams{$INDEXKEY});
} else {
$cgiparams{$INDEXKEY} = $dbi->IDQuery($ADDSQL,@fields);
}
# delete option if requested
if($cgiparams{'doaction'}) {
DeleteOptions() if($cgiparams{'doaction'} eq 'DeleteOption');
}
# save options
my $order = 1;
my @opts = $dbi->GetQuery('hash','GetOptions',$tvars{data}->{menuid});
for my $opt (@opts) {
my @fields = (
$cgiparams{"ORDER" . $opt->{optionid}}, # menu order
$cgiparams{"NAME" . $opt->{optionid}}, # CSS code name
$cgiparams{"SECT" . $opt->{optionid}}, # section name
$cgiparams{"TEXT" . $opt->{optionid}}, # menu text
$cgiparams{"HREF" . $opt->{optionid}}, # menu link
($cgiparams{"ACCESS" . $opt->{optionid}} || 0), # access level
);
$dbi->DoQuery('SaveOption',@fields,$opt->{optionid});
$order = $opt->{orderno} + 1;
my @rs = $dbi->GetQuery('hash','GetOptImages',$opt->{optionid});
my %images = map {$_->{typeid} => $_->{imageid}} @rs;
CheckImages($images{1},'IMAGEFILE',$opt->{optionid},1) if($tvars{data}->{typeid} > 1);
CheckImages($images{2},'ROLLOVER' ,$opt->{optionid},2) if($tvars{data}->{typeid} > 2);
CheckImages($images{3},'SELECTED' ,$opt->{optionid},3) if($tvars{data}->{typeid} > 3);
}
# add option if requested
if($cgiparams{'doaction'}) {
$dbi->DoQuery('AddOption',$tvars{data}->{menuid},$order,0) if($cgiparams{'doaction'} eq 'AddOption');
}
$tvars{thanks} = 1;
}
sub Delete {
return unless AccessUser($LEVEL);
my @ids = CGIArray('LISTED');
return unless @ids;
# remove menus
my $ids = join(",",@ids);
$dbi->DoQuery($DELETESQL,{ids=>$ids});
my @opts = $dbi->GetQuery('hash','FindOptions',{ids=>$ids});
# remove options
$ids = join(",",map {$_->{optionid}} @opts);
$dbi->DoQuery('DeleteOptions',{ids=>$ids});
$dbi->DoQuery('DeleteOptImages',{ids=>$ids});
}
sub DeleteOptions {
my @ids = CGIArray('LISTED');
return unless @ids;
my $ids = join(",",@ids);
$dbi->DoQuery('DeleteOptions',{ids=>$ids});
$dbi->DoQuery('DeleteOptImages',{ids=>$ids});
}
my %types = (
1 => 'text',
2 => 'image',
3 => 'rollover',
4 => 'highlighted',
);
my @types = map {{'id'=>$_,'value'=> $types{$_}}} sort keys %types;
sub TypeSelect {
my $opt = shift || 0;
DropDownRows($opt,"typeid",'id','value',@types);
}
sub TypeName {
my $id = shift || 1;
return $types{$id};
}
sub ParentSelect {
my $oinx = shift || 0;
my $minx = shift || 0;
my @opts = $dbi->GetQuery('hash','GetAllOptions',$minx);
my @rows = map {{optionid => $_->{optionid}, text => "$_->{name} - $_->{text}"}} @opts;
unshift @rows, {optionid => 0, text => 'Select Parent Option'};
DropDownRows($oinx,"parentid",'optionid','text',@rows);
}
sub CheckImages {
my ($oldid,$key,$optionid,$typeid) = @_;
my $param = $key . $optionid;
return unless($cgiparams{$param});
my $maximagewidth = $settings{maxmenuwidth} || MaxMenuWidth;
my $maximageheight = $settings{maxmenuheight} || MaxMenuHeight;
# my $file = CGIFile($key . $optionid);
my ($imageid) = SaveImageFile(
param => $key . $optionid,
stock => 'DRAFT'
);
my $sqlkey = ($oldid ? 'SaveOptImage' : 'AddOptImage');
$dbi->DoQuery($sqlkey,$imageid,$optionid,$typeid);
}
1;
__END__
=head1 SEE ALSO
L<Labyrinth>
=head1 AUTHOR
Barbie, <barbie@missbarbell.co.uk> for
Miss Barbell Productions, L<http://www.missbarbell.co.uk/>
=head1 COPYRIGHT & LICENSE
Copyright (C) 2002-2013 Barbie for Miss Barbell Productions
All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the Artistic License 2.0.
=cut