File Coverage

blib/lib/Labyrinth/Plugin/Links.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::Links;
2              
3 5     5   88833 use warnings;
  5         12  
  5         191  
4 5     5   21 use strict;
  5         6  
  5         253  
5              
6             our $VERSION = '1.08';
7              
8             =head1 NAME
9              
10             Labyrinth::Plugin::Links - Links plugin handler for Labyrinth
11              
12             =head1 DESCRIPTION
13              
14             Contains all the link handling functionality for the Labyrinth
15             framework.
16              
17             =cut
18              
19             # -------------------------------------
20             # Library Modules
21              
22 5     5   21 use base qw(Labyrinth::Plugin::Base);
  5         9  
  5         5256  
23              
24             use Labyrinth::DBUtils;
25             use Labyrinth::MLUtils;
26             use Labyrinth::Support;
27             use Labyrinth::Variables;
28              
29             # -------------------------------------
30             # Variables
31              
32             # type: 0 = optional, 1 = mandatory
33             # html: 0 = none, 1 = text, 2 = textarea
34              
35             my %cat_fields = (
36             catid => { type => 0, html => 0 },
37             orderno => { type => 0, html => 1 },
38             category => { type => 1, html => 1 },
39             );
40              
41             my (@cat_mandatory,@cat_allfields);
42             for(keys %cat_fields) {
43             push @cat_mandatory, $_ if($cat_fields{$_}->{type});
44             push @cat_allfields, $_;
45             }
46              
47             my %fields = (
48             linkid => { type => 0, html => 0 },
49             catid => { type => 0, html => 0 },
50             href => { type => 1, html => 1 },
51             title => { type => 1, html => 3 },
52             body => { type => 0, html => 2 },
53             );
54              
55             my (@mandatory,@allfields);
56             for(keys %fields) {
57             push @mandatory, $_ if($fields{$_}->{type});
58             push @allfields, $_;
59             }
60              
61             my @savefields = qw(title href body catid);
62             my $INDEXKEY = 'linkid';
63             my $ALLSQL = 'GetLinks';
64             my $SAVESQL = 'SaveLink';
65             my $ADDSQL = 'AddLink';
66             my $GETSQL = 'GetLinkByID';
67             my $DELETESQL = 'DeleteLink';
68              
69             my %adddata = (
70             linkid => 0,
71             href => '',
72             title => '',
73             body => '',
74             );
75              
76             my $protocol = qr{(?:http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|prospero|telnet|gopher|git)://};
77              
78             # -------------------------------------
79             # The Subs
80              
81             =head1 PUBLIC INTERFACE METHODS
82              
83             =head2 Default Methods
84              
85             =over 4
86              
87             =item List
88              
89             Provides a list of all the current links.
90              
91             =back
92              
93             =cut
94              
95             sub List {
96             # get link list for current realm
97             my @rows = $dbi->GetQuery('hash','GetLinks');
98             $tvars{links} = \@rows if(@rows);
99             }
100              
101             =head1 ADMIN INTERFACE METHODS
102              
103             =head2 Link Methods
104              
105             =over 4
106              
107             =item Admin
108              
109             Provides a list of all the current links, with additional administrator
110             functions.
111              
112             =item Add
113              
114             Add a link.
115              
116             =item Edit
117              
118             Edit an existing link.
119              
120             =item Save
121              
122             Validates the given fields and saves to the database.
123              
124             =item Delete
125              
126             Delete a link
127              
128             =item CheckLink
129              
130             Checks whether a link begins with an accepted protocol (http, https, ftp), and
131             if missing adds 'http://'.
132              
133             =back
134              
135             =cut
136              
137             sub Admin {
138             return unless(AccessUser(EDITOR));
139             if($cgiparams{doaction}) {
140             if($cgiparams{doaction} eq 'Delete' ) { Delete(); }
141             }
142             my @rows = $dbi->GetQuery('hash',$ALLSQL);
143             $tvars{data} = \@rows if(@rows);
144             }
145              
146             sub Add {
147             return unless AccessUser(EDITOR);
148             $tvars{data}{ddcats} = CatSelect();
149             }
150              
151             sub Edit {
152             return unless AccessUser(EDITOR);
153             return unless AuthorCheck($GETSQL,$INDEXKEY,EDITOR);
154             $tvars{data}{ddcats} = CatSelect($tvars{data}{catid});
155             $tvars{data}{ddpublish} = PublishSelect($tvars{data}{publish},1);
156             }
157              
158             sub Save {
159             return unless AccessUser(EDITOR);
160             return unless AuthorCheck($GETSQL,$INDEXKEY,EDITOR);
161             for(keys %fields) {
162             if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
163             elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
164             elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
165             }
166             return if FieldCheck(\@allfields,\@mandatory);
167             my @fields = map {$tvars{data}->{$_}} @savefields;
168             if($cgiparams{$INDEXKEY}) {
169             $dbi->DoQuery($SAVESQL,@fields,$cgiparams{$INDEXKEY});
170             } else {
171             $cgiparams{$INDEXKEY} = $dbi->IDQuery($ADDSQL,@fields);
172             }
173             $tvars{thanks} = 1;
174             }
175              
176             sub Delete {
177             return unless AccessUser(ADMIN);
178             my @ids = CGIArray('LISTED');
179             return unless @ids;
180              
181             $dbi->DoQuery($DELETESQL,{ids=>join(",",@ids)});
182             }
183              
184             sub CheckLink {
185             if($cgiparams{href} && $cgiparams{href} !~ m!^(/|$protocol)!) {
186             $cgiparams{href} = 'http://' . $cgiparams{href};
187             }
188             }
189              
190             =head2 Category Admin
191              
192             =over 4
193              
194             =item CatAdmin
195              
196             Provides a list of the link categories.
197              
198             =item CatEdit
199              
200             Edit a link category.
201              
202             =item CatSave
203              
204             Validates the fields returned from the edit page, and either saves or inserts
205             the record into the database.
206              
207             =item CatDelete
208              
209             Delete a link category.
210              
211             =item CatSelect
212              
213             Returns a HTML drop-down list of link categories.
214              
215             =cut
216              
217             sub CatAdmin {
218             return unless(AccessUser(EDITOR));
219             if($cgiparams{doaction}) {
220             if($cgiparams{doaction} eq 'Delete' ) { CatDelete(); }
221             }
222             my @rows = $dbi->GetQuery('hash','GetCategories');
223             $tvars{data} = \@rows if(@rows);
224             }
225              
226             sub CatEdit {
227             return unless AccessUser(EDITOR);
228             return unless AuthorCheck('GetCategoryByID','catid',EDITOR);
229             }
230              
231             sub CatSave {
232             return unless AccessUser(EDITOR);
233             return unless AuthorCheck('GetCategoryByID','catid',EDITOR);
234              
235             for(keys %cat_fields) {
236             if($cat_fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
237             elsif($cat_fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
238             elsif($cat_fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
239             }
240             return if FieldCheck(\@cat_allfields,\@cat_mandatory);
241              
242             $tvars{data}->{orderno} ||= 1;
243             my @fields = ($tvars{data}->{orderno},$tvars{data}->{category});
244             if($cgiparams{catid}) { $dbi->DoQuery('SaveCategory',@fields,$cgiparams{catid}); }
245             else { $cgiparams{catid} = $dbi->IDQuery('NewCategory',@fields); }
246             $tvars{thanks} = 1;
247             }
248              
249             sub CatDelete {
250             return unless AccessUser(ADMIN);
251             my @ids = CGIArray('LISTED');
252             return unless @ids;
253             $dbi->DoQuery('DeleteCategory',{ids=>join(",",@ids)});
254             $dbi->DoQuery('DeleteCatLinks',{ids=>join(",",@ids)});
255             }
256              
257             sub CatSelect {
258             my $opt = shift;
259             my @rows = $dbi->GetQuery('hash','GetCategories');
260             DropDownRows($opt,'catid','catid','category',@rows);
261             }
262              
263             1;
264              
265             __END__