View | Details | Raw Unified | Return to bug 18606
Collapse All | Expand All

(-)a/C4/RotatingCollections.pm (-198 lines)
Lines 48-62 BEGIN { Link Here
48
    require Exporter;
48
    require Exporter;
49
    @ISA    = qw( Exporter );
49
    @ISA    = qw( Exporter );
50
    @EXPORT = qw(
50
    @EXPORT = qw(
51
      CreateCollection
52
      UpdateCollection
53
      DeleteCollection
54
55
      GetItemsInCollection
51
      GetItemsInCollection
56
52
57
      GetCollection
58
      GetCollections
59
60
      AddItemToCollection
53
      AddItemToCollection
61
      RemoveItemFromCollection
54
      RemoveItemFromCollection
62
      TransferCollection
55
      TransferCollection
Lines 65-230 BEGIN { Link Here
65
    );
58
    );
66
}
59
}
67
60
68
=head2  CreateCollection
69
 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
70
 Creates a new collection
71
72
 Input:
73
   $title: short description of the club or service
74
   $description: long description of the club or service
75
76
 Output:
77
   $success: 1 if all database operations were successful, 0 otherwise
78
   $errorCode: Code for reason of failure, good for translating errors in templates
79
   $errorMessage: English description of error
80
81
=cut
82
83
sub CreateCollection {
84
    my ( $title, $description ) = @_;
85
86
    my $schema = Koha::Database->new()->schema();
87
    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
88
89
    ## Check for all necessary parameters
90
    if ( !$title ) {
91
        return ( 0, 1, "NO_TITLE" );
92
    } elsif ( $duplicate_titles ) {
93
        return ( 0, 2, "DUPLICATE_TITLE" );
94
    }
95
96
    $description ||= q{};
97
98
    my $success = 1;
99
100
    my $dbh = C4::Context->dbh;
101
102
    my $sth;
103
    $sth = $dbh->prepare(
104
        "INSERT INTO collections ( colId, colTitle, colDesc )
105
                        VALUES ( NULL, ?, ? )"
106
    );
107
    $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
108
109
    return 1;
110
111
}
112
113
=head2 UpdateCollection
114
115
 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
116
117
Updates a collection
118
119
 Input:
120
   $colId: id of the collection to be updated
121
   $title: short description of the club or service
122
   $description: long description of the club or service
123
124
 Output:
125
   $success: 1 if all database operations were successful, 0 otherwise
126
   $errorCode: Code for reason of failure, good for translating errors in templates
127
   $errorMessage: English description of error
128
129
=cut
130
131
sub UpdateCollection {
132
    my ( $colId, $title, $description ) = @_;
133
134
    my $schema = Koha::Database->new()->schema();
135
    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title,  -not => { colId => $colId } });
136
137
    ## Check for all necessary parameters
138
    if ( !$colId ) {
139
        return ( 0, 1, "NO_ID" );
140
    }
141
    if ( !$title ) {
142
        return ( 0, 2, "NO_TITLE" );
143
    }
144
    if ( $duplicate_titles ) {
145
        return ( 0, 3, "DUPLICATE_TITLE" );
146
    }
147
148
    my $dbh = C4::Context->dbh;
149
150
    $description ||= q{};
151
152
    my $sth;
153
    $sth = $dbh->prepare(
154
        "UPDATE collections
155
                        SET 
156
                        colTitle = ?, colDesc = ? 
157
                        WHERE colId = ?"
158
    );
159
    $sth->execute( $title, $description, $colId )
160
      or return ( 0, 4, $sth->errstr() );
161
162
    return 1;
163
164
}
165
166
=head2 DeleteCollection
167
168
 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
169
 Deletes a collection of the given id
170
171
 Input:
172
   $colId : id of the Archetype to be deleted
173
174
 Output:
175
   $success: 1 if all database operations were successful, 0 otherwise
176
   $errorCode: Code for reason of failure, good for translating errors in templates
177
   $errorMessage: English description of error
178
179
=cut
180
181
sub DeleteCollection {
182
    my ($colId) = @_;
183
184
    ## Parameter check
185
    if ( !$colId ) {
186
        return ( 0, 1, "NO_ID" );
187
    }
188
189
    my $dbh = C4::Context->dbh;
190
191
    my $sth;
192
193
    $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
194
    $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
195
196
    return 1;
197
}
198
199
=head2 GetCollections
200
201
 $collections = GetCollections();
202
 Returns data about all collections
203
204
 Output:
205
  On Success:
206
   $results: Reference to an array of associated arrays
207
  On Failure:
208
   $errorCode: Code for reason of failure, good for translating errors in templates
209
   $errorMessage: English description of error
210
211
=cut
212
213
sub GetCollections {
214
215
    my $dbh = C4::Context->dbh;
216
217
    my $sth = $dbh->prepare("SELECT * FROM collections");
218
    $sth->execute() or return ( 1, $sth->errstr() );
219
220
    my @results;
221
    while ( my $row = $sth->fetchrow_hashref ) {
222
        push( @results, $row );
223
    }
224
225
    return \@results;
226
}
227
228
=head2 GetItemsInCollection
61
=head2 GetItemsInCollection
229
62
230
 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
63
 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
Lines 274-310 sub GetItemsInCollection { Link Here
274
    return \@results;
107
    return \@results;
275
}
108
}
276
109
277
=head2 GetCollection
278
279
 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
280
281
Returns information about a collection
282
283
 Input:
284
   $colId: Id of the collection
285
 Output:
286
   $colId, $colTitle, $colDesc, $colBranchcode
287
288
=cut
289
290
sub GetCollection {
291
    my ($colId) = @_;
292
293
    my $dbh = C4::Context->dbh;
294
295
    my ( $sth, @results );
296
    $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
297
    $sth->execute($colId) or return 0;
298
299
    my $row = $sth->fetchrow_hashref;
300
301
    return (
302
        $$row{'colId'},   $$row{'colTitle'},
303
        $$row{'colDesc'}, $$row{'colBranchcode'}
304
    );
305
306
}
307
308
=head2 AddItemToCollection
110
=head2 AddItemToCollection
309
111
310
 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
112
 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
(-)a/rotating_collections/addItems.pl (-6 / +8 lines)
Lines 25-30 use C4::RotatingCollections; Link Here
25
use C4::Items;
25
use C4::Items;
26
26
27
use Koha::Items;
27
use Koha::Items;
28
use Koha::RotatingCollections;
28
29
29
use CGI qw ( -utf8 );
30
use CGI qw ( -utf8 );
30
31
Lines 90-107 if ( $query->param('action') eq 'addItem' ) { Link Here
90
    }
91
    }
91
}
92
}
92
93
93
my ( $colId, $colTitle, $colDescription, $colBranchcode ) =
94
my $colId = $query->param('colId');
94
  GetCollection( $query->param('colId') );
95
my $collection = Koha::RotatingCollections->find($colId);
96
95
my $collectionItems = GetItemsInCollection($colId);
97
my $collectionItems = GetItemsInCollection($colId);
96
if ($collectionItems) {
98
if ($collectionItems) {
97
    $template->param( collectionItemsLoop => $collectionItems );
99
    $template->param( collectionItemsLoop => $collectionItems );
98
}
100
}
99
101
100
$template->param(
102
$template->param(
101
    colId          => $colId,
103
    colId          => $collection->colId,
102
    colTitle       => $colTitle,
104
    colTitle       => $collection->colTitle,
103
    colDescription => $colDescription,
105
    colDescription => $collection->colDesc,
104
    colBranchcode  => $colBranchcode,
106
    colBranchcode  => $collection->colBranchcode,
105
);
107
);
106
108
107
output_html_with_http_headers $query, $cookie, $template->output;
109
output_html_with_http_headers $query, $cookie, $template->output;
(-)a/rotating_collections/editCollections.pl (-49 / +49 lines)
Lines 23-32 use CGI qw ( -utf8 ); Link Here
23
use C4::Output;
23
use C4::Output;
24
use C4::Auth;
24
use C4::Auth;
25
use C4::Context;
25
use C4::Context;
26
27
use C4::RotatingCollections;
26
use C4::RotatingCollections;
28
27
28
use Koha::RotatingCollections;
29
29
my $query = new CGI;
30
my $query = new CGI;
31
my $action = $query->param('action');
32
my @messages;
30
33
31
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32
    {
35
    {
Lines 39-117 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
39
    }
42
    }
40
);
43
);
41
44
42
my $action = $query->param('action');
43
$template->param( action => $action );
44
45
# Create new Collection
45
# Create new Collection
46
if ( $action eq 'create' ) {
46
if ( $action eq 'create' ) {
47
    my $title       = $query->param('title');
47
    my $title       = $query->param('title');
48
    my $description = $query->param('description');
48
    my $description = $query->param('description');
49
49
50
    my ( $createdSuccessfully, $errorCode, $errorMessage ) =
50
    my $collection = Koha::RotatingCollection->new(
51
      CreateCollection( $title, $description );
51
        {   colTitle => $title,
52
52
            colDesc  => $description,
53
    $template->param(
53
        }
54
        previousActionCreate => 1,
55
        createdTitle         => $title,
56
    );
54
    );
57
55
58
    if ($createdSuccessfully) {
56
    eval { $collection->store; };
59
        $template->param( createSuccess => 1 );
57
60
    }
58
    if ($@) {
61
    else {
59
        push @messages, { type => 'error', code => 'error_on_insert' };
62
        $template->param( createFailure  => 1 );
60
    } else {
63
        $template->param( failureMessage => $errorMessage );
61
        push @messages, { type => 'message', code => 'success_on_insert' };
64
    }
62
    }
65
}
66
63
67
## Delete a club or service
64
    $action = "list";
68
elsif ( $action eq 'delete' ) {
65
66
} elsif ( $action eq 'delete' ) { # Delete collection
69
    my $colId = $query->param('colId');
67
    my $colId = $query->param('colId');
70
    my ( $success, $errorCode, $errorMessage ) = DeleteCollection($colId);
68
    my $collection = Koha::RotatingCollections->find($colId);
69
    my $deleted = eval { $collection->delete; };
71
70
72
    $template->param( previousActionDelete => 1 );
71
    if ( $@ or not $deleted ) {
73
    if ($success) {
72
        push @messages, { type => 'error', code => 'error_on_delete' };
74
        $template->param( deleteSuccess => 1 );
73
    } else {
74
        push @messages, { type => 'message', code => 'success_on_delete' };
75
    }
75
    }
76
    else {
77
        $template->param( deleteFailure  => 1 );
78
        $template->param( failureMessage => $errorMessage );
79
    }
80
}
81
76
82
## Edit a club or service: grab data, put in form.
77
    $action = "list";
83
elsif ( $action eq 'edit' ) {
78
84
    my ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $query->param('colId') );
79
} elsif ( $action eq 'edit' ) { # Edit page of collection
80
    my $collection = Koha::RotatingCollections->find($query->param('colId'));
85
81
86
    $template->param(
82
    $template->param(
87
        previousActionEdit => 1,
83
        previousActionEdit => 1,
88
        editColId          => $colId,
84
        editColId          => $collection->colId,
89
        editColTitle       => $colTitle,
85
        editColTitle       => $collection->colTitle,
90
        editColDescription => $colDesc,
86
        editColDescription => $collection->colDesc,
91
    );
87
    );
92
}
93
88
94
# Update a Club or Service
89
} elsif ( $action eq 'update' ) { # Update collection
95
elsif ( $action eq 'update' ) {
96
    my $colId       = $query->param('colId');
90
    my $colId       = $query->param('colId');
97
    my $title       = $query->param('title');
91
    my $title       = $query->param('title');
98
    my $description = $query->param('description');
92
    my $description = $query->param('description');
99
93
100
    my ( $createdSuccessfully, $errorCode, $errorMessage ) =
94
    if ($colId) {
101
      UpdateCollection( $colId, $title, $description );
95
        my $collection = Koha::RotatingCollections->find($colId);
96
        $collection->colTitle($title);
97
        $collection->colDesc($description);
102
98
103
    $template->param(
99
        eval { $collection->store; };
104
        previousActionUpdate => 1,
105
        updatedTitle         => $title,
106
    );
107
100
108
    if ($createdSuccessfully) {
101
        if ($@) {
109
        $template->param( updateSuccess => 1 );
102
            push @messages, { type => 'error', code => 'error_on_update' };
110
    }
103
        } else {
111
    else {
104
            push @messages, { type => 'message', code => 'success_on_update' };
112
        $template->param( updateFailure  => 1 );
105
        }
113
        $template->param( failureMessage => $errorMessage );
106
107
        $action = "list";
114
    }
108
    }
109
115
}
110
}
116
111
112
$template->param(
113
    action   => $action,
114
    messages => \@messages,
115
);
116
117
output_html_with_http_headers $query, $cookie, $template->output;
117
output_html_with_http_headers $query, $cookie, $template->output;
(-)a/rotating_collections/rotatingCollections.pl (-2 / +3 lines)
Lines 23-29 use CGI qw ( -utf8 ); Link Here
23
use C4::Output;
23
use C4::Output;
24
use C4::Auth;
24
use C4::Auth;
25
use C4::Context;
25
use C4::Context;
26
use C4::RotatingCollections;
26
27
use Koha::RotatingCollections;
27
28
28
my $query = new CGI;
29
my $query = new CGI;
29
30
Lines 40-46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
40
41
41
my $branchcode = $query->cookie('branch');
42
my $branchcode = $query->cookie('branch');
42
43
43
my $collections = GetCollections();
44
my $collections = Koha::RotatingCollections->search;
44
45
45
$template->param(
46
$template->param(
46
    collectionsLoop => $collections,
47
    collectionsLoop => $collections,
(-)a/rotating_collections/transferCollection.pl (-7 / +8 lines)
Lines 23-28 use C4::Auth; Link Here
23
use C4::Context;
23
use C4::Context;
24
use C4::RotatingCollections;
24
use C4::RotatingCollections;
25
25
26
use Koha::RotatingCollections;
27
26
use CGI qw ( -utf8 );
28
use CGI qw ( -utf8 );
27
29
28
my $query = new CGI;
30
my $query = new CGI;
Lines 60-72 if ($toBranch) { Link Here
60
}
62
}
61
63
62
## Get data about collection
64
## Get data about collection
63
my ( $colTitle, $colDesc, $colBranchcode );
65
my $collection = Koha::RotatingCollections->find($colId);
64
( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection($colId);
66
65
$template->param(
67
$template->param(
66
    colId            => $colId,
68
    colId            => $collection->colId,
67
    colTitle         => $colTitle,
69
    colTitle         => $collection->colTitle,
68
    colDesc          => $colDesc,
70
    colDesc          => $collection->colDesc,
69
    colBranchcode    => $colBranchcode,
71
    colBranchcode    => $collection->colBranchcode,
70
);
72
);
71
73
72
output_html_with_http_headers $query, $cookie, $template->output;
74
output_html_with_http_headers $query, $cookie, $template->output;
73
- 

Return to bug 18606