File Coverage

blib/lib/MyLibrary/Patron/Links.pm
Criterion Covered Total %
statement 9 74 12.1
branch 0 30 0.0
condition 0 6 0.0
subroutine 3 11 27.2
pod 8 8 100.0
total 20 129 15.5


line stmt bran cond sub pod time code
1             package MyLibrary::Patron::Links;
2              
3 3     3   17 use MyLibrary::DB;
  3         4  
  3         77  
4 3     3   17 use Carp qw(croak);
  3         6  
  3         136  
5 3     3   15 use strict;
  3         5  
  3         2761  
6              
7             =head1 NAME
8              
9             MyLibrary::Patron::Links;
10              
11             =head1 SYNOPSIS
12              
13             # require the necessary module
14             use MyLibrary::Patron::Links;
15              
16             # create a new patron link
17             my $patron_link = MyLibrary::Patron::Links->new();
18              
19             # get a link id
20             my $link_id = $patron_link->link_id();
21              
22             # set the attributes of the link
23             $patron_link->link_name('CNN');
24             $patron_link->link_url('http://my.site.com');
25             $patron_link->patron_id(23);
26              
27             # save link to database
28             $patron_link->commit();
29              
30             # get all links for a patron
31             my @patron_links = MyLibrary::Patron::Links->get_links(patron_id => $patron_id);
32              
33             # delete a link from the database
34             $patron_link->delete();
35              
36             =head1 DESCRIPTION
37              
38             This is a sub module for creating and manipulating personal patron links. Every link has a name, which is the URL display text, and the URL href itself. Every link must be associated with a particular patron. The module also allows for the retrieval of the complete list of links associated with a patron. The list will be alphabetized according to link name.
39              
40             =head1 METHODS
41              
42             =head2 new()
43              
44             This class method is the constructor for this package. The method is responsible for initializing all attributes associated with a
45             given Patron::Link object. The method can also be used to create a Patron object using a patron link id.
46              
47             =head2 link_id()
48              
49             This method is used exclusively to retrieve an exising patron link database id, if the patron link has been committed to the database. This method may not be used to set the link id in the database.
50              
51             # get a link id
52             my $link_id = $patron_link->link_id();
53              
54             =head2 link_name()
55              
56             This method may be used to either get or set a link name. This is a required attribute, meaning that the object cannot be commited to the database if this attribute is left null.
57              
58             # set the link name
59             $patron_link->link_name('CNN');
60              
61             # get the link name
62             my $link_name = $patron_link->link_name();
63              
64             =head2 link_url()
65              
66             This attribute method should be used to either set or get the url associated with this patron link.
67              
68             # set the link URL
69             $patron_link->link_URL('http://my.favoriteplace.com');
70              
71             # get the link URL
72             my $link_URL = $patron_link->link_URL();
73              
74             =head2 patron_id();
75              
76             This method is used to set or get the patron id associated with a patron link. The patron must exist in the database or this method will throw an exception. The required attribute is the patron id, if setting the id. Otherwise, this method will always return the numeric patron id associated with the link.
77              
78             # set the patron id
79             $patron_link->patron_id(23);
80              
81             # get the patron id
82             my $patron_id = $patron_link->patron_id();
83              
84             =head2 commit()
85              
86             This method commits the patron link to the database. This method requires no arguments.
87              
88             # commit the patron link
89             $patron_link->commit();
90              
91             =head2 delete()
92              
93             Use this method to delete a particular patron link from the database.
94              
95             # delete the patron link
96             $patron_link->delete();
97              
98             =head2 get_links
99              
100             This is a class method that will return a list of patron link ids in link name order. The only required argument is the patron id. This method will return undef if no links exist corresponding to the patron id submitted.
101              
102             # get a list of patron link ids in name order
103             my @patron_links = MyLibrary::Patron::Links->get_links(patron_id => $patron_id);
104              
105             =head1 AUTHORS
106              
107             Robert Fox
108              
109             =cut
110              
111             sub new {
112              
113             # declare a few variables
114 0     0 1   my ($class, %opts) = @_;
115 0           my $self = {};
116              
117             # check for an id
118 0 0         if ($opts{id}) {
119            
120             # find this record
121 0           my $dbh = MyLibrary::DB->dbh();
122 0           my $rv = $dbh->selectrow_hashref('SELECT * FROM personallinks where link_id = ?', undef, $opts{id});
123 0 0         if (ref($rv) eq "HASH") { $self = $rv }
  0            
124 0           else { return }
125             }
126              
127             # return the object
128 0           return bless $self, $class;
129              
130             }
131              
132             sub link_name {
133              
134 0     0 1   my ($self, $name) = @_;
135 0 0         if ($name) {$self->{link_name} = $name }
  0            
136 0           else { return $self->{link_name} }
137              
138             }
139              
140             sub link_url {
141              
142 0     0 1   my ($self, $url) = @_;
143 0 0         if ($url) {
144 0 0         unless ($url =~ /^http:.+/) {
145 0           croak ( 'Patron link URL does not begin with http.' );
146             }
147 0           $self->{link_url} = $url;
148 0           return $self->{link_url};
149             } else {
150 0           return $self->{link_url};
151             }
152              
153             }
154              
155             sub patron_id {
156              
157 0     0 1   my ($self, $patron_id) = @_;
158 0           my $dbh = MyLibrary::DB->dbh();
159 0 0         if ($patron_id) {
160 0           my @patron_array = $dbh->selectrow_array('SELECT * FROM patrons WHERE patron_id = ?', undef, $patron_id);
161 0 0         unless (scalar(@patron_array)) {
162 0           croak ("Submitted patron id in patron_id(), $patron_id, does not correspond to an exising patron." );
163             }
164 0           $self->{patron_id} = $patron_id;
165 0           return $self->{patron_id};
166              
167             } else {
168              
169 0           return $self->{patron_id};
170              
171             }
172              
173             }
174              
175             sub link_id {
176            
177 0     0 1   my $self = shift;
178 0           return $self->{link_id};
179              
180             }
181              
182             sub commit {
183              
184 0     0 1   my $self = shift;
185 0           my $dbh = MyLibrary::DB->dbh();
186              
187 0 0         if ($self->link_id()) {
188              
189 0           my $return = $dbh->do('UPDATE personallinks SET link_name = ?, link_url = ?, patron_id = ? WHERE link_id = ?', undef, $self->link_name(), $self->link_url(), $self->patron_id(), $self->link_id());
190              
191 0 0 0       if ($return > 1 || ! $return) { croak "Patron link update in commit() failed. $return records were updated."; }
  0            
192              
193             } else {
194              
195 0           my $id = MyLibrary::DB->nextID();
196 0           my $return = $dbh->do('INSERT INTO personallinks (link_id, patron_id, link_name, link_url) VALUES (?, ?, ?, ?)', undef, $id, $self->patron_id(), $self->link_name(), $self->link_url());
197 0 0 0       if ($return > 1 || ! $return) { croak 'Patron link commit() failed.'; }
  0            
198 0           $self->{link_id} = $id;
199              
200             }
201              
202 0           return 1;
203              
204             }
205              
206             sub get_links {
207              
208 0     0 1   my $class = shift;
209 0           my %opts = @_;
210 0 0         unless ($opts{patron_id} =~ /^\d+$/) {
211              
212 0           croak "Patron id not submitted. A valid numeric patron id must be submitted to the get_links() method.";
213              
214             }
215              
216 0           my $dbh = MyLibrary::DB->dbh();
217 0           my @patron_array = $dbh->selectrow_array('SELECT * FROM patrons WHERE patron_id = ?', undef, $opts{patron_id});
218 0 0         unless (scalar(@patron_array)) {
219 0           croak "Valid patron id not submitted. No patron record exists corresponding to id number $opts{patron_id}.";
220             }
221              
222 0           my $link_ids = $dbh->selectcol_arrayref("SELECT link_id FROM personallinks WHERE patron_id = ? ORDER BY link_name", undef, $opts{patron_id});
223              
224 0 0         unless (scalar(@{$link_ids})) {
  0            
225 0           return;
226             }
227              
228 0           return @{$link_ids};
  0            
229              
230             }
231              
232             sub delete {
233              
234 0     0 1   my $self = shift;
235            
236 0 0         if ($self->link_id()) {
237              
238 0           my $dbh = MyLibrary::DB->dbh();
239 0           my $rv = $dbh->do('DELETE FROM personallinks WHERE link_id = ?', undef, $self->{link_id});
240 0 0         if ($rv != 1) {croak ("Deleted $rv records. Please check the personallinks table for errors.");}
  0            
241 0           return 1;
242              
243             }
244              
245 0           return 0;
246              
247             }
248              
249             1;