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

(-)a/C4/Utils/DataTables.pm (-154 lines)
Lines 1-154 Link Here
1
package C4::Utils::DataTables;
2
3
# Copyright 2011 BibLibre
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
require Exporter;
22
23
use vars qw(@ISA @EXPORT);
24
25
BEGIN {
26
27
    @ISA        = qw(Exporter);
28
    @EXPORT     = qw(dt_build_orderby dt_get_params);
29
}
30
31
=head1 NAME
32
33
! DEPRECATED - This module is deprecated, the REST API route and REST API Datatables wrapper must be used instead!
34
35
C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
36
37
=head1 SYNOPSYS
38
39
    use CGI qw ( -utf8 );
40
    use C4::Context;
41
    use C4::Utils::DataTables;
42
43
    my $input = new CGI;
44
    my $vars = $input->Vars;
45
46
    my $query = qq{
47
        SELECT surname, firstname
48
        FROM borrowers
49
        WHERE borrowernumber = ?
50
    };
51
    $query .= dt_build_orderby($vars);
52
    $query .= " LIMIT ?,? ";
53
54
    my $dbh = C4::Context->dbh;
55
    my $sth = $dbh->prepare($query);
56
    $sth->execute(
57
        $vars->{'borrowernumber'},
58
        @$having_params,
59
        $vars->{'iDisplayStart'},
60
        $vars->{'iDisplayLength'}
61
    );
62
    ...
63
64
=head1 DESCRIPTION
65
66
    This module provide two utility functions to build a part of the SQL query,
67
    depending on DataTables parameters.
68
    One function build the 'ORDER BY' part, and the other the 'HAVING' part.
69
70
=head1 FUNCTIONS
71
72
=head2 dt_build_orderby
73
74
    my $orderby = dt_build_orderby($dt_param);
75
    This function takes a reference to a hash containing DataTables parameters
76
    and build the corresponding 'ORDER BY' clause.
77
    This hash must contains the following keys:
78
        iSortCol_N, where N is a number from 0 to the number of columns to sort on minus 1
79
        sSortDir_N is the sorting order ('asc' or 'desc) for the corresponding column
80
        mDataProp_N is a mapping between the column index, and the name of a SQL field
81
82
=cut
83
84
sub dt_build_orderby {
85
    my $param = shift;
86
87
    my $i = 0;
88
    my @orderbys;
89
    my $dbh = C4::Context->dbh;
90
    while(exists $param->{'iSortCol_'.$i}){
91
        my $iSortCol = $param->{'iSortCol_'.$i};
92
        my $sSortDir = $param->{'sSortDir_'.$i};
93
        my $mDataProp = $param->{'mDataProp_'.$iSortCol};
94
        my @sort_fields = $param->{$mDataProp.'_sorton'}
95
            ? split(' ', $param->{$mDataProp.'_sorton'})
96
            : ();
97
        if(@sort_fields > 0) {
98
            push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
99
        } else {
100
            push @orderbys, "$mDataProp $sSortDir";
101
        }
102
        $i++;
103
    }
104
105
    return unless @orderbys;
106
107
    my @sanitized_orderbys;
108
109
    # Trick for virtualshelves, should not be extended
110
    push @sanitized_orderbys, 'count asc' if grep {$_ eq 'count asc'} @orderbys;
111
    push @sanitized_orderbys, 'count desc' if grep {$_ eq 'count desc'} @orderbys;
112
113
    # Must be "branches.branchname asc", "borrowers.firstname desc", etc.
114
    @orderbys = grep { /^\w+\.\w+ (asc|desc)$/ } @orderbys;
115
116
    for my $orderby (@orderbys) {
117
      my ($identifier, $direction) = split / /, $orderby, 2;
118
      my ($table, $column) = split /\./, $identifier, 2;
119
      my $sanitized_identifier = $dbh->quote_identifier(undef, $table, $column);
120
      my $sanitized_direction = $direction eq 'asc' ? 'ASC' : 'DESC';
121
      push @sanitized_orderbys, "$sanitized_identifier $sanitized_direction";
122
    }
123
124
    return " ORDER BY " . join(',', @sanitized_orderbys) . " " if @sanitized_orderbys;
125
}
126
127
=head2 dt_get_params
128
129
    my %dtparam = = dt_get_params( $input )
130
    This function takes a reference to a new CGI object.
131
    It prepares a hash containing Datatable parameters.
132
133
=cut
134
sub dt_get_params {
135
    my $input = shift;
136
    my %dtparam;
137
    my $vars = $input->Vars;
138
139
    foreach(qw/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
140
        $dtparam{$_} = $input->param($_);
141
    }
142
    foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
143
        $dtparam{$_} = $vars->{$_};
144
    }
145
    for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
146
        foreach(qw/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
147
            my $key = $_ . '_' . $i;
148
            $dtparam{$key} = $input->param($key) if defined $input->param($key);
149
        }
150
    }
151
    return %dtparam;
152
}
153
154
1;
(-)a/C4/Utils/DataTables/VirtualShelves.pm (-21 / +36 lines)
Lines 2-8 package C4::Utils::DataTables::VirtualShelves; Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use C4::Context;
4
use C4::Context;
5
use C4::Utils::DataTables qw( dt_build_orderby );
6
use Koha::Virtualshelves;
5
use Koha::Virtualshelves;
7
6
8
sub search {
7
sub search {
Lines 13-25 sub search { Link Here
13
    my $sortby = $params->{sortby};
12
    my $sortby = $params->{sortby};
14
    my $public = $params->{public} // 1;
13
    my $public = $params->{public} // 1;
15
    $public = $public ? 1 : 0;
14
    $public = $public ? 1 : 0;
16
    my $dt_params = $params->{dt_params};
15
    my $order_by = $params->{order_by};
16
    my $start    = $params->{start};
17
    my $length   = $params->{length};
17
18
18
    # If not logged in user, be carreful and set the borrowernumber to 0
19
    # If not logged in user, be carreful and set the borrowernumber to 0
19
    # to prevent private lists lack
20
    # to prevent private lists lack
20
    my $loggedinuser = C4::Context->userenv->{'number'} || 0;
21
    my $loggedinuser = C4::Context->userenv->{'number'} || 0;
21
22
22
    my ($iTotalRecords, $iTotalDisplayRecords);
23
    my ($recordsTotal, $recordsFiltered);
23
24
24
    my $dbh = C4::Context->dbh;
25
    my $dbh = C4::Context->dbh;
25
26
Lines 62-69 sub search { Link Here
62
    }
63
    }
63
    if ( defined $owner and $owner ne '' ) {
64
    if ( defined $owner and $owner ne '' ) {
64
        push @where_strs, '( bo.firstname LIKE ? OR bo.surname LIKE ? )';
65
        push @where_strs, '( bo.firstname LIKE ? OR bo.surname LIKE ? )';
65
        push @args, "%$owner%", "%$owner%";
66
    push @args, "%$owner%", "%$owner%";
66
    }
67
}
67
    if ( defined $sortby and $sortby ne '' ) {
68
    if ( defined $sortby and $sortby ne '' ) {
68
        push @where_strs, 'sortfield = ?';
69
        push @where_strs, 'sortfield = ?';
69
        push @args, $sortby;
70
        push @args, $sortby;
Lines 79-96 sub search { Link Here
79
80
80
    my $where;
81
    my $where;
81
    $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
82
    $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
82
    my $orderby = dt_build_orderby($dt_params);
83
83
    $orderby =~ s|shelfnumber|vs.shelfnumber| if $orderby;
84
85
    if ($order_by) {
86
        my @order_by;
87
        $order_by =~ s|shelfnumber|vs.shelfnumber|;
88
        my @sanitized_orderbys;
89
        for my $order ( split ',', $order_by ) {
90
            my ( $identifier, $direction ) = split / /,  $order,      2;
91
            my ( $table,      $column )    = split /\./, $identifier, 2;
92
            my $sanitized_identifier = $dbh->quote_identifier( undef, $table, $column );
93
            my $sanitized_direction  = $direction eq 'asc' ? 'ASC' : 'DESC';
94
            push @sanitized_orderbys, "$sanitized_identifier $sanitized_direction";
95
        }
96
97
        $order_by = ' ORDER BY ' . join( ',', @sanitized_orderbys );
98
    }
84
99
85
    my $limit;
100
    my $limit;
86
    # If iDisplayLength == -1, we want to display all shelves
101
    # If length == -1, we want to display all shelves
87
    if ( $dt_params->{iDisplayLength} > -1 ) {
102
    if ( $length > -1 ) {
88
        # In order to avoid sql injection
103
        # In order to avoid sql injection
89
        $dt_params->{iDisplayStart} =~ s/\D//g;
104
        $start  =~ s/\D//g;
90
        $dt_params->{iDisplayLength} =~ s/\D//g;
105
        $length =~ s/\D//g;
91
        $dt_params->{iDisplayStart} //= 0;
106
        $start  //= 0;
92
        $dt_params->{iDisplayLength} //= 20;
107
        $length //= 20;
93
        $limit = "LIMIT $dt_params->{iDisplayStart},$dt_params->{iDisplayLength}";
108
        $limit = sprintf "LIMIT %s,%s", $start, $length;
94
    }
109
    }
95
110
96
    my $group_by = " GROUP BY vs.shelfnumber, vs.shelfname, vs.owner, vs.public,
111
    my $group_by = " GROUP BY vs.shelfnumber, vs.shelfname, vs.owner, vs.public,
Lines 102-121 sub search { Link Here
102
        $from,
117
        $from,
103
        ($where ? $where : ""),
118
        ($where ? $where : ""),
104
        $group_by,
119
        $group_by,
105
        ($orderby ? $orderby : ""),
120
        ($order_by ? $order_by : ""),
106
        ($limit ? $limit : "")
121
        ($limit ? $limit : "")
107
    );
122
    );
108
    my $shelves = $dbh->selectall_arrayref( $query, { Slice => {} }, @args );
123
    my $shelves = $dbh->selectall_arrayref( $query, { Slice => {} }, @args );
109
124
110
    # Get the iTotalDisplayRecords DataTable variable
125
    # Get the recordsFiltered DataTable variable
111
    $query = "SELECT COUNT(vs.shelfnumber) " . $from_total . ($where ? $where : "");
126
    $query = "SELECT COUNT(vs.shelfnumber) " . $from_total . ($where ? $where : "");
112
    ($iTotalDisplayRecords) = $dbh->selectrow_array( $query, undef, @args );
127
    ($recordsFiltered) = $dbh->selectrow_array( $query, undef, @args );
113
128
114
    # Get the iTotalRecords DataTable variable
129
    # Get the recordsTotal DataTable variable
115
    $query = q|SELECT COUNT(vs.shelfnumber)| . $from_total . q| WHERE public = ?|;
130
    $query = q|SELECT COUNT(vs.shelfnumber)| . $from_total . q| WHERE public = ?|;
116
    $query .= q| AND (vs.owner = ? OR sh.borrowernumber = ?)| if !$public;
131
    $query .= q| AND (vs.owner = ? OR sh.borrowernumber = ?)| if !$public;
117
    @args = !$public ? ( $loggedinuser, $public, $loggedinuser, $loggedinuser ) : ( $public );
132
    @args = !$public ? ( $loggedinuser, $public, $loggedinuser, $loggedinuser ) : ( $public );
118
    ( $iTotalRecords ) = $dbh->selectrow_array( $query, undef, @args );
133
    ( $recordsTotal ) = $dbh->selectrow_array( $query, undef, @args );
119
134
120
    for my $shelf ( @$shelves ) {
135
    for my $shelf ( @$shelves ) {
121
        my $s = Koha::Virtualshelves->find( $shelf->{shelfnumber} );
136
        my $s = Koha::Virtualshelves->find( $shelf->{shelfnumber} );
Lines 124-131 sub search { Link Here
124
        $shelf->{is_shared} = $s->is_shared;
139
        $shelf->{is_shared} = $s->is_shared;
125
    }
140
    }
126
    return {
141
    return {
127
        iTotalRecords => $iTotalRecords,
142
        recordsTotal => $recordsTotal,
128
        iTotalDisplayRecords => $iTotalDisplayRecords,
143
        recordsFiltered => $recordsFiltered,
129
        shelves => $shelves,
144
        shelves => $shelves,
130
    }
145
    }
131
}
146
}
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt (-50 / +28 lines)
Lines 622-678 Link Here
622
            $(document).ready(function(){
622
            $(document).ready(function(){
623
                var public = [% public | html %];
623
                var public = [% public | html %];
624
624
625
                let sorton = [
626
                            'vs.shelfname',
627
                            'count',
628
                            'vs.public',
629
                            'vs.owner',
630
                            'vs.sortfield',
631
                            'vs.created_on',
632
                            'vs.lastmodified',
633
                            ];
634
625
                var dtListResults = $("#listresultst").dataTable($.extend(true, {}, dataTablesDefaults, {
635
                var dtListResults = $("#listresultst").dataTable($.extend(true, {}, dataTablesDefaults, {
626
                "order": [[ 5, "asc" ]],
636
                    order: [[ 5, "asc" ]],
627
                    "serverSide":  true,
637
                    serverSide:  true,
628
                    "ajax":  "/cgi-bin/koha/svc/virtualshelves/search",
638
                    ajax:  {
629
                    'fnServerData': function(sSource, aoData, fnCallback) {
639
                        url: "/cgi-bin/koha/svc/virtualshelves/search",
630
                        aoData.push({
640
                        type: "POST",
631
                            'name': 'public',
641
                        data: function ( d ) {
632
                            'value': public,
642
                            let order_by = [];
633
                        },{
643
                            d.order.forEach((o, i) => order_by.push(sorton[o.column - 1] + " " + o.dir));
634
                            'name': 'shelfname',
644
                            return $.extend( {}, d, {
635
                            'value': $("#searchshelfname_filter").val(),
645
                                public,
636
                        },{
646
                                order_by: order_by.join(','),
637
                            'name': 'owner',
647
                                shelfname: $("#searchshelfname_filter").val(),
638
                            'value': $("#searchowner_filter").val(),
648
                                owner: $("#searchowner_filter").val(),
639
                        },{
649
                                sortby: $("#searchsortby_filter").val(),
640
                            'name': 'sortby',
650
                                template_path: 'virtualshelves/tables/shelves_results.tt',
641
                            'value': $("#searchsortby_filter").val(),
651
                                allow_transfer: '[% allow_transfer | html %]',
642
                        },{
652
                            });
643
                            'name': 'template_path',
653
                        }
644
                            'value': 'virtualshelves/tables/shelves_results.tt',
645
                        },{
646
                            'name': 'allow_transfer',
647
                            'value': '[% allow_transfer | html %]',
648
                        },{
649
                            'name': 'shelfname_sorton',
650
                            'value': 'vs.shelfname',
651
                        },{
652
                            'name': 'is_shared_sorton',
653
                            'value': 'vs.public',
654
                        },{
655
                            'name': 'owner_sorton',
656
                            'value': 'vs.owner',
657
                        },{
658
                            'name': 'sortby_sorton',
659
                            'value': 'vs.sortfield',
660
                        },{
661
                            'name': 'created_on_sorton',
662
                            'value': 'vs.created_on',
663
                        },{
664
                            'name': 'modification_time_sorton',
665
                            'value': 'vs.lastmodified',
666
                        });
667
                        $.ajax({
668
                            'dataType': 'json',
669
                            'type': 'POST',
670
                            'url': sSource,
671
                            'data': aoData,
672
                            'success': function(json){
673
                                fnCallback(json);
674
                            }
675
                        });
676
                    },
654
                    },
677
                    'columns':[
655
                    'columns':[
678
                        { "data": 'dt_public' },
656
                        { "data": 'dt_public' },
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/tables/shelves_results.tt (-13 / +13 lines)
Lines 3-32 Link Here
3
[% USE To %]
3
[% USE To %]
4
[% PROCESS 'i18n.inc' %]
4
[% PROCESS 'i18n.inc' %]
5
{
5
{
6
    "sEcho": [% sEcho | html %],
6
    "draw": [% draw | html %],
7
    "iTotalRecords": [% iTotalRecords | html %],
7
    "recordsTotal": [% recordsTotal | html %],
8
    "iTotalDisplayRecords": [% iTotalDisplayRecords | html %],
8
    "recordsFiltered": [% recordsFiltered | html %],
9
    "data": [
9
    "data": [
10
        [% FOREACH data IN aaData %]
10
        [% FOREACH d IN data %]
11
            {
11
            {
12
                "dt_public":
12
                "dt_public":
13
                    "[% data.public | html %]",
13
                    "[% d.public | html %]",
14
                "dt_shelfname":
14
                "dt_shelfname":
15
                    "<a href='/cgi-bin/koha/virtualshelves/shelves.pl?op=view&shelfnumber=[% data.shelfnumber | html %]'>[% data.shelfname | html | $To %]</a>",
15
                    "<a href='/cgi-bin/koha/virtualshelves/shelves.pl?op=view&shelfnumber=[% d.shelfnumber | html %]'>[% d.shelfname | html | $To %]</a>",
16
                "dt_count":
16
                "dt_count":
17
                    "[% tnx('{count} item', '{count} items', count, { count = data.count }) | $raw %]",
17
                    "[% tnx('{count} item', '{count} items', count, { count = d.count }) | $raw %]",
18
                "dt_is_shared":
18
                "dt_is_shared":
19
                    "[% IF data.public %]<span>Public</span>[% ELSIF data.is_shared %]<span>Shared</span>[% ELSE %]<span>Private</span>[% END %]",
19
                    "[% IF d.public %]<span>Public</span>[% ELSIF d.is_shared %]<span>Shared</span>[% ELSE %]<span>Private</span>[% END %]",
20
                "dt_owner":
20
                "dt_owner":
21
                    "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=[% data.owner | html %]'>[% data.firstname | html | $To %] [% data.surname | html | $To %]</a>",
21
                    "<a href='/cgi-bin/koha/members/moremember.pl?borrowernumber=[% d.owner | html %]'>[% d.firstname | html | $To %] [% d.surname | html | $To %]</a>",
22
                "dt_sortby":
22
                "dt_sortby":
23
                    [% IF data.sortby == "author" %]"<span>Author</span>"[% ELSIF data.sortby == "copyrightdate" %]"<span>Year</span>"[% ELSIF data.sortby == "itemcallnumber" %]"<span>Call number</span>"[% ELSIF data.sortby == "dateadded" %]"<span>Date added</span>"[% ELSE %]"<span>Title</span>"[% END %],
23
                    [% IF d.sortby == "author" %]"<span>Author</span>"[% ELSIF d.sortby == "copyrightdate" %]"<span>Year</span>"[% ELSIF d.sortby == "itemcallnumber" %]"<span>Call number</span>"[% ELSIF d.sortby == "dateadded" %]"<span>Date added</span>"[% ELSE %]"<span>Title</span>"[% END %],
24
                "dt_created_on":
24
                "dt_created_on":
25
                    "[% data.created_on | $KohaDates %]",
25
                    "[% d.created_on | $KohaDates %]",
26
                "dt_modification_time":
26
                "dt_modification_time":
27
                    "[% data.modification_time | $KohaDates %]",
27
                    "[% d.modification_time | $KohaDates %]",
28
                "dt_action":
28
                "dt_action":
29
                    "[% PROCESS action_form shelfnumber=data.shelfnumber can_manage_shelf=data.can_manage_shelf can_delete_shelf=data.can_delete_shelf type=data.type %]"
29
                    "[% PROCESS action_form shelfnumber=d.shelfnumber can_manage_shelf=d.can_manage_shelf can_delete_shelf=d.can_delete_shelf type=d.type %]"
30
            }[% UNLESS loop.last %],[% END %]
30
            }[% UNLESS loop.last %],[% END %]
31
        [% END %]
31
        [% END %]
32
    ]
32
    ]
(-)a/svc/virtualshelves/search (-12 / +11 lines)
Lines 5-11 use CGI; Link Here
5
5
6
use C4::Auth qw( get_template_and_user );
6
use C4::Auth qw( get_template_and_user );
7
use C4::Output qw( output_with_http_headers );
7
use C4::Output qw( output_with_http_headers );
8
use C4::Utils::DataTables qw( dt_get_params );
9
use C4::Utils::DataTables::VirtualShelves qw( search );
8
use C4::Utils::DataTables::VirtualShelves qw( search );
10
9
11
my $input = CGI->new;
10
my $input = CGI->new;
Lines 25-38 my $owner = $input->param('owner'); Link Here
25
my $public = $input->param('public');
24
my $public = $input->param('public');
26
my $sortby = $input->param('sortby');
25
my $sortby = $input->param('sortby');
27
my $allow_transfer = $input->param('allow_transfer');
26
my $allow_transfer = $input->param('allow_transfer');
27
my $order_by = $input->param('order_by');
28
my $start    = $input->param('start');
29
my $length   = $input->param('length');
28
30
29
# variable information for DataTables (id)
31
# variable information for DataTables (id)
30
my $sEcho = $input->param('sEcho');
32
my $draw = $input->param('draw');
31
32
my %dt_params = dt_get_params($input);
33
foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
34
    $dt_params{$_} =~ s/^dt_//;
35
}
36
33
37
my $results = C4::Utils::DataTables::VirtualShelves::search(
34
my $results = C4::Utils::DataTables::VirtualShelves::search(
38
    {
35
    {
Lines 41-55 my $results = C4::Utils::DataTables::VirtualShelves::search( Link Here
41
        owner     => $owner,
38
        owner     => $owner,
42
        public    => $public,
39
        public    => $public,
43
        sortby    => $sortby,
40
        sortby    => $sortby,
44
        dt_params => \%dt_params,
41
        order_by  => $order_by,
42
        start     => $start,
43
        length    => $length,
45
    }
44
    }
46
);
45
);
47
46
48
$template->param(
47
$template->param(
49
    sEcho => $sEcho,
48
    draw => $draw,
50
    iTotalRecords => $results->{iTotalRecords},
49
    recordsTotal => $results->{recordsTotal},
51
    iTotalDisplayRecords => $results->{iTotalDisplayRecords},
50
    recordsFiltered => $results->{recordsFiltered},
52
    aaData => $results->{shelves},
51
    data => $results->{shelves},
53
    public => $public,
52
    public => $public,
54
    allow_transfer => $allow_transfer,
53
    allow_transfer => $allow_transfer,
55
);
54
);
(-)a/t/db_dependent/Utils/Datatables.t (-61 lines)
Lines 1-61 Link Here
1
#!/usr/bin/perl
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 Test::More tests => 1;
21
22
use C4::Utils::DataTables qw( dt_build_orderby );
23
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
27
use Koha::Database;
28
29
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
31
32
my $builder = t::lib::TestBuilder->new;
33
34
subtest 'dt_build_orderby' => sub {
35
    plan tests => 2;
36
37
    my $dt_params = {
38
        iSortCol_0  => 5,
39
        sSortDir_0  => "asc",
40
        mDataProp_5 => "branch",
41
        name_sorton => "borrowers.surname borrowers.firstname",
42
43
        iSortCol_1    => 2,
44
        sSortDir_1    => "desc",
45
        mDataProp_2   => "name",
46
        branch_sorton => "branches.branchname",
47
    };
48
49
    my $orderby = dt_build_orderby($dt_params);
50
    is( $orderby, " ORDER BY `branches`.`branchname` ASC,`borrowers`.`surname` DESC,`borrowers`.`firstname` DESC ", 'ORDER BY has been correctly built' );
51
52
    $dt_params = {
53
        %$dt_params,
54
        iSortCol_2                    => 3,
55
        sSortDir_2                    => "asc",
56
        mDataProp_3                   => "branch,somethingelse",
57
    };
58
59
    $orderby = dt_build_orderby($dt_params);
60
    is( $orderby, " ORDER BY `branches`.`branchname` ASC,`borrowers`.`surname` DESC,`borrowers`.`firstname` DESC ", 'ORDER BY has been correctly built, even with invalid stuff');
61
};
(-)a/t/db_dependent/Utils/Datatables_Virtualshelves.t (-15 / +14 lines)
Lines 177-184 for my $i ( 6 .. 15 ) { Link Here
177
177
178
# Set common datatables params
178
# Set common datatables params
179
my %dt_params = (
179
my %dt_params = (
180
    iDisplayLength   => 10,
180
    length   => 10,
181
    iDisplayStart    => 0
181
    start    => 0
182
);
182
);
183
my $search_results;
183
my $search_results;
184
184
Lines 187-200 t::lib::Mocks::mock_userenv({ patron => $john_doe_patron }); Link Here
187
# Search private lists by title
187
# Search private lists by title
188
$search_results = C4::Utils::DataTables::VirtualShelves::search({
188
$search_results = C4::Utils::DataTables::VirtualShelves::search({
189
    shelfname => "ist",
189
    shelfname => "ist",
190
    dt_params => \%dt_params,
190
    %dt_params,
191
    public    => 0,
191
    public    => 0,
192
});
192
});
193
193
194
is( $search_results->{ iTotalRecords }, 2,
194
is( $search_results->{ recordsTotal }, 2,
195
    "There should be 2 private shelves in total" );
195
    "There should be 2 private shelves in total" );
196
196
197
is( $search_results->{ iTotalDisplayRecords }, 2,
197
is( $search_results->{ recordsFiltered }, 2,
198
    "There should be 2 private shelves with title like '%ist%" );
198
    "There should be 2 private shelves with title like '%ist%" );
199
199
200
is( @{ $search_results->{ shelves } }, 2,
200
is( @{ $search_results->{ shelves } }, 2,
Lines 202-214 is( @{ $search_results->{ shelves } }, 2, Link Here
202
202
203
# Search by type only
203
# Search by type only
204
$search_results = C4::Utils::DataTables::VirtualShelves::search({
204
$search_results = C4::Utils::DataTables::VirtualShelves::search({
205
    dt_params => \%dt_params,
205
    %dt_params,
206
    public    => 1,
206
    public    => 1,
207
});
207
});
208
is( $search_results->{ iTotalRecords }, 12,
208
is( $search_results->{ recordsTotal }, 12,
209
    "There should be 12 public shelves in total" );
209
    "There should be 12 public shelves in total" );
210
210
211
is( $search_results->{ iTotalDisplayRecords }, 12,
211
is( $search_results->{ recordsFiltered }, 12,
212
    "There should be 12 private shelves" );
212
    "There should be 12 private shelves" );
213
213
214
is( @{ $search_results->{ shelves } }, 10,
214
is( @{ $search_results->{ shelves } }, 10,
Lines 217-229 is( @{ $search_results->{ shelves } }, 10, Link Here
217
# Search by owner
217
# Search by owner
218
$search_results = C4::Utils::DataTables::VirtualShelves::search({
218
$search_results = C4::Utils::DataTables::VirtualShelves::search({
219
    owner => "jane",
219
    owner => "jane",
220
    dt_params => \%dt_params,
220
    %dt_params,
221
    public    => 1,
221
    public    => 1,
222
});
222
});
223
is( $search_results->{ iTotalRecords }, 12,
223
is( $search_results->{ recordsTotal }, 12,
224
    "There should be 12 public shelves in total" );
224
    "There should be 12 public shelves in total" );
225
225
226
is( $search_results->{ iTotalDisplayRecords }, 2,
226
is( $search_results->{ recordsFiltered }, 2,
227
    "There should be 1 public shelves for jane" );
227
    "There should be 1 public shelves for jane" );
228
228
229
is( @{ $search_results->{ shelves } }, 2,
229
is( @{ $search_results->{ shelves } }, 2,
Lines 233-245 is( @{ $search_results->{ shelves } }, 2, Link Here
233
$search_results = C4::Utils::DataTables::VirtualShelves::search({
233
$search_results = C4::Utils::DataTables::VirtualShelves::search({
234
    owner => "smith",
234
    owner => "smith",
235
    shelfname => "public list 1",
235
    shelfname => "public list 1",
236
    dt_params => \%dt_params,
236
    %dt_params,
237
    public    => 1,
237
    public    => 1,
238
});
238
});
239
is( $search_results->{ iTotalRecords }, 12,
239
is( $search_results->{ recordsTotal }, 12,
240
    "There should be 12 public shelves in total" );
240
    "There should be 12 public shelves in total" );
241
241
242
is( $search_results->{ iTotalDisplayRecords }, 6,
242
is( $search_results->{ recordsFiltered }, 6,
243
    "There should be 6 public shelves for john with name like %public list 1%" );
243
    "There should be 6 public shelves for john with name like %public list 1%" );
244
244
245
is( @{ $search_results->{ shelves } }, 6,
245
is( @{ $search_results->{ shelves } }, 6,
246
- 

Return to bug 34913