3 # Finlay working on this file from 26-03-2002
4 # Reorganising this branches admin page.....
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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
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.
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
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";
37 #######################################################################################
41 my $branchcode=$input->param('branchcode');
42 my $op = $input->param('op');
47 # start the page and read in includes
49 print startmenu('admin');
52 # If the user has pressed the "add new branch" button.
53 print heading("Branches: Add Branch");
54 print editbranchform();
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);
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");
67 setbranchinfo($params);
68 default ("Branch record changed for branch: $params->{'branchname'}");
71 } elsif ($op eq 'delete') {
72 # if the user has pressed the "delete branch" button.
73 my $message = checkdatabasefor($branchcode);
77 print deleteconfirm($branchcode);
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.");
86 # if no operation has been set...
91 print endmenu('admin');
94 ######################################################################################################
96 # html output functions....
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();
109 return "<FONT SIZE=6><em>$head</em></FONT><br>";
113 # prepares the edit form...
114 my ($branchcode) = @_;
117 $data = getbranchinfo($branchcode);
120 # make the checkboxs.....
121 my $catinfo = getcategoryinfo();
123 foreach my $cat (@$catinfo) {
125 my $tmp = $cat->{'categorycode'};
126 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
127 $checked = "CHECKED";
129 $catcheckbox .= <<EOF;
130 <tr><td>$cat->{'categoryname'}</td>
131 <td><INPUT TYPE="checkbox" NAME="$cat->{'categorycode'}" VALUE="1" $checked>$cat->{'codedescription'}</td></tr>
135 <form action='$script_name' name=Aform method=post>
136 <input type=hidden name=op value='add_validate'>
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'}'> </td></tr>
141 <tr><td>Address</td><td><input type=text name=branchaddress1 value='$data->{'branchaddress1'}'></td></tr>
142 <tr><td> </td><td><input type=text name=branchaddress2 value='$data->{'branchaddress2'}'></td></tr>
143 <tr><td> </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> </td><td><input type=submit value='Submit'></td></tr>
155 # message to print if the
156 my ($branchcode) = @_;
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>
169 sub branchinfotable {
170 # makes the html for a table of branch info from reference to an array of hashs.
172 my ($branchcode) = @_;
175 $branchinfo = getbranchinfo($branchcode);
177 $branchinfo = getbranchinfo();
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> </b></td>
193 foreach my $branch (@$branchinfo) {
194 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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);
204 foreach my $cat (@{$branch->{'categories'}}) {
205 my ($catinfo) = @{getcategoryinfo($cat)};
206 $categories .= $catinfo->{'categoryname'}."<br>";
208 $categories = '(no categories set)' unless ($categories);
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>
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>
228 $table .= "</table><br>";
232 sub branchcategoriestable {
233 #Needs to be implemented...
235 my $categoryinfo = getcategoryinfo();
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>
247 foreach my $cat (@$categoryinfo) {
248 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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>
257 $table .= "</table>";
261 ######################################################################################################
263 # Database functions....
266 # returns a reference to an array of hashes containing branches,
268 my ($branchcode) = @_;
269 my $dbh = &C4Connect;
272 my $bc = $dbh->quote($branchcode);
273 $query = "Select * from branches where branchcode = $bc";
275 else {$query = "Select * from branches";}
276 my $sth = $dbh->prepare($query);
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);
285 while (my ($cat) = $nsth->fetchrow_array) {
289 $data->{'categories'} = \@cats;
290 push(@results, $data);
297 sub getcategoryinfo {
298 # returns a reference to an array of hashes containing branches,
300 my $dbh = &C4Connect;
303 my $cc = $dbh->quote($catcode);
304 $query = "select * from branchcategories where categorycode = $cc";
306 $query = "Select * from branchcategories";
308 my $sth = $dbh->prepare($query);
311 while (my $data = $sth->fetchrow_hashref) {
312 push(@results, $data);
320 # sets the data from the editbranch form, and writes to the database...
323 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
325 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
326 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
327 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
328 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
329 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
330 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
331 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
332 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
333 my $sth=$dbh->prepare($query);
337 # sort out the categories....
339 my $cats = getcategoryinfo();
340 foreach my $cat (@$cats) {
341 my $code = $cat->{'categorycode'};
342 if ($data->{$code}) {
343 push(@checkedcats, $code);
346 my $branchcode = $data->{'branchcode'};
347 my $branch = getbranchinfo($branchcode);
348 $branch = $branch->[0];
349 my $branchcats = $branch->{'categories'};
352 foreach my $bcat (@$branchcats) {
353 unless (grep {/^$bcat$/} @checkedcats) {
354 push(@removecats, $bcat);
357 foreach my $ccat (@checkedcats){
358 unless (grep {/^$ccat$/} @$branchcats) {
359 push(@addcats, $ccat);
363 foreach my $cat (@addcats) {
364 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
365 my $sth = $dbh->prepare($query);
369 foreach my $cat (@removecats) {
370 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
371 my $sth = $dbh->prepare($query);
380 my ($branchcode) = @_;
381 my $query = "delete from branches where branchcode = '$branchcode'";
383 my $sth=$dbh->prepare($query);
389 sub checkdatabasefor {
390 # check to see if the branchcode is being used in the database somewhere....
391 my ($branchcode) = @_;
392 my $dbh = &C4Connect;
393 my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
395 my ($total) = $sth->fetchrow_array;
400 $message = "Branch cannot be deleted because there are $total items using that branch.";