| 1 | package MyLibrary::Stylesheet; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use MyLibrary::DB; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 <rfox2@nd.edu> | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=26.8
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 121 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # check for an id | ||||||||||||||||||||||||||||
| 124 | 1 | ▶ | if ($opts{id}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $dbh = MyLibrary::DB->dbh(); | |||||||||||||||||||||||||||
| 127 | 0 | my $rv = $dbh->selectrow_hashref('SELECT * FROM stylesheets WHERE stylesheet_id = ?', undef, $opts{id}); | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (ref($rv) eq "HASH") { $self = $rv } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | else { return } | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # check for username | ||||||||||||||||||||||||||||
| 132 | } elsif ($opts{name}) { | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # get a record based on this username | ||||||||||||||||||||||||||||
| 135 | 0 | my $dbh = MyLibrary::DB->dbh(); | |||||||||||||||||||||||||||
| 136 | 0 | my $rv = $dbh->selectrow_hashref('SELECT * FROM stylesheets WHERE stylesheet_name = ?', undef, $opts{name}); | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | if (ref($rv) eq "HASH") { $self = $rv } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | else { return } | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # return the object | ||||||||||||||||||||||||||||
| 143 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | sub stylesheet_name { | ||||||||||||||||||||||||||
|
Subroutine
stylesheet_name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 150 | 2 | ▶ | if ($name) { $self->{stylesheet_name} = $name; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 1 | else { return $self->{stylesheet_name} } | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | ▶ | sub stylesheet_description { | ||||||||||||||||||||||||||
|
Subroutine
stylesheet_description: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | my ($self, $desc) = @_; | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | if ($desc) { $self->{stylesheet_description} = $desc; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | else { return $self->{stylesheet_description} } | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 2 | ▶ | sub stylesheet { | ||||||||||||||||||||||||||
|
Subroutine
stylesheet: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | my ($self, $sheet) = @_; | |||||||||||||||||||||||||||
| 166 | 2 | ▶ | if ($sheet) { $self->{stylesheet} = $sheet; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 1 | else { return $self->{stylesheet} } | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub stylesheet_id { | ||||||||||||||||||||||||||
|
Subroutine
stylesheet_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 0 | return $self->{stylesheet_id}; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | sub commit { | ||||||||||||||||||||||||||
|
Subroutine
commit: called CC=6 SCAR=35.2
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 180 | 1 | my $dbh = MyLibrary::DB->dbh(); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if ($self->stylesheet_id()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($return > 1 || ! $return) { croak "Stylesheet update in commit() failed. $return records were updated."; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$return > 1 or not $return
MC/DC0%0/2
$return > 1 or not $return
$return > 1
not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($return > 1 || ! $return) { croak 'Stylesheet commit() failed.'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$return > 1 or not $return
MC/DC0%0/2
$return > 1 or not $return
$return > 1
not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
| |||||||||||||||||||||||||||||
| 193 | 0 | $self->{stylesheet_id} = $id; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | return 1; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub get_stylesheets { | ||||||||||||||||||||||||||
|
Subroutine
get_stylesheets: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 204 | 0 | my %opts = @_; | |||||||||||||||||||||||||||
| 205 | 0 | my @rf = (); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | my $sort; | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | if (defined($opts{'sort'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if ($opts{'sort'} eq 'name') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $sort = 'stylesheet_name'; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | my $dbh = MyLibrary::DB->dbh(); | |||||||||||||||||||||||||||
| 215 | 0 | my @stylesheet_ids = (); | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ($opts{'sort'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | my $stylesheet_ids = $dbh->selectcol_arrayref('SELECT stylesheet_id FROM stylesheets ORDER BY stylesheet_name'); | |||||||||||||||||||||||||||
| 218 | 0 | @stylesheet_ids = @{$stylesheet_ids}; | |||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||
| 220 | 0 | my $stylesheet_ids = selectcol_arrayref('SELECT stylesheet_id FROM stylesheets'); | |||||||||||||||||||||||||||
| 221 | 0 | @stylesheet_ids = @{$stylesheet_ids}; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | return @stylesheet_ids; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | ▶ | if ($self->stylesheet_id()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if (scalar(@patron_ids) >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||