Interface/Admin.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 91 CC 8 SCAR 20.5
CC
8
Cov
0%
commit_interface
20.0
edit_interface
6.0
interface_text
6.0
CRAP 7.8
1 package MyLibrary::Interface::Admin;
2  
3 use MyLibrary::DB;
4 use base qw(MyLibrary::Interface);
5 use Carp qw(croak);
6 use strict;
7  
8 my $dbh = MyLibrary::DB->dbh();
9  
10 =head1 NAME
11  
12 MyLibrary::Interface::Admin
13  
14 =head1 SYNOPSIS
15  
16 use MyLibrary::Interface::Admin;
17  
18 =head1 DESCRIPTION
19  
20 Modular admin interface components for MyLibrary.
21  
22 =head1 EXPORT
23  
24 Few methods will be exported.
25  
26 =head1 METHODS
27  
28 Most of the methods employed here are class methods. However, class objects are created with
29 embedded attributes.
30  
31 =head1 edit_html(\%attr)
32  
33 =cut
34  
35 sub edit_interface {
36 my $q = qq(SELECT * FROM interface WHERE name='edit_html');
37 my $interface = $dbh->selectrow_hashref($q);
38 my ($class, %opts) = @_;
39 my @names = @{get_interface_names()};
40 if ($opts{interface_name}) {
41 my $q = qq(SELECT * FROM interface where name='$opts{interface_name}');
42 $opts{interface_text} = $dbh->selectrow_hashref($q)->{'html'};
43 $opts{interface_text} =~ s/</&lt;/g;
44 $opts{interface_text} =~ s/>/&gt;/g;
45 $opts{interface_options} = $dbh->selectrow_hashref($q)->{'options'};
46 $opts{interface_id} = $dbh->selectrow_hashref($q)->{'interface_id'};
47 }
48 my $self = {
49 interface_name => undef,
50 interface_text => undef,
51 interface_options => undef,
52 html => $interface->{'html'},
53 name_menu => 1,
54 names => [@names],
55 %opts
56 };
57 return bless $self, $class;
58 } # end sub edit_html
59  
60 =head1 define_interface(\%attr)
61  
62 This is a constructor method which can be used to create the vital attributes
63 of an interface object. These attributes are then subsumed when the edit_html
64 constructor is called. Use this method to directly manipulate the attributes
65 of the interface being called.
66  
67 =cut
68  
69  
70 sub define_interface {
71 my ($class, %opts) = @_;
72 my $self = {
73 interface_id => undef,
74 interface_name => undef,
75 interface_text => undef,
76 interface_options => undef,
77 %opts
78 };
79 return bless $self, $class;
80 }
81  
82 =head1 interface_text()
83  
84 =cut
85  
86 sub interface_text {
87 my ($self, $interface_text) = @_;
88 if ($interface_text) {
89 $self->{interface_text} = $interface_text;
90 } else {
91 return $self->{interface_text};
92 }
93 }
94  
95 sub html {
96 my ($self, $html) = @_;
97 if ($html) {
98 $self->{html} = $html;
99 } else {
100 return $self->{html};
101 }
102 }
103  
104 =head1 interface_name()
105  
106 =cut
107  
108 sub interface_name {
109 my ($self, $interface_name) = @_;
110 if ($interface_name) {
111 $self->{interface_name} = $interface_name;
112 } else {
113 return $self->{interface_name};
114 }
115 }
116  
117 =head1 get_interface_names(\%attr)
118  
119 =cut
120  
121 sub get_interface_names {
122 my $q = qq(SELECT name FROM interface);
123 my $ary_ref = $dbh->selectcol_arrayref($q);
124 return $ary_ref;
125 }
126  
127 =head1 commit_interface()
128  
129 This method can be used to either update the definition of an interface or
130 creat a new interface.
131  
132 # create and commit a new interface (use the interface() constructor method)
133 $interface = $MyLibrary::Interface::Admin->interface(interface_name => $name, interface_text => $html);
134 $interface->commit_interface();
135  
136 # update the values of an existing interface
137
138  
139 =cut
140  
141 sub commit_interface {
142 my $self = shift;
143 if ($self->{interface_id}) {
144 $self->{interface_text} =~ s/&lt;/</g;
145 $self->{interface_text} =~ s/&gt;/>/g;
146 my $return = $dbh->do('UPDATE interface SET html = ?, options = ? WHERE interface_id = ?', undef, $self->{interface_text}, $self->{interface_options}, $self->{interface_id});
147 if ($return != 1) { croak 'commit_interface() failed.'; }
148 } else {
149 my $id = MyLibrary::DB->nextID();
150 my $return = $dbh->do('INSERT INTO interface (interface_id, name, html, options) VALUES (?, ?, ?, ?)', undef, $id, $self->{interface_name}, $self->{interface_text}, $self->{interface_options});
151 if ($return != 1) { croak 'commit_interface() failed.'; }
152 $self->{interface_id} = $id;
153 }
154 return 1;
155 }
156  
157 1;