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

(-)a/Koha/Item/Template.pm (+69 lines)
Line 0 Link Here
1
package Koha::Item::Template;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use JSON qw( encode_json decode_json );
21
22
use base qw(Koha::Object);
23
24
=head1 NAME
25
26
Koha::Item::Template - Koha Item Template Object class
27
28
=head1 API
29
30
=head2 Class Methods
31
32
=head3 store
33
34
Override base store method
35
to serialize the template contents as JSON
36
37
=cut
38
39
sub store {
40
    my ($self) = @_;
41
42
    if ( ref( $self->contents ) eq 'HASH' ) {
43
        $self->contents( encode_json( $self->contents ) );
44
    }
45
46
    $self = $self->SUPER::store;
47
}
48
49
=head3 decoded_contents
50
51
Returns a deserilized perl structure of the JSON formatted contents
52
53
=cut
54
55
sub decoded_contents {
56
    my ($self) = @_;
57
58
    return decode_json( $self->contents ) if $self->contents;
59
}
60
61
=head3 type
62
63
=cut
64
65
sub _type {
66
    return 'ItemEditorTemplate';
67
}
68
69
1;
(-)a/Koha/Item/Templates.pm (+79 lines)
Line 0 Link Here
1
package Koha::Item::Templates;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
21
use Koha::Database;
22
23
use Koha::Item::Template;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Item::Templates - Koha Item Template Object set class
30
31
=head3 get_available
32
33
Returns a hashref with keys 'owned' and 'shared' pointing to Koha::Item::Templats objects
34
representing templates owned by the user or shared to the user respectivetly.
35
36
=cut
37
38
sub get_available {
39
    my ( $class, $borrowernumber ) = @_;
40
41
    my $params = {
42
        order_by => 'name',
43
        columns  => [ 'id', 'borrowernumber', 'name', 'is_shared' ],
44
    };
45
46
    return {
47
        owned => Koha::Item::Templates->search(
48
            {
49
                borrowernumber => $borrowernumber
50
            },
51
            $params
52
        ),
53
        shared => Koha::Item::Templates->search(
54
            {
55
                borrowernumber => { "!=" => $borrowernumber },
56
                is_shared      => 1
57
            },
58
            $params
59
        ),
60
    };
61
}
62
63
=head3 _type
64
65
=cut
66
67
sub _type {
68
    return 'ItemEditorTemplate';
69
}
70
71
=head3 object_class
72
73
=cut
74
75
sub object_class {
76
    return 'Koha::Item::Template';
77
}
78
79
1;
(-)a/t/db_dependent/Koha/Item/Template.t (+48 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2022 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
24
use t::lib::TestBuilder;
25
26
use Test::More tests => 2;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
use_ok("Koha::Item::Template");
32
33
subtest 'Serializing and deserializing contents' => sub {
34
    plan tests => 2;
35
36
    $schema->storage->txn_begin;
37
38
    my $data = { location => 'test' };
39
    my $template = Koha::Item::Template->new({
40
            name => 'My template',
41
            contents => $data,
42
    })->store();
43
44
    is( $template->contents, '{"location":"test"}', 'Contents serialized correctly' );
45
    is( $template->decoded_contents->{location}, 'test', 'Contents deserialized correctly' );
46
47
    $schema->storage->txn_rollback;
48
};
(-)a/t/db_dependent/Koha/Item/Templates.t (-1 / +91 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2022 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Koha::Database;
23
24
use t::lib::TestBuilder;
25
26
use Test::More tests => 2;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
use_ok("Koha::Item::Templates");
32
33
subtest 'get_available' => sub {
34
    plan tests => 2;
35
36
    $schema->storage->txn_begin;
37
38
    Koha::Item::Templates->search->delete;
39
40
    my $library  = $builder->build( { source => 'Branch' } );
41
    my $category = $builder->build( { source => 'Category' } );
42
    my $patron_1 = Koha::Patron->new(
43
        {
44
            branchcode   => $library->{branchcode},
45
            categorycode => $category->{categorycode},
46
            surname      => 'surname for patron1',
47
            firstname    => 'firstname for patron1',
48
        }
49
    )->store;
50
    my $patron_2 = Koha::Patron->new(
51
        {
52
            branchcode   => $library->{branchcode},
53
            categorycode => $category->{categorycode},
54
            surname      => 'surname for patron2',
55
            firstname    => 'firstname for patron2',
56
        }
57
    )->store;
58
59
    my $owner_template = Koha::Item::Template->new(
60
        {
61
            borrowernumber => $patron_1->id,
62
            name           => 'My template',
63
            contents       => { location => 'test' },
64
            is_shared      => 0,
65
        }
66
    )->store();
67
68
    my $shared_template = Koha::Item::Template->new(
69
        {
70
            borrowernumber => $patron_2->id,
71
            name           => 'My template',
72
            contents       => { location => 'test' },
73
            is_shared      => 1,
74
        }
75
    )->store();
76
77
    my $unshared_template = Koha::Item::Template->new(
78
        {
79
            borrowernumber => $patron_2->id,
80
            name           => 'My template',
81
            contents       => { location => 'test' },
82
            is_shared      => 0,
83
        }
84
    )->store();
85
86
    my $templates = Koha::Item::Templates->get_available( $patron_1->id );
87
    is( $templates->{owned}->count, 1, "Got back one owned template" );
88
    is( $templates->{shared}->count, 1, "Got back one shared templated" );
89
90
    $schema->storage->txn_rollback;
91
};

Return to bug 24606