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