3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
22 FIXME: individual fields in branch address need to be exported to templates,
23 in order to fix bug 180; need to notify translators
24 FIXME: looped html (e.g., list of checkboxes) need to be properly
25 TMPL_LOOP'ized; doing this properly will fix bug 130; need to
27 FIXME: need to implement the branch categories stuff
28 FIXME: there are too many TMPL_IF's; the proper way to do it is to have
29 separate templates for each individual action; need to notify
31 FIXME: there are lots of error messages exported to the template; a lot
32 of these should be converted into exported booleans / counters etc
33 so that the error messages can be localized; need to notify translators
35 Finlay working on this file from 26-03-2002
36 Reorganising this branches admin page.....
50 my $script_name = "/cgi-bin/koha/admin/branches.pl";
52 ################################################################################
55 my $branchcode = $input->param('branchcode');
56 my $branchname = $input->param('branchname');
57 my $categorycode = $input->param('categorycode');
58 my $op = $input->param('op') || '';
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62 template_name => "admin/branches.tmpl",
66 flagsrequired => { parameters => 1},
71 script_name => $script_name,
72 action => $script_name,
74 $template->param( ($op || 'else') => 1 );
78 # If the user has pressed the "add new branch" button.
79 $template->param( 'heading-branches-add-branch-p' => 1 );
80 editbranchform($branchcode,$template);
83 elsif ( $op eq 'edit' ) {
85 # if the user has pressed the "edit branch settings" button.
86 $template->param( 'heading-branches-add-branch-p' => 0,
88 editbranchform($branchcode,$template);
90 elsif ( $op eq 'add_validate' ) {
92 # confirm settings change...
93 my $params = $input->Vars;
94 unless ( $params->{'branchcode'} && $params->{'branchname'} ) {
95 $template->param( else => 1 );
96 default("MESSAGE1",$template);
99 my $error = ModBranch($params); # FIXME: causes warnings to log on duplicate branchcode
100 # if error saving, stay on edit and rise error
102 # copy input parameters back to form
103 # FIXME - doing this doesn't preserve any branch group selections, but good enough for now
104 $template->param(%$params); # FIXME: Allows user to set ANY TMPL_VAR to ANY value!!
105 $template->param(branch_name => $params->{branchname});
106 $template->param( 'heading-branches-add-branch-p' => 1, 'add' => 1, "ERROR$error" => 1 );
108 $template->param( else => 1);
109 default("MESSAGE2",$template);
113 elsif ( $op eq 'delete' ) {
114 # if the user has pressed the "delete branch" button.
116 # check to see if the branchcode is being used in the database somewhere....
117 my $dbh = C4::Context->dbh;
118 my $sth = $dbh->prepare("select count(*) from items where holdingbranch=? or homebranch=?");
119 $sth->execute( $branchcode, $branchcode );
120 my ($total) = $sth->fetchrow_array;
122 $template->param( else => 1 );
123 default("MESSAGE7", $template);
126 $template->param( delete_confirm => 1 );
127 $template->param( branchname => $branchname );
128 $template->param( branchcode => $branchcode );
131 elsif ( $op eq 'delete_confirmed' ) {
133 # actually delete branch and return to the main screen....
134 DelBranch($branchcode);
135 $template->param( else => 1 );
136 default("MESSAGE3",$template);
138 elsif ( $op eq 'editcategory' ) {
140 # If the user has pressed the "add new category" or "modify" buttons.
141 $template->param( 'heading-branches-edit-category-p' => 1 );
142 editcatform($categorycode,$template);
144 elsif ( $op eq 'addcategory_validate' ) {
146 $template->param( else => 1 );
147 # confirm settings change...
148 my $params = $input->Vars;
149 unless ( $params->{'categorycode'} && $params->{'categoryname'} ) {
150 default("MESSAGE4",$template);
153 ModBranchCategoryInfo($params);
154 default("MESSAGE5",$template);
157 elsif ( $op eq 'delete_category' ) {
159 # if the user has pressed the "delete branch" button.
160 my $message = "MESSAGE8" if CheckBranchCategorycode($categorycode);
162 $template->param( else => 1 );
163 default($message,$template);
166 $template->param( delete_category => 1 );
167 $template->param( categorycode => $categorycode );
170 elsif ( $op eq 'categorydelete_confirmed' ) {
172 # actually delete branch and return to the main screen....
173 DelBranchCategory($categorycode);
174 $template->param( else => 1 );
175 default("MESSAGE6",$template);
179 # if no operation has been set...
180 default("",$template);
183 ################################################################################
185 # html output functions....
188 my $message = shift || '';
189 my $innertemplate = shift or return;
190 $innertemplate->param($message => 1) if $message;
191 $innertemplate->param(
192 'heading-branches-p' => 1,
194 branchinfotable("",$innertemplate);
198 my ($branchcode,$innertemplate) = @_;
199 # initiate the scrolling-list to select the printers
200 my $printers = GetPrinters();
206 $data = GetBranchInfo($branchcode);
209 # get the old printer of the branch
210 $oldprinter = $data->{'branchprinter'} || '';
211 $innertemplate->param(
212 branchcode => $data->{'branchcode'},
213 branch_name => $data->{'branchname'},
214 branchaddress1 => $data->{'branchaddress1'},
215 branchaddress2 => $data->{'branchaddress2'},
216 branchaddress3 => $data->{'branchaddress3'},
217 branchphone => $data->{'branchphone'},
218 branchfax => $data->{'branchfax'},
219 branchemail => $data->{'branchemail'},
220 branchip => $data->{'branchip'}
224 foreach my $thisprinter ( keys %$printers ) {
226 value => $thisprinter,
227 selected => ( $oldprinter eq $printers->{$thisprinter} ),
228 branchprinter => $printers->{$thisprinter}->{'printqueue'},
232 $innertemplate->param( printerloop => \@printerloop );
233 # make the checkboxes.....
235 # We export a "categoryloop" array to the template, each element of which
236 # contains separate 'categoryname', 'categorycode', 'codedescription', and
237 # 'checked' fields. The $checked field is either '' or 'checked="checked"'
239 my $catinfo = GetBranchCategory();
240 my @categoryloop = ();
241 foreach my $cat (@$catinfo) {
243 my $tmp = quotemeta( $cat->{'categorycode'} );
244 if ( grep { /^$tmp$/ } @{ $data->{'categories'} } ) {
245 $checked = "checked=\"checked\"";
247 push @categoryloop, {
248 categoryname => $cat->{'categoryname'},
249 categorycode => $cat->{'categorycode'},
250 categorytype => $cat->{'categorytype'},
251 codedescription => $cat->{'codedescription'},
255 $innertemplate->param( categoryloop => \@categoryloop );
257 for my $obsolete ( 'categoryname', 'categorycode', 'codedescription' ) {
258 $innertemplate->param(
259 $obsolete => 'Your template is out of date (bug 130)' );
265 # prepares the edit form...
266 my ($categorycode,$innertemplate) = @_;
267 # warn "cat : $categorycode";
271 my $data = GetBranchCategory($categorycode);
273 $innertemplate->param(
274 categorycode => $data->{'categorycode'},
275 categoryname => $data->{'categoryname'},
276 codedescription => $data->{'codedescription'},
279 for my $ctype (GetCategoryTypes()) {
280 push @cats , { type => $ctype , selected => ($data->{'categorytype'} and $data->{'categorytype'} eq $ctype) };
282 $innertemplate->param(categorytype => \@cats);
285 sub branchinfotable {
287 # makes the html for a table of branch info from reference to an array of hashs.
289 my ($branchcode,$innertemplate) = @_;
290 my $branchinfo = $branchcode ? GetBranchInfo($branchcode) : GetBranchInfo();
292 foreach my $branch (@$branchinfo) {
294 # We export the following fields to the template. These are not
295 # pre-composed as a single "address" field because the template
296 # might (and should) escape what is exported here. (See bug 180)
298 # - branch_name (Note: not "branchname")
299 # - branch_code (Note: not "branchcode")
300 # - address (containing a static error message)
303 # - branchaddress3 | comprising the old "address" field
307 # - address-empty-p (1 if no address information, 0 otherwise)
308 # - categories (containing a static error message)
309 # - category_list (loop containing "categoryname")
310 # - no-categories-p (1 if no categories set, 0 otherwise)
315 # Handle address fields separately
316 my $address_empty_p = 1;
318 'branchaddress1', 'branchaddress2',
319 'branchaddress3', 'branchphone',
320 'branchfax', 'branchemail',
321 'branchip', 'branchprinter'
324 $row{$field} = $branch->{$field};
325 $address_empty_p = 0 if ( $branch->{$field} );
327 $row{'address-empty-p'} = $address_empty_p;
330 my $no_categories_p = 1;
332 foreach my $cat ( @{ $branch->{'categories'} } ) {
333 my ($catinfo) = @{ GetBranchCategory($cat) };
334 push @categories, { 'categoryname' => $catinfo->{'categoryname'} };
335 $no_categories_p = 0;
338 $row{'category_list'} = \@categories;
339 $row{'no-categories-p'} = $no_categories_p;
340 $row{'branch_name'} = $branch->{'branchname'};
341 $row{'branch_code'} = $branch->{'branchcode'};
342 $row{'value'} = $branch->{'branchcode'};
344 push @loop_data, \%row;
346 my @branchcategories = ();
347 for my $ctype ( GetCategoryTypes() ) {
348 my $catinfo = GetBranchCategories(undef,$ctype);
350 foreach my $cat (@$catinfo) {
352 categoryname => $cat->{'categoryname'},
353 categorycode => $cat->{'categorycode'},
354 codedescription => $cat->{'codedescription'},
355 categorytype => $cat->{'categorytype'},
358 push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
360 $innertemplate->param(
361 branches => \@loop_data,
362 branchcategories => \@branchcategories
367 output_html_with_http_headers $input, $cookie, $template->output;