<?php

/*
 *
 *
 *
 * V2.7.7
 */

mh_define('MAILBEEZ_REPORT_DETAILS_IMG', '');

$popup = false;
if (mh_get('popup')) {
    mh_define('MH_POPUP', true);
    $popup = true;
}

if (mh_get('back') == 'dashboard') {
    $back_url = mh_href_link(FILENAME_MAILBEEZ, '');
    $back_button = mh_lng('MH_BUTTON_BACK_DASHBOARD');
} else {
    $back_url = mh_href_link(FILENAME_MAILBEEZ, 'module=' . mh_get('module'));
    $back_button = mh_lng('MH_BUTTON_BACK_REPORTS');
}

$app_action = mh_get('app_action', '');
$msg = '';

if (mh_not_null($app_action)) {
    switch ($app_action) {
        case 'save':
            break;
    }
}

$coupon_reporting = false;
if (file_exists(MH_DIR_CONFIG . 'config_coupon_engine.php')) {
    require_once(MH_DIR_CONFIG . 'config_coupon_engine.php');
    $cfg_ce = new config_coupon_engine();
    if ($cfg_ce->version > 2.2 && $cfg_ce->enabled) {
        $coupon_reporting = true;
    }
}
$email_archive = false;
if (mh_cfg('MAILBEEZ_EMAIL_ARCHIVE_STATUS') == 'True') {
    require_once(MH_DIR_CONFIG . 'config_email_archive.php');
    $cfg_ea = new config_email_archive();

    $email_archive = $cfg_ea->enabled;
}

$selected_module_raw = mh_get('selected_module', false);
$selected_module_iteration = '';
$selected_module_iteration_loopstart = '';
if (stristr($selected_module_raw, '-')) {
    if (stristr($selected_module_raw, '-loop')) {
        list($selected_module, $selected_module_iteration, , $selected_module_iteration_loopstart) = explode('-', $selected_module_raw);
    } else {
        list($selected_module, $selected_module_iteration) = explode('-', $selected_module_raw);
    }
} else {
    $selected_module = $selected_module_raw;
}

$selected_selected_campaign = mh_get('selected_campaign', false);
$selected_selected_campaign_item = mh_get('selected_campaign_item', false);
$start_date_db = mh_get('selected_startdate') ? mailbeez_system::convert_realdate_to_db(mh_get('selected_startdate')) : false;
$end_date_db = mh_get('selected_enddate') ? mailbeez_system::convert_realdate_to_db(mh_get('selected_enddate'), true) : false;

?>

<?php
// back
if (!$popup) {
    echo mb_admin_button($back_url, $back_button, '', 'link') . '<br /><br />';
}
?>
<?php if ($msg != ''): ?>
    <?php echo $msg; ?>
<?php endif; ?>

<table border="0" width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <table border="0" width="100%" cellspacing="0" cellpadding="0">
                <tr>
                    <td class="pageTitle"><?php echo mh_lng('MAILBEEZ_REPORT_TRACK_TEXT_TITLE'); ?></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table border="0" width="100%" cellspacing="0" cellpadding="0">
                <tr>
                    <td valign="top">
                        <table border="0" width="100%" cellspacing="0" cellpadding="2">
                            <tr class="dataTableHeadingRow">
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_ID'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_DATE'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_MODULE'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_ITERATION'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_CUSTOMER_ID'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_CUSTOMER_EMAIL'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_CUSTOMER_LNG'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_MODE'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_CHANNEL'); ?></td>
                                <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_BATCH_ID'); ?></td>
                                <?php if (mh_cfg('MH_OPENTRACKER_ENABLED')) { ?>
                                    <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_OPENED'); ?></td>
                                <?php } ?>
                                <?php if (mh_cfg('MH_CLICKTRACKER_ENABLED')) { ?>
                                    <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_CLICKED'); ?></td>
                                <?php } ?>
                                <?php if (mh_cfg('MH_ORDERTRACKER_ENABLED')) { ?>
                                    <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_ORDERED'); ?></td>
                                <?php } ?>
                                <?php if ($coupon_reporting) { ?>
                                    <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_COUPON'); ?></td>
                                <?php } ?>
                                <?php if (mh_cfg('MH_BOUNCEHANDLING_ENABLED')) { ?>
                                    <td class="dataTableHeadingContent"><?php echo mh_lng('TABLE_HEADING_BOUNCED'); ?>
                                        <?php if (mh_cfg('MAILBEEZ_BOUNCEHIVE_MSG_LOG') == 'True') { ?>
                                        <?php } ?>
                                    </td>
                                <?php } ?>
                            </tr>
                            <?php

                            //$report_query_numrows = 25;
                            $rows = 0;
                            if (mh_get(MH_PAGE_NAME) > 1) {
                                $rows = mh_get(MH_PAGE_NAME) * mh_cfg('MH_SPLITPAGE_NUM') - mh_cfg('MH_SPLITPAGE_NUM');
                            }

                            $filter_sql = ' where true ';
                            $group_by_sql = '';
                            $join_sql = '';

                            switch (mh_get('filter')) {
                                case 'bounce':
                                    $filter_sql .= ' and bounce_status = "H" or  bounce_status = "S"';
                                    break;
                                case 'opened':
                                    $filter_sql .= ' and opened > "2000-01-01 00:00:00" ';
                                    break;
                                case 'clicked':
                                    $filter_sql .= ' and clicked > "2000-01-01 00:00:00" ';
                                    break;
                                case 'ordered':
                                    $filter_sql .= ' and ordered > "2000-01-01 00:00:00" ';
                                    break;
                            }
                            $selected_selected_campaign_items_ids = '';
                            if ($selected_selected_campaign && !$selected_selected_campaign_item) {
                                require_once(MH_DIR_FS_CATALOG . MH_ROOT_PATH . 'mailbeez/mb_newsletter/classes/mb_newsletter_analytics.php');

                                $mb_nl_analytics = new mb_newsletter_analytics();
                                $mb_nl_analytics->set_campaign_id($selected_selected_campaign);

                                $data = $mb_nl_analytics->get_campaign_items_ids();

                                if ($data) {
                                    $selected_selected_campaign_items_ids = implode(',', $data);
                                }
                            }

                            $filter_sql .= ($start_date_db) ? " and '" . $start_date_db . "' <= t.date_sent" : '';
                            $filter_sql .= ($end_date_db) ? " and '" . $end_date_db . "' >= t.date_sent" : '';
                            $filter_sql .= ($selected_module) ? " and module = '" . $selected_module . "' " : '';
                            if ($selected_module_iteration_loopstart) {
                                $filter_sql .= " and iteration >= " . (int)$selected_module_iteration_loopstart . " ";
                            } elseif ($selected_module_iteration) {
                                $filter_sql .= " and iteration = " . (int)$selected_module_iteration . " ";
                            }
                            $filter_sql .= ($selected_selected_campaign && !$selected_selected_campaign_item) ? " and iteration in (" . $selected_selected_campaign_items_ids . ") " : '';
                            $filter_sql .= ($selected_selected_campaign_item) ? " and iteration = " . (int)$selected_selected_campaign_item . " " : '';


                            if (mh_get('order_time_stamp')) {
                                list($order_track_timestamp, ,) = explode(',', mh_get('order_time_stamp'));
                                // Use a sargable range query instead of DATE_FORMAT() on the column,
                                // so an index on ot.date_record can be used.
                                $order_track_ts = (int)($order_track_timestamp / 1000);
                                $order_track_date_start = date("Y-m-d", $order_track_ts);
                                $order_track_date_end = date("Y-m-d", strtotime('+1 day', $order_track_ts));
                                $join_sql = " join " . TABLE_MAILBEEZ_TRACKING_ORDERS . " ot on (t.message_id = ot.message_id and ot.date_record >= '" . $order_track_date_start . " 00:00:00' and ot.date_record < '" . $order_track_date_end . " 00:00:00')";
                                $group_by_sql = " group by ot.orders_id";
                            }

                            //$report_query_raw = "select autoemail_id, message_id, iteration, customers_id, customers_email, orders_id, date_sent, simulation, batch_id, language_id, channel, bounce_status, message_id,opened,  clicked, ordered, coupon_id, coupon_redeemed, mobile from " . TABLE_MAILBEEZ_TRACKING . " t " . $join_sql . " " . $filter_sql . $group_by_sql . " order by autoemail_id DESC";
                            $report_query_raw = "select * from " . TABLE_MAILBEEZ_TRACKING . " t 
                            " . $join_sql . " 
                            " . $filter_sql .
                                $group_by_sql .
                                " order by autoemail_id DESC";

                            //        echo $report_query_raw; mh_exit();

                            if (mh_get('order_time_stamp')) {
                                // advoid incompatibility of query with modified-shop splitpageresults
                                $report_split = null;
                            } else {
                                $page_val = mh_get(MH_PAGE_NAME);
                                $report_split = new splitPageResults($page_val, mh_cfg('MH_SPLITPAGE_NUM'), $report_query_raw, $report_query_numrows);
                            }


                            $report_query = mh_db_query($report_query_raw);
                            while ($report = mh_db_fetch_array($report_query)) {
                                $rows++;

                                if (strlen($rows) < 2) {
                                    $rows = '0' . $rows;
                                }
                                $click_action_archive = '';
                                $click_action_cis = '';

                                if ($email_archive) {
                                    $click_url = mh_href_link(mh_fn('FILENAME_MAILBEEZ'), 'app=load_app&app_path=config_email_archive/admin_application_plugins/view_email_archive.php&popup=true&message_id=' . $report['message_id']);
                                    $click_action_archive = 'onclick="openReportPopup(\'' . $click_url . '\')"';
                                }

                                if (mh_cfg('MAILBEEZ_INSIGHT_VIEW_STATUS') == 'True') {
                                    $click_action_cis = 'onclick="return openCISPopup(\'' . $report['customers_id'] . '\', \'view_overview\');"';
                                }


                                ?>
                                <tr class="dataTableRow"
                                    onmouseover="this.className='dataTableRowOver';this.style.cursor='pointer'"
                                    onmouseout="this.className='dataTableRow'"
                                >
                                    <td class="dataTableContent"
                                        title="message-id: <?php echo $report['message_id']; ?>" <?php echo $click_action_archive; ?> <?php if ($report['message_id'] == -1) { ?> bgcolor="red" style="color: #fff; font-weight:bold" <?php } ?> ><?php echo $report['autoemail_id']; ?></td>
                                    <td class="dataTableContent" <?php echo $click_action_archive; ?> ><?php echo $report['date_sent']; ?>
                                        &nbsp;
                                    </td>
                                    <td class="dataTableContent" <?php echo $click_action_archive; ?> >
                                        <?php
                                        if ($email_archive) {
                                            echo mh_lng('MAILBEEZ_REPORT_DETAILS_IMG');
                                        }
                                        ?>
                                        <?php echo $report['module']; ?></td>
                                    <td class="dataTableContent" <?php echo $click_action_archive; ?> ><?php echo $report['iteration']; ?></td>
                                    <td class="dataTableContent" <?php echo $click_action_cis; ?> ><?php echo $report['customers_id']; ?>
                                        &nbsp;
                                    </td>
                                    <td class="dataTableContent" <?php echo $click_action_cis; ?> >
                                        <?php
                                        if (mh_cfg('MAILBEEZ_INSIGHT_VIEW_STATUS') == 'True') {
                                            echo mh_lng('MAILBEEZ_REPORT_DETAILS_IMG');
                                        }
                                        ?>
                                        <?php
                                        if ($report['mobile']) {
                                            ?>
                                            <img
                                            src="<?php echo MH_CATALOG_SERVER . MH_ADMIN_DIR_WS_IMAGES ?>icon_mobile_32.png"
                                            width="12"
                                            height="12" align="absmiddle" hspace="0" border="0" alt="mobile"><?php
                                        }

                                        ?><?php echo $report['customers_email']; ?>&nbsp;
                                    </td>
                                    <td class="dataTableContent" <?php echo '' ?> ><?php echo $report['language_id']; ?>
                                        &nbsp;
                                    </td>
                                    <td class="dataTableContent" <?php echo '' ?> ><?php echo ($report['simulation'] > 0)
                                            ? mh_lng('MAILBEEZ_SIMULATION_TAG') : 'PROD'; ?>&nbsp;
                                    </td>
                                    <td class="dataTableContent"><?php echo $report['channel']; ?></td>
                                    <td class="dataTableContent"><?php echo $report['batch_id']; ?></td>

                                    <?php if (mh_cfg('MH_OPENTRACKER_ENABLED')) { ?>
                                        <td class="dataTableContent"
                                            bgcolor="<?php echo ($report['opened'] != '0000-00-00 00:00:00' && $report['opened'] != '') ? mh_lng('MH_COLOR_BLUE') : ''; ?>"><?php if ($report['opened'] != '0000-00-00 00:00:00' && $report['opened'] != '') { ?>
                                                <?php echo mb_admin_button(mh_href_link(FILENAME_MAILBEEZ, 'app=load_app&app_path=report_track/admin_application_plugins/view_opens.php&popup=true&message_id=' . $report['message_id']), mh_lng('MAILBEEZ_REPORT_DETAILS'), '', 'popup', 'link') ?>
                                            <?php } ?>
                                        </td>
                                    <?php } ?>

                                    <?php if (mh_cfg('MH_CLICKTRACKER_ENABLED')) { ?>
                                        <td class="dataTableContent"
                                            bgcolor="<?php echo ($report['clicked'] != '0000-00-00 00:00:00' && $report['clicked'] != '') ? mh_lng('MH_COLOR_ORANGE') : ''; ?>"><?php if ($report['clicked'] != '0000-00-00 00:00:00' && $report['clicked'] != '') { ?>

                                                <?php echo mb_admin_button(mh_href_link(mh_fn('FILENAME_MAILBEEZ'), 'app=load_app&app_path=report_track/admin_application_plugins/view_clicks.php&popup=true&message_id=' . $report['message_id']), mh_lng('MAILBEEZ_REPORT_DETAILS'), '', 'popup', 'link') ?>
                                            <?php } ?>
                                        </td>
                                    <?php } ?>

                                    <?php if (mh_cfg('MH_ORDERTRACKER_ENABLED')) { ?>
                                        <td class="dataTableContent"
                                            bgcolor="<?php echo (($report['ordered'] != '0000-00-00 00:00:00' && $report['ordered'] != '') ||
                                                ($coupon_reporting && isset($report['cpn_orders_id']) && $report['cpn_orders_id'] > 0)) ? mh_cfg('MH_COLOR_GREEN') : ''; ?>">
                                            <?php if (($report['ordered'] != '0000-00-00 00:00:00' && $report['ordered'] != '') ||
                                                ($coupon_reporting && isset($report['cpn_orders_id']) && $report['cpn_orders_id'] > 0)
                                            ) {
                                                ?>
                                                <?php echo mb_admin_button(mh_href_link(FILENAME_MAILBEEZ, 'app=load_app&app_path=report_track/admin_application_plugins/view_orders.php&popup=true&message_id=' . $report['message_id']), mh_lng('MAILBEEZ_REPORT_DETAILS'), '', 'popup', 'link') ?>
                                            <?php } ?>
                                        </td>
                                    <?php } ?>

                                    <?php if ($coupon_reporting) { ?>
                                        <td class="dataTableContent"
                                            bgcolor="<?php echo ($report['coupon_redeemed'] > 0) ? mh_cfg('MH_COLOR_GREEN') : ''; ?>">
                                            <?php if ((int)$report['coupon_id'] != 0 && $report['coupon_id'] != '') { ?>
                                                <?php
                                                // show coupon code
                                                // echo couponbeez::get_coupon_code_by_id($report['coupon_id']);
                                                ?>
                                                <?php echo mb_admin_button(mh_href_link(FILENAME_MAILBEEZ, 'app=load_app&app_path=config_coupon_engine/admin_application_plugins/view_coupon.php&popup=true&coupon_id=' . $report['coupon_id']), mh_lng('MAILBEEZ_REPORT_DETAILS'), '', 'popup', 'link') ?>
                                            <?php } ?>

                                        </td>
                                    <?php } ?>

                                    <?php if (mh_cfg('MH_BOUNCEHANDLING_ENABLED')) { ?>
                                        <td class="dataTableContent"
                                            bgcolor="<?php echo ($report['bounce_status'] != '') ? mh_cfg('MH_COLOR_PINK') : ''; ?>"><?php echo $report['bounce_status']; ?>
                                            <?php if (mh_cfg('MAILBEEZ_BOUNCEHIVE_MSG_LOG') == 'True' && $report['bounce_status'] != '') { ?>
                                                <?php echo mb_admin_button(mh_href_link(FILENAME_MAILBEEZ, 'app=load_app&app_path=config_bouncehive_advanced/admin_application_plugins/view_msg.php&popup=true&autoemail_id=' . $report['autoemail_id']), mh_lng('MAILBEEZ_REPORT_DETAILS'), '', 'popup', 'link') ?>
                                            <?php } ?>
                                        </td>
                                    <?php } ?>
                                </tr>
                                <?php
                            }
                            ?>
                        </table>
                    </td>
                </tr>
                <?php if ($report_split) { ?>
                    <tr>
                        <td colspan="3">
                            <table border="0" width="100%" cellspacing="0" cellpadding="2">
                                <tr>
                                    <td class="smallText"
                                        valign="top"><?php echo $report_split->display_count($report_query_numrows, mh_cfg('MH_SPLITPAGE_NUM'), mh_get(MH_PAGE_NAME), mh_lng('MAILBEEZ_TEXT_DISPLAY_NUMBER_OF_ITEMS')); ?></td>
                                    <td class="smallText"
                                        align="right"><?php echo $report_split->display_links($report_query_numrows, mh_cfg('MH_SPLITPAGE_NUM'), mh_cfg('MAX_DISPLAY_PAGE_LINKS'), mh_get(MH_PAGE_NAME), mh_get_all_get_params(array(MH_PAGE_NAME))); ?></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                <?php }
                ?>
            </table>
        </td>
    </tr>
</table>
			