Modified Files
[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     foreach my $branch (@$branchinfo) {
171         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
172         my $address = '';
173         $address .= $branch->{'branchaddress1'}          if ($branch->{'branchaddress1'});
174         $address .= '<br>'.$branch->{'branchaddress2'}   if ($branch->{'branchaddress2'});
175         $address .= '<br>'.$branch->{'branchaddress3'}   if ($branch->{'branchaddress3'});
176         $address .= '<br>ph: '.$branch->{'branchphone'}   if ($branch->{'branchphone'});
177         $address .= '<br>fax: '.$branch->{'branchfax'}    if ($branch->{'branchfax'});
178         $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
179         $address = '(nothing entered)' unless ($address);
180         my $categories = '';
181         foreach my $cat (@{$branch->{'categories'}}) {
182             my ($catinfo) = @{getcategoryinfo($cat)};
183             $categories .= $catinfo->{'categoryname'}."<br>";
184         }
185         $categories = '(no categories set)' unless ($categories);
186 $template->param(color => $color);
187 $template->param(branch_name => $branch->{'branchname'});
188 $template->param(adress => $address);
189 $template->param(categories => $categories);
190 $template->param(branch_code => $branch->{'branchcode'});
191 $template->param(value => $branch->{'branchcode'});
192     }
193
194 }
195
196 sub branchcategoriestable {
197 #Needs to be implemented...
198
199     my $categoryinfo = getcategoryinfo();
200 my $color;
201     foreach my $cat (@$categoryinfo) {
202         ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
203 $template->param(color => $color);
204 $template->param(categoryname => $cat->{'categoryname'});
205 $template->param(categorycode => $cat->{'categorycode'});
206 $template->param(codedescription => $cat->{'codedescription'});
207     }
208 }
209
210 ######################################################################################################
211 #
212 # Database functions....
213
214 sub getbranchinfo {
215 # returns a reference to an array of hashes containing branches,
216
217     my ($branchcode) = @_;
218     my $dbh = C4::Context->dbh;
219     my $query;
220     if ($branchcode) {
221         my $bc = $dbh->quote($branchcode);
222         $query = "Select * from branches where branchcode = $bc";
223     }
224     else {$query = "Select * from branches";}
225     my $sth = $dbh->prepare($query);
226     $sth->execute;
227     my @results;
228     while (my $data = $sth->fetchrow_hashref) { 
229         my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
230         $query = "select categorycode from branchrelations where branchcode = $brc";
231         my $nsth = $dbh->prepare($query);
232         $nsth->execute;
233         my @cats = ();
234         while (my ($cat) = $nsth->fetchrow_array) {
235             push(@cats, $cat);
236         }
237         $nsth->finish;
238         $data->{'categories'} = \@cats;
239         push(@results, $data);
240     }
241     $sth->finish;
242     return \@results;
243 }
244
245 sub getcategoryinfo {
246 # returns a reference to an array of hashes containing branches,
247     my ($catcode) = @_;
248     my $dbh = C4::Context->dbh;
249     my $query;
250     if ($catcode) {
251         my $cc = $dbh->quote($catcode);
252         $query = "select * from branchcategories where categorycode = $cc";
253     } else {
254         $query = "Select * from branchcategories";
255     }
256     my $sth = $dbh->prepare($query);
257     $sth->execute;
258     my @results;
259     while (my $data = $sth->fetchrow_hashref) { 
260         push(@results, $data);
261     }
262     $sth->finish;
263     return \@results;
264 }
265
266 sub setbranchinfo {
267 # sets the data from the editbranch form, and writes to the database...
268     my ($data) = @_;
269     my $dbh = C4::Context->dbh;
270     my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
271     my $tmp;
272     $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
273     $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
274     $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
275     $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
276     $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
277     $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
278     $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
279     $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
280     my $sth=$dbh->prepare($query);
281     $sth->execute;
282     $sth->finish;
283 # sort out the categories....
284     my @checkedcats;
285     my $cats = getcategoryinfo();
286     foreach my $cat (@$cats) {
287         my $code = $cat->{'categorycode'};
288         if ($data->{$code}) {
289             push(@checkedcats, $code);
290         }
291     }
292     my $branchcode = $data->{'branchcode'};
293     my $branch = getbranchinfo($branchcode);
294     $branch = $branch->[0];
295     my $branchcats = $branch->{'categories'};
296     my @addcats;
297     my @removecats;
298     foreach my $bcat (@$branchcats) {
299         unless (grep {/^$bcat$/} @checkedcats) {
300             push(@removecats, $bcat);
301         }
302     }
303     foreach my $ccat (@checkedcats){
304         unless (grep {/^$ccat$/} @$branchcats) {
305             push(@addcats, $ccat);
306         }
307     }   
308     # FIXME - There's already a $dbh in this scope.
309     my $dbh = C4::Context->dbh;
310     foreach my $cat (@addcats) {
311         my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
312         my $sth = $dbh->prepare($query);
313         $sth->execute;
314         $sth->finish;
315     }
316     foreach my $cat (@removecats) {
317         my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
318         my $sth = $dbh->prepare($query);
319         $sth->execute;
320         $sth->finish;
321     }
322 }
323
324 sub deletebranch {
325 # delete branch...
326     my ($branchcode) = @_;
327     my $query = "delete from branches where branchcode = '$branchcode'";
328     my $dbh = C4::Context->dbh;
329     my $sth=$dbh->prepare($query);
330     $sth->execute;
331     $sth->finish;
332 }
333
334 sub checkdatabasefor {
335 # check to see if the branchcode is being used in the database somewhere....
336     my ($branchcode) = @_;
337     my $dbh = C4::Context->dbh;
338     my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
339     $sth->execute;
340     my ($total) = $sth->fetchrow_array;
341     $sth->finish;
342     my $message;
343     if ($total) {
344         $message = "Branch cannot be deleted because there are $total items using that branch.";
345     } 
346     return $message;
347 }
348
349 print "Content-Type: text/html\n\n", $template->output;