2 # NOTE: Use standard 8-space tabs for this file (indents are 4 spaces)
4 #require '/u/acli/lib/cvs.pl';#DEBUG
5 open(DEBUG,'>/tmp/koha.debug');
7 # FIXME: individual fields in branch address need to be exported to templates,
8 # in order to fix bug 180; need to notify translators
9 # FIXME: looped html (e.g., list of checkboxes) need to be properly
10 # TMPL_LOOP'ized; doing this properly will fix bug 130; need to
12 # FIXME: need to implement the branch categories stuff
13 # FIXME: heading() need to be moved to templates, need to notify translators
14 # FIXME: there are too many TMPL_IF's; the proper way to do it is to have
15 # separate templates for each individual action; need to notify
18 # Finlay working on this file from 26-03-2002
19 # Reorganising this branches admin page.....
22 # Copyright 2000-2002 Katipo Communications
24 # This file is part of Koha.
26 # Koha is free software; you can redistribute it and/or modify it under the
27 # terms of the GNU General Public License as published by the Free Software
28 # Foundation; either version 2 of the License, or (at your option) any later
31 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
32 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
33 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
35 # You should have received a copy of the GNU General Public License along with
36 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
37 # Suite 330, Boston, MA 02111-1307 USA
44 use C4::Interface::CGI::Output;
48 my $linecolor1='#ffffcc';
49 my $linecolor2='white';
50 my $backgroundimage="/images/background-mem.gif";
51 my $script_name="/cgi-bin/koha/admin/branches.pl";
55 #######################################################################################
59 my $branchcode=$input->param('branchcode');
60 my $op = $input->param('op');
62 my ($template, $borrowernumber, $cookie)
63 = get_template_and_user({template_name => "parameters/branches.tmpl",
67 flagsrequired => {parameters => 1},
71 $template->param(script_name => $script_name,
72 $op => 1); # we show only the TMPL_VAR names $op
74 $template->param(script_name => $script_name,
75 else => 1); # we show only the TMPL_VAR names $op
77 $template->param(action => $script_name);
80 # If the user has pressed the "add new branch" button.
81 heading("Branches: Add Branch");
84 } elsif ($op eq 'edit') {
85 # if the user has pressed the "edit branch settings" button.
86 heading("Branches: Edit Branch");
87 $template->param(add => 1);
88 editbranchform($branchcode);
90 } elsif ($op eq 'add_validate') {
91 # confirm settings change...
92 my $params = $input->Vars;
93 unless ($params->{'branchcode'} && $params->{'branchname'}) {
94 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
96 setbranchinfo($params);
97 $template->param(else => 1);
98 default ("Branch record changed for branch: $params->{'branchname'}");
101 } elsif ($op eq 'delete') {
102 # if the user has pressed the "delete branch" button.
103 my $message = checkdatabasefor($branchcode);
105 $template->param(else => 1);
108 deleteconfirm($branchcode);
109 $template->param(delete_confirm => 1);
110 $template->param(branchcode => $branchcode);
113 } elsif ($op eq 'delete_confirmed') {
114 # actually delete branch and return to the main screen....
115 deletebranch($branchcode);
116 $template->param(else => 1);
117 default("The branch with code $branchcode has been deleted.");
119 } elsif ($op eq 'add_cat') {
120 # If the user has pressed the "add new category" button.
121 heading("Branches: Add Branch");
125 # if no operation has been set...
131 ######################################################################################################
133 # html output functions....
138 $template->param(message => $message);
139 $template->param(action => $script_name);
145 # FIXME: this function should not exist; otherwise headings are untranslatable
148 $template->param(head => $head);
152 # prepares the edit form...
153 my ($branchcode) = @_;
156 $data = getbranchinfo($branchcode);
158 $template->param(branchcode => $data->{'branchcode'});
159 $template->param(branchname => $data->{'branchname'});
160 $template->param(branchaddress1 => $data->{'branchaddress1'});
161 $template->param(branchaddress2 => $data->{'branchaddress2'});
162 $template->param(branchaddress3 => $data->{'branchaddress3'});
163 $template->param(branchphone => $data->{'branchphone'});
164 $template->param(branchfax => $data->{'branchfax'});
165 $template->param(branchemail => $data->{'branchemail'});
167 # make the checkboxs.....
168 # FIXME: this way of doing it is wrong (bug 130)
169 my $catinfo = getcategoryinfo();
171 print DEBUG "catinfo=".cvs($catinfo)."\n";
172 foreach my $cat (@$catinfo) {
174 my $tmp = $cat->{'categorycode'};
175 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
176 $checked = "CHECKED";
178 $template->param(categoryname => $cat->{'categoryname'});
179 $template->param(categorycode => $cat->{'categorycode'});
180 $template->param(codedescription => $checked>$cat->{'codedescription'});
186 # message to print if the
187 my ($branchcode) = @_;
191 sub branchinfotable {
192 # makes the html for a table of branch info from reference to an array of hashs.
194 my ($branchcode) = @_;
197 $branchinfo = getbranchinfo($branchcode);
199 $branchinfo = getbranchinfo();
203 foreach my $branch (@$branchinfo) {
204 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
206 $address .= $branch->{'branchaddress1'} if ($branch->{'branchaddress1'});
207 $address .= '<br>'.$branch->{'branchaddress2'} if ($branch->{'branchaddress2'});
208 $address .= '<br>'.$branch->{'branchaddress3'} if ($branch->{'branchaddress3'});
209 $address .= '<br>ph: '.$branch->{'branchphone'} if ($branch->{'branchphone'});
210 $address .= '<br>fax: '.$branch->{'branchfax'} if ($branch->{'branchfax'});
211 $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
212 $address = '(nothing entered)' unless ($address);
214 foreach my $cat (@{$branch->{'categories'}}) {
215 my ($catinfo) = @{getcategoryinfo($cat)};
216 $categories .= $catinfo->{'categoryname'}."<br>";
218 $categories = '(no categories set)' unless ($categories);
220 my @branch_name = ();
221 my @branch_code = ();
226 push(@colors,$color);
227 push(@branch_name,$branch->{'branchname'});
228 push(@branch_code,$branch->{'branchcode'});
229 push(@address,$address);
230 push(@categories,$categories);
231 push(@value,$branch->{'branchcode'});
232 push(@action,"/cgi-bin/koha/admin/branches.pl");
233 while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
235 $row_data{color} = shift @colors;
236 $row_data{branch_name} = shift @branch_name;
237 $row_data{branch_code} = shift @branch_code;
238 $row_data{address} = shift @address;
239 $row_data{categories} = shift @categories;
240 $row_data{value} = shift @value;
241 $row_data{action} = shift @action;
242 push(@loop_data, \%row_data);
246 $template->param(branches => \@loop_data);
250 # FIXME logic seems wrong
251 sub branchcategoriestable {
252 #Needs to be implemented...
254 my $categoryinfo = getcategoryinfo();
256 foreach my $cat (@$categoryinfo) {
257 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
258 $template->param(color => $color);
259 $template->param(categoryname => $cat->{'categoryname'});
260 $template->param(categorycode => $cat->{'categorycode'});
261 $template->param(codedescription => $cat->{'codedescription'});
265 ######################################################################################################
267 # Database functions....
270 # returns a reference to an array of hashes containing branches,
272 my ($branchcode) = @_;
273 my $dbh = C4::Context->dbh;
276 my $bc = $dbh->quote($branchcode);
277 $query = "Select * from branches where branchcode = $bc";
279 $query = "Select * from branches";
281 my $sth = $dbh->prepare($query);
284 while (my $data = $sth->fetchrow_hashref) {
285 my $tmp = $data->{'branchcode'}; my $brc = $dbh->quote($tmp);
286 $query = "select categorycode from branchrelations where branchcode = $brc";
287 my $nsth = $dbh->prepare($query);
290 while (my ($cat) = $nsth->fetchrow_array) {
294 $data->{'categories'} = \@cats;
295 push(@results, $data);
301 # FIXME This doesn't belong here; it should be moved into a module
302 sub getcategoryinfo {
303 # returns a reference to an array of hashes containing branches,
305 my $dbh = C4::Context->dbh;
307 print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
309 my $cc = $dbh->quote($catcode);
310 $query = "select * from branchcategories where categorycode = $cc";
312 $query = "Select * from branchcategories";
314 print DEBUG "getcategoryinfo: query=".cvs($query)."\n";
315 my $sth = $dbh->prepare($query);
318 while (my $data = $sth->fetchrow_hashref) {
319 push(@results, $data);
322 print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
327 # sets the data from the editbranch form, and writes to the database...
329 my $dbh = C4::Context->dbh;
330 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
332 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
333 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
334 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
335 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
336 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
337 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
338 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
339 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
340 my $sth=$dbh->prepare($query);
343 # sort out the categories....
345 my $cats = getcategoryinfo();
346 foreach my $cat (@$cats) {
347 my $code = $cat->{'categorycode'};
348 if ($data->{$code}) {
349 push(@checkedcats, $code);
352 my $branchcode = $data->{'branchcode'};
353 my $branch = getbranchinfo($branchcode);
354 $branch = $branch->[0];
355 my $branchcats = $branch->{'categories'};
358 foreach my $bcat (@$branchcats) {
359 unless (grep {/^$bcat$/} @checkedcats) {
360 push(@removecats, $bcat);
363 foreach my $ccat (@checkedcats){
364 unless (grep {/^$ccat$/} @$branchcats) {
365 push(@addcats, $ccat);
368 # FIXME - There's already a $dbh in this scope.
369 my $dbh = C4::Context->dbh;
370 foreach my $cat (@addcats) {
371 my $query = "insert into branchrelations (branchcode, categorycode) values('$branchcode', '$cat')";
372 my $sth = $dbh->prepare($query);
376 foreach my $cat (@removecats) {
377 my $query = "delete from branchrelations where branchcode='$branchcode' and categorycode='$cat'";
378 my $sth = $dbh->prepare($query);
386 my ($branchcode) = @_;
387 my $query = "delete from branches where branchcode = '$branchcode'";
388 my $dbh = C4::Context->dbh;
389 my $sth=$dbh->prepare($query);
394 sub checkdatabasefor {
395 # check to see if the branchcode is being used in the database somewhere....
396 my ($branchcode) = @_;
397 my $dbh = C4::Context->dbh;
398 my $sth=$dbh->prepare("select count(*) from items where holdingbranch='$branchcode' or homebranch='$branchcode'");
400 my ($total) = $sth->fetchrow_array;
404 $message = "Branch cannot be deleted because there are $total items using that branch.";
409 output_html_with_http_headers $input, $cookie, $template->output;