From 922ccaca8bac4ffa84c69974d1c8cb4f1550db40 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 16 Apr 2021 16:59:44 +0000
Subject: [PATCH] Bug 24154: Get rules rather than values to determine if set
 or not

Template plugin CirculationRules.Search returns "" if a rule doesn't exist or if
the rule exists but is set to ""

To get more info, we add a 'want_rule' parameter and use that to test if defined but
blank

To test:
1 - In circulation rules set the 'Default checkout, hold and return policy' without setting values
2 - Unset it
3 - Notice there is no visual difference
4 - Apply patch
5 - Reload the page
6 - note there is no 'Unset button' and first column says 'Not set'
7 - Save the rule with no values
8 - Now 'Defaults' is displayed and the unset button exists
---
 Koha/Template/Plugin/CirculationRules.pm           |  4 +-
 .../prog/en/modules/admin/smart-rules.tt           | 57 +++++++++++++---------
 2 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/Koha/Template/Plugin/CirculationRules.pm b/Koha/Template/Plugin/CirculationRules.pm
index e82dce01ed..0e39e908bc 100644
--- a/Koha/Template/Plugin/CirculationRules.pm
+++ b/Koha/Template/Plugin/CirculationRules.pm
@@ -43,7 +43,7 @@ sub Get {
 }
 
 sub Search {
-    my ( $self, $branchcode, $categorycode, $itemtype, $rule_name ) = @_;
+    my ( $self, $branchcode, $categorycode, $itemtype, $rule_name, $want_rule) = @_;
 
     $branchcode   = undef if $branchcode eq q{}   or $branchcode eq q{*};
     $categorycode = undef if $categorycode eq q{} or $categorycode eq q{*};
@@ -58,8 +58,8 @@ sub Search {
         }
     )->next;
 
+    return $rule if $want_rule;
     return $rule->rule_value if $rule;
 }
 
-
 1;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
index 79a87ae164..647a60c5d1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
@@ -543,28 +543,39 @@
                     <th>Return policy</th>
                     <th class="noExport">Actions</th>
                 </tr>
+                [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxissueqty', 1, 1 ) %]
+                [% SET patron_maxonsiteissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxonsiteissueqty', 1, 1 ) %]
+                [% SET rule_value = CirculationRules.Search( current_branch, undef , undef, 'max_holds', 1, 1 ) %]
+                [% SET holdallowed = CirculationRules.Search( current_branch, undef, undef, 'holdallowed', 1 ) %]
+                [% SET hold_fulfillment_policy = CirculationRules.Search( current_branch, undef, undef, 'hold_fulfillment_policy', 1 ) %]
+                [% SET returnbranch = CirculationRules.Search( current_branch, undef, undef, 'returnbranch', 1 ) %]
+                [% SET dcharp = ( patron_maxissueqty || patron_maxonsiteissueqty || rule_value || holdallowed || hold_fulfillment_policy || returnbranch ) %] 
                 <tr>
-                    <td><em>Defaults</em></td>
                     <td>
-                        [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxissueqty' ) %]
-                        <input type="text" name="patron_maxissueqty" size="3" value="[% patron_maxissueqty | html %]"/>
+                        [% IF ( dcharp ) %]
+                            <em>
+                            Defaults
+                            </em>
+                        [% ELSE %]
+                            Not set
+                        [% END %]
+                    </td>
+                    <td>
+                        <input type="text" name="patron_maxissueqty" size="9" value="[% patron_maxissueqty.rule_value | html %]" placeholder="Unlimited"/>
                     </td>
                     <td>
-                        [% SET patron_maxonsiteissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxonsiteissueqty' ) %]
-                        <input type="text" name="patron_maxonsiteissueqty" size="3" value="[% patron_maxonsiteissueqty | html %]"/>
+                        <input type="text" name="patron_maxonsiteissueqty" size="9" value="[% patron_maxonsiteissueqty.rule_value | html %]" placeholder="Unlimited"/>
                     </td>
                     <td>
-                        [% SET rule_value = CirculationRules.Search( current_branch, undef , undef, 'max_holds' ) %]
-                        <input name="max_holds" size="3" value="[% rule_value | html %]" />
+                        <input name="max_holds" size="9" value="[% rule_value.rule_value | html %]" placeholder="Unlimited"/>
                     </td>
                     <td>
                         <select name="holdallowed">
-                            [% SET holdallowed = CirculationRules.Search( current_branch, undef, undef, 'holdallowed' ) %]
                             <option value="">
                                 Not set
                             </option>
 
-                            [% IF holdallowed == 'from_any_library' %]
+                            [% IF holdallowed.rule_value == 'from_any_library' %]
                                 <option value="from_any_library" selected="selected">
                             [% ELSE %]
                                 <option value="from_any_library">
@@ -572,7 +583,7 @@
                                 From any library
                             </option>
 
-                            [% IF holdallowed == 'from_local_hold_group' %]
+                            [% IF holdallowed.rule_value == 'from_local_hold_group' %]
                             <option value="from_local_hold_group" selected="selected">
                             [% ELSE %]
                             <option value="from_local_hold_group">
@@ -580,7 +591,7 @@
                                 From local hold group
                             </option>
 
-                            [% IF holdallowed == 'from_home_library' %]
+                            [% IF holdallowed.rule_value == 'from_home_library' %]
                                 <option value="from_home_library" selected="selected">
                             [% ELSE %]
                                 <option value="from_home_library">
@@ -588,7 +599,7 @@
                                 From home library
                             </option>
 
-                            [% IF holdallowed == 'not_allowed' %]
+                            [% IF holdallowed.rule_value == 'not_allowed' %]
                                 <option value="not_allowed" selected="selected">
                             [% ELSE %]
                                 <option value="not_allowed">
@@ -599,13 +610,12 @@
                     </td>
                     <td>
                         <select name="hold_fulfillment_policy">
-                            [% SET hold_fulfillment_policy = CirculationRules.Search( current_branch, undef, undef, 'hold_fulfillment_policy' ) %]
 
                             <option value="">
                                 Not set
                             </option>
 
-                            [% IF hold_fulfillment_policy == 'any' %]
+                            [% IF hold_fulfillment_policy.rule_value == 'any' %]
                                 <option value="any" selected="selected">
                                     any library
                                 </option>
@@ -615,7 +625,7 @@
                                 </option>
                             [% END %]
 
-                            [% IF hold_fulfillment_policy == 'holdgroup' %]
+                            [% IF hold_fulfillment_policy.rule_value == 'holdgroup' %]
                                 <option value="holdgroup" selected="selected">
                                     item's hold group
                                 </option>
@@ -625,7 +635,7 @@
                                 </option>
                             [% END %]
 
-                            [% IF hold_fulfillment_policy == 'patrongroup' %]
+                            [% IF hold_fulfillment_policy.rule_value == 'patrongroup' %]
                                 <option value="patrongroup" selected="selected">
                                     patron's hold group
                                 </option>
@@ -635,7 +645,7 @@
                                 </option>
                             [% END %]
 
-                            [% IF hold_fulfillment_policy == 'homebranch' %]
+                            [% IF hold_fulfillment_policy.rule_value == 'homebranch' %]
                                 <option value="homebranch" selected="selected">
                                     item's home library
                                 </option>
@@ -645,7 +655,7 @@
                                 </option>
                             [% END %]
 
-                            [% IF hold_fulfillment_policy == 'holdingbranch' %]
+                            [% IF hold_fulfillment_policy.rule_value == 'holdingbranch' %]
                                 <option value="holdingbranch" selected="selected">
                                     item's holding library
                                 </option>
@@ -658,27 +668,26 @@
                     </td>
                     <td>
                         <select name="returnbranch">
-                            [% SET returnbranch = CirculationRules.Search( current_branch, undef, undef, 'returnbranch' ) %]
 
                             <option value="">
                                 Not set
                             </option>
 
-                            [% IF returnbranch == 'homebranch' %]
+                            [% IF returnbranch.rule_value == 'homebranch' %]
                             <option value="homebranch" selected="selected">
                             [% ELSE %]
                             <option value="homebranch">
                             [% END %]
                                 Item returns home
                             </option>
-                            [% IF returnbranch == 'holdingbranch' %]
+                            [% IF returnbranch.rule_value == 'holdingbranch' %]
                             <option value="holdingbranch" selected="selected">
                             [% ELSE %]
                             <option value="holdingbranch">
                             [% END %]
                                 Item returns to issuing library
                             </option>
-                            [% IF returnbranch == 'noreturn' %]
+                            [% IF returnbranch.rule_value == 'noreturn' %]
                             <option value="noreturn" selected="selected">
                             [% ELSE %]
                             <option value="noreturn">
@@ -689,7 +698,9 @@
                     </td>
                     <td class="actions">
                         <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-save"></i> Save</button>
-                        <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=*&amp;branch=[% current_branch | html %]" id="unset"><i class="fa fa-undo"></i> Unset</a>
+                        [% IF (dcharp) %]
+                            <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=*&amp;branch=[% current_branch | html %]" id="unset"><i class="fa fa-undo"></i> Unset</a>
+                        [% END %]
                     </td>
                 </tr>
             </table>
-- 
2.11.0