Bug 9021 - Add SMS via email as an alternative to SMS services via SMS::Send drivers
[koha.git] / Koha / SMS / Provider.pm
1 package Koha::SMS::Provider;
2
3 # Copyright 2012 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 Koha::SMS::Provider - class to manage sms providers
23
24 =head1 SYNOPSIS
25
26 Object-oriented class that encapsulates sms providers in Koha.
27
28 =head1 DESCRIPTION
29
30 SMS::Provider data.
31
32 =cut
33
34 use Modern::Perl;
35
36 use C4::Context;
37
38 use base qw(Class::Accessor);
39
40 __PACKAGE__->mk_accessors(qw( id name domain ));
41
42 =head2 new
43
44     my $provider = Koha::SMS::Provider->new($data);
45
46 Create a new Koha::SMS::Provider object based on the provided record.
47
48 =cut
49
50 sub new {
51     my $class = shift;
52     my $data  = shift;
53
54     my $self = $class->SUPER::new($data);
55
56     bless $self, $class;
57     return $self;
58 }
59
60 =head2 store
61
62     Creates or updates the object in the database
63
64 =cut
65
66 sub store {
67     my $self = shift;
68
69     if ( $self->id ) {
70         return C4::Context->dbh->do( "UPDATE sms_providers SET name = ?, domain = ? WHERE id = ?", undef, ( $self->name, $self->domain, $self->id ) );
71     } else {
72         return C4::Context->dbh->do( "INSERT INTO sms_providers ( name, domain ) VALUES ( ?, ? )", undef, ( $self->name, $self->domain ) );
73     }
74 }
75
76 =head2 delete
77
78 =cut
79
80 sub delete {
81     my $self = shift;
82
83     return C4::Context->dbh->do( "DELETE FROM sms_providers WHERE id = ?", undef, ( $self->id ) );
84 }
85
86 =head2 all
87
88     my $providers = Koha::SMS::Provider->all();
89
90 =cut
91
92 sub all {
93     my $class = shift;
94
95     my $query = "SELECT * FROM sms_providers ORDER BY name";
96     my $sth   = C4::Context->dbh->prepare($query);
97     $sth->execute();
98
99     my @providers;
100     while ( my $row = $sth->fetchrow_hashref() ) {
101         my $p = Koha::SMS::Provider->new($row);
102         push( @providers, $p );
103     }
104
105     return @providers;
106 }
107
108 =head2 find
109
110   my $provider = Koha::SMS::Provider->find( $id );
111
112 =cut
113
114 sub find {
115     my $class = shift;
116     my $id    = shift;
117
118     my $query = "SELECT * FROM sms_providers WHERE ID = ?";
119     my $sth   = C4::Context->dbh->prepare($query);
120     $sth->execute($id);
121
122     my $row = $sth->fetchrow_hashref();
123     my $p   = Koha::SMS::Provider->new($row);
124
125     return $p;
126 }
127
128 =head2 search
129
130   my @providers = Koha::SMS::Provider->search({ [name => $name], [domain => $domain] });
131
132 =cut
133
134 sub search {
135     my $class  = shift;
136     my $params = shift;
137
138     my $query = "SELECT * FROM sms_providers WHERE ";
139
140     my @params = map( $params->{$_}, keys %$params );
141     $query .= join( " AND ", map( "$_ = ?", keys %$params ) );
142
143     $query .= " ORDER BY name";
144
145     my $sth = C4::Context->dbh->prepare($query);
146     $sth->execute(@params);
147
148     my @providers;
149     while ( my $row = $sth->fetchrow_hashref() ) {
150         my $p = Koha::SMS::Provider->new($row);
151         push( @providers, $p );
152     }
153
154     return @providers;
155 }
156
157 1;