Bug 15295: Koha::Libraries - Remove GetBranchesCount
[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                 &GetBranchDetail
37                 &get_branchinfos_of
38                 &ModBranch
39                 &GetBranchInfo
40                 &GetBranchesInCategory
41                 &ModBranchCategoryInfo
42                 &mybranch
43         );
44     @EXPORT_OK = qw( &onlymine &mybranch );
45 }
46
47 =head1 NAME
48
49 C4::Branch - Koha branch module
50
51 =head1 SYNOPSIS
52
53 use C4::Branch;
54
55 =head1 DESCRIPTION
56
57 The functions in this module deal with branches.
58
59 =head1 FUNCTIONS
60
61 =head2 GetBranches
62
63   $branches = &GetBranches();
64
65 Returns informations about ALL branches, IndependentBranches Insensitive.
66 GetBranchInfo() returns the same information.
67
68 Create a branch selector with the following code.
69
70 =head3 in PERL SCRIPT
71
72     my $branches = GetBranches;
73     my @branchloop;
74     foreach my $thisbranch (sort keys %$branches) {
75         my $selected = 1 if $thisbranch eq $branch;
76         my %row =(value => $thisbranch,
77                     selected => $selected,
78                     branchname => $branches->{$thisbranch}->{branchname},
79                 );
80         push @branchloop, \%row;
81     }
82
83 =head3 in TEMPLATE
84
85     <select name="branch" id="branch">
86         <option value=""></option>
87             [% FOREACH branchloo IN branchloop %]
88                 [% IF ( branchloo.selected ) %]
89                     <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
90                 [% ELSE %]
91                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
92                 [% END %]
93             [% END %]
94     </select>
95
96 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
97
98 =cut
99
100 sub GetBranches {
101     my ($onlymine) = @_;
102
103     # returns a reference to a hash of references to ALL branches...
104     my %branches;
105     my $dbh = C4::Context->dbh;
106     my $sth;
107     my $query = "SELECT * FROM branches";
108     my @bind_parameters;
109     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
110         $query .= ' WHERE branchcode = ? ';
111         push @bind_parameters, C4::Context->userenv->{branch};
112     }
113     $query .= " ORDER BY branchname";
114     $sth = $dbh->prepare($query);
115     $sth->execute(@bind_parameters);
116
117     my $relations_sth =
118       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
119     $relations_sth->execute();
120     my %relations;
121     while ( my $rel = $relations_sth->fetchrow_hashref ) {
122         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
123     }
124
125     while ( my $branch = $sth->fetchrow_hashref ) {
126         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
127             $branch->{category}{$cat} = 1;
128         }
129         $branches{ $branch->{'branchcode'} } = $branch;
130     }
131     return ( \%branches );
132 }
133
134 sub onlymine {
135     return
136          C4::Context->preference('IndependentBranches')
137       && C4::Context->userenv
138       && !C4::Context->IsSuperLibrarian()
139       && C4::Context->userenv->{branch};
140 }
141
142 # always returns a string for OK comparison via "eq" or "ne"
143 sub mybranch {
144     C4::Context->userenv           or return '';
145     return C4::Context->userenv->{branch} || '';
146 }
147
148 sub GetBranchesLoop {  # since this is what most pages want anyway
149     my $branch   = @_ ? shift : mybranch();     # optional first argument is branchcode of "my branch", if preselection is wanted.
150     my $onlymine = @_ ? shift : onlymine();
151     my $branches = GetBranches($onlymine);
152     my @loop;
153     foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
154         push @loop, {
155             value      => $branchcode,
156             branchcode => $branchcode,
157             selected   => ($branchcode eq $branch) ? 1 : 0,
158             branchname => $branches->{$branchcode}->{branchname},
159         };
160     }
161     return \@loop;
162 }
163
164 =head2 GetBranchName
165
166 =cut
167
168 sub GetBranchName {
169     my ($branchcode) = @_;
170     my $dbh = C4::Context->dbh;
171     my $sth;
172     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
173     $sth->execute($branchcode);
174     my $branchname = $sth->fetchrow_array;
175     return ($branchname);
176 }
177
178 =head2 ModBranch
179
180 $error = &ModBranch($newvalue);
181
182 This function modifies an existing branch
183
184 C<$newvalue> is a ref to an array which contains all the columns from branches table.
185
186 =cut
187
188 sub ModBranch {
189     my ($data) = @_;
190     
191     my $dbh    = C4::Context->dbh;
192     if ($data->{add}) {
193         my $query  = "
194             INSERT INTO branches
195             (branchcode,branchname,branchaddress1,
196             branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
197             branchcountry,branchphone,branchfax,branchemail,
198             branchurl,branchip,branchprinter,branchnotes,opac_info,
199             branchreplyto, branchreturnpath)
200             VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
201         ";
202         my $sth    = $dbh->prepare($query);
203         $sth->execute(
204             $data->{'branchcode'},       $data->{'branchname'},
205             $data->{'branchaddress1'},   $data->{'branchaddress2'},
206             $data->{'branchaddress3'},   $data->{'branchzip'},
207             $data->{'branchcity'},       $data->{'branchstate'},
208             $data->{'branchcountry'},
209             $data->{'branchphone'},      $data->{'branchfax'},
210             $data->{'branchemail'},      $data->{'branchurl'},
211             $data->{'branchip'},         $data->{'branchprinter'},
212             $data->{'branchnotes'},      $data->{opac_info},
213             $data->{'branchreplyto'},    $data->{'branchreturnpath'}
214         );
215         return 1 if $dbh->err;
216     } else {
217         my $query  = "
218             UPDATE branches
219             SET branchname=?,branchaddress1=?,
220                 branchaddress2=?,branchaddress3=?,branchzip=?,
221                 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
222                 branchfax=?,branchemail=?,branchurl=?,branchip=?,
223                 branchprinter=?,branchnotes=?,opac_info=?,
224                 branchreplyto=?, branchreturnpath=?
225             WHERE branchcode=?
226         ";
227         my $sth    = $dbh->prepare($query);
228         $sth->execute(
229             $data->{'branchname'},
230             $data->{'branchaddress1'},   $data->{'branchaddress2'},
231             $data->{'branchaddress3'},   $data->{'branchzip'},
232             $data->{'branchcity'},       $data->{'branchstate'},       
233             $data->{'branchcountry'},
234             $data->{'branchphone'},      $data->{'branchfax'},
235             $data->{'branchemail'},      $data->{'branchurl'},
236             $data->{'branchip'},         $data->{'branchprinter'},
237             $data->{'branchnotes'},      $data->{opac_info},
238             $data->{'branchreplyto'},    $data->{'branchreturnpath'},
239             $data->{'branchcode'},
240         );
241     }
242     # sort out the categories....
243     my @checkedcats;
244     my @cats = Koha::LibraryCategories->search;
245     foreach my $cat (@cats) {
246         my $code = $cat->categorycode;
247         if ( $data->{$code} ) {
248             push( @checkedcats, $code );
249         }
250     }
251     my $branchcode = uc( $data->{'branchcode'} );
252     my $branch     = GetBranchInfo($branchcode);
253     $branch = $branch->[0];
254     my $branchcats = $branch->{'categories'};
255     my @addcats;
256     my @removecats;
257     foreach my $bcat (@$branchcats) {
258
259         unless ( grep { /^$bcat$/ } @checkedcats ) {
260             push( @removecats, $bcat );
261         }
262     }
263     foreach my $ccat (@checkedcats) {
264         unless ( grep { /^$ccat$/ } @$branchcats ) {
265             push( @addcats, $ccat );
266         }
267     }
268     foreach my $cat (@addcats) {
269         my $sth =
270           $dbh->prepare(
271 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
272           );
273         $sth->execute( $branchcode, $cat );
274     }
275     foreach my $cat (@removecats) {
276         my $sth =
277           $dbh->prepare(
278             "delete from branchrelations where branchcode=? and categorycode=?"
279           );
280         $sth->execute( $branchcode, $cat );
281     }
282 }
283
284 =head2 GetBranch
285
286 $branch = GetBranch( $query, $branches );
287
288 =cut
289
290 sub GetBranch {
291     my ( $query, $branches ) = @_;    # get branch for this query from branches
292     my $branch = $query->param('branch');
293     my %cookie = $query->cookie('userenv');
294     ($branch)                || ($branch = $cookie{'branchname'});
295     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
296     return $branch;
297 }
298
299 =head2 GetBranchDetail
300
301     $branch = &GetBranchDetail($branchcode);
302
303 Given the branch code, the function returns a
304 hashref for the corresponding row in the branches table.
305
306 =cut
307
308 sub GetBranchDetail {
309     my ($branchcode) = shift or return;
310     my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
311     $sth->execute($branchcode);
312     return $sth->fetchrow_hashref();
313 }
314
315 =head2 GetBranchesInCategory
316
317   my $branches = GetBranchesInCategory($categorycode);
318
319 Returns a href:  keys %$branches eq (branchcode,branchname) .
320
321 =cut
322
323 sub GetBranchesInCategory {
324     my ($categorycode) = @_;
325         my @branches;
326         my $dbh = C4::Context->dbh();
327         my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b 
328                                                         where r.branchcode=b.branchcode and r.categorycode=?");
329     $sth->execute($categorycode);
330         while (my $branch = $sth->fetchrow) {
331                 push @branches, $branch;
332         }
333         return( \@branches );
334 }
335
336 =head2 GetBranchInfo
337
338 $results = GetBranchInfo($branchcode);
339
340 returns C<$results>, a reference to an array of hashes containing branches.
341 if $branchcode, just this branch, with associated categories.
342 if ! $branchcode && $categorytype, all branches in the category.
343
344 =cut
345
346 sub GetBranchInfo {
347     my ($branchcode,$categorytype) = @_;
348     my $dbh = C4::Context->dbh;
349     my $sth;
350
351
352         if ($branchcode) {
353         $sth =
354           $dbh->prepare(
355             "Select * from branches where branchcode = ? order by branchcode");
356         $sth->execute($branchcode);
357     }
358     else {
359         $sth = $dbh->prepare("Select * from branches order by branchcode");
360         $sth->execute();
361     }
362     my @results;
363     while ( my $data = $sth->fetchrow_hashref ) {
364                 my @bind = ($data->{'branchcode'});
365         my $query= "select r.categorycode from branchrelations r";
366                 $query .= ", branchcategories c " if($categorytype);
367                 $query .= " where  branchcode=? ";
368                 if($categorytype) { 
369                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
370                         push @bind, $categorytype;
371                 }
372         my $nsth=$dbh->prepare($query);
373                 $nsth->execute( @bind );
374         my @cats = ();
375         while ( my ($cat) = $nsth->fetchrow_array ) {
376             push( @cats, $cat );
377         }
378         $data->{'categories'} = \@cats;
379         push( @results, $data );
380     }
381     return \@results;
382 }
383
384 =head2 ModBranchCategoryInfo
385
386 &ModBranchCategoryInfo($data);
387 sets the data from the editbranch form, and writes to the database...
388
389 =cut
390
391 sub ModBranchCategoryInfo {
392     my ($data) = @_;
393     my $dbh    = C4::Context->dbh;
394     if ($data->{'add'}){
395         # we are doing an insert
396   my $sth   = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
397         $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
398     }
399     else {
400         # modifying
401         my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
402         $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
403     }
404 }
405 1;
406 __END__
407
408 =head1 AUTHOR
409
410 Koha Development Team <http://koha-community.org/>
411
412 =cut