return $res;
}
+my sub get_filter_text {
+ my ($dbh, $field, $filter) = @_;
+
+ if (!$filter || !$field) {
+ return '';
+ }
+
+ my $pattern = $dbh->quote(encode('UTF-8', "%${filter}%"));
+
+ return "AND ${field} like ${pattern} ";
+}
+
sub user_stat_contact_details {
my ($self, $rdb, $receiver, $limit, $sorters, $filter) = @_;
my $cond_good_mail = $self->query_cond_good_mail ($from, $to);
- my $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%"));
+ my $filter_text = get_filter_text($rdb->{dbh}, 'sender', $filter);
my $query = "SELECT * FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid AND $cond_good_mail " .
"AND NOT direction AND sender != '' AND receiver = ? " .
- ($filter_pattern ? "AND sender like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"ORDER BY $orderby limit $limit";
my $sth = $rdb->{dbh}->prepare($query);
my $cond_good_mail = $self->query_cond_good_mail($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'receiver', $filter);
my $query = "SELECT receiver as contact, count(*) AS count, sum (bytes) AS bytes, " .
"count (virusinfo) as viruscount " .
"FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid " .
- ($filter_pattern ? "AND receiver like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"AND $cond_good_mail AND NOT direction AND sender != '' ";
if ($advfilter) {
my $cond_good_mail = $self->query_cond_good_mail($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'receiver', $filter);
my $sth = $rdb->{dbh}->prepare(
"SELECT " .
"FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid AND " .
"$cond_good_mail AND NOT direction AND sender = ? " .
- ($filter_pattern ? "AND receiver like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"ORDER BY $orderby limit $limit");
$sth->execute(encode('UTF-8',$sender));
my $cond_good_mail = $self->query_cond_good_mail ($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'sender', $filter);
my $query = "SELECT sender,count(*) AS count, sum (bytes) AS bytes, " .
"count (virusinfo) as viruscount, " .
"count (CASE WHEN spamlevel >= 3 THEN 1 ELSE NULL END) as spamcount " .
"FROM CStatistic WHERE $cond_good_mail AND NOT direction AND sender != '' " .
- ($filter_pattern ? "AND sender like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"GROUP BY sender ORDER BY $orderby limit $limit";
my $sth = $rdb->{dbh}->prepare($query);
my $cond_good_mail = $self->query_cond_good_mail($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'sender', $filter);
my $sth = $rdb->{dbh}->prepare(
"SELECT blocked, bytes, ptime, sender, receiver, spamlevel, time, virusinfo " .
"FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid AND $cond_good_mail AND receiver = ? " .
- ($filter_pattern ? "AND sender like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"ORDER BY $orderby limit $limit");
$sth->execute(encode('UTF-8',$receiver));
my $cond_good_mail = $self->query_cond_good_mail ($from, $to) . " AND " .
"receiver IS NOT NULL AND receiver != ''";
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'receiver', $filter);
my $query = "SELECT receiver, " .
"count(*) AS count, " .
}
$query .= "AND $cond_good_mail and direction " .
- ($filter_pattern ? "AND receiver like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"GROUP BY receiver ORDER BY $orderby LIMIT $limit";
my $sth = $rdb->{dbh}->prepare($query);