From 3236f51c28edbf7e293fb4f74440a6dcf87115fc Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Fri, 14 Oct 2016 16:38:25 +0200 Subject: [PATCH] Bug 18606: Easy ones - CRUD --- C4/RotatingCollections.pm | 198 ---------------------------- rotating_collections/addItems.pl | 15 ++- rotating_collections/editCollections.pl | 98 +++++++------- rotating_collections/rotatingCollections.pl | 5 +- rotating_collections/transferCollection.pl | 14 +- 5 files changed, 69 insertions(+), 261 deletions(-) diff --git a/C4/RotatingCollections.pm b/C4/RotatingCollections.pm index 709eaf2..66a64c4 100644 --- a/C4/RotatingCollections.pm +++ b/C4/RotatingCollections.pm @@ -48,15 +48,8 @@ BEGIN { require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( - CreateCollection - UpdateCollection - DeleteCollection - GetItemsInCollection - GetCollection - GetCollections - AddItemToCollection RemoveItemFromCollection TransferCollection @@ -65,166 +58,6 @@ BEGIN { ); } -=head2 CreateCollection - ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description ); - Creates a new collection - - Input: - $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 CreateCollection { - my ( $title, $description ) = @_; - - my $schema = Koha::Database->new()->schema(); - my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title }); - - ## Check for all necessary parameters - if ( !$title ) { - return ( 0, 1, "NO_TITLE" ); - } elsif ( $duplicate_titles ) { - return ( 0, 2, "DUPLICATE_TITLE" ); - } - - $description ||= q{}; - - my $success = 1; - - my $dbh = C4::Context->dbh; - - my $sth; - $sth = $dbh->prepare( - "INSERT INTO collections ( colId, colTitle, colDesc ) - VALUES ( NULL, ?, ? )" - ); - $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() ); - - return 1; - -} - -=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 DeleteCollection - - ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId ); - Deletes a collection of the given id - - Input: - $colId : id of the Archetype to be deleted - - 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 DeleteCollection { - my ($colId) = @_; - - ## Parameter check - if ( !$colId ) { - return ( 0, 1, "NO_ID" ); - } - - my $dbh = C4::Context->dbh; - - my $sth; - - $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?"); - $sth->execute($colId) or return ( 0, 4, $sth->errstr() ); - - return 1; -} - -=head2 GetCollections - - $collections = GetCollections(); - Returns data about all collections - - Output: - On Success: - $results: Reference to an array of associated arrays - On Failure: - $errorCode: Code for reason of failure, good for translating errors in templates - $errorMessage: English description of error - -=cut - -sub GetCollections { - - my $dbh = C4::Context->dbh; - - my $sth = $dbh->prepare("SELECT * FROM collections"); - $sth->execute() or return ( 1, $sth->errstr() ); - - my @results; - while ( my $row = $sth->fetchrow_hashref ) { - push( @results, $row ); - } - - return \@results; -} - =head2 GetItemsInCollection ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId ); @@ -274,37 +107,6 @@ sub GetItemsInCollection { return \@results; } -=head2 GetCollection - - ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId ); - -Returns information about a collection - - Input: - $colId: Id of the collection - Output: - $colId, $colTitle, $colDesc, $colBranchcode - -=cut - -sub GetCollection { - my ($colId) = @_; - - my $dbh = C4::Context->dbh; - - my ( $sth, @results ); - $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?"); - $sth->execute($colId) or return 0; - - my $row = $sth->fetchrow_hashref; - - return ( - $$row{'colId'}, $$row{'colTitle'}, - $$row{'colDesc'}, $$row{'colBranchcode'} - ); - -} - =head2 AddItemToCollection ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber ); diff --git a/rotating_collections/addItems.pl b/rotating_collections/addItems.pl index cd24d50..1a3f358 100755 --- a/rotating_collections/addItems.pl +++ b/rotating_collections/addItems.pl @@ -24,6 +24,8 @@ use C4::Context; use C4::RotatingCollections; use C4::Items; +use Koha::RotatingCollections; + use CGI qw ( -utf8 ); my $query = new CGI; @@ -87,18 +89,19 @@ if ( $query->param('action') eq 'addItem' ) { } } -my ( $colId, $colTitle, $colDescription, $colBranchcode ) = - GetCollection( $query->param('colId') ); +my $colId = $query->param('colId'); +my $collection = Koha::RotatingCollections->find($colId); + my $collectionItems = GetItemsInCollection($colId); if ($collectionItems) { $template->param( collectionItemsLoop => $collectionItems ); } $template->param( - colId => $colId, - colTitle => $colTitle, - colDescription => $colDescription, - colBranchcode => $colBranchcode, + colId => $collection->colId, + colTitle => $collection->colTitle, + colDescription => $collection->colDesc, + colBranchcode => $collection->colBranchcode, ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/rotating_collections/editCollections.pl b/rotating_collections/editCollections.pl index c8ed8ed..6aa6c1c 100755 --- a/rotating_collections/editCollections.pl +++ b/rotating_collections/editCollections.pl @@ -23,10 +23,13 @@ use CGI qw ( -utf8 ); use C4::Output; use C4::Auth; use C4::Context; - use C4::RotatingCollections; +use Koha::RotatingCollections; + my $query = new CGI; +my $action = $query->param('action'); +my @messages; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { @@ -39,79 +42,76 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); -my $action = $query->param('action'); -$template->param( action => $action ); - # Create new Collection if ( $action eq 'create' ) { my $title = $query->param('title'); my $description = $query->param('description'); - my ( $createdSuccessfully, $errorCode, $errorMessage ) = - CreateCollection( $title, $description ); - - $template->param( - previousActionCreate => 1, - createdTitle => $title, + my $collection = Koha::RotatingCollection->new( + { colTitle => $title, + colDesc => $description, + } ); - if ($createdSuccessfully) { - $template->param( createSuccess => 1 ); - } - else { - $template->param( createFailure => 1 ); - $template->param( failureMessage => $errorMessage ); + eval { $collection->store; }; + + if ($@) { + push @messages, { type => 'error', code => 'error_on_insert' }; + } else { + push @messages, { type => 'message', code => 'success_on_insert' }; } -} -## Delete a club or service -elsif ( $action eq 'delete' ) { + $action = "list"; + +} elsif ( $action eq 'delete' ) { # Delete collection my $colId = $query->param('colId'); - my ( $success, $errorCode, $errorMessage ) = DeleteCollection($colId); + my $collection = Koha::RotatingCollections->find($colId); + my $deleted = eval { $collection->delete; }; - $template->param( previousActionDelete => 1 ); - if ($success) { - $template->param( deleteSuccess => 1 ); + if ( $@ or not $deleted ) { + push @messages, { type => 'error', code => 'error_on_delete' }; + } else { + push @messages, { type => 'message', code => 'success_on_delete' }; } - else { - $template->param( deleteFailure => 1 ); - $template->param( failureMessage => $errorMessage ); - } -} -## Edit a club or service: grab data, put in form. -elsif ( $action eq 'edit' ) { - my ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $query->param('colId') ); + $action = "list"; + +} elsif ( $action eq 'edit' ) { # Edit page of collection + my $collection = Koha::RotatingCollections->find($query->param('colId')); $template->param( previousActionEdit => 1, - editColId => $colId, - editColTitle => $colTitle, - editColDescription => $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( + action => $action, + messages => \@messages, +); + output_html_with_http_headers $query, $cookie, $template->output; diff --git a/rotating_collections/rotatingCollections.pl b/rotating_collections/rotatingCollections.pl index acf4586..0092349 100755 --- a/rotating_collections/rotatingCollections.pl +++ b/rotating_collections/rotatingCollections.pl @@ -23,7 +23,8 @@ use CGI qw ( -utf8 ); use C4::Output; use C4::Auth; use C4::Context; -use C4::RotatingCollections; + +use Koha::RotatingCollections; my $query = new CGI; @@ -40,7 +41,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( my $branchcode = $query->cookie('branch'); -my $collections = GetCollections(); +my $collections = Koha::RotatingCollections->search; $template->param( collectionsLoop => $collections, diff --git a/rotating_collections/transferCollection.pl b/rotating_collections/transferCollection.pl index 49b09b3..8c48ed9 100755 --- a/rotating_collections/transferCollection.pl +++ b/rotating_collections/transferCollection.pl @@ -23,6 +23,8 @@ use C4::Auth; use C4::Context; use C4::RotatingCollections; +use Koha::RotatingCollections; + use CGI qw ( -utf8 ); my $query = new CGI; @@ -60,13 +62,13 @@ if ($toBranch) { } ## Get data about collection -my ( $colTitle, $colDesc, $colBranchcode ); -( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection($colId); +my $collection = Koha::RotatingCollections->find($colId); + $template->param( - colId => $colId, - colTitle => $colTitle, - colDesc => $colDesc, - colBranchcode => $colBranchcode, + colId => $collection->colId, + colTitle => $collection->colTitle, + colDesc => $collection->colDesc, + colBranchcode => $collection->colBranchcode, ); output_html_with_http_headers $query, $cookie, $template->output; -- 2.1.4