File Coverage

blib/lib/MyLibrary/Stylesheet.pm
Criterion Covered Total %
statement 27 76 35.5
branch 8 30 26.6
condition 0 6 0.0
subroutine 8 11 72.7
pod 7 8 87.5
total 50 131 38.1


line stmt bran cond sub pod time code
1             package MyLibrary::Stylesheet;
2              
3 1     1   1302 use MyLibrary::DB;
  1         5  
  1         31  
4 1     1   4 use Carp qw(croak);
  1         2  
  1         43  
5 1     1   5 use strict;
  1         1  
  1         4021  
6              
7              
8             =head1 NAME
9              
10             MyLibrary::Stylesheet
11              
12             =head1 SYNOPSIS
13              
14             # require the necessary module
15             use MyLibrary::Stylesheet;
16              
17             # create an undefined Stylesheet object
18             my $stylesheet = MyLibrary::Stylesheet->new();
19              
20             # get stylesheet id
21             my $stylesheet_id = $stylesheet->stylesheet_id();
22              
23             # set the attributes for the stylesheet
24             $stylesheet->stylesheet_name('Gothic');
25             $stylesheet->stylesheet_description('Dark colors, gothic script.');
26             $stylesheet->stylesheet('CSS code');
27              
28             # commit stylesheet to database
29             $stylesheet->commit();
30              
31             # get a list of stylesheet objects
32             my @stylesheets = MyLibrary::Stylesheet->get_stylesheets();
33             my @stylesheets = MyLibrary::Stylesheet->get_stylesheets(sort => 'name');
34              
35             # delete a stylesheet from the database
36             $stylesheet->delete();
37              
38             =head1 DESCRIPTION
39              
40             This module simply allows for the creation and maniuplation of HTML CSS stylesheets. These stylesheets will be used to present data in various contexts throught the browser medium. It also allows for association of stylesheets with patron objects so that patrons can select various styles for the presentation of MyLibrary data. Stylesheets could also be used to syndicate content to other venues and can help to separate style and presentation from content.
41              
42             =head1 METHODS
43              
44             =head2 new()
45              
46             This class method is the constructor for this package. The method is responsible for initializing all attributes associated with a
47             given Stylesheet object. The method can also be used to create a Stylesheet object using stylesheet id or name. The stylesheet would thus need to already exist in the database for these parameters to have any effect.
48              
49             =head2 stylesheet_id()
50              
51             This method is used exclusively to retrieve an exising stylesheet object id. This method will only return a valid id if the stylesheet has been commited to the database. This accessor method cannot set a stylesheet id.
52              
53             # get stylesheet id
54             my $stylesheet_id = $stylesheet->stylesheet_id();
55              
56             =head2 stylesheet_name()
57              
58             This accessor method simply sets and gets the name of the stylesheet.
59              
60             # set the stylesheet name
61             $stylesheet->stylesheet_name('Gothic');
62            
63             # get the stylesheet name
64             my $style_name = $stylesheet->stylesheet_name();
65              
66             =head2 stylesheet_note()
67              
68             Set or get the stylesheet note. This text will be used to describe the stylesheet in question.
69              
70             # set the stylesheet note
71             $stylesheet->stylesheet_note('This style is slightly gothic with medieval overtones.');
72              
73             # get the stylesheet note
74             my $style_desc = $stylesheet->stylesheet_note();
75              
76             =head2 stylesheet()
77              
78             Depending upon how you want your application to function, the content of this attribute can be either a pointer to a stylesheet located external to the database or it can be the text of a stylesheet itself.
79              
80             # set the stylesheet content
81             $stylesheet->stylesheet('CONTENT');
82              
83             # retrieve the stylesheet content
84             my $stylesheet = $stylesheet->stylesheet();
85              
86             =head2 get_stylesheets()
87              
88             This class method should be used to retrieve a list of all of the stylesheet object ids from the database. The list can be sorted according to stylesheet name. The sort parameter is optional. A default stylesheet should always be present in the database with a stylesheet id of '0'. This stylesheet is used if no other stylesheet has been created.
89              
90             # get a sorted list of stylesheets
91             my @stylesheet_ids = MyLibrary::Stylesheet->get_stylesheets(sort => 'name');
92              
93             =head2 commit()
94              
95             Save the stylesheet to the database.
96              
97             # commit the stylesheet
98             $stylesheet->commit();
99              
100             =head2 delete()
101              
102             Delete the stylesheet from the database.
103              
104             # delete the stlyesheet
105             $stylesheet->delete();
106              
107              
108             =head1 SEE ALSO
109              
110             For more information, see the MyLibrary home page: http://dewey.library.nd.edu/mylibrary/.
111              
112             =head1 AUTHORS
113              
114             Robert Fox
115              
116             =cut
117              
118             sub new {
119              
120 1     1 1 836 my ($class, %opts) = @_;
121 1         3 my $self = {};
122              
123             # check for an id
124 1 50       8 if ($opts{id}) {
    50          
125              
126 0         0 my $dbh = MyLibrary::DB->dbh();
127 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM stylesheets WHERE stylesheet_id = ?', undef, $opts{id});
128 0 0       0 if (ref($rv) eq "HASH") { $self = $rv }
  0         0  
129 0         0 else { return }
130              
131             # check for username
132             } elsif ($opts{name}) {
133              
134             # get a record based on this username
135 0         0 my $dbh = MyLibrary::DB->dbh();
136 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM stylesheets WHERE stylesheet_name = ?', undef, $opts{name});
137 0 0       0 if (ref($rv) eq "HASH") { $self = $rv }
  0         0  
138 0         0 else { return }
139              
140             }
141              
142             # return the object
143 1         5 return bless $self, $class;
144              
145             }
146              
147             sub stylesheet_name {
148              
149 2     2 1 449 my ($self, $name) = @_;
150 2 100       6 if ($name) { $self->{stylesheet_name} = $name; }
  1         6  
151 1         6 else { return $self->{stylesheet_name} }
152              
153             }
154              
155             sub stylesheet_description {
156              
157 2     2 0 3 my ($self, $desc) = @_;
158 2 100       13 if ($desc) { $self->{stylesheet_description} = $desc; }
  1         5  
159 1         5 else { return $self->{stylesheet_description} }
160              
161             }
162              
163             sub stylesheet {
164            
165 2     2 1 23 my ($self, $sheet) = @_;
166 2 100       6 if ($sheet) { $self->{stylesheet} = $sheet; }
  1         4  
167 1         7 else { return $self->{stylesheet} }
168             }
169              
170             sub stylesheet_id {
171              
172 0     0 1 0 my $self = shift;
173 0         0 return $self->{stylesheet_id};
174              
175             }
176              
177             sub commit {
178              
179 1     1 1 2 my $self = shift;
180 1         6 my $dbh = MyLibrary::DB->dbh();
181              
182 0 0         if ($self->stylesheet_id()) {
183              
184 0           my $return = $dbh->do('UPDATE stylesheets SET stylesheet_name = ?, stylesheet_description = ?, stylesheet = ? WHERE stylesheet_id =?', undef, $self->{stylesheet_name}, $self->{stylesheet_description}, $self->{stylesheet}, $self->{stylesheet_id});
185              
186 0 0 0       if ($return > 1 || ! $return) { croak "Stylesheet update in commit() failed. $return records were updated."; }
  0            
187            
188             } else {
189              
190 0           my $id = MyLibrary::DB->nextID();
191 0           my $return = $dbh->do('INSERT INTO stylesheets (stylesheet_id, stylesheet_name, stylesheet_description, stylesheet) VALUES (?,?,?,?)', undef, $id, $self->stylesheet_name(), $self->stylesheet_description(), $self->stylesheet());
192 0 0 0       if ($return > 1 || ! $return) { croak 'Stylesheet commit() failed.'; }
  0            
193 0           $self->{stylesheet_id} = $id;
194              
195             }
196              
197 0           return 1;
198              
199             }
200              
201             sub get_stylesheets {
202              
203 0     0 1   my $class = shift;
204 0           my %opts = @_;
205 0           my @rf = ();
206            
207 0           my $sort;
208 0 0         if (defined($opts{'sort'})) {
209 0 0         if ($opts{'sort'} eq 'name') {
210 0           $sort = 'stylesheet_name';
211             }
212             }
213              
214 0           my $dbh = MyLibrary::DB->dbh();
215 0           my @stylesheet_ids = ();
216 0 0         if ($opts{'sort'}) {
217 0           my $stylesheet_ids = $dbh->selectcol_arrayref('SELECT stylesheet_id FROM stylesheets ORDER BY stylesheet_name');
218 0           @stylesheet_ids = @{$stylesheet_ids};
  0            
219             } else {
220 0           my $stylesheet_ids = selectcol_arrayref('SELECT stylesheet_id FROM stylesheets');
221 0           @stylesheet_ids = @{$stylesheet_ids};
  0            
222             }
223              
224 0           return @stylesheet_ids;
225             }
226              
227             sub delete {
228              
229 0     0 1   my $self = shift;
230              
231 0 0         if ($self->stylesheet_id()) {
232              
233 0           my $dbh = MyLibrary::DB->dbh();
234 0           my $rv = $dbh->do('DELETE FROM stylesheets WHERE stylesheet_id = ?', undef, $self->{stylesheet_id});
235 0           my @patron_ids = $dbh->selectcol_arrayref('SELECT patron_id FROM patrons WHERE patron_stylesheet_id = ?', undef, $self->{stylesheet_id});
236 0 0         if (scalar(@patron_ids) >= 1) {
237 0           my $patron_id_string = join(', ', split(/ /, @patron_ids));
238 0           my $rv = $dbh->do("UPDATE patrons SET patron_stylesheet_id = ? WHERE patron_id IN ($patron_id_string)", undef, $self->{stylesheet_id});
239             }
240              
241 0           return 1;
242             }
243              
244 0           return 0;
245             }
246              
247             1;