fix problem that only one branches was on the screenbr
[koha.git] / admin / branches.pl
1 #!/usr/bin/perl
2
3 # Finlay working on this file from 26-03-2002
4 # Reorganising this branches admin page.....
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Output;
28 use HTML::Template;
29
30 # Fixed variables
31 my $linecolor1='#ffffcc';
32 my $linecolor2='white';
33 my $backgroundimage="/images/background-mem.gif";
34 my $script_name="/cgi-bin/koha/admin/branches.pl";
35 my $pagesize=20;
36
37
38 #######################################################################################
39 # Main loop....
40
41 my $input = new CGI;
42 my $branchcode=$input->param('branchcode');
43 my $op = $input->param('op');
44
45 my $template = gettemplate("parameters/branches.tmpl",0);
46 if ($op) {
47 $template->param(script_name => $script_name,
48                                                 $op              => 1); # we show only the TMPL_VAR names $op
49 } else {
50 $template->param(script_name => $script_name,
51                                                 else              => 1); # we show only the TMPL_VAR names $op
52 }
53 $template->param(action => $script_name);
54
55 if ($op eq 'add') {
56 # If the user has pressed the "add new branch" button. 
57     heading("Branches: Add Branch");
58     editbranchform();
59
60 } elsif ($op eq 'edit') {
61 # if the user has pressed the "edit branch settings" button.
62     heading("Branches: Edit Branch");
63     $template->param(add => 1);
64     editbranchform($branchcode);
65
66 } elsif ($op eq 'add_validate') {
67 # confirm settings change...
68     my $params = $input->Vars;
69     unless ($params->{'branchcode'} && $params->{'branchname'}) {
70         default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
71     } else {
72         setbranchinfo($params);
73         $template->param(else => 1);
74         default ("Branch record changed for branch: $params->{'branchname'}");
75     }
76
77 } elsif ($op eq 'delete') {
78 # if the user has pressed the "delete branch" button.
79     my $message = checkdatabasefor($branchcode);
80     if ($message) {
81         $template->param(else => 1);
82         default($message);
83     } else {
84         deleteconfirm($branchcode);
85         $template->param(delete_confirm => 1);
86         $template->param(branchcode => $branchcode);
87     }
88
89 } elsif ($op eq 'delete_confirmed') {
90 # actually delete branch and return to the main screen....
91     deletebranch($branchcode);
92     $template->param(else => 1);
93     default("The branch with code $branchcode has been deleted.");
94
95 } else {
96 # if no operation has been set...
97     default();
98 }
99
100
101
102 ######################################################################################################
103 #
104 # html output functions....
105
106 sub default {
107     my ($message) = @_;
108     heading("Branches");
109     $template->param(message => $message);
110     $template->param(action => $script_name);
111     branchinfotable();
112     
113     
114 }
115
116 sub heading {
117     my ($head) = @_;
118     $template->param(head => $head);
119 }
120
121 sub editbranchform {
122 # prepares the edit form...
123     my ($branchcode) = @_;
124     my $data;
125     if ($branchcode) {
126         $data = getbranchinfo($branchcode);
127         $data = $data->[0];
128         $template->param(branchcode => $data->{'branchcode'});
129         $template->param(branchname => $data->{'branchname'});
130         $template->param(branchaddress1 => $data->{'branchaddress1'});
131         $template->param(branchaddress2 => $data->{'branchaddress2'});
132         $template->param(branchaddress3 => $data->{'branchaddress3'});
133         $template->param(branchphone => $data->{'branchphone'});
134         $template->param(branchfax => $data->{'branchfax'});
135         $template->param(branchemail => $data->{'branchemail'});
136     }
137 # make the checkboxs.....
138     my $catinfo = getcategoryinfo();
139     my $catcheckbox;
140     foreach my $cat (@$catinfo) {
141         my $checked = "";
142         my $tmp = $cat->{'categorycode'};
143         if (grep {/^$tmp$/} @{$data->{'categories'}}) {
144             $checked = "CHECKED";
145                 }
146     $template->param(categoryname => $cat->{'categoryname'});
147     $template->param(categorycode => $cat->{'categorycode'});
148     $template->param(codedescription => $checked>$cat->{'codedescription'});    
149     }
150    
151 }
152
153 sub deleteconfirm {
154 # message to print if the 
155     my ($branchcode) = @_;
156 }
157
158
159 sub branchinfotable {
160 # makes the html for a table of branch info from reference to an array of hashs.
161
162     my ($branchcode) = @_;
163     my $branchinfo;
164     if ($branchcode) {
165         $branchinfo = getbranchinfo($branchcode);
166     } else {
167         $branchinfo = getbranchinfo();
168     }
169     my $color;
170     my @loop_data =();
171     foreach my $branch (@$branchinfo) {
172         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
173         my $address = '';
174         $address .= $branch->{'branchaddress1'}          if ($branch->{'branchaddress1'});
175         $address .= '<br>'.$branch->{'branchaddress2'}   if ($branch->{'branchaddress2'});
176         $address .= '<br>'.$branch->{'branchaddress3'}   if ($branch->{'branchaddress3'});
177         $address .= '<br>ph: '.$branch->{'branchphone'}   if ($branch->{'branchphone'});
178         $address .= '<br>fax: '.$branch->{'branchfax'}    if ($branch->{'branchfax'});
179         $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
180         $address = '(nothing entered)' unless ($address);
181         my $categories = '';
182         foreach my $cat (@{$branch->{'categories'}}) {
183             my ($catinfo) = @{getcategoryinfo($cat)};
184             $categories .= $catinfo->{'categoryname'}."<br>";
185         }
186         $categories = '(no categories set)' unless ($categories);
187         my @colors = ();
188         my @branch_name = ();
189         my @branch_code = ();
190         my @address = ();
191         my @categories = ();
192         my @value = ();
193         my @action =();
194         push(@colors,$color);
195         push(@branch_name,$branch->{'branchname'});
196         push(@branch_code,$branch->{'branchcode'});
197         push(@address,$address);
198         push(@categories,$categories);
199         push(@value,$branch->{'branchcode'});
200         push(@action,"/cgi-bin/koha/admin/branches.pl");
201         while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
202         my %row_data;
203         $row_data{color} = shift @colors;
204         $row_data{branch_name} = shift @branch_name;
205         $row_data{branch_code} = shift @branch_code;
206         $row_data{address} = shift @address;
207         $row_data{categories} = shift @categories;
208         $row_data{value} = shift @value;
209         $row_data{action} = shift @action;
210         push(@loop_data, \%row_data);
211     }
212     
213     }
214     $template->param(branches => \@loop_data);
215
216 }
217
218 sub branchcategoriestable {
219 #Needs to be implemented...
220
221     my $categoryinfo = getcategoryinfo();
222 my $color;
223     foreach my $cat (@$categoryinfo) {
224         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
225 $template->param(color => $color);
226 $template->param(categoryname => $cat->{'categoryname'});
227 $template->param(categorycode => $cat->{'categorycode'});
228 $template->param(codedescription => $cat->{'codedescription'});
229     }
230 }
231
232 ######################################################################################################
233 #
234 # Database functions....
235
236 sub getbranchinfo {
237 # returns a reference to an array of hashes containing branches,
238
239     my ($branchcode) = @_;
240     my $dbh = C4::Context->dbh;
241     my $query;
242     if ($branchcode) {
243         my $bc = $dbh->quote($branchcode);
244         $query = "Select * from branches where branchcode = $bc";
245     }
246     else {$query = "Select * from branches";}
247     my $sth = $dbh->prepare($query);
248     $sth->execute;
249     my @results;
250     while (my $data = $sth->fetchrow_hashref) { 
251         my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
252         $query = "select categorycode from branchrelations where branchcode = $brc";
253         my $nsth = $dbh->prepare($query);
254         $nsth->execute;
255         my @cats = ();
256         while (my ($cat) = $nsth->fetchrow_array) {
257             push(@cats, $cat);
258         }
259         $nsth->finish;
260         $data->{'categories'} = \@cats;
261         push(@results, $data);
262     }
263     $sth->finish;
264     return \@results;
265 }
266
267 sub getcategoryinfo {
268 # returns a reference to an array of hashes containing branches,
269     my ($catcode) = @_;
270     my $dbh = C4::Context->dbh;
271     my $query;
272     if ($catcode) {
273         my $cc = $dbh->quote($catcode);
274         $query = "select * from branchcategories where categorycode = $cc";
275     } else {
276         $query = "Select * from branchcategories";
277     }
278     my $sth = $dbh->prepare($query);
279     $sth->execute;
280     my @results;
281     while (my $data = $sth->fetchrow_hashref) { 
282         push(@results, $data);
283     }
284     $sth->finish;
285     return \@results;
286 }
287
288 sub setbranchinfo {
289 # sets the data from the editbranch form, and writes to the database...
290     my ($data) = @_;
291     my $dbh = C4::Context->dbh;
292     my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
293     my $tmp;
294     $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
295     $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
296     $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
297     $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
298     $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
299     $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
300     $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
301     $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
302     my $sth=$dbh->prepare($query);
303     $sth->execute;
304     $sth->finish;
305 # sort out the categories....
306     my @checkedcats;
307     my $cats = getcategoryinfo();
308     foreach my $cat (@$cats) {
309         my $code = $cat->{'categorycode'};
310         if ($data->{$code}) {
311             push(@checkedcats, $code);
312         }
313     }
314     my $branchcode = $data->{'branchcode'};
315     my $branch = getbranchinfo($branchcode);
316     $branch = $branch->[0];
317     my $branchcats = $branch->{'categories'};
318     my @addcats;
319     my @removecats;
320     foreach my $bcat (@$branchcats) {
321         unless (grep {/^$bcat$/} @checkedcats) {
322             push(@removecats, $bcat);
323         }
324     }
325     foreach my $ccat (@checkedcats){
326         unless (grep {/^$ccat$/} @$branchcats) {
327             push(@addcats, $ccat);
328         }
329     }   
330     # FIXME - There's already a $dbh in this scope.
331     my $dbh = C4::Context->dbh;
332     foreach my $cat (@addcats) {
333         my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
334         my $sth = $dbh->prepare($query);
335         $sth->execute;
336         $sth->finish;
337     }
338     foreach my $cat (@removecats) {
339         my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
340         my $sth = $dbh->prepare($query);
341         $sth->execute;
342         $sth->finish;
343     }
344 }
345
346 sub deletebranch {
347 # delete branch...
348     my ($branchcode) = @_;
349     my $query = "delete from branches where branchcode = '$branchcode'";
350     my $dbh = C4::Context->dbh;
351     my $sth=$dbh->prepare($query);
352     $sth->execute;
353     $sth->finish;
354 }
355
356 sub checkdatabasefor {
357 # check to see if the branchcode is being used in the database somewhere....
358     my ($branchcode) = @_;
359     my $dbh = C4::Context->dbh;
360     my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
361     $sth->execute;
362     my ($total) = $sth->fetchrow_array;
363     $sth->finish;
364     my $message;
365     if ($total) {
366         $message = "Branch cannot be deleted because there are $total items using that branch.";
367     } 
368     return $message;
369 }
370
371 print "Content-Type: text/html\n\n", $template->output;