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