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