__PACKAGE__->load_components(
qw/
+App::Netdisco::DB::SetOperations
Helper::ResultSet::Shortcut
Helper::ResultSet::CorrelateRelationship
/
);
sub
get_distinct_col {
my
(
$rs
,
$col
) =
@_
;
return
$rs
unless
$col
;
return
$rs
->search(
{},
{
columns
=> [
$col
],
order_by
=>
$col
,
distinct
=> 1
}
)->get_column(
$col
)->all;
}
sub
get_datatables_data {
my
$rs
=
shift
;
my
$params
=
shift
;
my
$attrs
=
shift
;
die
"condition parameter to search_by_field must be hashref\n"
if
ref
{} ne
ref
$params
or 0 ==
scalar
keys
%$params
;
$rs
=
$rs
->_with_datatables_paging(
$params
);
$rs
=
$rs
->_with_datatables_order_clause(
$params
);
$rs
=
$rs
->_with_datatables_where_clause(
$params
);
return
$rs
;
}
sub
get_datatables_filtered_count {
my
$rs
=
shift
;
my
$params
=
shift
;
return
$rs
->_with_datatables_where_clause(
$params
)->count;
}
sub
_with_datatables_order_clause {
my
$rs
=
shift
;
my
$params
=
shift
;
my
$attrs
=
shift
;
my
@order
= ();
if
(
defined
$params
->{
'order'
}{0} ) {
for
(
my
$i
= 0;
$i
< (
scalar
keys
%{
$params
->{
'order'
}});
$i
++ ) {
my
$direction
=
'-'
.
$params
->{
'order'
}{
$i
}{
'dir'
};
my
$column_name
= _datatables_index_to_column(
$params
,
$params
->{
'order'
}{
$i
}{
'column'
} );
my
$csa
=
$rs
->current_source_alias;
$column_name
=~ s/^(\w+)$/
$csa
\.$1/x;
push
@order
, {
$direction
=>
$column_name
};
}
}
$rs
=
$rs
->order_by( \
@order
);
return
$rs
;
}
sub
_with_datatables_where_clause {
my
$rs
=
shift
;
my
$params
=
shift
;
my
$attrs
=
shift
;
my
%where
= ();
if
(
defined
$params
->{
'search'
}{
'value'
}
&&
$params
->{
'search'
}{
'value'
} )
{
my
$search_string
=
$params
->{
'search'
}{
'value'
};
for
(
my
$i
= 0;
$i
< (
scalar
keys
%{
$params
->{
'columns'
}});
$i
++ ) {
if
(
$params
->{
'columns'
}{
$i
}{
'searchable'
}
and
$params
->{
'columns'
}{
$i
}{
'searchable'
} eq
'true'
)
{
my
$column
= _datatables_index_to_column(
$params
,
$i
);
my
$csa
=
$rs
->current_source_alias;
$column
=~ s/^(\w+)$/
$csa
\.$1/x;
$column
=
$column
.
'::text'
;
push
@{
$where
{
'-or'
} },
{
$column
=> {
-like
=>
'%'
.
$search_string
.
'%'
} };
}
}
}
$rs
=
$rs
->search( \
%where
,
$attrs
);
return
$rs
;
}
sub
_with_datatables_paging {
my
$rs
=
shift
;
my
$params
=
shift
;
my
$attrs
=
shift
;
my
$limit
=
$params
->{
'length'
};
my
$offset
= 0;
if
(
defined
$params
->{
'start'
} &&
$params
->{
'start'
} ) {
$offset
=
$params
->{
'start'
};
}
$attrs
->{
'offset'
} =
$offset
;
$rs
=
$rs
->search( {},
$attrs
);
$rs
=
$rs
->limit(
$limit
)
if
(
$limit
and
$limit
> 0);
return
$rs
;
}
sub
_datatables_index_to_column {
my
$params
=
shift
;
my
$i
=
shift
;
my
$field
;
if
( !
defined
(
$i
) ) {
$i
= 0;
}
$field
=
$params
->{
'columns'
}{
$i
}{
'data'
};
return
$field
;
}
1;