Bug 15520: Add permission to restrict circ rules editing to own library
[koha.git] / admin / smart-rules.pl
index eb4ef7b..74a0674 100755 (executable)
@@ -32,7 +32,10 @@ use Koha::Logger;
 use Koha::RefundLostItemFeeRule;
 use Koha::RefundLostItemFeeRules;
 use Koha::Libraries;
+use Koha::CirculationRules;
 use Koha::Patron::Categories;
+use Koha::Caches;
+use Koha::Patrons;
 
 my $input = CGI->new;
 my $dbh = C4::Context->dbh;
@@ -59,11 +62,20 @@ unless ( $branch ) {
         $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
     }
 }
+
+my $uid = Koha::Patrons->find( $loggedinuser )->userid;
+my $restricted_to_own_library = $uid && haspermission( $uid, { parameters => 'manage_circ_rules_restricted' }, { no_inherit => 1 } );
+$template->param( restricted_to_own_library => $restricted_to_own_library );
+$branch = C4::Context::mybranch() if $restricted_to_own_library;
+
 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
 
 my $op = $input->param('op') || q{};
 my $language = C4::Languages::getlanguage();
 
+my $cache = Koha::Caches->get_instance;
+$cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
+
 if ($op eq 'delete') {
     my $itemtype     = $input->param('itemtype');
     my $categorycode = $input->param('categorycode');
@@ -93,6 +105,15 @@ elsif ($op eq 'delete-branch-cat') {
                                         AND categorycode = ?");
         $sth_delete->execute($branch, $categorycode);
     }
+    Koha::CirculationRules->set_rule(
+        {
+            branchcode   => $branch,
+            categorycode => $categorycode,
+            itemtype     => undef,
+            rule_name    => 'max_holds',
+            rule_value   => undef,
+        }
+    );
 }
 elsif ($op eq 'delete-branch-item') {
     my $itemtype  = $input->param('itemtype');
@@ -210,12 +231,15 @@ elsif ($op eq "set-branch-defaults") {
     my $holdallowed   = $input->param('holdallowed');
     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
     my $returnbranch  = $input->param('returnbranch');
+    my $max_holds = $input->param('max_holds');
     $maxissueqty =~ s/\s//g;
     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
     $maxonsiteissueqty =~ s/\s//g;
     $maxonsiteissueqty = undef if $maxonsiteissueqty !~ /^\d+/;
     $holdallowed =~ s/\s//g;
     $holdallowed = undef if $holdallowed !~ /^\d+/;
+    $max_holds =~ s/\s//g;
+    $max_holds = '' if $max_holds !~ /^\d+/;
 
     if ($branch eq "*") {
         my $sth_search = $dbh->prepare("SELECT count(*) AS total
@@ -251,6 +275,15 @@ elsif ($op eq "set-branch-defaults") {
             $sth_insert->execute($branch, $maxissueqty, $maxonsiteissueqty, $holdallowed, $hold_fulfillment_policy, $returnbranch);
         }
     }
+    Koha::CirculationRules->set_rule(
+        {
+            branchcode   => $branch,
+            categorycode => undef,
+            itemtype     => undef,
+            rule_name    => 'max_holds',
+            rule_value   => $max_holds,
+        }
+    );
 }
 elsif ($op eq "add-branch-cat") {
     my $categorycode  = $input->param('categorycode');
@@ -266,50 +299,69 @@ elsif ($op eq "add-branch-cat") {
 
     if ($branch eq "*") {
         if ($categorycode eq "*") {
+            #FIXME This block is will probably be never used
             my $sth_search = $dbh->prepare("SELECT count(*) AS total
                                             FROM default_circ_rules");
             my $sth_insert = $dbh->prepare(q|
                 INSERT INTO default_circ_rules
-                    (maxissueqty, maxonsiteissueqty, max_holds)
-                    VALUES (?, ?, ?)
+                    (maxissueqty, maxonsiteissueqty)
+                    VALUES (?, ?)
             |);
             my $sth_update = $dbh->prepare(q|
                 UPDATE default_circ_rules
                 SET maxissueqty = ?,
                     maxonsiteissueqty = ?,
-                    max_holds = ?
             |);
 
             $sth_search->execute();
             my $res = $sth_search->fetchrow_hashref();
             if ($res->{total}) {
-                $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $max_holds );
+                $sth_update->execute( $maxissueqty, $maxonsiteissueqty );
             } else {
-                $sth_insert->execute( $maxissueqty, $maxonsiteissueqty, $max_holds );
+                $sth_insert->execute( $maxissueqty, $maxonsiteissueqty );
             }
+
+            Koha::CirculationRules->set_rule(
+                {
+                    branchcode   => undef,
+                    categorycode => undef,
+                    itemtype     => undef,
+                    rule_name    => 'max_holds',
+                    rule_value   => $max_holds,
+                }
+            );
         } else {
             my $sth_search = $dbh->prepare("SELECT count(*) AS total
                                             FROM default_borrower_circ_rules
                                             WHERE categorycode = ?");
             my $sth_insert = $dbh->prepare(q|
                 INSERT INTO default_borrower_circ_rules
-                    (categorycode, maxissueqty, maxonsiteissueqty, max_holds)
-                    VALUES (?, ?, ?, ?)
+                    (categorycode, maxissueqty, maxonsiteissueqty)
+                    VALUES ( ?, ?, ?)
             |);
             my $sth_update = $dbh->prepare(q|
                 UPDATE default_borrower_circ_rules
                 SET maxissueqty = ?,
                     maxonsiteissueqty = ?,
-                    max_holds = ?
                 WHERE categorycode = ?
             |);
             $sth_search->execute($categorycode);
             my $res = $sth_search->fetchrow_hashref();
             if ($res->{total}) {
-                $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $max_holds, $categorycode );
+                $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $categorycode );
             } else {
-                $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty, $max_holds );
+                $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty );
             }
+
+            Koha::CirculationRules->set_rule(
+                {
+                    branchcode   => undef,
+                    categorycode => $categorycode,
+                    itemtype     => undef,
+                    rule_name    => 'max_holds',
+                    rule_value   => $max_holds,
+                }
+            );
         }
     } elsif ($categorycode eq "*") {
         my $sth_search = $dbh->prepare("SELECT count(*) AS total
@@ -340,14 +392,13 @@ elsif ($op eq "add-branch-cat") {
                                         AND   categorycode = ?");
         my $sth_insert = $dbh->prepare(q|
             INSERT INTO branch_borrower_circ_rules
-            (branchcode, categorycode, maxissueqty, maxonsiteissueqty, max_holds)
-            VALUES (?, ?, ?, ?, ?)
+            (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
+            VALUES (?, ?, ?, ?)
         |);
         my $sth_update = $dbh->prepare(q|
             UPDATE branch_borrower_circ_rules
             SET maxissueqty = ?,
                 maxonsiteissueqty = ?
-                max_holds = ?
             WHERE branchcode = ?
             AND categorycode = ?
         |);
@@ -355,10 +406,20 @@ elsif ($op eq "add-branch-cat") {
         $sth_search->execute($branch, $categorycode);
         my $res = $sth_search->fetchrow_hashref();
         if ($res->{total}) {
-            $sth_update->execute($maxissueqty, $maxonsiteissueqty, $max_holds, $branch, $categorycode);
+            $sth_update->execute($maxissueqty, $maxonsiteissueqty, $branch, $categorycode);
         } else {
-            $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty, $max_holds);
+            $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
         }
+
+        Koha::CirculationRules->set_rule(
+            {
+                branchcode   => $branch,
+                categorycode => $categorycode,
+                itemtype     => undef,
+                rule_name    => 'max_holds',
+                rule_value   => $max_holds,
+            }
+        );
     }
 }
 elsif ($op eq "add-branch-item") {