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
17 # FIXME: there are lots of error messages exported to the template; a lot
18 # of these should be converted into exported booleans / counters etc
19 # so that the error messages can be localized; need to notify translators
21 # Finlay working on this file from 26-03-2002
22 # Reorganising this branches admin page.....
25 # Copyright 2000-2002 Katipo Communications
27 # This file is part of Koha.
29 # Koha is free software; you can redistribute it and/or modify it under the
30 # terms of the GNU General Public License as published by the Free Software
31 # Foundation; either version 2 of the License, or (at your option) any later
34 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
35 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
36 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
38 # You should have received a copy of the GNU General Public License along with
39 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
40 # Suite 330, Boston, MA 02111-1307 USA
47 use C4::Interface::CGI::Output;
51 my $linecolor1='#ffffcc';
52 my $linecolor2='white';
53 my $backgroundimage="/images/background-mem.gif";
54 my $script_name="/cgi-bin/koha/admin/branches.pl";
58 #######################################################################################
62 my $branchcode=$input->param('branchcode');
63 my $op = $input->param('op');
65 my ($template, $borrowernumber, $cookie)
66 = get_template_and_user({template_name => "parameters/branches.tmpl",
70 flagsrequired => {parameters => 1},
74 $template->param(script_name => $script_name,
75 $op => 1); # we show only the TMPL_VAR names $op
77 $template->param(script_name => $script_name,
78 else => 1); # we show only the TMPL_VAR names $op
80 $template->param(action => $script_name);
83 # If the user has pressed the "add new branch" button.
84 heading("Branches: Add Branch");
87 } elsif ($op eq 'edit') {
88 # if the user has pressed the "edit branch settings" button.
89 heading("Branches: Edit Branch");
90 $template->param(add => 1);
91 editbranchform($branchcode);
93 } elsif ($op eq 'add_validate') {
94 # confirm settings change...
95 my $params = $input->Vars;
96 unless ($params->{'branchcode'} && $params->{'branchname'}) {
97 default ("Cannot change branch record: You must specify a Branchname and a Branchcode");
99 setbranchinfo($params);
100 $template->param(else => 1);
101 default ("Branch record changed for branch: $params->{'branchname'}");
104 } elsif ($op eq 'delete') {
105 # if the user has pressed the "delete branch" button.
106 my $message = checkdatabasefor($branchcode);
108 $template->param(else => 1);
111 deleteconfirm($branchcode);
112 $template->param(delete_confirm => 1);
113 $template->param(branchcode => $branchcode);
116 } elsif ($op eq 'delete_confirmed') {
117 # actually delete branch and return to the main screen....
118 deletebranch($branchcode);
119 $template->param(else => 1);
120 default("The branch with code $branchcode has been deleted.");
122 } elsif ($op eq 'add_cat') {
123 # If the user has pressed the "add new category" button.
124 heading("Branches: Add Branch");
128 # if no operation has been set...
134 ######################################################################################################
136 # html output functions....
141 $template->param(message => $message);
142 $template->param(action => $script_name);
148 # FIXME: this function should not exist; otherwise headings are untranslatable
151 $template->param(head => $head);
155 # prepares the edit form...
156 my ($branchcode) = @_;
159 $data = getbranchinfo($branchcode);
161 $template->param(branchcode => $data->{'branchcode'});
162 $template->param(branchname => $data->{'branchname'});
163 $template->param(branchaddress1 => $data->{'branchaddress1'});
164 $template->param(branchaddress2 => $data->{'branchaddress2'});
165 $template->param(branchaddress3 => $data->{'branchaddress3'});
166 $template->param(branchphone => $data->{'branchphone'});
167 $template->param(branchfax => $data->{'branchfax'});
168 $template->param(branchemail => $data->{'branchemail'});
171 # make the checkboxs.....
173 # We export a "categoryloop" array to the template, each element of which
174 # contains separate 'categoryname', 'categorycode', 'codedescription', and
175 # 'checked' fields. The $checked field is either '' or 'checked'
178 my $catinfo = getcategoryinfo();
180 # print DEBUG "catinfo=".cvs($catinfo)."\n";
181 my @categoryloop = ();
182 foreach my $cat (@$catinfo) {
184 my $tmp = quotemeta($cat->{'categorycode'});
185 if (grep {/^$tmp$/} @{$data->{'categories'}}) {
186 $checked = "CHECKED";
188 push @categoryloop, {
189 categoryname => $cat->{'categoryname'},
190 categorycode => $cat->{'categorycode'},
191 codedescription => $cat->{'codedescription'},
195 $template->param(categoryloop => \@categoryloop);
197 # {{{ Leave this here until bug 130 is completely resolved in the templates
198 for my $obsolete ('categoryname', 'categorycode', 'codedescription') {
199 $template->param($obsolete => 'Your template is out of date (bug 130)');
205 # message to print if the
206 my ($branchcode) = @_;
210 sub branchinfotable {
211 # makes the html for a table of branch info from reference to an array of hashs.
213 my ($branchcode) = @_;
216 $branchinfo = getbranchinfo($branchcode);
218 $branchinfo = getbranchinfo();
222 foreach my $branch (@$branchinfo) {
223 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
224 # FIXME. The $address should not be pre-composed (bug 180)
226 $address .= $branch->{'branchaddress1'} if ($branch->{'branchaddress1'});
227 $address .= '<br>'.$branch->{'branchaddress2'} if ($branch->{'branchaddress2'});
228 $address .= '<br>'.$branch->{'branchaddress3'} if ($branch->{'branchaddress3'});
229 $address .= '<br>ph: '.$branch->{'branchphone'} if ($branch->{'branchphone'});
230 $address .= '<br>fax: '.$branch->{'branchfax'} if ($branch->{'branchfax'});
231 $address .= '<br>email: '.$branch->{'branchemail'} if ($branch->{'branchemail'});
232 $address = '(nothing entered)' unless ($address);
234 foreach my $cat (@{$branch->{'categories'}}) {
235 my ($catinfo) = @{getcategoryinfo($cat)};
236 $categories .= $catinfo->{'categoryname'}."<br>";
238 $categories = '(no categories set)' unless ($categories);
240 my @branch_name = ();
241 my @branch_code = ();
246 push(@colors,$color);
247 push(@branch_name,$branch->{'branchname'});
248 push(@branch_code,$branch->{'branchcode'});
249 push(@address,$address);
250 push(@categories,$categories);
251 push(@value,$branch->{'branchcode'});
252 push(@action,"/cgi-bin/koha/admin/branches.pl");
253 while (@colors and @branch_name and @branch_code and @address and @categories and @value and @action) {
255 $row_data{color} = shift @colors;
256 $row_data{branch_name} = shift @branch_name;
257 $row_data{branch_code} = shift @branch_code;
258 $row_data{address} = shift @address;
259 $row_data{categories} = shift @categories;
260 $row_data{value} = shift @value;
261 $row_data{action} = shift @action;
262 push(@loop_data, \%row_data);
266 $template->param(branches => \@loop_data);
270 # FIXME logic seems wrong
271 sub branchcategoriestable {
272 #Needs to be implemented...
274 my $categoryinfo = getcategoryinfo();
276 foreach my $cat (@$categoryinfo) {
277 ($color eq $linecolor1) ? ($color=$linecolor2) : ($color=$linecolor1);
278 $template->param(color => $color);
279 $template->param(categoryname => $cat->{'categoryname'});
280 $template->param(categorycode => $cat->{'categorycode'});
281 $template->param(codedescription => $cat->{'codedescription'});
285 ######################################################################################################
287 # Database functions....
290 # returns a reference to an array of hashes containing branches,
292 my ($branchcode) = @_;
293 my $dbh = C4::Context->dbh;
294 my ($query, @query_args);
296 $query = "Select * from branches where branchcode = ?";
297 @query_args = ($branchcode);
299 $query = "Select * from branches";
301 my $sth = $dbh->prepare($query);
302 $sth->execute(@query_args);
304 while (my $data = $sth->fetchrow_hashref) {
305 $query = "select categorycode from branchrelations where branchcode = ?";
306 my $nsth = $dbh->prepare($query);
307 $nsth->execute($data->{'branchcode'});;
309 while (my ($cat) = $nsth->fetchrow_array) {
313 $data->{'categories'} = \@cats;
314 push(@results, $data);
320 # FIXME This doesn't belong here; it should be moved into a module
321 sub getcategoryinfo {
322 # returns a reference to an array of hashes containing branches,
324 my $dbh = C4::Context->dbh;
325 my ($query, @query_args);
326 # print DEBUG "getcategoryinfo: entry: catcode=".cvs($catcode)."\n";
328 $query = "select * from branchcategories where categorycode = ?";
329 @query_args = ($catcode);
331 $query = "Select * from branchcategories";
333 # print DEBUG "getcategoryinfo: query=".cvs($query)."\n";
334 my $sth = $dbh->prepare($query);
335 $sth->execute(@query_args);
337 while (my $data = $sth->fetchrow_hashref) {
338 push(@results, $data);
341 # print DEBUG "getcategoryinfo: exit: returning ".cvs(\@results)."\n";
345 # FIXME This doesn't belong here; it should be moved into a module
347 # sets the data from the editbranch form, and writes to the database...
349 my $dbh = C4::Context->dbh;
350 my $query = "replace branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchphone,branchfax,branchemail) values (";
352 $tmp = $data->{'branchcode'}; $query.= $dbh->quote($tmp).",";
353 $tmp = $data->{'branchname'}; $query.= $dbh->quote($tmp).",";
354 $tmp = $data->{'branchaddress1'}; $query.= $dbh->quote($tmp).",";
355 $tmp = $data->{'branchaddress2'}; $query.= $dbh->quote($tmp).",";
356 $tmp = $data->{'branchaddress3'}; $query.= $dbh->quote($tmp).",";
357 $tmp = $data->{'branchphone'}; $query.= $dbh->quote($tmp).",";
358 $tmp = $data->{'branchfax'}; $query.= $dbh->quote($tmp).",";
359 $tmp = $data->{'branchemail'}; $query.= $dbh->quote($tmp).")";
360 my $sth=$dbh->prepare($query);
363 # sort out the categories....
365 my $cats = getcategoryinfo();
366 foreach my $cat (@$cats) {
367 my $code = $cat->{'categorycode'};
368 if ($data->{$code}) {
369 push(@checkedcats, $code);
372 my $branchcode = $data->{'branchcode'};
373 my $branch = getbranchinfo($branchcode);
374 $branch = $branch->[0];
375 my $branchcats = $branch->{'categories'};
378 foreach my $bcat (@$branchcats) {
379 unless (grep {/^$bcat$/} @checkedcats) {
380 push(@removecats, $bcat);
383 foreach my $ccat (@checkedcats){
384 unless (grep {/^$ccat$/} @$branchcats) {
385 push(@addcats, $ccat);
388 # FIXME - There's already a $dbh in this scope.
389 my $dbh = C4::Context->dbh;
390 foreach my $cat (@addcats) {
391 my $query = "insert into branchrelations (branchcode, categorycode) values(?, ?)";
392 my $sth = $dbh->prepare($query);
393 $sth->execute($branchcode, $cat);
396 foreach my $cat (@removecats) {
397 my $query = "delete from branchrelations where branchcode=? and categorycode=?";
398 my $sth = $dbh->prepare($query);
399 $sth->execute($branchcode, $cat);
406 my ($branchcode) = @_;
407 my $query = "delete from branches where branchcode = ?";
408 my $dbh = C4::Context->dbh;
409 my $sth=$dbh->prepare($query);
410 $sth->execute($branchcode);
414 sub checkdatabasefor {
415 # check to see if the branchcode is being used in the database somewhere....
416 my ($branchcode) = @_;
417 my $dbh = C4::Context->dbh;
418 my $sth=$dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
419 $sth->execute($branchcode, $branchcode);
420 my ($total) = $sth->fetchrow_array;
424 # FIXME: need to be replaced by an exported boolean parameter
425 $message = "Branch cannot be deleted because there are $total items using that branch.";
430 output_html_with_http_headers $input, $cookie, $template->output;