Bug 15629: Koha::Libraries - Remove GetBranchDetail
[koha.git] / C4 / Branch.pm
1 package C4::Branch;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23 use Koha::LibraryCategories;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 BEGIN {
28         # set the version for version checking
29     $VERSION = 3.07.00.049;
30         @ISA    = qw(Exporter);
31         @EXPORT = qw(
32                 &GetBranchName
33                 &GetBranch
34                 &GetBranches
35                 &GetBranchesLoop
36                 &ModBranch
37                 &GetBranchInfo
38                 &mybranch
39         );
40     @EXPORT_OK = qw( &onlymine &mybranch );
41 }
42
43 =head1 NAME
44
45 C4::Branch - Koha branch module
46
47 =head1 SYNOPSIS
48
49 use C4::Branch;
50
51 =head1 DESCRIPTION
52
53 The functions in this module deal with branches.
54
55 =head1 FUNCTIONS
56
57 =head2 GetBranches
58
59   $branches = &GetBranches();
60
61 Returns informations about ALL branches, IndependentBranches Insensitive.
62 GetBranchInfo() returns the same information.
63
64 Create a branch selector with the following code.
65
66 =head3 in PERL SCRIPT
67
68     my $branches = GetBranches;
69     my @branchloop;
70     foreach my $thisbranch (sort keys %$branches) {
71         my $selected = 1 if $thisbranch eq $branch;
72         my %row =(value => $thisbranch,
73                     selected => $selected,
74                     branchname => $branches->{$thisbranch}->{branchname},
75                 );
76         push @branchloop, \%row;
77     }
78
79 =head3 in TEMPLATE
80
81     <select name="branch" id="branch">
82         <option value=""></option>
83             [% FOREACH branchloo IN branchloop %]
84                 [% IF ( branchloo.selected ) %]
85                     <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
86                 [% ELSE %]
87                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
88                 [% END %]
89             [% END %]
90     </select>
91
92 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
93
94 =cut
95
96 sub GetBranches {
97     my ($onlymine) = @_;
98
99     # returns a reference to a hash of references to ALL branches...
100     my %branches;
101     my $dbh = C4::Context->dbh;
102     my $sth;
103     my $query = "SELECT * FROM branches";
104     my @bind_parameters;
105     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
106         $query .= ' WHERE branchcode = ? ';
107         push @bind_parameters, C4::Context->userenv->{branch};
108     }
109     $query .= " ORDER BY branchname";
110     $sth = $dbh->prepare($query);
111     $sth->execute(@bind_parameters);
112
113     my $relations_sth =
114       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
115     $relations_sth->execute();
116     my %relations;
117     while ( my $rel = $relations_sth->fetchrow_hashref ) {
118         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
119     }
120
121     while ( my $branch = $sth->fetchrow_hashref ) {
122         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
123             $branch->{category}{$cat} = 1;
124         }
125         $branches{ $branch->{'branchcode'} } = $branch;
126     }
127     return ( \%branches );
128 }
129
130 sub onlymine {
131     return
132          C4::Context->preference('IndependentBranches')
133       && C4::Context->userenv
134       && !C4::Context->IsSuperLibrarian()
135       && C4::Context->userenv->{branch};
136 }
137
138 # always returns a string for OK comparison via "eq" or "ne"
139 sub mybranch {
140     C4::Context->userenv           or return '';
141     return C4::Context->userenv->{branch} || '';
142 }
143
144 sub GetBranchesLoop {  # since this is what most pages want anyway
145     my $branch   = @_ ? shift : mybranch();     # optional first argument is branchcode of "my branch", if preselection is wanted.
146     my $onlymine = @_ ? shift : onlymine();
147     my $branches = GetBranches($onlymine);
148     my @loop;
149     foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
150         push @loop, {
151             value      => $branchcode,
152             branchcode => $branchcode,
153             selected   => ($branchcode eq $branch) ? 1 : 0,
154             branchname => $branches->{$branchcode}->{branchname},
155         };
156     }
157     return \@loop;
158 }
159
160 =head2 GetBranchName
161
162 =cut
163
164 sub GetBranchName {
165     my ($branchcode) = @_;
166     my $dbh = C4::Context->dbh;
167     my $sth;
168     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
169     $sth->execute($branchcode);
170     my $branchname = $sth->fetchrow_array;
171     return ($branchname);
172 }
173
174 =head2 ModBranch
175
176 $error = &ModBranch($newvalue);
177
178 This function modifies an existing branch
179
180 C<$newvalue> is a ref to an array which contains all the columns from branches table.
181
182 =cut
183
184 sub ModBranch {
185     my ($data) = @_;
186     
187     my $dbh    = C4::Context->dbh;
188     if ($data->{add}) {
189         my $query  = "
190             INSERT INTO branches
191             (branchcode,branchname,branchaddress1,
192             branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
193             branchcountry,branchphone,branchfax,branchemail,
194             branchurl,branchip,branchprinter,branchnotes,opac_info,
195             branchreplyto, branchreturnpath)
196             VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
197         ";
198         my $sth    = $dbh->prepare($query);
199         $sth->execute(
200             $data->{'branchcode'},       $data->{'branchname'},
201             $data->{'branchaddress1'},   $data->{'branchaddress2'},
202             $data->{'branchaddress3'},   $data->{'branchzip'},
203             $data->{'branchcity'},       $data->{'branchstate'},
204             $data->{'branchcountry'},
205             $data->{'branchphone'},      $data->{'branchfax'},
206             $data->{'branchemail'},      $data->{'branchurl'},
207             $data->{'branchip'},         $data->{'branchprinter'},
208             $data->{'branchnotes'},      $data->{opac_info},
209             $data->{'branchreplyto'},    $data->{'branchreturnpath'}
210         );
211         return 1 if $dbh->err;
212     } else {
213         my $query  = "
214             UPDATE branches
215             SET branchname=?,branchaddress1=?,
216                 branchaddress2=?,branchaddress3=?,branchzip=?,
217                 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
218                 branchfax=?,branchemail=?,branchurl=?,branchip=?,
219                 branchprinter=?,branchnotes=?,opac_info=?,
220                 branchreplyto=?, branchreturnpath=?
221             WHERE branchcode=?
222         ";
223         my $sth    = $dbh->prepare($query);
224         $sth->execute(
225             $data->{'branchname'},
226             $data->{'branchaddress1'},   $data->{'branchaddress2'},
227             $data->{'branchaddress3'},   $data->{'branchzip'},
228             $data->{'branchcity'},       $data->{'branchstate'},       
229             $data->{'branchcountry'},
230             $data->{'branchphone'},      $data->{'branchfax'},
231             $data->{'branchemail'},      $data->{'branchurl'},
232             $data->{'branchip'},         $data->{'branchprinter'},
233             $data->{'branchnotes'},      $data->{opac_info},
234             $data->{'branchreplyto'},    $data->{'branchreturnpath'},
235             $data->{'branchcode'},
236         );
237     }
238     # sort out the categories....
239     my @checkedcats;
240     my @cats = Koha::LibraryCategories->search;
241     foreach my $cat (@cats) {
242         my $code = $cat->categorycode;
243         if ( $data->{$code} ) {
244             push( @checkedcats, $code );
245         }
246     }
247     my $branchcode = uc( $data->{'branchcode'} );
248     my $branch     = GetBranchInfo($branchcode);
249     $branch = $branch->[0];
250     my $branchcats = $branch->{'categories'};
251     my @addcats;
252     my @removecats;
253     foreach my $bcat (@$branchcats) {
254
255         unless ( grep { /^$bcat$/ } @checkedcats ) {
256             push( @removecats, $bcat );
257         }
258     }
259     foreach my $ccat (@checkedcats) {
260         unless ( grep { /^$ccat$/ } @$branchcats ) {
261             push( @addcats, $ccat );
262         }
263     }
264     foreach my $cat (@addcats) {
265         my $sth =
266           $dbh->prepare(
267 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
268           );
269         $sth->execute( $branchcode, $cat );
270     }
271     foreach my $cat (@removecats) {
272         my $sth =
273           $dbh->prepare(
274             "delete from branchrelations where branchcode=? and categorycode=?"
275           );
276         $sth->execute( $branchcode, $cat );
277     }
278 }
279
280 =head2 GetBranch
281
282 $branch = GetBranch( $query, $branches );
283
284 =cut
285
286 sub GetBranch {
287     my ( $query, $branches ) = @_;    # get branch for this query from branches
288     my $branch = $query->param('branch');
289     my %cookie = $query->cookie('userenv');
290     ($branch)                || ($branch = $cookie{'branchname'});
291     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
292     return $branch;
293 }
294
295 =head2 GetBranchInfo
296
297 $results = GetBranchInfo($branchcode);
298
299 returns C<$results>, a reference to an array of hashes containing branches.
300 if $branchcode, just this branch, with associated categories.
301 if ! $branchcode && $categorytype, all branches in the category.
302
303 =cut
304
305 sub GetBranchInfo {
306     my ($branchcode,$categorytype) = @_;
307     my $dbh = C4::Context->dbh;
308     my $sth;
309
310
311         if ($branchcode) {
312         $sth =
313           $dbh->prepare(
314             "Select * from branches where branchcode = ? order by branchcode");
315         $sth->execute($branchcode);
316     }
317     else {
318         $sth = $dbh->prepare("Select * from branches order by branchcode");
319         $sth->execute();
320     }
321     my @results;
322     while ( my $data = $sth->fetchrow_hashref ) {
323                 my @bind = ($data->{'branchcode'});
324         my $query= "select r.categorycode from branchrelations r";
325                 $query .= ", branchcategories c " if($categorytype);
326                 $query .= " where  branchcode=? ";
327                 if($categorytype) { 
328                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
329                         push @bind, $categorytype;
330                 }
331         my $nsth=$dbh->prepare($query);
332                 $nsth->execute( @bind );
333         my @cats = ();
334         while ( my ($cat) = $nsth->fetchrow_array ) {
335             push( @cats, $cat );
336         }
337         $data->{'categories'} = \@cats;
338         push( @results, $data );
339     }
340     return \@results;
341 }
342
343 1;
344 __END__
345
346 =head1 AUTHOR
347
348 Koha Development Team <http://koha-community.org/>
349
350 =cut