1<?php
2
3verifyCsrfGetToken();
4
5if (isset($_GET['id'])) {
6    $id = sprintf('%d', $_GET['id']);
7} else {
8    $id = 0;
9}
10if (isset($_GET['start'])) {
11    $start = sprintf('%d', $_GET['start']);
12} else {
13    $start = 0;
14}
15
16$addcomparison = 0;
17$access = accessLevel('mviews');
18//print "Access level: $access";
19switch ($access) {
20    case 'owner':
21        $subselect = ' and owner = '.$_SESSION['logindetails']['id'];
22        if ($id) {
23            $allow = Sql_Fetch_Row_query(sprintf('select owner from %s where id = %d %s', $GLOBALS['tables']['message'],
24                $id, $subselect));
25            if ($allow[0] != $_SESSION['logindetails']['id']) {
26                echo s('You do not have access to this page');
27
28                return;
29            }
30        }
31        $addcomparison = 1;
32        break;
33    case 'all':
34        $subselect = '';
35        break;
36    case 'none':
37    default:
38        $subselect = ' where id = 0';
39        echo s('You do not have access to this page');
40
41        return;
42        break;
43}
44
45$status = '';
46
47$download = !empty($_GET['dl']);
48
49if (!$id) {
50    if ($download) {
51        ob_end_clean();
52        //  header("Content-type: text/plain");
53        header('Content-type: text/csv');
54        header('Content-disposition:  attachment; filename="phpList Message open statistics.csv"');
55        ob_start();
56    }
57    $status .= '<p class="pull-right">'.PageLinkButton('page=pageaction&action=mviews&dl=true', s('Download as CSV file')).'</p><div class="clearfix"></div>';
58//  print '<p>'.s('Select Message to view').'</p>';
59    $timerange = ' and msg.entered  > date_sub(now(),interval 12 month)';
60    $timerange = '';
61    $limit = 'limit 10';
62
63    $req = Sql_Query(sprintf('select msg.id as messageid,count(um.viewed) as views, count(um.status) as total,
64    subject,sent,bouncecount as bounced from %s um,%s msg
65    where um.messageid = msg.id and um.status = "sent" %s %s
66    group by msg.id order by msg.entered desc limit 50', $GLOBALS['tables']['usermessage'],
67        $GLOBALS['tables']['message'], $subselect, $timerange));
68    if (!Sql_Affected_Rows()) {
69        $status .= '<p class="information">'.s('There are currently no messages to view').'</p>';
70    }
71
72    $ls = new WebblerListing(s('Available Messages'));
73    while ($row = Sql_Fetch_Array($req)) {
74        //  $element = $row['messageid'].' '.substr($row['subject'],0,50);
75        $messagedata = loadMessageData($row['messageid']);
76        if (!$download) {
77            if ($messagedata['subject'] != $messagedata['campaigntitle']) {
78                $element = '<!--'.$row['messageid'].'-->'.stripslashes($messagedata['campaigntitle']).'<br/><strong>'.shortenTextDisplay($messagedata['subject'],
79                        30).'</strong>';
80            } else {
81                $element = '<!--'.$row['messageid'].'-->'.shortenTextDisplay($messagedata['subject'], 30);
82            }
83        } else {
84            $element = $messagedata['subject'];
85        }
86        $ls->addElement($element, PageUrl2('mviews&amp;id='.$row['messageid']));
87        $ls->setClass($element, 'row1');
88        if (!empty($row['sent'])) {
89            $ls->addRow($element,
90                '<div class="listingsmall gray">'.s('date').': '.formatDate($row['sent'], true).'</div>', '');
91        } else {
92            $ls->addRow($element,
93                '<div class="listingsmall gray">'.s('date').': '.s('in progress').'</div>',
94                '');
95        }
96        $ls->addColumn($element, s('sent'), number_format($row['total']));
97        //   $ls->addColumn($element,s('bounced'),$row['bounced']);
98        $ls->addColumn($element, s('unique views'), number_format($row['views']),
99            $row['views'] ? PageURL2('mviews&amp;id='.$row['messageid']) : '');
100        $openrate = sprintf('%0.2f', ($row['views'] / $row['total'] * 100));
101        $ls->addColumn($element, s('rate'), $openrate.' %');
102        /*
103            $bouncerate = sprintf('%0.2f',($row['bounced'] / $row['total'] * 100));
104            $ls->addColumn($element,s('bounce rate'),$bouncerate.' %');
105
106        */
107    }
108    if ($addcomparison) {
109        $total = Sql_Fetch_Array_Query(sprintf('select count(entered) as total from %s um where um.status = "sent"',
110            $GLOBALS['tables']['usermessage']));
111        $viewed = Sql_Fetch_Array_Query(sprintf('select count(viewed) as viewed from %s um where um.status = "sent"',
112            $GLOBALS['tables']['usermessage']));
113        $overall = s('Comparison to other admins');
114        $ls->addElement($overall);
115        $ls->addColumn($overall, s('views'), $viewed['viewed']);
116        $perc = sprintf('%0.2f', ($viewed['viewed'] / $total['total'] * 100));
117        $ls->addColumn($overall, s('rate'), $perc.' %');
118    }
119    if ($download) {
120        ob_end_clean();
121        $status .= $ls->tabDelimited();
122    }
123
124    $status .= $ls->display();
125}
126