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
21 # This script clones issuing rules from a library to another
23 # - frombranch : the branch we want to clone issuing rules from
24 # - tobranch : the branch we want to clone issuing rules to
26 # The script can be called with one of the parameters, both or none
35 use C4
::Branch
; # GetBranches
38 my $dbh = C4
::Context
->dbh;
40 my ($template, $loggedinuser, $cookie)
41 = get_template_and_user
({template_name
=> "admin/clone-rules.tmpl",
45 flagsrequired
=> {parameters
=> 1},
49 my $frombranch = $input->param("frombranch");
50 my $tobranch = $input->param("tobranch");
51 my $branchloop = GetBranchesLoop
;
53 $template->param(frombranch
=> $frombranch) if ($frombranch);
54 $template->param(frombranchname
=> GetBranchName
($frombranch)) if ($frombranch);
55 $template->param(tobranch
=> $tobranch) if ($tobranch);
56 $template->param(tobranchname
=> GetBranchName
($tobranch)) if ($tobranch);
58 $template->param(branchloop
=> $branchloop);
60 if ($frombranch && $tobranch) {
64 # First, we create a temporary table with the rules we want to clone
65 my $query = "CREATE TEMPORARY TABLE tmpissuingrules ENGINE=memory SELECT * FROM issuingrules WHERE branchcode=?";
66 my $sth = $dbh->prepare($query);
67 my $res = $sth->execute($frombranch);
68 $error = 1 unless ($res);
71 # We modify these rules according to the new branchcode
72 $query = "UPDATE tmpissuingrules SET branchcode=? WHERE branchcode=?";
73 $sth = $dbh->prepare($query);
74 $res = $sth->execute($tobranch, $frombranch);
75 $error = 1 unless ($res);
79 # We delete the rules for the existing branchode
80 $query = "DELETE FROM issuingrules WHERE branchcode=?";
81 $sth = $dbh->prepare($query);
82 $res = $sth->execute($tobranch);
83 $error = 1 unless ($res);
88 # We insert the new rules from our temporary table
89 $query = "INSERT INTO issuingrules SELECT * FROM tmpissuingrules WHERE branchcode=?";
90 $sth = $dbh->prepare($query);
91 $res = $sth->execute($tobranch);
92 $error = 1 unless ($res);
95 # Finally, we delete our temporary table
96 $query = "DROP TABLE tmpissuingrules";
97 $sth = $dbh->prepare($query);
98 $res = $sth->execute();
100 $template->param(result
=> "1");
101 $template->param(error
=> $error);
106 output_html_with_http_headers
$input, $cookie, $template->output;