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