From 3e561e8af6f5a59b86472850cd5ec0ffb4d3ecfa Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Tue, 7 Jan 2014 07:50:43 -0500
Subject: [PATCH] Bug 6254 [QA Followup 1] - can't set patron privacy by default

* Adds default privacy column to summary table
* Adds default privacy to delete category summary
* Adds "AFTER categorycode" to the database update
* Whitespace cleanup and formatting for affected code blocks
* Switch basic DBI queries to DBIx::Class to simplify code
* Adds reference to misc/cronjobs/batch_anonymise.pl to description
---
 admin/categorie.pl                                 |   66 +++++----------
 installer/data/mysql/updatedatabase.pl             |    2 +-
 .../prog/en/modules/admin/categorie.tt             |   89 ++++++++++++--------
 .../prog/en/modules/admin/patron-attr-types.tt     |    2 +-
 4 files changed, 77 insertions(+), 82 deletions(-)

diff --git a/admin/categorie.pl b/admin/categorie.pl
index abd73f8..5a7e5fd 100755
--- a/admin/categorie.pl
+++ b/admin/categorie.pl
@@ -45,6 +45,7 @@ use C4::Branch;
 use C4::Output;
 use C4::Dates;
 use C4::Form::MessagingPreferences;
+use Koha::Database;
 
 sub StringSearch  {
 	my ($searchstring,$type)=@_;
@@ -188,36 +189,14 @@ if ($op eq 'add_form') {
 ################## DELETE_CONFIRM ##################################
 # called by default form, used to confirm deletion of data in DB
 } elsif ($op eq 'delete_confirm') {
+    my $schema = Koha::Database->new()->schema();
 	$template->param(delete_confirm => 1);
 
-	my $dbh = C4::Context->dbh;
-	my $sth=$dbh->prepare("select count(*) as total from borrowers where categorycode=?");
-	$sth->execute($categorycode);
-	my $total = $sth->fetchrow_hashref;
-	$sth->finish;
-	$template->param(total => $total->{'total'});
-	
-	my $sth2=$dbh->prepare("select categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,enrolmentfee,issuelimit,reservefee,hidelostitems,overduenoticerequired,category_type from categories where categorycode=?");
-	$sth2->execute($categorycode);
-	my $data=$sth2->fetchrow_hashref;
-	$sth2->finish;
-	if ($total->{'total'} >0) {
-		$template->param(totalgtzero => 1);
-	}
-
-        $template->param(       description             => $data->{'description'},
-                                enrolmentperiod         => $data->{'enrolmentperiod'},
-                                enrolmentperioddate     => C4::Dates::format_date($data->{'enrolmentperioddate'}),
-                                upperagelimit           => $data->{'upperagelimit'},
-                                dateofbirthrequired     => $data->{'dateofbirthrequired'},
-                                enrolmentfee            =>  sprintf("%.2f",$data->{'enrolmentfee'}),
-                                overduenoticerequired   => $data->{'overduenoticerequired'},
-                                issuelimit              => $data->{'issuelimit'},
-                                reservefee              =>  sprintf("%.2f",$data->{'reservefee'}),
-                                hidelostitems           => $data->{'hidelostitems'},
-                                category_type           => $data->{'category_type'},
-                                );
-													# END $OP eq DELETE_CONFIRM
+    my $count = $schema->resultset('Borrower')->search( { categorycode => $categorycode } )->count();
+    my $category = $schema->resultset('Category')->find($categorycode);
+    $category->enrolmentperioddate( C4::Dates::format_date( $category->enrolmentperioddate() ) );
+    $template->param( category => $category, patrons_in_category => $count );
+# END $OP eq DELETE_CONFIRM
 ################## DELETE_CONFIRMED ##################################
 # called by delete_confirm, used to effectively confirm deletion of data in DB
 } elsif ($op eq 'delete_confirmed') {
@@ -243,21 +222,22 @@ if ($op eq 'add_form') {
         while ( my $branch = $sth->fetchrow_hashref ) {
             push @selected_branches, $branch;
         }
-		my %row = (
-		        categorycode            => $results->[$i]{'categorycode'},
-				description             => $results->[$i]{'description'},
-				enrolmentperiod         => $results->[$i]{'enrolmentperiod'},
-				enrolmentperioddate     => C4::Dates::format_date($results->[$i]{'enrolmentperioddate'}),
-				upperagelimit           => $results->[$i]{'upperagelimit'},
-				dateofbirthrequired     => $results->[$i]{'dateofbirthrequired'},
-				enrolmentfee            => sprintf("%.2f",$results->[$i]{'enrolmentfee'}),
-				overduenoticerequired   => $results->[$i]{'overduenoticerequired'},
-				issuelimit              => $results->[$i]{'issuelimit'},
-				reservefee              => sprintf("%.2f",$results->[$i]{'reservefee'}),
-                                hidelostitems           => $results->[$i]{'hidelostitems'},
-				category_type           => $results->[$i]{'category_type'},
-                "type_".$results->[$i]{'category_type'} => 1,
-                branches                => \@selected_branches,
+        my %row = (
+            branches              => \@selected_branches,
+            categorycode          => $results->[$i]{'categorycode'},
+            description           => $results->[$i]{'description'},
+            enrolmentperiod       => $results->[$i]{'enrolmentperiod'},
+            upperagelimit         => $results->[$i]{'upperagelimit'},
+            dateofbirthrequired   => $results->[$i]{'dateofbirthrequired'},
+            overduenoticerequired => $results->[$i]{'overduenoticerequired'},
+            issuelimit            => $results->[$i]{'issuelimit'},
+            hidelostitems         => $results->[$i]{'hidelostitems'},
+            category_type         => $results->[$i]{'category_type'},
+            default_privacy       => $results->[$i]{'default_privacy'},
+            enrolmentfee          => sprintf( "%.2f", $results->[$i]{'enrolmentfee'} ),
+            reservefee            => sprintf( "%.2f", $results->[$i]{'reservefee'} ),
+            enrolmentperioddate   => C4::Dates::format_date( $results->[$i]{'enrolmentperioddate'} ),
+            "type_" . $results->[$i]{'category_type'} => 1,
         );
         if (C4::Context->preference('EnhancedMessagingPreferences')) {
             my $brief_prefs = _get_brief_messaging_prefs($results->[$i]{'categorycode'});
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 3854752..4065a46 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -7914,7 +7914,7 @@ if ( CheckVersion($DBversion) ) {
 
 $DBversion = "3.15.00.XXX";
 if(CheckVersion($DBversion)) {
-    $dbh->do("ALTER TABLE categories ADD default_privacy ENUM( 'default', 'never', 'forever' ) NOT NULL DEFAULT 'default'");
+    $dbh->do("ALTER TABLE categories ADD default_privacy ENUM( 'default', 'never', 'forever' ) NOT NULL DEFAULT 'default' AFTER categorycode");
     print "Upgrade to $DBversion done (Bug 6254 - can't set patron privacy by default)\n";
     SetVersion($DBversion);
 }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt
index 56b667a..d50afe6 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categorie.tt
@@ -1,7 +1,7 @@
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; Patron categories &rsaquo; [% IF ( add_form ) %][% IF ( categorycode ) %]Modify category '[% categorycode |html %]'[% ELSE %]New category[% END %][% END %]
 [% IF ( add_validate ) %]Data recorded[% END %]
-[% IF ( delete_confirm ) %][% IF ( totalgtzero ) %]Cannot delete: category [% categorycode |html %] in use[% ELSE %]Confirm deletion of category '[% categorycode |html %]'[% END %][% END %]
+[% IF ( delete_confirm ) %][% IF ( patrons_in_category > 0 ) %]Cannot delete: category [% categorycode |html %] in use[% ELSE %]Confirm deletion of category '[% categorycode |html %]'[% END %][% END %]
 [% IF ( delete_confirmed ) %]Category deleted[% END %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
 [% INCLUDE 'calendar.inc' %]
@@ -113,7 +113,7 @@
 
 <div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; [% IF ( add_form ) %] <a href="/cgi-bin/koha/admin/categorie.pl">Patron categories</a> &rsaquo; [% IF ( categorycode ) %]Modify category '[% categorycode |html %]'[% ELSE %]New category[% END %][% END %]
 [% IF ( add_validate ) %] <a href="/cgi-bin/koha/admin/categorie.pl">Patron categories</a> &rsaquo; Data recorded[% END %]
-[% IF ( delete_confirm ) %] <a href="/cgi-bin/koha/admin/categorie.pl">Patron categories</a> &rsaquo; [% IF ( totalgtzero ) %]Cannot delete: Category [% categorycode |html %] in use[% ELSE %]Confirm deletion of category '[% categorycode |html %]'[% END %][% END %]
+[% IF ( delete_confirm ) %] <a href="/cgi-bin/koha/admin/categorie.pl">Patron categories</a> &rsaquo; [% IF ( patrons_in_category > 0 ) %]Cannot delete: Category [% categorycode |html %] in use[% ELSE %]Confirm deletion of category '[% categorycode |html %]'[% END %][% END %]
 [% IF ( delete_confirmed ) %] <a href="/cgi-bin/koha/admin/categorie.pl">Patron categories</a> &rsaquo; Category deleted[% END %]
 [% IF ( else ) %]Patron categories[% END %]</div>
 
@@ -200,7 +200,7 @@
               [% END %]
             [% END %]
         </select>
-        <span>Select All if this category type must to be displayed all the time. Otherwise select librairies you want to associate with this value.
+        <span>Select <i>All branches</i> if this category type must to be displayed all the time. Otherwise select libraries you want to associate with this value.
         </span>
     </li>
     <li>
@@ -221,7 +221,7 @@
                 <option value="forever">Forever</option>
             [% END %]
         </select>
-        <span>Controls how long a patrons checkout history is kept for new patrons of this category. "Default" lets the library decide, "Never" anonymizes checkouts on return, and "Forever" keeps a patron's checkout history indefinitely.</span>
+        <span>Controls how long a patrons checkout history is kept for new patrons of this category. "Never" anonymizes checkouts on return, and "Forever" keeps a patron's checkout history indefinitely. When set to "Default", the amount of history kept is controlled by the cronjob <i>batch_anonymise.pl</i> which should be set up by your system administrator.</span>
     </li>
     </ol>
 </fieldset>
@@ -242,43 +242,54 @@
 	<form action="[% script_name %]" method="post">
 		<input type="submit" value="OK" />
 	</form>
-
 [% END %]
 
 [% IF ( delete_confirm ) %]
-	
-    	<form action="[% script_name %]" method="post">
-	<fieldset><legend>    	
-	[% IF ( totalgtzero ) %]
-	Category [% categorycode |html %] is in use.  Deletion not possible![% ELSE %]
-Confirm deletion of category [% categorycode |html %][% END %]</legend>
+    <form action="[% script_name %]" method="post">
+        <fieldset>
+            <legend>    	
+                [% IF ( patrons_in_category > 0 ) %]
+                    Category [% categorycode |html %] is in use.  Deletion not possible!
+                [% ELSE %]
+                    Confirm deletion of category [% categorycode |html %]
+                [% END %]
+            </legend>
 
-[% IF ( totalgtzero ) %]<div class="dialog alert"><strong>This category is used [% total %] times</strong>. Deletion not possible</div>[% END %]
-	<table>
-	<tr><th scope="row">Category code: </th><td>[% categorycode |html %]</td></tr>
-	<tr><th scope="row">Description: </th><td>[% description |html %]</td></tr>
-	<tr><th scope="row">Enrollment period: </th>
-		<td>
-			[% IF ( enrolmentperiod ) %]
-				[% enrolmentperiod %] months
-			[% ELSE %]
-				until [% enrolmentperioddate %]
-			[% END %]
-		</td>
-	</tr>
-	<tr><th scope="row">Age required: </th><td>[% dateofbirthrequired %] years</td></tr>
-	<tr><th scope="row">Upperage limit: </th><td>[% upperagelimit %] years</td></tr>
-	<tr><th scope="row">Enrollment fee: </th><td>[% enrolmentfee %]</td></tr>
-	<tr><th scope="row">Receives overdue notices: </th><td>[% IF ( overduenoticerequired ) %]Yes[% ELSE %]No[% END %]</td></tr>
-	<tr><th scope="row">Lost items in staff client</th><td>[% IF ( hidelostitems ) %]Hidden by default[% ELSE %]Shown[% END %]</td></tr>
-	<tr><th scope="row">Hold fee: </th><td>[% reservefee %]</td></tr>
-</table>
-		<fieldset class="action">[% IF ( totalgtzero ) %]
-<input type="submit" value="OK" /></form>
-		[% ELSE %]
-			<input type="hidden" name="op" value="delete_confirmed" />
-            <input type="hidden" name="categorycode" value="[% categorycode |html %]" /> <input type="submit" value="Delete this category" /> <a class="cancel" href="/cgi-bin/koha/admin/categorie.pl">Cancel</a>
-		[% END %]</fieldset></fieldset></form>
+            [% IF ( patrons_in_category > 0 ) %]
+                <div class="dialog alert"><strong>This category is used [% patrons_in_category %] times</strong>. Deletion not possible</div>
+            [% END %]
+
+            <table>
+                <tr><th scope="row">Category code: </th><td>[% categorycode |html %]</td></tr>
+                <tr><th scope="row">Description: </th><td>[% description |html %]</td></tr>
+                <tr><th scope="row">Enrollment period: </th>
+                    <td>
+                        [% IF ( enrolmentperiod ) %]
+                            [% enrolmentperiod %] months
+                        [% ELSE %]
+                            until [% enrolmentperioddate %]
+                        [% END %]
+                    </td>
+                </tr>
+                <tr><th scope="row">Age required: </th><td>[% category.dateofbirthrequired %] years</td></tr>
+                <tr><th scope="row">Upperage limit: </th><td>[% category.upperagelimit %] years</td></tr>
+                <tr><th scope="row">Enrollment fee: </th><td>[% category.enrolmentfee | format('%.02f') %]</td></tr>
+                <tr><th scope="row">Receives overdue notices: </th><td>[% IF ( category.overduenoticerequired ) %]Yes[% ELSE %]No[% END %]</td></tr>
+                <tr><th scope="row">Lost items in staff client</th><td>[% IF ( category.hidelostitems ) %]Hidden by default[% ELSE %]Shown[% END %]</td></tr>
+                <tr><th scope="row">Hold fee: </th><td>[% category.reservefee | format('%.02f') %]</td></tr>
+                <tr><th scope="row">Default privacy: </th><td>[% category.default_privacy %]</td></tr>
+            </table>
+
+            <fieldset class="action">
+                [% IF ( patrons_in_category > 0 ) %]
+                    <input type="submit" value="OK" /></form>
+                [% ELSE %]
+                    <input type="hidden" name="op" value="delete_confirmed" />
+                    <input type="hidden" name="categorycode" value="[% categorycode |html %]" /> <input type="submit" value="Delete this category" /> <a class="cancel" href="/cgi-bin/koha/admin/categorie.pl">Cancel</a>
+                [% END %]
+            </fieldset>
+        </fieldset>
+    </form>
 [% END %]
 
 [% IF ( delete_confirmed ) %]
@@ -319,6 +330,7 @@ Confirm deletion of category [% categorycode |html %][% END %]</legend>
             <th scope="col">Messaging</th>
             [% END %]
             <th scope="col">Branches limitations</th>
+            <th scope="col">Default privacy</th>
             <th scope="col">&nbsp; </th>
             <th scope="col">&nbsp; </th>
         </tr>
@@ -392,6 +404,9 @@ Confirm deletion of category [% categorycode |html %][% END %]</legend>
                                 No limitation
                             [% END %]
                         </td>
+                        <td>
+                            [% loo.default_privacy %]
+                        </td>
                         <td><a href="[% loo.script_name %]?op=add_form&amp;categorycode=[% loo.categorycode |uri %]">Edit</a></td>
                         <td><a href="[% loo.script_name %]?op=delete_confirm&amp;categorycode=[% loo.categorycode |uri %]">Delete</a></td>
 		</tr>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt
index e561ae5..2fb8a68 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/patron-attr-types.tt
@@ -202,7 +202,7 @@ function CheckAttributeTypeForm(f) {
                   [% END %]
                 [% END %]
             </select>
-            <span>Select All if this attribute type must to be displayed all the time. Otherwise select librairies you want to associate with this value.
+            <span>Select All if this attribute type must to be displayed all the time. Otherwise select libraries you want to associate with this value.
             </span>
         </li>
         <li>
-- 
1.7.2.5