From 7abefa0f17d738cc92d9ef9e02d27f18e81e1076 Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Fri, 14 Oct 2016 18:41:50 +0200
Subject: [PATCH] Bug 18606: Get rid of UpdateCollection

---
 C4/RotatingCollections.pm               | 55 ---------------------------------
 rotating_collections/editCollections.pl | 35 ++++++++++-----------
 2 files changed, 17 insertions(+), 73 deletions(-)

diff --git a/C4/RotatingCollections.pm b/C4/RotatingCollections.pm
index 0f1ef69..66a64c4 100644
--- a/C4/RotatingCollections.pm
+++ b/C4/RotatingCollections.pm
@@ -48,8 +48,6 @@ BEGIN {
     require Exporter;
     @ISA    = qw( Exporter );
     @EXPORT = qw(
-      UpdateCollection
-
       GetItemsInCollection
 
       AddItemToCollection
@@ -60,59 +58,6 @@ BEGIN {
     );
 }
 
-=head2 UpdateCollection
-
- ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
-
-Updates a collection
-
- Input:
-   $colId: id of the collection to be updated
-   $title: short description of the club or service
-   $description: long description of the club or service
-
- Output:
-   $success: 1 if all database operations were successful, 0 otherwise
-   $errorCode: Code for reason of failure, good for translating errors in templates
-   $errorMessage: English description of error
-
-=cut
-
-sub UpdateCollection {
-    my ( $colId, $title, $description ) = @_;
-
-    my $schema = Koha::Database->new()->schema();
-    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title,  -not => { colId => $colId } });
-
-    ## Check for all necessary parameters
-    if ( !$colId ) {
-        return ( 0, 1, "NO_ID" );
-    }
-    if ( !$title ) {
-        return ( 0, 2, "NO_TITLE" );
-    }
-    if ( $duplicate_titles ) {
-        return ( 0, 3, "DUPLICATE_TITLE" );
-    }
-
-    my $dbh = C4::Context->dbh;
-
-    $description ||= q{};
-
-    my $sth;
-    $sth = $dbh->prepare(
-        "UPDATE collections
-                        SET 
-                        colTitle = ?, colDesc = ? 
-                        WHERE colId = ?"
-    );
-    $sth->execute( $title, $description, $colId )
-      or return ( 0, 4, $sth->errstr() );
-
-    return 1;
-
-}
-
 =head2 GetItemsInCollection
 
  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
diff --git a/rotating_collections/editCollections.pl b/rotating_collections/editCollections.pl
index b4dcba6..6aa6c1c 100755
--- a/rotating_collections/editCollections.pl
+++ b/rotating_collections/editCollections.pl
@@ -81,33 +81,32 @@ if ( $action eq 'create' ) {
 
     $template->param(
         previousActionEdit => 1,
-        editColId          => $collection->{colId},
-        editColTitle       => $collection->{colTitle},
-        editColDescription => $collection->{colDesc},
+        editColId          => $collection->colId,
+        editColTitle       => $collection->colTitle,
+        editColDescription => $collection->colDesc,
     );
-}
 
-# Update a Club or Service
-elsif ( $action eq 'update' ) {
+} elsif ( $action eq 'update' ) { # Update collection
     my $colId       = $query->param('colId');
     my $title       = $query->param('title');
     my $description = $query->param('description');
 
-    my ( $createdSuccessfully, $errorCode, $errorMessage ) =
-      UpdateCollection( $colId, $title, $description );
+    if ($colId) {
+        my $collection = Koha::RotatingCollections->find($colId);
+        $collection->colTitle($title);
+        $collection->colDesc($description);
 
-    $template->param(
-        previousActionUpdate => 1,
-        updatedTitle         => $title,
-    );
+        eval { $collection->store; };
 
-    if ($createdSuccessfully) {
-        $template->param( updateSuccess => 1 );
-    }
-    else {
-        $template->param( updateFailure  => 1 );
-        $template->param( failureMessage => $errorMessage );
+        if ($@) {
+            push @messages, { type => 'error', code => 'error_on_update' };
+        } else {
+            push @messages, { type => 'message', code => 'success_on_update' };
+        }
+
+        $action = "list";
     }
+
 }
 
 $template->param(
-- 
2.1.4