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
31 my $linecolor1='#ffffcc';
32 my $linecolor2='white';
33 my $backgroundimage="/images/background-mem.gif";
34 my $script_name="/cgi-bin/koha/admin/branches.pl";
38 #######################################################################################
42 my $branchcode=$input->param('branchcode');
43 my $op = $input->param('op');
45 my $template = gettemplate("parameters/branches.tmpl",0);
47 $template->param(script_name => $script_name,
48 $op => 1); # we show only the TMPL_VAR names $op
50 $template->param(script_name => $script_name,
51 else => 1); # we show only the TMPL_VAR names $op
53 $template->param(action => $script_name);
56 # If the user has pressed the "add new branch" button.
57 heading("Branches: Add Branch");
60 } elsif ($op eq 'edit') {
61 # if the user has pressed the "edit branch settings" button.
62 heading("Branches: Edit Branch");
63 $template->param(add => 1);
64 editbranchform($branchcode);
66 } elsif ($op eq 'add_validate') {
67 # confirm settings change...
68 my $params = $input->Vars;
69 unless ($params->{'branchcode'} && $params->{'branchname'}) {
70 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
72 setbranchinfo($params);
73 $template->param(else => 1);
74 default ("Branch record changed for branch: $params->{'branchname'}");
77 } elsif ($op eq 'delete') {
78 # if the user has pressed the "delete branch" button.
79 my $message = checkdatabasefor($branchcode);
81 $template->param(else => 1);
84 deleteconfirm($branchcode);
85 $template->param(delete_confirm => 1);
86 $template->param(branchcode => $branchcode);
89 } elsif ($op eq 'delete_confirmed') {
90 # actually delete branch and return to the main screen....
91 deletebranch($branchcode);
92 $template->param(else => 1);
93 default("The branch with code $branchcode has been deleted.");
96 # if no operation has been set...
102 ######################################################################################################
104 # html output functions....
109 $template->param(message => $message);
110 $template->param(action => $script_name);
118 $template->param(head => $head);
122 # prepares the edit form...
123 my ($branchcode) = @_;
126 $data = getbranchinfo($branchcode);
128 $template->param(branchcode => $data->{'branchcode'});
129 $template->param(branchname => $data->{'branchname'});
130 $template->param(branchaddress1 => $data->{'branchaddress1'});
131 $template->param(branchaddress2 => $data->{'branchaddress2'});
132 $template->param(branchaddress3 => $data->{'branchaddress3'});
133 $template->param(branchphone => $data->{'branchphone'});
134 $template->param(branchfax => $data->{'branchfax'});
135 $template->param(branchemail => $data->{'branchemail'});
137 # make the checkboxs.....
138 my $catinfo = getcategoryinfo();
140 foreach my $cat (@$catinfo) {
142 my $tmp = $cat->{'categorycode'};
143 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
144 $checked = "CHECKED";
146 $template->param(categoryname => $cat->{'categoryname'});
147 $template->param(categorycode => $cat->{'categorycode'});
148 $template->param(codedescription => $checked>$cat->{'codedescription'});
154 # message to print if the
155 my ($branchcode) = @_;
159 sub branchinfotable {
160 # makes the html for a table of branch info from reference to an array of hashs.
162 my ($branchcode) = @_;
165 $branchinfo = getbranchinfo($branchcode);
167 $branchinfo = getbranchinfo();
170 foreach my $branch (@$branchinfo) {
171 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
173 $address .= $branch->{'branchaddress1'} if ($branch->{'branchaddress1'});
174 $address .= '<br>'.$branch->{'branchaddress2'} if ($branch->{'branchaddress2'});
175 $address .= '<br>'.$branch->{'branchaddress3'} if ($branch->{'branchaddress3'});
176 $address .= '<br>ph: '.$branch->{'branchphone'} if ($branch->{'branchphone'});
177 $address .= '<br>fax: '.$branch->{'branchfax'} if ($branch->{'branchfax'});
178 $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
179 $address = '(nothing entered)' unless ($address);
181 foreach my $cat (@{$branch->{'categories'}}) {
182 my ($catinfo) = @{getcategoryinfo($cat)};
183 $categories .= $catinfo->{'categoryname'}."<br>";
185 $categories = '(no categories set)' unless ($categories);
186 $template->param(color => $color);
187 $template->param(branch_name => $branch->{'branchname'});
188 $template->param(adress => $address);
189 $template->param(categories => $categories);
190 $template->param(branch_code => $branch->{'branchcode'});
191 $template->param(value => $branch->{'branchcode'});
196 sub branchcategoriestable {
197 #Needs to be implemented...
199 my $categoryinfo = getcategoryinfo();
201 foreach my $cat (@$categoryinfo) {
202 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
203 $template->param(color => $color);
204 $template->param(categoryname => $cat->{'categoryname'});
205 $template->param(categorycode => $cat->{'categorycode'});
206 $template->param(codedescription => $cat->{'codedescription'});
210 ######################################################################################################
212 # Database functions....
215 # returns a reference to an array of hashes containing branches,
217 my ($branchcode) = @_;
218 my $dbh = C4::Context->dbh;
221 my $bc = $dbh->quote($branchcode);
222 $query = "Select * from branches where branchcode = $bc";
224 else {$query = "Select * from branches";}
225 my $sth = $dbh->prepare($query);
228 while (my $data = $sth->fetchrow_hashref) {
229 my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
230 $query = "select categorycode from branchrelations where branchcode = $brc";
231 my $nsth = $dbh->prepare($query);
234 while (my ($cat) = $nsth->fetchrow_array) {
238 $data->{'categories'} = \@cats;
239 push(@results, $data);
245 sub getcategoryinfo {
246 # returns a reference to an array of hashes containing branches,
248 my $dbh = C4::Context->dbh;
251 my $cc = $dbh->quote($catcode);
252 $query = "select * from branchcategories where categorycode = $cc";
254 $query = "Select * from branchcategories";
256 my $sth = $dbh->prepare($query);
259 while (my $data = $sth->fetchrow_hashref) {
260 push(@results, $data);
267 # sets the data from the editbranch form, and writes to the database...
269 my $dbh = C4::Context->dbh;
270 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
272 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
273 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
274 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
275 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
276 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
277 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
278 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
279 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
280 my $sth=$dbh->prepare($query);
283 # sort out the categories....
285 my $cats = getcategoryinfo();
286 foreach my $cat (@$cats) {
287 my $code = $cat->{'categorycode'};
288 if ($data->{$code}) {
289 push(@checkedcats, $code);
292 my $branchcode = $data->{'branchcode'};
293 my $branch = getbranchinfo($branchcode);
294 $branch = $branch->[0];
295 my $branchcats = $branch->{'categories'};
298 foreach my $bcat (@$branchcats) {
299 unless (grep {/^$bcat$/} @checkedcats) {
300 push(@removecats, $bcat);
303 foreach my $ccat (@checkedcats){
304 unless (grep {/^$ccat$/} @$branchcats) {
305 push(@addcats, $ccat);
308 # FIXME - There's already a $dbh in this scope.
309 my $dbh = C4::Context->dbh;
310 foreach my $cat (@addcats) {
311 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
312 my $sth = $dbh->prepare($query);
316 foreach my $cat (@removecats) {
317 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
318 my $sth = $dbh->prepare($query);
326 my ($branchcode) = @_;
327 my $query = "delete from branches where branchcode = '$branchcode'";
328 my $dbh = C4::Context->dbh;
329 my $sth=$dbh->prepare($query);
334 sub checkdatabasefor {
335 # check to see if the branchcode is being used in the database somewhere....
336 my ($branchcode) = @_;
337 my $dbh = C4::Context->dbh;
338 my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
340 my ($total) = $sth->fetchrow_array;
344 $message = "Branch cannot be deleted because there are $total items using that branch.";
349 print "Content-Type: text/html\n\n", $template->output;