3 # Finlay working on this file from 26-03-2002
4 # Reorganising this branches admin page.....
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";
19 #######################################################################################
23 my $branchcode=$input->param('branchcode');
24 my $op = $input->param('op');
29 # start the page and read in includes
31 print startmenu('admin');
34 # If the user has pressed the "add new branch" button.
35 print heading("Branches: Add Branch");
36 print editbranchform();
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);
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");
49 setbranchinfo($params);
50 default ("Branch record changed for branch: $params->{'branchname'}");
53 } elsif ($op eq 'delete') {
54 # if the user has pressed the "delete branch" button.
55 my $message = checkdatabasefor($branchcode);
59 print deleteconfirm($branchcode);
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.");
68 # if no operation has been set...
73 print endmenu('admin');
76 ######################################################################################################
78 # html output functions....
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();
91 return "<FONT SIZE=6><em>$head</em></FONT><br>";
95 # prepares the edit form...
96 my ($branchcode) = @_;
99 $data = getbranchinfo($branchcode);
102 # make the checkboxs.....
103 my $catinfo = getcategoryinfo();
105 foreach my $cat (@$catinfo) {
107 my $tmp = $cat->{'categorycode'};
108 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
109 $checked = "CHECKED";
111 $catcheckbox .= <<EOF;
112 <tr><td>$cat->{'categoryname'}</td>
113 <td><INPUT TYPE="checkbox" NAME="$cat->{'categorycode'}" VALUE="1" $checked>$cat->{'codedescription'}</td></tr>
117 <form action='$script_name' name=Aform method=post>
118 <input type=hidden name=op value='add_validate'>
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'}'> </td></tr>
123 <tr><td>Address</td><td><input type=text name=branchaddress1 value='$data->{'branchaddress1'}'></td></tr>
124 <tr><td> </td><td><input type=text name=branchaddress2 value='$data->{'branchaddress2'}'></td></tr>
125 <tr><td> </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> </td><td><input type=submit value='Submit'></td></tr>
137 # message to print if the
138 my ($branchcode) = @_;
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>
151 sub branchinfotable {
152 # makes the html for a table of branch info from reference to an array of hashs.
154 my ($branchcode) = @_;
157 $branchinfo = getbranchinfo($branchcode);
159 $branchinfo = getbranchinfo();
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> </b></td>
175 foreach my $branch (@$branchinfo) {
176 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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);
186 foreach my $cat (@{$branch->{'categories'}}) {
187 my ($catinfo) = @{getcategoryinfo($cat)};
188 $categories .= $catinfo->{'categoryname'}."<br>";
190 $categories = '(no categories set)' unless ($categories);
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>
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>
210 $table .= "</table><br>";
214 sub branchcategoriestable {
215 #Needs to be implemented...
217 my $categoryinfo = getcategoryinfo();
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>
229 foreach my $cat (@$categoryinfo) {
230 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
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>
239 $table .= "</table>";
243 ######################################################################################################
245 # Database functions....
248 # returns a reference to an array of hashes containing branches,
250 my ($branchcode) = @_;
251 my $dbh = &C4Connect;
254 my $bc = $dbh->quote($branchcode);
255 $query = "Select * from branches where branchcode = $bc";
257 else {$query = "Select * from branches";}
258 my $sth = $dbh->prepare($query);
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);
267 while (my ($cat) = $nsth->fetchrow_array) {
271 $data->{'categories'} = \@cats;
272 push(@results, $data);
279 sub getcategoryinfo {
280 # returns a reference to an array of hashes containing branches,
282 my $dbh = &C4Connect;
285 my $cc = $dbh->quote($catcode);
286 $query = "select * from branchcategories where categorycode = $cc";
288 $query = "Select * from branchcategories";
290 my $sth = $dbh->prepare($query);
293 while (my $data = $sth->fetchrow_hashref) {
294 push(@results, $data);
302 # sets the data from the editbranch form, and writes to the database...
305 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
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);
319 # sort out the categories....
321 my $cats = getcategoryinfo();
322 foreach my $cat (@$cats) {
323 my $code = $cat->{'categorycode'};
324 if ($data->{$code}) {
325 push(@checkedcats, $code);
328 my $branchcode = $data->{'branchcode'};
329 my $branch = getbranchinfo($branchcode);
330 $branch = $branch->[0];
331 my $branchcats = $branch->{'categories'};
334 foreach my $bcat (@$branchcats) {
335 unless (grep {/^$bcat$/} @checkedcats) {
336 push(@removecats, $bcat);
339 foreach my $ccat (@checkedcats){
340 unless (grep {/^$ccat$/} @$branchcats) {
341 push(@addcats, $ccat);
345 foreach my $cat (@addcats) {
346 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
347 my $sth = $dbh->prepare($query);
351 foreach my $cat (@removecats) {
352 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
353 my $sth = $dbh->prepare($query);
362 my ($branchcode) = @_;
363 my $query = "delete from branches where branchcode = '$branchcode'";
365 my $sth=$dbh->prepare($query);
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'");
377 my ($total) = $sth->fetchrow_array;
382 $message = "Branch cannot be deleted because there are $total items using that branch.";