@ISA
=
qw/ DHTMLX::Core /
;
$VERSION
);
$VERSION
=
'0.002'
;
my
$table
;
my
$primary_key
;
my
$columns
;
my
$identifies_column
;
my
$nRegPag
;
my
$isSmartRendering
;
my
$codigoCurrency
=
'BRL'
;
sub
new
{
my
$class
=
shift
;
my
$self
= {
currency
=>
shift
||
undef
,
};
if
(
defined
(
$self
->{currency}))
{
$codigoCurrency
=
$self
->{currency};
}
bless
$self
,
$class
;
return
$self
;
}
sub
jsonForGrid
{
my
(
$self
,
$table
,
$primary_key
,
$columns
,
$nRegPag
,
$isSmartRendering
,
$identifies_column
,
$user_id
,
$user_group
) =
@_
;
$table
=
$table
||
die
"you must define table"
;
$columns
=
$columns
||
die
"you must define columns"
;
$primary_key
=
$primary_key
||
die
"you must define primary key"
;
$isSmartRendering
=
$isSmartRendering
||
undef
(
$isSmartRendering
);
$identifies_column
=
$identifies_column
||
undef
(
$identifies_column
);
$nRegPag
=
$nRegPag
|| 50;
$user_id
=
$user_id
|| 0;
$user_group
=
$user_group
||
undef
(
$user_group
);
my
$estruturaJson
;
my
@rows
;
my
$sql
;
my
$sql_count
;
my
$sql_ms
;
my
$ordenamentogrid
;
my
$count
;
my
$posStart
;
my
$totalCount
;
my
$nCurrentPag
;
my
$conexao
=
$self
->conectar();
$sql
=
"SELECT * FROM $table WHERE 1=1 "
;
foreach
(
keys
%{
$columns
})
{
my
$colunaLabel
=
$_
;
my
$colunaOperador
= ${
$columns
}{
$_
}{operator};
my
$colunaTipo
= ${
$columns
}{
$_
}{type};
if
(
defined
(
$self
->Post(
"$colunaLabel"
)))
{
if
(
$colunaTipo
eq
"string"
)
{
if
(
$colunaOperador
eq
"ILIKE"
)
{
$sql
=
$sql
.
" AND $colunaLabel $colunaOperador '%"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"%' "
;
$sql_count
=
$sql_count
.
" AND $colunaLabel $colunaOperador '%"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"%' "
;
$sql_ms
=
$sql_ms
.
" AND $colunaLabel LIKE '%"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"%' "
;
}
else
{
$sql
=
$sql
.
" AND $colunaLabel $colunaOperador '"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"' "
;
$sql_count
=
$sql_count
.
" AND $colunaLabel $colunaOperador '"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"' "
;
$sql_ms
=
$sql_ms
.
" AND $colunaLabel $colunaOperador '"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"' "
;
}
}
elsif
(
$colunaTipo
eq
"date"
)
{
my
$strDataBR
=
$self
->noInjection(
$self
->Post(
"$colunaLabel"
));
my
@vDataBR
=
split
(/\//,
$strDataBR
);
my
$strDataUS
=
$vDataBR
[2].
"-"
.
$vDataBR
[1].
"-"
.
$vDataBR
[0];
$sql
=
$sql
.
" AND $colunaLabel = '"
.
$strDataUS
.
"' "
;
$sql_count
=
$sql_count
.
" AND $colunaLabel = '"
.
$strDataUS
.
"' "
;
$sql_ms
=
$sql_ms
.
" AND $colunaLabel = '"
.
$strDataUS
.
"' "
;
}
}
}
if
(
defined
(
$identifies_column
))
{
if
(
$user_id
>0)
{
if
(
defined
(
$user_group
) &&
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
)
{
$sql
=
$sql
.
" AND $identifies_column = ? "
;
$sql_count
=
$sql_count
.
" AND $identifies_column = ? "
;
$sql_ms
=
$sql_ms
.
" AND $identifies_column = ? "
;
}
}
}
if
(
$isSmartRendering
)
{
$posStart
=
$self
->noInjection(
$self
->Get(
"posStart"
));
if
(!
length
(
$posStart
)>0)
{
$posStart
= 0;
}
$count
=
$self
->noInjection(
$self
->Get(
"count"
));
if
(
undef
(
$count
))
{
$count
=
$nRegPag
;
}
else
{
if
(!
length
(
$count
)>0)
{
$count
=
$nRegPag
;
}
}
if
(
$posStart
eq
"0"
)
{
$totalCount
=0;
my
$sqlcount
;
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sqlcount
=
"SELECT COUNT($primary_key) as totalregistros FROM $table WHERE 1=1 $sql_count;"
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql_ms
=~ s/ILIKE/LIKE/;
$sqlcount
=
"SELECT COUNT($primary_key) as totalregistros FROM $table WHERE 1=1 $sql_ms;"
;
}
my
$dbh
=
$conexao
->prepare(
$sqlcount
);
if
(
defined
(
$identifies_column
) &&
$user_id
>0 && (
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
))
{
$dbh
->execute(
$user_id
) or
die
$conexao
->errstr;
}
else
{
$dbh
->execute() or
die
$conexao
->errstr;
}
while
(
my
$registro
=
$dbh
->fetchrow_hashref())
{
$totalCount
=
$registro
->{
'totalregistros'
};
}
$dbh
->finish;
}
else
{
$totalCount
=
""
;
}
}
if
(
$self
->Get(
"ordena"
) eq 1)
{
my
$direcaoOrdena
=
$self
->Get(
"direcaoOrdena"
);
my
$colunaOrdena
=
$self
->Get(
"colunaOrdena"
);
$ordenamentogrid
=
" ORDER BY $colunaOrdena $direcaoOrdena "
;
}
else
{
$ordenamentogrid
=
" ORDER BY $primary_key DESC "
;
}
$sql
=
$sql
.
$ordenamentogrid
;
if
(
$isSmartRendering
)
{
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sql
=
$sql
.
" LIMIT $count OFFSET $posStart "
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql
=";WITH results AS (
SELECT
rowNo = ROW_NUMBER() OVER( ORDER BY
$primary_key
ASC )
, *
FROM
$table
WHERE 1=1
$sql_ms
)
SELECT *
FROM results
WHERE rowNo between
$posStart
and
$posStart
+
$nRegPag
";
}
}
else
{
$nCurrentPag
= (
$self
->Get(
"nCurrentPag"
) || 1) -1;
$nCurrentPag
=
$nRegPag
*
$nCurrentPag
;
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sql
=
$sql
.
" LIMIT $nRegPag OFFSET $nCurrentPag "
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql
=";WITH results AS (
SELECT
rowNo = ROW_NUMBER() OVER( ORDER BY
$primary_key
ASC )
, *
FROM
$table
WHERE 1=1
$sql_ms
)
SELECT *
FROM results
WHERE rowNo between
$nCurrentPag
and
$nCurrentPag
+
$nRegPag
";
}
}
my
$dbh
=
$conexao
->prepare(
$sql
);
if
(
defined
(
$identifies_column
) &&
$user_id
>0 && (
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
))
{
$dbh
->execute(
$user_id
) or
die
$conexao
->errstr;
}
else
{
$dbh
->execute() or
die
$conexao
->errstr;
}
while
(
my
$registro
=
$dbh
->fetchrow_hashref())
{
my
@datarow
;
my
$id
=
$registro
->{
$primary_key
};
foreach
(
keys
%{
$columns
})
{
my
$colunaLabel
=
$_
;
my
$colunaOperador
= ${
$columns
}{
$_
}{operator};
my
$colunaTipo
= ${
$columns
}{
$_
}{type};
if
(
$colunaTipo
eq
"moeda"
)
{
my
$valCurrency
= currency_format(
$codigoCurrency
,
$registro
->{
$colunaLabel
}, FMT_HTML);
$valCurrency
=~ s[R\$][]isg;
push
@datarow
,
$valCurrency
||
""
;
}
else
{
push
@datarow
,
$registro
->{
$colunaLabel
} ||
""
;
}
}
my
$row
= {
id
=>
$id
,
data
=> [
@datarow
],
};
push
@rows
,
$row
;
}
$dbh
->finish;
$conexao
->disconnect;
if
(
$isSmartRendering
)
{
$estruturaJson
= {
total_count
=>
$totalCount
,
pos
=>
$posStart
,
rows
=> [
@rows
],
};
print
to_json(
$estruturaJson
, {
utf8
=> 1 });
}
else
{
$estruturaJson
= {
rows
=> [
@rows
] };
print
"data = "
.to_json(
$estruturaJson
, {
utf8
=> 1 });
}
}
sub
xmlForGrid
{
my
(
$self
,
$table
,
$primary_key
,
$columns
,
$nRegPag
,
$isSmartRendering
,
$identifies_column
,
$user_id
,
$user_group
) =
@_
;
$table
=
$table
||
die
"you must define table"
;
$columns
=
$columns
||
die
"you must define columns"
;
$primary_key
=
$primary_key
||
die
"you must define primary key"
;
$isSmartRendering
=
$isSmartRendering
||
undef
(
$isSmartRendering
);
$identifies_column
=
$identifies_column
||
undef
(
$identifies_column
);
$nRegPag
=
$nRegPag
|| 50;
$user_id
=
$user_id
|| 0;
$user_group
=
$user_group
||
undef
(
$user_group
);
my
$estruturaJson
;
my
@rows
;
my
$sql
;
my
$sql_count
;
my
$sql_ms
;
my
$ordenamentogrid
;
my
$count
;
my
$posStart
;
my
$totalCount
;
my
$nCurrentPag
;
my
$conexao
=
$self
->conectar();
$sql
=
"SELECT * FROM $table WHERE 1=1 "
;
foreach
(
keys
%{
$columns
})
{
my
$colunaLabel
=
$_
;
my
$colunaOperador
= ${
$columns
}{
$_
}{operator};
my
$colunaTipo
= ${
$columns
}{
$_
}{type};
if
(
defined
(
$self
->Post(
"$colunaLabel"
)))
{
if
(
$colunaTipo
eq
"string"
)
{
if
(
$colunaOperador
eq
"ILIKE"
)
{
$sql
=
$sql
.
" AND $colunaLabel $colunaOperador '%"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"%' "
;
$sql_count
=
$sql_count
.
" AND $colunaLabel $colunaOperador '%"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"%' "
;
$sql_ms
=
$sql_ms
.
" AND $colunaLabel LIKE '%"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"%' "
;
}
else
{
$sql
=
$sql
.
" AND $colunaLabel $colunaOperador '"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"' "
;
$sql_count
=
$sql_count
.
" AND $colunaLabel $colunaOperador '"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"' "
;
$sql_ms
=
$sql_ms
.
" AND $colunaLabel $colunaOperador '"
.
$self
->noInjection(
$self
->Post(
"$colunaLabel"
)).
"' "
;
}
}
elsif
(
$colunaTipo
eq
"date"
)
{
my
$strDataBR
=
$self
->noInjection(
$self
->Post(
"$colunaLabel"
));
my
@vDataBR
=
split
(/\//,
$strDataBR
);
my
$strDataUS
=
$vDataBR
[2] .
"-"
.
$vDataBR
[1] .
"-"
.
$vDataBR
[0];
$sql
=
$sql
.
" AND $colunaLabel = '"
.
$strDataUS
.
"' "
;
$sql_count
=
$sql_count
.
" AND $colunaLabel = '"
.
$strDataUS
.
"' "
;
$sql_ms
=
$sql_ms
.
" AND $colunaLabel = '"
.
$strDataUS
.
"' "
;
}
}
}
if
(
defined
(
$identifies_column
))
{
if
(
$user_id
>0)
{
if
(
defined
(
$user_group
) &&
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
)
{
$sql
=
$sql
.
" AND $identifies_column = ? "
;
$sql_count
=
$sql_count
.
" AND $identifies_column = ? "
;
$sql_ms
=
$sql_ms
.
" AND $identifies_column = ? "
;
}
}
}
if
(
$isSmartRendering
)
{
$posStart
=
$self
->noInjection(
$self
->Get(
"posStart"
));
if
(!
length
(
$posStart
)>0)
{
$posStart
= 0;
}
$count
=
$self
->noInjection(
$self
->Get(
"count"
));
if
(
undef
(
$count
))
{
$count
=
$nRegPag
;
}
else
{
if
(!
length
(
$count
)>0)
{
$count
=
$nRegPag
;
}
}
if
(
$posStart
eq
"0"
)
{
$totalCount
=0;
my
$sqlcount
;
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sqlcount
=
"SELECT COUNT($primary_key) as totalregistros FROM $table WHERE 1=1 $sql_count;"
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql_ms
=~ s/ILIKE/LIKE/;
$sqlcount
=
"SELECT COUNT($primary_key) as totalregistros FROM $table WHERE 1=1 $sql_ms;"
;
}
my
$dbh
=
$conexao
->prepare(
$sqlcount
);
if
(
defined
(
$identifies_column
) &&
$user_id
>0 && (
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
))
{
$dbh
->execute(
$user_id
) or
die
$conexao
->errstr;
}
else
{
$dbh
->execute() or
die
$conexao
->errstr;
}
while
(
my
$registro
=
$dbh
->fetchrow_hashref())
{
$totalCount
=
$registro
->{
'totalregistros'
};
}
$dbh
->finish;
}
else
{
$totalCount
=
""
;
}
}
else
{
}
if
(
$self
->Get(
"ordena"
) eq 1)
{
my
$direcaoOrdena
=
$self
->Get(
"direcaoOrdena"
);
my
$colunaOrdena
=
$self
->Get(
"colunaOrdena"
);
$ordenamentogrid
=
" ORDER BY $colunaOrdena $direcaoOrdena "
;
}
else
{
$ordenamentogrid
=
" ORDER BY $primary_key DESC "
;
}
$sql
=
$sql
.
$ordenamentogrid
;
if
(
$isSmartRendering
)
{
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sql
=
$sql
.
" LIMIT $count OFFSET $posStart "
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql
=";WITH results AS (
SELECT
rowNo = ROW_NUMBER() OVER( ORDER BY
$primary_key
ASC )
, *
FROM
$table
WHERE 1=1
$sql_ms
)
SELECT *
FROM results
WHERE rowNo between
$posStart
and
$posStart
+
$nRegPag
";
}
}
else
{
$nCurrentPag
= (
$self
->Get(
"nCurrentPag"
) || 1) -1;
$nCurrentPag
=
$nRegPag
*
$nCurrentPag
;
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sql
=
$sql
.
" LIMIT $nRegPag OFFSET $nCurrentPag "
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql
=";WITH results AS (
SELECT
rowNo = ROW_NUMBER() OVER( ORDER BY
$primary_key
ASC )
, *
FROM
$table
WHERE 1=1
$sql_ms
)
SELECT *
FROM results
WHERE rowNo between
$nCurrentPag
and
$nCurrentPag
+
$nRegPag
";
}
}
my
$newDoc
= XML::Mini::Document->new();
my
$newDocRoot
=
$newDoc
->getRoot();
my
$xmlHeader
=
$newDocRoot
->header(
'xml'
);
$xmlHeader
->attribute(
'version'
,
'1.0'
);
$xmlHeader
->attribute(
'encoding'
,
'UTF-8'
);
my
$rows
=
$newDocRoot
->createChild(
'rows'
);
if
(
$isSmartRendering
)
{
$rows
->attribute(
'total_count'
,
"$totalCount"
);
$rows
->attribute(
'pos'
,
"$posStart"
);
}
my
@row
= [];
my
@cell
= [];
my
$dbh
=
$conexao
->prepare(
$sql
);
if
(
defined
(
$identifies_column
) &&
$user_id
> 0 && (
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
))
{
$dbh
->execute(
$user_id
) or
die
$conexao
->errstr;
}
else
{
$dbh
->execute() or
die
$conexao
->errstr;
}
while
(
my
$registro
=
$dbh
->fetchrow_hashref())
{
my
$id
=
$registro
->{
$primary_key
};
$row
[
"$id"
] =
$rows
->createChild(
'row'
);
$row
[
"$id"
]->attribute(
'id'
,
"$id"
);
@cell
= [];
foreach
(
keys
%{
$columns
})
{
my
$colunaLabel
=
$_
;
my
$colunaOperador
= ${
$columns
}{
$_
}{operator};
my
$colunaTipo
= ${
$columns
}{
$_
}{type};
if
(
$colunaTipo
eq
"moeda"
)
{
my
$valCurrency
= currency_format(
$codigoCurrency
,
$registro
->{
$colunaLabel
}, FMT_HTML);
$valCurrency
=~ s[R\$][]isg;
$cell
[
"$_"
] =
$row
[
"$id"
]->createChild(
'cell'
)->text(
$valCurrency
||
""
);
}
else
{
$cell
[
"$_"
] =
$row
[
"$id"
]->createChild(
'cell'
)->text(
$registro
->{
$colunaLabel
} ||
""
);
}
}
}
$dbh
->finish;
$conexao
->disconnect;
print
$newDoc
->toString();
}
sub
count
{
my
(
$self
,
$table
,
$primary_key
,
$identifies_column
,
$user_id
,
$user_group
) =
@_
;
$table
=
$table
||
die
"you must define table"
;
$primary_key
=
$primary_key
||
die
"you must define primary key"
;
$identifies_column
=
$identifies_column
||
undef
(
$identifies_column
);
$user_id
=
$user_id
|| 0;
$user_group
=
$user_group
||
undef
(
$user_group
);
my
$estruturaJson
;
my
@rows
;
my
$sql
;
my
$sql_count
;
my
$sql_ms
;
my
$totalCount
;
my
$conexao
=
$self
->conectar();
$sql
=
"SELECT * FROM $table WHERE 1=1 "
;
if
(
defined
(
$identifies_column
))
{
if
(
$user_id
>0)
{
if
(
defined
(
$user_group
) &&
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
)
{
$sql
=
$sql
.
" AND $identifies_column = ? "
;
$sql_count
=
$sql_count
.
" AND $identifies_column = ? "
;
$sql_ms
=
$sql_ms
.
" AND $identifies_column = ? "
;
}
}
}
my
$sqlcount
;
if
(
$self
->SGDB() eq
"PostgreSQL"
)
{
$sqlcount
=
"SELECT COUNT($primary_key) as totalregistros FROM $table WHERE 1=1 $sql_count;"
;
}
elsif
(
$self
->SGDB() eq
"SQL Server"
)
{
$sql_ms
=~ s/ILIKE/LIKE/;
$sqlcount
=
"SELECT COUNT($primary_key) as totalregistros FROM $table WHERE 1=1 $sql_ms;"
;
}
my
$dbh
=
$conexao
->prepare(
$sqlcount
);
if
(
defined
(
$identifies_column
) &&
$user_id
>0 && (
$user_group
ne
"manutencao"
&&
$user_group
ne
"administrador"
))
{
$dbh
->execute(
$user_id
) or
die
$conexao
->errstr;
}
else
{
$dbh
->execute() or
die
$conexao
->errstr;
}
while
(
my
$registro
=
$dbh
->fetchrow_hashref())
{
$totalCount
=
$registro
->{
'totalregistros'
};
}
$dbh
->finish;
$estruturaJson
= {
total_count
=>
"$totalCount"
,
};
print
to_json(
$estruturaJson
, {
utf8
=> 1 });
}
1;