New styles for bulk hold and bulk tag inputs on search results page.
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20 require Exporter;
21 use C4::Context;
22 use C4::Koha;
23
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
25
26 BEGIN {
27         # set the version for version checking
28         $VERSION = 3.01;
29         @ISA    = qw(Exporter);
30         @EXPORT = qw(
31                 &GetBranchCategory
32                 &GetBranchName
33                 &GetBranch
34                 &GetBranches
35                 &GetBranchesLoop
36                 &GetBranchDetail
37                 &get_branchinfos_of
38                 &ModBranch
39                 &CheckBranchCategorycode
40                 &GetBranchInfo
41                 &GetCategoryTypes
42                 &GetBranchCategories
43                 &GetBranchesInCategory
44                 &ModBranchCategoryInfo
45                 &DelBranch
46                 &DelBranchCategory
47         );
48 }
49
50 =head1 NAME
51
52 C4::Branch - Koha branch module
53
54 =head1 SYNOPSIS
55
56 use C4::Branch;
57
58 =head1 DESCRIPTION
59
60 The functions in this module deal with branches.
61
62 =head1 FUNCTIONS
63
64 =head2 GetBranches
65
66   $branches = &GetBranches();
67
68   Returns informations about ALL branches, IndependantBranches Insensitive.
69   GetBranchInfo() returns the same information without the problems of this function 
70   (namespace collision, mainly).
71   Create a branch selector with the following code.
72   
73 =head3 in PERL SCRIPT
74
75     my $branches = GetBranches;
76     my @branchloop;
77     foreach my $thisbranch (sort keys %$branches) {
78         my $selected = 1 if $thisbranch eq $branch;
79         my %row =(value => $thisbranch,
80                     selected => $selected,
81                     branchname => $branches->{$thisbranch}->{branchname},
82                 );
83         push @branchloop, \%row;
84     }
85
86 =head3 in TEMPLATE
87
88     <select name="branch">
89         <option value="">Default</option>
90         <!-- TMPL_LOOP name="branchloop" -->
91         <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
92         <!-- /TMPL_LOOP -->
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     # returns a reference to a hash of references to ALL branches...
102     my %branches;
103     my $dbh = C4::Context->dbh;
104     my $sth;
105     my $query="SELECT * FROM branches";
106     my @bind_parameters;
107     if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
108       $query .= ' WHERE branchcode = ? ';
109       push @bind_parameters, C4::Context->userenv->{branch};
110     }
111         $query.=" ORDER BY branchname";
112     $sth = $dbh->prepare($query);
113     $sth->execute( @bind_parameters );
114     while ( my $branch = $sth->fetchrow_hashref ) {
115         my $nsth =
116           $dbh->prepare(
117             "SELECT categorycode FROM branchrelations WHERE branchcode = ?");
118         $nsth->execute( $branch->{'branchcode'} );
119         while ( my ($cat) = $nsth->fetchrow_array ) {
120
121             # FIXME - This seems wrong. It ought to be
122             # $branch->{categorycodes}{$cat} = 1;
123             # otherwise, there's a namespace collision if there's a
124             # category with the same name as a field in the 'branches'
125             # table (i.e., don't create a category called "issuing").
126             # In addition, the current structure doesn't really allow
127             # you to list the categories that a branch belongs to:
128             # you'd have to list keys %$branch, and remove those keys
129             # that aren't fields in the "branches" table.
130          #   $branch->{$cat} = 1;
131             $branch->{category}{$cat} = 1;
132         }
133         $branches{ $branch->{'branchcode'} } = $branch;
134     }
135     return ( \%branches );
136 }
137
138 sub GetBranchesLoop (;$$) {  # since this is what most pages want anyway
139     my $branch   = @_ ? shift : '';     # optional first argument is branchcode of "my branch", if preselection is wanted.
140     my $onlymine = @_ ? shift : C4::Context->preference("IndependantBranches");
141     my $branches = GetBranches($onlymine);
142     my @loop;
143     foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
144         push @loop, {
145             value => $_,
146             selected => ($_ eq $branch) ? 1 : 0, 
147             branchname => $branches->{$_}->{branchname},
148         };
149     }
150     return \@loop;
151 }
152
153 =head2 GetBranchName
154
155 =cut
156
157 sub GetBranchName {
158     my ($branchcode) = @_;
159     my $dbh = C4::Context->dbh;
160     my $sth;
161     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
162     $sth->execute($branchcode);
163     my $branchname = $sth->fetchrow_array;
164     $sth->finish;
165     return ($branchname);
166 }
167
168 =head2 ModBranch
169
170 &ModBranch($newvalue);
171
172 This function modify an existing branches.
173
174 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
175
176 =cut
177
178 sub ModBranch {
179     my ($data) = @_;
180     
181     my $dbh    = C4::Context->dbh;
182     if ($data->{add}) {
183         my $query  = "
184             INSERT INTO branches
185             (branchcode,branchname,branchaddress1,
186             branchaddress2,branchaddress3,branchphone,
187             branchfax,branchemail,branchip,branchprinter)
188             VALUES (?,?,?,?,?,?,?,?,?,?)
189         ";
190         my $sth    = $dbh->prepare($query);
191         $sth->execute(
192             $data->{'branchcode'},       $data->{'branchname'},
193             $data->{'branchaddress1'},   $data->{'branchaddress2'},
194             $data->{'branchaddress3'},   $data->{'branchphone'},
195             $data->{'branchfax'},        $data->{'branchemail'},
196             $data->{'branchip'},         $data->{'branchprinter'},
197         );
198     } else {
199         my $query  = "
200             UPDATE branches
201             SET branchname=?,branchaddress1=?,
202                 branchaddress2=?,branchaddress3=?,branchphone=?,
203                 branchfax=?,branchemail=?,branchip=?,branchprinter=?
204             WHERE branchcode=?
205         ";
206         my $sth    = $dbh->prepare($query);
207         $sth->execute(
208             $data->{'branchname'},
209             $data->{'branchaddress1'},   $data->{'branchaddress2'},
210             $data->{'branchaddress3'},   $data->{'branchphone'},
211             $data->{'branchfax'},        $data->{'branchemail'},
212             $data->{'branchip'},         $data->{'branchprinter'},
213             $data->{'branchcode'},
214         );
215     }
216     # sort out the categories....
217     my @checkedcats;
218     my $cats = GetBranchCategory();
219     foreach my $cat (@$cats) {
220         my $code = $cat->{'categorycode'};
221         if ( $data->{$code} ) {
222             push( @checkedcats, $code );
223         }
224     }
225     my $branchcode = uc( $data->{'branchcode'} );
226     my $branch     = GetBranchInfo($branchcode);
227     $branch = $branch->[0];
228     my $branchcats = $branch->{'categories'};
229     my @addcats;
230     my @removecats;
231     foreach my $bcat (@$branchcats) {
232
233         unless ( grep { /^$bcat$/ } @checkedcats ) {
234             push( @removecats, $bcat );
235         }
236     }
237     foreach my $ccat (@checkedcats) {
238         unless ( grep { /^$ccat$/ } @$branchcats ) {
239             push( @addcats, $ccat );
240         }
241     }
242     foreach my $cat (@addcats) {
243         my $sth =
244           $dbh->prepare(
245 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
246           );
247         $sth->execute( $branchcode, $cat );
248         $sth->finish;
249     }
250     foreach my $cat (@removecats) {
251         my $sth =
252           $dbh->prepare(
253             "delete from branchrelations where branchcode=? and categorycode=?"
254           );
255         $sth->execute( $branchcode, $cat );
256         $sth->finish;
257     }
258 }
259
260 =head2 GetBranchCategory
261
262 $results = GetBranchCategory($categorycode);
263
264 C<$results> is an ref to an array.
265
266 =cut
267
268 sub GetBranchCategory {
269
270     # returns a reference to an array of hashes containing branches,
271     my ($catcode) = @_;
272     my $dbh = C4::Context->dbh;
273     my $sth;
274
275     #    print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
276     if ($catcode) {
277         $sth =
278           $dbh->prepare(
279             "select * from branchcategories where categorycode = ?");
280         $sth->execute($catcode);
281     }
282     else {
283         $sth = $dbh->prepare("Select * from branchcategories");
284         $sth->execute();
285     }
286     my @results;
287     while ( my $data = $sth->fetchrow_hashref ) {
288         push( @results, $data );
289     }
290     $sth->finish;
291
292     #    print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
293     return \@results;
294 }
295
296 =head2 GetBranchCategories
297
298   my $categories = GetBranchCategories($branchcode,$categorytype);
299
300 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
301 i.e. categorycode, categorydescription, categorytype, categoryname.
302 if $branchcode and/or $categorytype are passed, limit set to categories that
303 $branchcode is a member of , and to $categorytype.
304
305 =cut
306
307 sub GetBranchCategories {
308     my ($branchcode,$categorytype) = @_;
309         my $dbh = C4::Context->dbh();
310         my $query = "SELECT c.* FROM branchcategories c";
311         my (@where, @bind);
312         if($branchcode) {
313                 $query .= ",branchrelations r, branches b ";
314                 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";  
315                 push @bind , $branchcode;
316         }
317         if ($categorytype) {
318                 push @where, " c.categorytype=? ";
319                 push @bind, $categorytype;
320         }
321         $query .= " where " . join(" and ", @where) if(@where);
322         $query .= " order by categorytype,c.categorycode";
323         my $sth=$dbh->prepare( $query);
324         $sth->execute(@bind);
325         
326         my $branchcats = $sth->fetchall_arrayref({});
327         $sth->finish();
328         return( $branchcats );
329 }
330
331 =head2 GetCategoryTypes
332
333 $categorytypes = GetCategoryTypes;
334 returns a list of category types.
335 Currently these types are HARDCODED.
336 type: 'searchdomain' defines a group of agencies that the calling library may search in.
337 Other usage of agency categories falls under type: 'properties'.
338         to allow for other uses of categories.
339 The searchdomain bit may be better implemented as a separate module, but
340 the categories were already here, and minimally used.
341 =cut
342
343         #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
344 sub GetCategoryTypes() {
345         return ( 'searchdomain','properties');
346 }
347
348 =head2 GetBranch
349
350 $branch = GetBranch( $query, $branches );
351
352 =cut
353
354 sub GetBranch ($$) {
355     my ( $query, $branches ) = @_;    # get branch for this query from branches
356     my $branch = $query->param('branch');
357     my %cookie = $query->cookie('userenv');
358     ($branch)                || ($branch = $cookie{'branchname'});
359     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
360     return $branch;
361 }
362
363 =head2 GetBranchDetail
364
365   $branchname = &GetBranchDetail($branchcode);
366
367 Given the branch code, the function returns the corresponding
368 branch name for a comprehensive information display
369
370 =cut
371
372 sub GetBranchDetail {
373     my ($branchcode) = @_;
374     my $dbh = C4::Context->dbh;
375     my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
376     $sth->execute($branchcode);
377     my $branchname = $sth->fetchrow_hashref();
378     $sth->finish();
379     return $branchname;
380 }
381
382 =head2 get_branchinfos_of
383
384   my $branchinfos_of = get_branchinfos_of(@branchcodes);
385
386 Associates a list of branchcodes to the information of the branch, taken in
387 branches table.
388
389 Returns a href where keys are branchcodes and values are href where keys are
390 branch information key.
391
392   print 'branchname is ', $branchinfos_of->{$code}->{branchname};
393
394 =cut
395
396 sub get_branchinfos_of {
397     my @branchcodes = @_;
398
399     my $query = '
400     SELECT branchcode,
401        branchname
402     FROM branches
403     WHERE branchcode IN ('
404       . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
405 ';
406     return C4::Koha::get_infos_of( $query, 'branchcode' );
407 }
408
409
410 =head2 GetBranchesInCategory
411
412   my $branches = GetBranchesInCategory($categorycode);
413
414 Returns a href:  keys %$branches eq (branchcode,branchname) .
415
416 =cut
417
418 sub GetBranchesInCategory($) {
419     my ($categorycode) = @_;
420         my @branches;
421         my $dbh = C4::Context->dbh();
422         my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b 
423                                                         where r.branchcode=b.branchcode and r.categorycode=?");
424     $sth->execute($categorycode);
425         while (my $branch = $sth->fetchrow) {
426                 push @branches, $branch;
427         }
428         $sth->finish();
429         return( \@branches );
430 }
431
432 =head2 GetBranchInfo
433
434 $results = GetBranchInfo($branchcode);
435
436 returns C<$results>, a reference to an array of hashes containing branches.
437 if $branchcode, just this branch, with associated categories.
438 if ! $branchcode && $categorytype, all branches in the category.
439 =cut
440
441 sub GetBranchInfo {
442     my ($branchcode,$categorytype) = @_;
443     my $dbh = C4::Context->dbh;
444     my $sth;
445
446
447         if ($branchcode) {
448         $sth =
449           $dbh->prepare(
450             "Select * from branches where branchcode = ? order by branchcode");
451         $sth->execute($branchcode);
452     }
453     else {
454         $sth = $dbh->prepare("Select * from branches order by branchcode");
455         $sth->execute();
456     }
457     my @results;
458     while ( my $data = $sth->fetchrow_hashref ) {
459                 my @bind = ($data->{'branchcode'});
460         my $query= "select r.categorycode from branchrelations r";
461                 $query .= ", branchcategories c " if($categorytype);
462                 $query .= " where  branchcode=? ";
463                 if($categorytype) { 
464                         $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
465                         push @bind, $categorytype;
466                 }
467         my $nsth=$dbh->prepare($query);
468                 $nsth->execute( @bind );
469         my @cats = ();
470         while ( my ($cat) = $nsth->fetchrow_array ) {
471             push( @cats, $cat );
472         }
473         $nsth->finish;
474         $data->{'categories'} = \@cats;
475         push( @results, $data );
476     }
477     $sth->finish;
478     return \@results;
479 }
480
481 =head2 DelBranch
482
483 &DelBranch($branchcode);
484
485 =cut
486
487 sub DelBranch {
488     my ($branchcode) = @_;
489     my $dbh = C4::Context->dbh;
490     my $sth = $dbh->prepare("delete from branches where branchcode = ?");
491     $sth->execute($branchcode);
492     $sth->finish;
493 }
494
495 =head2 ModBranchCategoryInfo
496
497 &ModBranchCategoryInfo($data);
498 sets the data from the editbranch form, and writes to the database...
499
500 =cut
501
502 sub ModBranchCategoryInfo {
503     my ($data) = @_;
504     my $dbh    = C4::Context->dbh;
505         if ($data->{'add'}){
506                 # we are doing an insert
507                 my $sth   = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
508                 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
509                 $sth->finish();         
510         }
511         else {
512                 # modifying
513                 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
514                 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
515                 $sth->finish();
516         }
517 }
518
519 =head2 DeleteBranchCategory
520
521 DeleteBranchCategory($categorycode);
522
523 =cut
524
525 sub DelBranchCategory {
526     my ($categorycode) = @_;
527     my $dbh = C4::Context->dbh;
528     my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
529     $sth->execute($categorycode);
530     $sth->finish;
531 }
532
533 =head2 CheckBranchCategorycode
534
535 $number_rows_affected = CheckBranchCategorycode($categorycode);
536
537 =cut
538
539 sub CheckBranchCategorycode {
540
541     # check to see if the branchcode is being used in the database somewhere....
542     my ($categorycode) = @_;
543     my $dbh            = C4::Context->dbh;
544     my $sth            =
545       $dbh->prepare(
546         "select count(*) from branchrelations where categorycode=?");
547     $sth->execute($categorycode);
548     my ($total) = $sth->fetchrow_array;
549     return $total;
550 }
551
552 1;
553 __END__
554
555 =head1 AUTHOR
556
557 Koha Developement team <info@koha.org>
558
559 =cut