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