options['logs']['enable_logs']) ){
echo '
Email report is disabled
';
return;
}
$date = gosmtp_optget('date');
$custom_start = gosmtp_optget('start-date');
$custom_end = gosmtp_optget('end-date');
$search = gosmtp_optget('search_email_reports');
$multiselect = gosmtp_optget('multiselect');
$all_data = array();
$logger = new GOSMTP\Logger();
// Appropriate date setup
if($date == 'custom_date'){
$start = $custom_start;
$end = $custom_end;
if($start != '' && $end == ''){
$end = date("Y-m-d");
}
}else if($date == 'all' || $date == ''){
$start = '';
$end = '';
}else{
$start = date('Y-m-d', strtotime('-'.$date.' days'));
$end = date("Y-m-d");
}
// Assign all data in option array
$options = array(
'interval' => array(
'start' => $start,
'end' => $end
),
'search' => $search,
'multiselect' => $multiselect,
'pagination' => false,
);
$email_logs = $logger->get_logs('records', 0, $options);
// TODO: Get only records as paged
$mails = gosmtp_group_by($email_logs);
// Pagination
$perpage = 10;
$curpage = (int) gosmtp_optget('paged', 1);
$records_ct = count($mails);
$tpages = ceil($records_ct / $perpage);
$offset = ($curpage - 1) * $perpage;
$args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'total' => $tpages,
'current' => $curpage,
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'type' => 'array',
'add_args' => false
);
// Set limit value
$limit = array(
'perpage' => $perpage,
'offset' => $offset,
);
$pages = paginate_links( $args );
$table_cols = array(
'subject' => __('Subject'),
'from' => __('From'),
'to' => __('To'),
'resent_count' => __('Resent'),
'retries' => __('Retry'),
'sent' => __('Sent'),
'failed' => __('Failed'),
'total' => __('Total')
);
?>
|
'.$col.'';
}
?>
$mail){
$mail_array = $mail['total'];
echo "";
foreach($table_cols as $ck => $col){
echo ''.$mail_array[$ck].' | ';
}
echo '
';
// Add by date data in all data
array_push($all_data, $mail['by_dates']);
}
}else{
// Empty all data when no result found
$all_data = array();
?>
|
';
}
?>
$vv){
switch($kk){
case 'to':
$tos = maybe_unserialize($vv);
$to_list = [];
foreach($tos as $keys => $to){
$to_list[] = $to[0];
}
$return['total']['to'] = implode(',', $to_list);
break;
case 'status':
if($vv == 'sent'){
$return['total']['sent'] = 1;
$return['total']['failed'] = 0;
}elseif($vv == 'failed'){
$return['total']['sent'] = 0;
$return['total']['failed'] = 1;
}
break;
case 'subject':
$return['total'][$kk] = empty($vv) ? '[No Subject]' : $vv;
break;
default:
$return['total'][$kk] = $vv;
}
}
$return['total']['total'] = $return['total']['sent'] + $return['total']['failed'];
$return['by_dates'][$val->created_at] = array(
'sent' => $return['total']['sent'],
'failed' => $return['total']['failed'],
'retries' => $return['total']['retries'],
'resent_count' => $return['total']['resent_count'],
'total' => $return['total']['total'],
);
return $return;
}
// Generate Array with group by
function gosmtp_group_by($logs, $limit = array(), $multiselect = array('subject', 'from', 'to')){
$groups = array();
$i = 0;
$key_array = array();
if(empty($logs)){
return array();
}
foreach($logs as $val){
$val = gosmtp_set_custom_array($val);
$total = $val['total'];
$groups_val = array();
foreach($multiselect as $multi_val){
array_push($groups_val, $total[$multi_val]);
}
// Add new group
if(!in_array($groups_val, $key_array)){
$key_array[$i] = $groups_val;
$groups[$i]['total'] = $val['total'];
$groups[$i]['by_dates'] = $val['by_dates'];
$i++;
}else{
foreach($key_array as $kk => $vv){
if($groups_val != $vv){
continue;
}
$group_total = $groups[$kk]['total'];
$total['sent'] = $group_total['sent'] + $total['sent'];
$total['failed'] = $group_total['failed'] + $total['failed'];
$total['retries'] = $group_total['retries'] + $total['retries'];
$total['resent_count'] = $group_total['resent_count'] + $total['resent_count'];
$total['total'] = $group_total['total'] + $total['total'];
$groups[$kk]['total'] = $total;
$groups[$kk]['by_dates'][array_keys($val['by_dates'])[0]] = $val['by_dates'][array_keys($val['by_dates'])[0]];
}
}
}
// Set limit for Pagination
if(!empty($limit)){
$limit_res = array();
for($i = 0; $i< $limit['perpage']; $i++){
$j = $i+$limit['offset'];
if(isset($groups[$j])){
array_push($limit_res, $groups[$j] );
}
}
$groups = $limit_res;
}
return $groups;
}