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